swig-4.4.0/0000775000175000017500000000000015075470670012363 5ustar williamwilliamswig-4.4.0/INSTALL0000664000175000017500000002200515075443613013410 0ustar williamwilliamBasic Installation ================== These are generic installation instructions. The `configure' shell script attempts to guess correct values for various system-dependent variables used during compilation. It uses those values to create a `Makefile' in each directory of the package. It may also create one or more `.h' files containing system-dependent definitions. Finally, it creates a shell script `config.status' that you can run in the future to recreate the current configuration, and a file `config.log' containing compiler output (useful mainly for debugging `configure'). It can also use an optional file (typically called `config.cache' and enabled with `--cache-file=config.cache' or simply `-C') that saves the results of its tests to speed up reconfiguring. (Caching is disabled by default to prevent problems with accidental use of stale cache files.) If you need to do unusual things to compile the package, please try to figure out how `configure' could check whether to do them, and mail diffs or instructions to the address given in the `README' so they can be considered for the next release. If you are using the cache, and at some point `config.cache' contains results you don't want to keep, you may remove or edit it. The file `configure.ac' (or `configure.in') is used to create `configure' by a program called `autoconf'. You only need `configure.ac' if you want to change it or regenerate `configure' using a newer version of `autoconf'. The simplest way to compile this package is: 1. `cd' to the directory containing the package's source code and type `./configure' to configure the package for your system. If you're using `csh' on an old version of System V, you might need to type `sh ./configure' instead to prevent `csh' from trying to execute `configure' itself. Running `configure' takes awhile. While running, it prints some messages telling which features it is checking for. 2. Type `make' to compile the package. 3. Optionally, type `make check' to run any self-tests that come with the package. 4. Type `make install' to install the programs and any data files and documentation. 5. You can remove the program binaries and object files from the source code directory by typing `make clean'. To also remove the files that `configure' created (so you can compile the package for a different kind of computer), type `make distclean'. There is also a `make maintainer-clean' target, but that is intended mainly for the package's developers. If you use it, you may have to get all sorts of other programs in order to regenerate files that came with the distribution. Compilers and Options ===================== Some systems require unusual options for compilation or linking that the `configure' script does not know about. Run `./configure --help' for details on some of the pertinent environment variables. You can give `configure' initial values for variables by setting them in the environment. You can do that on the command line like this: ./configure CC=c89 CFLAGS=-O2 LIBS=-lposix *Note Environment Variables::, for more details. Compiling For Multiple Architectures ==================================== You can compile the package for more than one kind of computer at the same time, by placing the object files for each architecture in their own directory. To do this, you must use a version of `make' that supports the `VPATH' variable, such as GNU `make'. `cd' to the directory where you want the object files and executables to go and run the `configure' script. `configure' automatically checks for the source code in the directory that `configure' is in and in `..'. If you have to use a `make' that does not support the `VPATH' variable, you have to compile the package for one architecture at a time in the source code directory. After you have installed the package for one architecture, use `make distclean' before reconfiguring for another architecture. Installation Names ================== By default, `make install' will install the package's files in `/usr/local/bin', `/usr/local/man', etc. You can specify an installation prefix other than `/usr/local' by giving `configure' the option `--prefix=PATH'. You can specify separate installation prefixes for architecture-specific files and architecture-independent files. If you give `configure' the option `--exec-prefix=PATH', the package will use PATH as the prefix for installing programs and libraries. Documentation and other data files will still use the regular prefix. In addition, if you use an unusual directory layout you can give options like `--bindir=PATH' to specify different values for particular kinds of files. Run `configure --help' for a list of the directories you can set and what kinds of files go in them. If the package supports it, you can cause programs to be installed with an extra prefix or suffix on their names by giving `configure' the option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. Optional Features ================= Some packages pay attention to `--enable-FEATURE' options to `configure', where FEATURE indicates an optional part of the package. They may also pay attention to `--with-PACKAGE' options, where PACKAGE is something like `gnu-as' or `x' (for the X Window System). The `README' should mention any `--enable-' and `--with-' options that the package recognizes. For packages that use the X Window System, `configure' can usually find the X include and library files automatically, but if it doesn't, you can use the `configure' options `--x-includes=DIR' and `--x-libraries=DIR' to specify their locations. Specifying the System Type ========================== There may be some features `configure' cannot figure out automatically, but needs to determine by the type of host the package will run on. Usually `configure' can figure that out, but if it prints a message saying it cannot guess the host type, give it the `--build=TYPE' option. TYPE can either be a short name for the system type, such as `sun4', or a canonical name which has the form: CPU-COMPANY-SYSTEM where SYSTEM can have one of these forms: OS KERNEL-OS See the file `config.sub' for the possible values of each field. If `config.sub' isn't included in this package, then this package doesn't need to know the host type. If you are _building_ compiler tools for cross-compiling, you should use the `--target=TYPE' option to select the type of system they will produce code for. If you want to _use_ a cross compiler, that generates code for a platform different from the build platform, you should specify the host platform (i.e., that on which the generated programs will eventually be run) with `--host=TYPE'. In this case, you should also specify the build platform with `--build=TYPE', because, in this case, it may not be possible to guess the build platform (it sometimes involves compiling and running simple test programs, and this can't be done if the compiler is a cross compiler). Sharing Defaults ================ If you want to set default values for `configure' scripts to share, you can create a site shell script called `config.site' that gives default values for variables like `CC', `cache_file', and `prefix'. `configure' looks for `PREFIX/share/config.site' if it exists, then `PREFIX/etc/config.site' if it exists. Or, you can set the `CONFIG_SITE' environment variable to the location of the site script. A warning: not all `configure' scripts look for a site script. Environment Variables ===================== Variables not defined in a site shell script can be set in the environment passed to configure. However, some packages may run configure again during the build, and the customized values of these variables may be lost. In order to avoid this problem, you should set them in the `configure' command line, using `VAR=value'. For example: ./configure CC=/usr/local2/bin/gcc will cause the specified gcc to be used as the C compiler (unless it is overridden in the site shell script). `configure' Invocation ====================== `configure' recognizes the following options to control how it operates. `--help' `-h' Print a summary of the options to `configure', and exit. `--version' `-V' Print the version of Autoconf used to generate the `configure' script, and exit. `--cache-file=FILE' Enable the cache: use and save the results of the tests in FILE, traditionally `config.cache'. FILE defaults to `/dev/null' to disable caching. `--config-cache' `-C' Alias for `--cache-file=config.cache'. `--quiet' `--silent' `-q' Do not print messages saying which checks are being made. To suppress all normal output, redirect it to `/dev/null' (any error messages will still be shown). `--srcdir=DIR' Look for the package's source code in directory DIR. Usually `configure' can determine that directory automatically. `configure' also accepts some other, not widely useful, options. Run `configure --help' for more details. swig-4.4.0/Source/0000775000175000017500000000000015075470612013617 5ustar williamwilliamswig-4.4.0/Source/Preprocessor/0000775000175000017500000000000015075470607016311 5ustar williamwilliamswig-4.4.0/Source/Preprocessor/expr.c0000664000175000017500000003510615075443613017435 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * expr.c * * Integer arithmetic expression evaluator used to handle expressions * encountered during preprocessing. * * Note that this is used for expressions in `#if` and the like, but not * for expressions in `#define` which SWIG wraps as constants - for those * we inject a `%constant` directive which is handled by the parser in * `Source/CParse/parser.y`. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "preprocessor.h" static Scanner *scan = 0; typedef struct { /* One of the EXPR_xxx values defined below. */ int op; /* op == EXPR_OP: value is the token specifying which operator. * * op == EXPR_VALUE && svalue == NULL: Numeric expression value. * * Otherwise unused. */ long value; /* op == EXPR_VALUE: If non-NULL, string expression value; if NULL see value. * * Otherwise unused. */ String *svalue; } exprval; #define EXPR_TOP 1 #define EXPR_VALUE 2 #define EXPR_OP 3 #define EXPR_GROUP 4 /* Special token values used here to distinguish from SWIG_TOKEN_MINUS * and SWIG_TOKEN_PLUS (which we use here for a two argument versions). */ #define OP_UMINUS 100 #define OP_UPLUS 101 static exprval stack[256]; /* Parsing stack */ static int sp = 0; /* Stack pointer */ static int prec[256]; /* Precedence rules */ static int expr_init = 0; /* Initialization flag */ static const char *errmsg = 0; /* Parsing error */ /* Initialize the precedence table for various operators. Low values have higher precedence */ static void init_precedence(void) { prec[SWIG_TOKEN_NOT] = 10; prec[SWIG_TOKEN_LNOT] = 10; prec[OP_UMINUS] = 10; prec[OP_UPLUS] = 10; prec[SWIG_TOKEN_STAR] = 20; prec[SWIG_TOKEN_SLASH] = 20; prec[SWIG_TOKEN_PERCENT] = 20; prec[SWIG_TOKEN_PLUS] = 30; prec[SWIG_TOKEN_MINUS] = 30; prec[SWIG_TOKEN_LSHIFT] = 40; prec[SWIG_TOKEN_RSHIFT] = 40; prec[SWIG_TOKEN_LESSTHAN] = 50; prec[SWIG_TOKEN_GREATERTHAN] = 50; prec[SWIG_TOKEN_LTEQUAL] = 50; prec[SWIG_TOKEN_GTEQUAL] = 50; prec[SWIG_TOKEN_EQUALTO] = 60; prec[SWIG_TOKEN_NOTEQUAL] = 60; prec[SWIG_TOKEN_AND] = 70; prec[SWIG_TOKEN_XOR] = 80; prec[SWIG_TOKEN_OR] = 90; prec[SWIG_TOKEN_LAND] = 100; prec[SWIG_TOKEN_LOR] = 110; expr_init = 1; } #define UNARY_OP(token) (((token) == SWIG_TOKEN_NOT) || \ ((token) == SWIG_TOKEN_LNOT) || \ ((token) == OP_UMINUS) || \ ((token) == OP_UPLUS)) /* Reduce a single operator on the stack */ /* return 0 on failure, 1 on success */ static int reduce_op(void) { long op_token = stack[sp - 1].value; assert(sp > 0); assert(stack[sp - 1].op == EXPR_OP); /* do some basic checking first: */ if (stack[sp].op != EXPR_VALUE) { errmsg = "Right-hand side is not value"; return 0; } if (UNARY_OP(op_token)) { if (stack[sp].svalue) { errmsg = "Syntax error: attempt to apply unary operator to string"; return 0; } } else { /* binary operator: */ if (sp == 1) { /* top of stack: don't attempt to use sp-2! */ errmsg = "Missing left-hand side for binary operator"; return 0; } if (stack[sp].op != EXPR_VALUE) { errmsg = "Left-hand side of binary operator is not a value"; return 0; } if ((!stack[sp - 2].svalue) != (!stack[sp].svalue)) { errmsg = "Can't mix strings and integers in expression"; return 0; } } if (stack[sp].svalue) { /* A binary string expression */ switch (stack[sp - 1].value) { case SWIG_TOKEN_EQUALTO: stack[sp - 2].value = (Strcmp(stack[sp - 2].svalue, stack[sp].svalue) == 0); Delete(stack[sp - 2].svalue); Delete(stack[sp].svalue); sp -= 2; break; case SWIG_TOKEN_NOTEQUAL: stack[sp - 2].value = (Strcmp(stack[sp - 2].svalue, stack[sp].svalue) != 0); Delete(stack[sp - 2].svalue); Delete(stack[sp].svalue); sp -= 2; break; default: errmsg = "Syntax error: bad binary operator for strings"; return 0; } } else { switch (op_token) { case SWIG_TOKEN_STAR: stack[sp - 2].value = stack[sp - 2].value * stack[sp].value; sp -= 2; break; case SWIG_TOKEN_EQUALTO: stack[sp - 2].value = stack[sp - 2].value == stack[sp].value; sp -= 2; break; case SWIG_TOKEN_NOTEQUAL: stack[sp - 2].value = stack[sp - 2].value != stack[sp].value; sp -= 2; break; case SWIG_TOKEN_PLUS: stack[sp - 2].value = stack[sp - 2].value + stack[sp].value; sp -= 2; break; case SWIG_TOKEN_MINUS: stack[sp - 2].value = stack[sp - 2].value - stack[sp].value; sp -= 2; break; case SWIG_TOKEN_AND: stack[sp - 2].value = stack[sp - 2].value & stack[sp].value; sp -= 2; break; case SWIG_TOKEN_LAND: stack[sp - 2].value = stack[sp - 2].value && stack[sp].value; sp -= 2; break; case SWIG_TOKEN_OR: stack[sp - 2].value = stack[sp - 2].value | stack[sp].value; sp -= 2; break; case SWIG_TOKEN_LOR: stack[sp - 2].value = stack[sp - 2].value || stack[sp].value; sp -= 2; break; case SWIG_TOKEN_XOR: stack[sp - 2].value = stack[sp - 2].value ^ stack[sp].value; sp -= 2; break; case SWIG_TOKEN_LESSTHAN: stack[sp - 2].value = stack[sp - 2].value < stack[sp].value; sp -= 2; break; case SWIG_TOKEN_GREATERTHAN: stack[sp - 2].value = stack[sp - 2].value > stack[sp].value; sp -= 2; break; case SWIG_TOKEN_LTEQUAL: stack[sp - 2].value = stack[sp - 2].value <= stack[sp].value; sp -= 2; break; case SWIG_TOKEN_GTEQUAL: stack[sp - 2].value = stack[sp - 2].value >= stack[sp].value; sp -= 2; break; case SWIG_TOKEN_NOT: stack[sp - 1].value = ~stack[sp].value; sp--; break; case SWIG_TOKEN_LNOT: stack[sp - 1].value = !stack[sp].value; sp--; break; case OP_UMINUS: stack[sp - 1].value = -stack[sp].value; sp--; break; case OP_UPLUS: stack[sp - 1].value = stack[sp].value; sp--; break; case SWIG_TOKEN_SLASH: if (stack[sp].value != 0) { stack[sp - 2].value = stack[sp - 2].value / stack[sp].value; sp -= 2; } else { errmsg = "Division by zero in expression"; return 0; } break; case SWIG_TOKEN_PERCENT: if (stack[sp].value != 0) { stack[sp - 2].value = stack[sp - 2].value % stack[sp].value; sp -= 2; } else { errmsg = "Modulo by zero in expression"; return 0; } break; case SWIG_TOKEN_LSHIFT: stack[sp - 2].value = stack[sp - 2].value << stack[sp].value; sp -= 2; break; case SWIG_TOKEN_RSHIFT: stack[sp - 2].value = stack[sp - 2].value >> stack[sp].value; sp -= 2; break; default: errmsg = "Syntax error: bad operator"; return 0; } } stack[sp].op = EXPR_VALUE; stack[sp].svalue = 0; /* ensure it's not a string! */ return 1; } /* ----------------------------------------------------------------------------- * Preprocessor_expr_init() * * Initialize the expression evaluator * ----------------------------------------------------------------------------- */ void Preprocessor_expr_init(void) { if (!expr_init) init_precedence(); if (!scan) scan = NewScanner(); } void Preprocessor_expr_delete(void) { DelScanner(scan); } /* ----------------------------------------------------------------------------- * Tokenizer * ----------------------------------------------------------------------------- */ static int expr_token(Scanner * s) { int t; while (1) { t = Scanner_token(s); if (!((t == SWIG_TOKEN_BACKSLASH) || (t == SWIG_TOKEN_ENDLINE) || (t == SWIG_TOKEN_COMMENT))) break; } return t; } /* ----------------------------------------------------------------------------- * Preprocessor_expr() * * Evaluates an arithmetic expression. Returns the result and sets an error code. * ----------------------------------------------------------------------------- */ int Preprocessor_expr(DOH *s, int *error) { int token = 0; int op = 0; sp = 0; assert(s); assert(scan); Seek(s, 0, SEEK_SET); /* Printf(stdout,"evaluating : '%s'\n", s); */ *error = 0; Scanner_clear(scan); Scanner_push(scan, s); /* Put initial state onto the stack */ stack[sp].op = EXPR_TOP; while (1) { /* Look at the top of the stack */ switch (stack[sp].op) { case EXPR_TOP: /* EXPR_TOP is a place-holder which can only appear on the top of the * stack. We can reduce it to any expression - a number, a string, an * unary operator, or another expression enclosed in parentheses. */ token = expr_token(scan); if (!token) { errmsg = "Expected an expression"; *error = 1; return 0; } if (token == SWIG_TOKEN_BOOL) { /* A boolean value. Reduce EXPR_TOP to an EXPR_VALUE */ String *cc = Scanner_text(scan); if (Strcmp(cc, "true") == 0) { stack[sp].value = (long) 1; } else { stack[sp].value = (long) 0; } stack[sp].svalue = 0; stack[sp].op = EXPR_VALUE; } else if ((token == SWIG_TOKEN_INT) || (token == SWIG_TOKEN_UINT) || (token == SWIG_TOKEN_LONG) || (token == SWIG_TOKEN_ULONG)) { /* A number. Reduce EXPR_TOP to an EXPR_VALUE */ char *c = Char(Scanner_text(scan)); if (c[0] == '0' && (c[1] == 'b' || c[1] == 'B')) { /* strtol() doesn't handle binary constants */ stack[sp].value = (long) strtol(c + 2, 0, 2); } else { stack[sp].value = (long) strtol(c, 0, 0); } stack[sp].svalue = 0; stack[sp].op = EXPR_VALUE; } else if ((token == SWIG_TOKEN_MINUS) || (token == SWIG_TOKEN_PLUS) || (token == SWIG_TOKEN_LNOT) || (token == SWIG_TOKEN_NOT)) { if (token == SWIG_TOKEN_MINUS) token = OP_UMINUS; else if (token == SWIG_TOKEN_PLUS) token = OP_UPLUS; stack[sp].value = token; stack[sp].op = EXPR_OP; sp++; stack[sp].op = EXPR_TOP; } else if (token == SWIG_TOKEN_LPAREN) { stack[sp].op = EXPR_GROUP; sp++; stack[sp].op = EXPR_TOP; } else if (token == SWIG_TOKEN_ENDLINE) { } else if (token == SWIG_TOKEN_STRING) { stack[sp].svalue = NewString(Scanner_text(scan)); stack[sp].op = EXPR_VALUE; } else if (token == SWIG_TOKEN_ID) { int next_token = expr_token(scan); if (next_token == SWIG_TOKEN_LPAREN) { /* This is a use of an unknown function-like macro so we emit a * warning. */ errmsg = "Use of undefined function-like macro"; *error = 1; return 0; } Scanner_pushtoken(scan, next_token, Scanner_text(scan)); /* Defined macros have been expanded already so this is an unknown * macro, which gets treated as zero. */ stack[sp].value = 0; stack[sp].svalue = 0; stack[sp].op = EXPR_VALUE; } else if (token == SWIG_TOKEN_FLOAT || token == SWIG_TOKEN_DOUBLE || token == SWIG_TOKEN_LONGDOUBLE) { errmsg = "Floating point constant in preprocessor expression"; *error = 1; return 0; } else goto syntax_error; break; case EXPR_VALUE: /* A value is on top of the stack. We may reduce or evaluate depending * on what the next token is. */ token = expr_token(scan); if (!token) { /* End of input. Might have to reduce if an operator is on stack */ while (sp > 0) { if (stack[sp - 1].op == EXPR_OP) { if (!reduce_op()) goto reduce_error; } else if (stack[sp - 1].op == EXPR_GROUP) { errmsg = "Missing \')\'"; *error = 1; return 0; } else goto syntax_error; } return stack[sp].value; } /* Token must be an operator */ switch (token) { case SWIG_TOKEN_STAR: case SWIG_TOKEN_EQUALTO: case SWIG_TOKEN_NOTEQUAL: case SWIG_TOKEN_PLUS: case SWIG_TOKEN_MINUS: case SWIG_TOKEN_AND: case SWIG_TOKEN_LAND: case SWIG_TOKEN_OR: case SWIG_TOKEN_LOR: case SWIG_TOKEN_XOR: case SWIG_TOKEN_LESSTHAN: case SWIG_TOKEN_GREATERTHAN: case SWIG_TOKEN_LTEQUAL: case SWIG_TOKEN_GTEQUAL: case SWIG_TOKEN_SLASH: case SWIG_TOKEN_PERCENT: case SWIG_TOKEN_LSHIFT: case SWIG_TOKEN_RSHIFT: if ((sp == 0) || (stack[sp - 1].op == EXPR_GROUP)) { /* No possibility of reduce. Push operator and expression */ sp++; stack[sp].op = EXPR_OP; stack[sp].value = token; sp++; stack[sp].op = EXPR_TOP; } else { if (stack[sp - 1].op != EXPR_OP) goto syntax_error_expected_operator; op = stack[sp - 1].value; /* Previous operator */ /* Now, depending on the precedence relationship between the last operator and the current we will reduce or push */ if (prec[op] <= prec[token]) { /* Reduce the previous operator */ if (!reduce_op()) goto reduce_error; } sp++; stack[sp].op = EXPR_OP; stack[sp].value = token; sp++; stack[sp].op = EXPR_TOP; } break; case SWIG_TOKEN_RPAREN: if (sp == 0) goto extra_rparen; /* Might have to reduce operators first */ while ((sp > 0) && (stack[sp - 1].op == EXPR_OP)) { if (!reduce_op()) goto reduce_error; } if ((sp == 0) || (stack[sp - 1].op != EXPR_GROUP)) goto extra_rparen; stack[sp - 1].op = EXPR_VALUE; stack[sp - 1].value = stack[sp].value; stack[sp - 1].svalue = stack[sp].svalue; sp--; break; case SWIG_TOKEN_LTEQUALGT: goto spaceship_not_allowed; default: goto syntax_error_expected_operator; break; } break; default: Printf(stderr, "Internal error in expression evaluator.\n"); Exit(EXIT_FAILURE); } } syntax_error: errmsg = "Syntax error"; *error = 1; return 0; syntax_error_expected_operator: errmsg = "Syntax error: expected operator"; *error = 1; return 0; reduce_error: /* errmsg has been set by reduce_op */ *error = 1; return 0; extra_rparen: errmsg = "Extra \')\'"; *error = 1; return 0; spaceship_not_allowed: errmsg = "Spaceship operator (<=>) not allowed in preprocessor expression"; *error = 1; return 0; } /* ----------------------------------------------------------------------------- * Preprocessor_expr_error() * * Return error message set by the evaluator (if any) * ----------------------------------------------------------------------------- */ const char *Preprocessor_expr_error(void) { return errmsg; } swig-4.4.0/Source/Preprocessor/preprocessor.h0000664000175000017500000000303315075443613021204 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * preprocessor.h * * SWIG preprocessor module. * ----------------------------------------------------------------------------- */ #ifndef SWIG_PREPROCESSOR_H #define SWIG_PREPROCESSOR_H #include "swigwarn.h" #ifdef __cplusplus extern "C" { #endif extern int Preprocessor_expr(String *s, int *error); extern const char *Preprocessor_expr_error(void); extern Hash *Preprocessor_define(const_String_or_char_ptr str, int swigmacro); extern void Preprocessor_undef(const_String_or_char_ptr name); extern int Preprocessor_defined(const_String_or_char_ptr str); extern void Preprocessor_init(void); extern void Preprocessor_delete(void); extern String *Preprocessor_parse(String *s); extern void Preprocessor_include_all(int); extern void Preprocessor_import_all(int); extern void Preprocessor_ignore_missing(int); extern void Preprocessor_error_as_warning(int); extern List *Preprocessor_depend(void); extern void Preprocessor_expr_init(void); extern void Preprocessor_expr_delete(void); #ifdef __cplusplus } #endif #endif swig-4.4.0/Source/Preprocessor/cpp.c0000664000175000017500000015564715075443613017256 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * cpp.c * * An implementation of a C preprocessor plus some support for additional * SWIG directives. * * - SWIG directives such as %include and %import are handled * - A new macro %define ... %enddef can be used for multiline macros * - No preprocessing is performed in %{ ... %} blocks * - Lines beginning with %# are stripped down to #... and passed through. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "preprocessor.h" #include static Hash *cpp = 0; /* C preprocessor data */ static int include_all = 0; /* Follow all includes */ static int ignore_missing = 0; static int import_all = 0; /* Follow all includes, but as %import statements */ static int imported_depth = 0; /* Depth of %imported files */ static int single_include = 1; /* Only include each file once */ static Hash *included_files = 0; static List *dependencies = 0; static Scanner *id_scan = 0; static int error_as_warning = 0; /* Understand the cpp #error directive as a special #warning */ static int expand_defined_operator = 0; static int macro_level = 0; static int macro_start_line = 0; static const String * macro_start_file = 0; /* Test a character to see if it starts an identifier */ #define isidentifier(c) ((isalpha(c)) || (c == '_') || (c == '$')) /* Test a character to see if it valid in an identifier (after the first letter) */ #define isidchar(c) ((isalnum(c)) || (c == '_') || (c == '$')) static DOH *Preprocessor_replace(DOH *, DOH *); /* Skip whitespace */ static void skip_whitespace(String *s, String *out) { int c; while ((c = Getc(s)) != EOF) { if (!isspace(c)) { Ungetc(c, s); break; } else if (out) Putc(c, out); } } /* Skip to a specified character taking line breaks into account */ static int skip_tochar(String *s, int ch, String *out) { int c; while ((c = Getc(s)) != EOF) { if (out) Putc(c, out); if (c == ch) break; if (c == '\\') { c = Getc(s); if ((c != EOF) && (out)) Putc(c, out); } } if (c == EOF) return -1; return 0; } /* Skip to a specified character or the end of the line */ static int skip_tochar_or_eol(String *s, int ch, String *out) { int c; while ((c = Getc(s)) != EOF) { if (c == '\n') { Ungetc(c, s); break; } if (out) Putc(c, out); if (c == ch) break; if (c == '\\') { c = Getc(s); if ((c != EOF) && (out)) Putc(c, out); } } if (c == EOF) return -1; return 0; } static void copy_location(const DOH *s1, DOH *s2) { Setfile(s2, Getfile((DOH *) s1)); Setline(s2, Getline((DOH *) s1)); } static String *cpp_include(const_String_or_char_ptr fn, int sysfile) { String *s = sysfile ? Swig_include_sys(fn) : Swig_include(fn); if (s && single_include) { String *file = Getfile(s); if (Getattr(included_files, file)) { Delete(s); return 0; } Setattr(included_files, file, file); } if (!s) { if (ignore_missing) { Swig_warning(WARN_PP_MISSING_FILE, Getfile(fn), Getline(fn), "Unable to find '%s'\n", fn); } else { Swig_error(Getfile(fn), Getline(fn), "Unable to find '%s'\n", fn); } } else { String *lf; Seek(s, 0, SEEK_SET); if (!dependencies) { dependencies = NewList(); } lf = Copy(Swig_last_file()); Append(dependencies, lf); Delete(lf); } return s; } static int is_digits(const String *str) { const char *s = Char(str); int isdigits = (*s != 0); while (*s) { if (!isdigit((int)*s)) { isdigits = 0; break; } s++; } return isdigits; } List *Preprocessor_depend(void) { return dependencies; } /* ----------------------------------------------------------------------------- * void Preprocessor_cpp_init() - Initialize the preprocessor * ----------------------------------------------------------------------------- */ static String *kpp_args = 0; static String *kpp_define = 0; static String *kpp_defined = 0; static String *kpp_elif = 0; static String *kpp_else = 0; static String *kpp_endif = 0; static String *kpp_expanded = 0; static String *kpp_if = 0; static String *kpp_ifdef = 0; static String *kpp_ifndef = 0; static String *kpp_name = 0; static String *kpp_swigmacro = 0; static String *kpp_symbols = 0; static String *kpp_undef = 0; static String *kpp_value = 0; static String *kpp_varargs = 0; static String *kpp_error = 0; static String *kpp_warning = 0; static String *kpp_line = 0; static String *kpp_include = 0; static String *kpp_pragma = 0; static String *kpp_level = 0; static String *kpp_dline = 0; static String *kpp_ddefine = 0; static String *kpp_dinclude = 0; static String *kpp_dimport = 0; static String *kpp_dbeginfile = 0; static String *kpp_LINE = 0; static String *kpp_FILE = 0; static String *kpp_hash_if = 0; static String *kpp_hash_elif = 0; void Preprocessor_init(void) { Hash *s; kpp_args = NewString("args"); kpp_define = NewString("define"); kpp_defined = NewString("defined"); kpp_else = NewString("else"); kpp_elif = NewString("elif"); kpp_endif = NewString("endif"); kpp_expanded = NewString("*expanded*"); kpp_if = NewString("if"); kpp_ifdef = NewString("ifdef"); kpp_ifndef = NewString("ifndef"); kpp_name = NewString("name"); kpp_swigmacro = NewString("swigmacro"); kpp_symbols = NewString("symbols"); kpp_undef = NewString("undef"); kpp_value = NewString("value"); kpp_error = NewString("error"); kpp_warning = NewString("warning"); kpp_pragma = NewString("pragma"); kpp_level = NewString("level"); kpp_line = NewString("line"); kpp_include = NewString("include"); kpp_varargs = NewString("varargs"); kpp_dinclude = NewString("%include"); kpp_dimport = NewString("%import"); kpp_dbeginfile = NewString("%beginfile"); kpp_ddefine = NewString("%define"); kpp_dline = NewString("%line"); kpp_LINE = NewString("__LINE__"); kpp_FILE = NewString("__FILE__"); kpp_hash_if = NewString("#if"); kpp_hash_elif = NewString("#elif"); cpp = NewHash(); s = NewHash(); Setattr(cpp, kpp_symbols, s); Delete(s); Preprocessor_expr_init(); /* Initialize the expression evaluator */ included_files = NewHash(); id_scan = NewScanner(); } void Preprocessor_delete(void) { Delete(kpp_args); Delete(kpp_define); Delete(kpp_defined); Delete(kpp_else); Delete(kpp_elif); Delete(kpp_endif); Delete(kpp_expanded); Delete(kpp_if); Delete(kpp_ifdef); Delete(kpp_ifndef); Delete(kpp_name); Delete(kpp_swigmacro); Delete(kpp_symbols); Delete(kpp_undef); Delete(kpp_value); Delete(kpp_error); Delete(kpp_warning); Delete(kpp_pragma); Delete(kpp_level); Delete(kpp_line); Delete(kpp_include); Delete(kpp_varargs); Delete(kpp_dinclude); Delete(kpp_dimport); Delete(kpp_dbeginfile); Delete(kpp_ddefine); Delete(kpp_dline); Delete(kpp_LINE); Delete(kpp_FILE); Delete(kpp_hash_if); Delete(kpp_hash_elif); Delete(cpp); Delete(included_files); Preprocessor_expr_delete(); DelScanner(id_scan); Delete(dependencies); Delete(Swig_add_directory(0)); } /* ----------------------------------------------------------------------------- * void Preprocessor_include_all() - Instruct preprocessor to include all files * ----------------------------------------------------------------------------- */ void Preprocessor_include_all(int a) { include_all = a; } void Preprocessor_import_all(int a) { import_all = a; } void Preprocessor_ignore_missing(int a) { ignore_missing = a; } void Preprocessor_error_as_warning(int a) { error_as_warning = a; } /* ----------------------------------------------------------------------------- * Preprocessor_define() * * Defines a new C preprocessor symbol. swigmacro specifies whether or not the macro has * SWIG macro semantics. * ----------------------------------------------------------------------------- */ static String *Macro_vararg_name(const_String_or_char_ptr str, const_String_or_char_ptr line) { String *varargname; char *s, *dots; s = Char(str); dots = strchr(s, '.'); if (!dots) { return NULL; } if (strcmp(dots, "...") != 0) { Swig_error(Getfile(line), Getline(line), "Illegal macro argument name '%s'\n", str); return NULL; } if (dots == s) { varargname = NewString("__VA_ARGS__"); } else { varargname = NewStringWithSize(s, (int)(dots - s)); } return varargname; } Hash *Preprocessor_define(const_String_or_char_ptr _str, int swigmacro) { String *macroname = 0, *argstr = 0, *macrovalue = 0, *file = 0, *s = 0; Hash *macro = 0, *symbols = 0, *m1; List *arglist = 0; int c, line; int varargs = 0; String *str; assert(cpp); assert(_str); /* First make sure that string is actually a string */ if (DohCheck(_str)) { s = Copy(_str); copy_location(_str, s); str = s; } else { str = NewString((char *) _str); } Seek(str, 0, SEEK_SET); line = Getline(str); file = Getfile(str); /* Skip over any leading whitespace */ skip_whitespace(str, 0); /* Now look for a macro name */ macroname = NewStringEmpty(); copy_location(str, macroname); while ((c = Getc(str)) != EOF) { if (c == '(') { argstr = NewStringEmpty(); copy_location(str, argstr); /* It is a macro. Go extract its argument string */ while ((c = Getc(str)) != EOF) { if (c == ')') break; else Putc(c, argstr); } if (c != ')') { Swig_error(Getfile(argstr), Getline(argstr), "Missing \')\' in macro parameters\n"); goto macro_error; } break; } else if (isidchar(c) || (c == '%')) { Putc(c, macroname); } else if (isspace(c)) { break; } else if (c == '\\') { c = Getc(str); if (c != '\n') { Ungetc(c, str); Ungetc('\\', str); break; } } else { Ungetc(c, str); break; } } if (!swigmacro) skip_whitespace(str, 0); macrovalue = NewStringEmpty(); copy_location(str, macrovalue); while ((c = Getc(str)) != EOF) { Putc(c, macrovalue); } /* If there are any macro arguments, convert into a list */ if (argstr) { String *argname, *varargname; arglist = NewList(); Seek(argstr, 0, SEEK_SET); argname = NewStringEmpty(); while ((c = Getc(argstr)) != EOF) { if (c == ',') { varargname = Macro_vararg_name(argname, argstr); if (varargname) { Delete(varargname); Swig_error(Getfile(argstr), Getline(argstr), "Variable length macro argument must be last parameter\n"); } else { Append(arglist, argname); } Delete(argname); argname = NewStringEmpty(); } else if (isidchar(c) || (c == '.')) { Putc(c, argname); } else if (!(isspace(c) || (c == '\\'))) { Delete(argname); Swig_error(Getfile(argstr), Getline(argstr), "Illegal character in macro argument name\n"); goto macro_error; } } if (Len(argname)) { /* Check for varargs */ varargname = Macro_vararg_name(argname, argstr); if (varargname) { Append(arglist, varargname); Delete(varargname); varargs = 1; } else { Append(arglist, argname); } } Delete(argname); } if (!swigmacro) { Replace(macrovalue, "\\\n", " ", DOH_REPLACE_NOQUOTE); } /* Look for special # substitutions. We only consider # that appears outside of quotes and comments */ { int state = 0; char *cc = Char(macrovalue); while (*cc) { switch (state) { case 0: if (*cc == '#') *cc = '\001'; else if (*cc == '/') state = 10; else if (*cc == '\'') state = 20; else if (*cc == '\"') state = 30; break; case 10: if (*cc == '*') state = 11; else if (*cc == '/') state = 15; else { state = 0; cc--; } break; case 11: if (*cc == '*') state = 12; break; case 12: if (*cc == '/') state = 0; else if (*cc != '*') state = 11; break; case 15: if (*cc == '\n') state = 0; break; case 20: if (*cc == '\'') state = 0; if (*cc == '\\') state = 21; break; case 21: state = 20; break; case 30: if (*cc == '\"') state = 0; if (*cc == '\\') state = 31; break; case 31: state = 30; break; default: break; } cc++; } } /* Get rid of whitespace surrounding # */ /* Replace(macrovalue,"#","\001",DOH_REPLACE_NOQUOTE); */ while (Replace(macrovalue, "\001 ", "\001", DOH_REPLACE_ANY) > 0) { } while (Replace(macrovalue, " \001", "\001", DOH_REPLACE_ANY) > 0) { } /* Replace '##' with a special token */ Replace(macrovalue, "\001\001", "\002", DOH_REPLACE_ANY); /* Replace '#@' with a special token */ Replace(macrovalue, "\001@", "\004", DOH_REPLACE_ANY); /* Replace '##@' with a special token */ Replace(macrovalue, "\002@", "\005", DOH_REPLACE_ANY); if (varargs) { /* Replace '__VA_OPT__' with a special token */ Replace(macrovalue, "__VA_OPT__", "\006", DOH_REPLACE_ID|DOH_REPLACE_ANY); } /* Go create the macro */ macro = NewHash(); Setattr(macro, kpp_name, macroname); if (arglist) { Setattr(macro, kpp_args, arglist); Delete(arglist); if (varargs) { Setattr(macro, kpp_varargs, "1"); } } Setattr(macro, kpp_value, macrovalue); Setline(macro, line); Setfile(macro, file); if (swigmacro) { Setattr(macro, kpp_swigmacro, "1"); } symbols = Getattr(cpp, kpp_symbols); if ((m1 = Getattr(symbols, macroname))) { if (!Checkattr(m1, kpp_value, macrovalue)) { Swig_error(Getfile(macroname), Getline(macroname), "Macro '%s' redefined,\n", macroname); Swig_error(Getfile(m1), Getline(m1), "previous definition of '%s'.\n", macroname); goto macro_error; } } else { Setattr(symbols, macroname, macro); Delete(macro); } Delete(macroname); Delete(macrovalue); Delete(str); Delete(argstr); return macro; macro_error: Delete(str); Delete(argstr); Delete(arglist); Delete(macroname); Delete(macrovalue); return 0; } /* ----------------------------------------------------------------------------- * Preprocessor_undef() * * Undefines a macro. * ----------------------------------------------------------------------------- */ void Preprocessor_undef(const_String_or_char_ptr str) { Hash *symbols; assert(cpp); symbols = Getattr(cpp, kpp_symbols); Delattr(symbols, str); } /* ----------------------------------------------------------------------------- * Preprocessor_defined() * * Check if a macro is defined. * ----------------------------------------------------------------------------- */ int Preprocessor_defined(const_String_or_char_ptr str) { Hash *symbols; assert(cpp); symbols = Getattr(cpp, kpp_symbols); return Getattr(symbols, str) != NULL; } /* ----------------------------------------------------------------------------- * find_args() * * Isolates macro arguments and returns them in a list. For each argument, * leading and trailing whitespace is stripped (ala K&R, pg. 230). * ----------------------------------------------------------------------------- */ static List *find_args(String *s, int ismacro, String *macro_name) { List *args; String *str; int c, level; long pos; /* Create a new list */ args = NewList(); copy_location(s, args); /* First look for a '(' */ pos = Tell(s); skip_whitespace(s, 0); /* Now see if the next character is a '(' */ c = Getc(s); if (c != '(') { /* Not a macro, bail out now! */ assert(pos != -1); (void)Seek(s, pos, SEEK_SET); Delete(args); return 0; } c = Getc(s); /* Okay. This appears to be a macro so we will start isolating arguments */ while (c != EOF) { if (isspace(c)) { skip_whitespace(s, 0); /* Skip leading whitespace */ c = Getc(s); } str = NewStringEmpty(); copy_location(s, str); level = 0; while (c != EOF) { if (c == '\"') { Putc(c, str); skip_tochar(s, '\"', str); c = Getc(s); continue; } else if (c == '\'') { Putc(c, str); skip_tochar(s, '\'', str); c = Getc(s); continue; } else if (c == '/') { /* Ensure comments are ignored by eating up the characters */ c = Getc(s); /* Handle / * ... * / type comments (multi-line) */ if (c == '*') { while ((c = Getc(s)) != EOF) { if (c == '*') { another_star: c = Getc(s); if (c == '/' || c == EOF) break; if (c == '*') goto another_star; } } c = Getc(s); continue; } /* Handle // ... type comments (single-line) */ if (c == '/') { while ((c = Getc(s)) != EOF) { if (c == '\n') { break; } } c = Getc(s); continue; } /* ensure char is available in the stream as this was not a comment*/ Ungetc(c, s); c = '/'; } if ((c == ',') && (level == 0)) break; if ((c == ')') && (level == 0)) break; Putc(c, str); if (c == '(') level++; if (c == ')') level--; c = Getc(s); } if (level > 0) { goto unterm; } Chop(str); Append(args, str); Delete(str); if (c == ')') return args; c = Getc(s); } unterm: if (ismacro) Swig_error(Getfile(args), Getline(args), "Unterminated call invoking macro '%s'\n", macro_name); else Swig_error(Getfile(args), Getline(args), "Unterminated call to '%s'\n", macro_name); return args; } /* ----------------------------------------------------------------------------- * DOH *get_filename() * * Read a filename from str. A filename can be enclosed in quotes, angle brackets, * or bare. * ----------------------------------------------------------------------------- */ static String *get_filename(String *str, int *sysfile) { String *fn; int c; fn = NewStringEmpty(); copy_location(str, fn); c = Getc(str); *sysfile = 0; if (c == '\"') { while (((c = Getc(str)) != EOF) && (c != '\"')) Putc(c, fn); } else if (c == '<') { *sysfile = 1; while (((c = Getc(str)) != EOF) && (c != '>')) Putc(c, fn); } else { String *preprocessed_str; Putc(c, fn); while (((c = Getc(str)) != EOF) && (!isspace(c))) Putc(c, fn); if (isspace(c)) Ungetc(c, str); preprocessed_str = Preprocessor_replace(fn, NULL); Seek(preprocessed_str, 0, SEEK_SET); Delete(fn); fn = NewStringEmpty(); copy_location(preprocessed_str, fn); c = Getc(preprocessed_str); if (c == '\"') { while (((c = Getc(preprocessed_str)) != EOF) && (c != '\"')) Putc(c, fn); } else if (c == '<') { *sysfile = 1; while (((c = Getc(preprocessed_str)) != EOF) && (c != '>')) Putc(c, fn); } else { fn = Copy(preprocessed_str); } Delete(preprocessed_str); } Swig_filename_unescape(fn); Swig_filename_correct(fn); Seek(fn, 0, SEEK_SET); return fn; } static String *get_options(String *str) { int c; c = Getc(str); if (c == '(') { String *opt; int level = 1; opt = NewString("("); while (((c = Getc(str)) != EOF)) { Putc(c, opt); switch (c) { case ')': level--; if (!level) return opt; break; case '(': level++; break; case '"': /* Skip over quoted strings */ while (1) { c = Getc(str); if (c == EOF) goto bad; Putc(c, opt); if (c == '"') break; if (c == '\\') { c = Getc(str); if (c == EOF) goto bad; Putc(c, opt); } } break; } } bad: Delete(opt); return 0; } else { Ungetc(c, str); return 0; } } /* ----------------------------------------------------------------------------- * expand_macro() * * Perform macro expansion and return a new string. Returns NULL if some sort * of error occurred. * name - name of the macro * args - arguments passed to the macro * line_file - global context, used for line/file name when reporting errors * ----------------------------------------------------------------------------- */ static String *expand_macro(String *name, List *args, String *line_file) { String *ns; DOH *symbols, *macro, *margs, *mvalue, *temp, *tempa, *e; int i, l; int isvarargs = 0; symbols = Getattr(cpp, kpp_symbols); if (!symbols) return 0; /* See if the name is actually defined */ macro = Getattr(symbols, name); if (!macro) return 0; if (macro_level == 0) { /* Store the start of the macro should the macro contain __LINE__ and __FILE__ for expansion */ macro_start_line = Getline(args ? args : line_file); macro_start_file = Getfile(args ? args : line_file); } macro_level++; if (Getattr(macro, kpp_expanded)) { ns = NewStringEmpty(); Append(ns, name); if (args) { int lenargs = Len(args); if (lenargs) Putc('(', ns); for (i = 0; i < lenargs; i++) { Append(ns, Getitem(args, i)); if (i < (lenargs - 1)) Putc(',', ns); } if (i) Putc(')', ns); } macro_level--; return ns; } /* Get macro arguments and value */ mvalue = Getattr(macro, kpp_value); assert(mvalue); margs = Getattr(macro, kpp_args); if (args && Getattr(macro, kpp_varargs)) { isvarargs = 1; /* Variable length argument macro. We need to collect all of the extra arguments into a single argument */ if (Len(args) >= (Len(margs) - 1)) { int i; int vi, na; String *vararg = NewStringEmpty(); vi = Len(margs) - 1; na = Len(args); for (i = vi; i < na; i++) { Append(vararg, Getitem(args, i)); if ((i + 1) < na) { Append(vararg, ","); } } /* Remove arguments */ for (i = vi; i < na; i++) { Delitem(args, vi); } Append(args, vararg); Delete(vararg); } } if (args && margs && Len(margs) == 0 && Len(args) == 1 && Len(Getitem(args, 0)) == 0) { /* FOO() can invoke a macro defined as FOO(X) as well as one defined FOO(). * * Handle this by removing the only argument if it's empty and the macro * expects no arguments. * * We don't need to worry about varargs here - a varargs macro will always have * Len(margs) >= 1, since the varargs are put in the final macro argument. */ Delitem(args, 0); } /* If there are arguments, see if they match what we were given */ if (args && (!margs || Len(margs) != Len(args))) { if (margs && Len(margs) > (1 + isvarargs)) Swig_error(macro_start_file, macro_start_line, "Macro '%s' expects %d arguments\n", name, Len(margs) - isvarargs); else if (margs && Len(margs) == (1 + isvarargs)) Swig_error(macro_start_file, macro_start_line, "Macro '%s' expects 1 argument\n", name); else Swig_error(macro_start_file, macro_start_line, "Macro '%s' expects no arguments\n", name); macro_level--; return 0; } /* If the macro expects arguments, but none were supplied, we leave it in place */ if (!args && margs) { macro_level--; return NewString(name); } /* Copy the macro value */ ns = Copy(mvalue); copy_location(mvalue, ns); /* Tag the macro as being expanded. This is to avoid recursion in macro expansion */ temp = NewStringEmpty(); tempa = NewStringEmpty(); if (args && margs) { l = Len(margs); for (i = 0; i < l; i++) { DOH *arg, *aname; String *reparg; arg = Getitem(args, i); /* Get an argument value */ reparg = Preprocessor_replace(arg, NULL); aname = Getitem(margs, i); /* Get macro argument name */ if (strchr(Char(ns), '\001')) { /* Try to replace a quoted version of the argument */ Clear(temp); Clear(tempa); Printf(temp, "\001%s", aname); Printf(tempa, "\"%s\"", arg); Replace(ns, temp, tempa, DOH_REPLACE_ID_END); } if (isvarargs && i == l - 1) { char *s = Char(ns); char *a = s; while ((a = strchr(a, '\006')) != NULL) { *a = ' '; while (isspace((unsigned char)*++a)) { } if (*a == '(') { char *e = a; int depth = 1; while (*++e) { if (*e == ')') { if (--depth == 0) break; } else if (*e == '(') { ++depth; } } if (*e) { if (Len(arg) == 0) { // Empty varargs so replace ( and ) and everything between with // spaces. memset(a, ' ', e - a + 1); } else { // Non-empty varargs so replace ( and ) with spaces. *a = ' '; *e = ' '; } } a = e + 1; } } } if (strchr(Char(ns), '\002')) { /* Look for concatenation tokens */ Clear(temp); Clear(tempa); Printf(temp, "\002%s", aname); Append(tempa, "\002\003"); Replace(ns, temp, tempa, DOH_REPLACE_ID_END); if (isvarargs && i == l - 1 && Len(arg) == 0) { // ## followed by a zero length varargs macro argument - if preceded // by a comma (possibly with whitespace in between) we nuke the // comma. char *s = Char(ns); char *a = s + 1; while ((a = strstr(a, "\002\003")) != NULL) { char *t = a; while (--t >= s) { if (!isspace((unsigned char)*t)) { if (*t == ',') *t = ' '; break; } } // Advance 3 since we're only interested in \002\003 when it could // be preceded by a comma. a += 3; } } Clear(temp); Clear(tempa); Printf(temp, "%s\002", aname); Append(tempa, "\003\002"); Replace(ns, temp, tempa, DOH_REPLACE_ID_BEGIN); } /* Non-standard macro expansion. The value `x` is replaced by a quoted version of the argument except that if the argument is already quoted nothing happens */ if (strchr(Char(ns), '`')) { String *rep; char *c; Clear(temp); Printf(temp, "`%s`", aname); c = Char(arg); if (*c == '"') { rep = arg; } else { Clear(tempa); Printf(tempa, "\"%s\"", arg); rep = tempa; } Replace(ns, temp, rep, DOH_REPLACE_ANY); } /* Non-standard mangle expansions. The #@Name is replaced by mangle_arg(Name). */ if (strchr(Char(ns), '\004')) { String *marg = Swig_name_mangle_string(arg); Clear(temp); Printf(temp, "\004%s", aname); Replace(ns, temp, marg, DOH_REPLACE_ID_END); Delete(marg); } if (strchr(Char(ns), '\005')) { String *marg = Swig_name_mangle_string(arg); Clear(temp); Clear(tempa); Printf(temp, "\005%s", aname); Printf(tempa, "\"%s\"", marg); Replace(ns, temp, tempa, DOH_REPLACE_ID_END); Delete(marg); } /* Replace(ns, aname, arg, DOH_REPLACE_ID); */ Replace(ns, aname, reparg, DOH_REPLACE_ID); /* Replace expanded args */ Replace(ns, "\003", arg, DOH_REPLACE_ANY); /* Replace unexpanded arg */ Delete(reparg); } } Replace(ns, "\002", "", DOH_REPLACE_ANY); /* Get rid of concatenation tokens */ Replace(ns, "\001", "#", DOH_REPLACE_ANY); /* Put # back (non-standard C) */ Replace(ns, "\004", "#@", DOH_REPLACE_ANY); /* Put # back (non-standard C) */ /* Expand this macro even further */ Setattr(macro, kpp_expanded, "1"); e = Preprocessor_replace(ns, line_file); Delattr(macro, kpp_expanded); Delete(ns); if (Getattr(macro, kpp_swigmacro)) { String *g; String *f = NewStringEmpty(); Seek(e, 0, SEEK_SET); copy_location(macro, e); g = Preprocessor_parse(e); #if 0 /* Drop the macro in place, but with a marker around it */ Printf(f, "/*@%s,%d,%s@*/%s/*@@*/", Getfile(macro), Getline(macro), name, g); #else /* Use simplified around markers to properly count lines in cscanner.c */ if (strchr(Char(g), '\n')) { Printf(f, "/*@SWIG:%s,%d,%s@*/%s/*@SWIG@*/", Getfile(macro), Getline(macro), name, g); #if 0 Printf(f, "/*@SWIG:%s@*/%s/*@SWIG@*/", name, g); #endif } else { Append(f, g); } #endif Delete(g); Delete(e); e = f; } macro_level--; Delete(temp); Delete(tempa); return e; } /* ----------------------------------------------------------------------------- * DOH *Preprocessor_replace(DOH *s, DOH *line_file) * * Performs a macro substitution on a string s. Returns a new string with * substitutions applied. This function works by walking down s and looking * for identifiers. When found, a check is made to see if they are macros * which are then expanded. * ----------------------------------------------------------------------------- */ /* #define SWIG_PUT_BUFF */ static DOH *Preprocessor_replace(DOH *s, DOH *line_file) { DOH *ns, *symbols, *m; int c, i, state = 0; String *id = NewStringEmpty(); assert(cpp); symbols = Getattr(cpp, kpp_symbols); ns = NewStringEmpty(); copy_location(s, ns); Seek(s, 0, SEEK_SET); /* Try to locate identifiers in s and replace them with macro replacements */ while ((c = Getc(s)) != EOF) { switch (state) { case 0: if (isidentifier(c)) { Clear(id); Putc(c, id); state = 4; } else if (c == '%') { Clear(id); Putc(c, id); state = 2; } else if (c == '#') { Clear(id); Putc(c, id); state = 4; } else if (c == '\"') { Putc(c, ns); skip_tochar(s, '\"', ns); } else if (c == '\'') { Putc(c, ns); skip_tochar(s, '\'', ns); } else if (c == '/') { Putc(c, ns); state = 10; } else if (c == '\\') { Putc(c, ns); c = Getc(s); if (c == '\n') { Putc(c, ns); } else { Ungetc(c, s); } } else if (c == '\n') { Putc(c, ns); expand_defined_operator = 0; } else { Putc(c, ns); } break; case 2: /* Found '%#' */ if (c == '#') { Putc(c, id); state = 4; } else { Ungetc(c, s); state = 4; } break; case 4: /* An identifier */ if (isidchar(c)) { Putc(c, id); state = 4; } else { /* We found the end of a valid identifier */ Ungetc(c, s); /* See if this is the special "defined" operator */ if (Equal(kpp_defined, id)) { if (expand_defined_operator) { int lenargs = 0; DOH *args = 0; /* See whether or not a parenthesis has been used */ skip_whitespace(s, 0); c = Getc(s); if (c == '(') { Ungetc(c, s); args = find_args(s, 0, kpp_defined); } else if (isidchar(c)) { DOH *arg = NewStringEmpty(); args = NewList(); Putc(c, arg); while (((c = Getc(s)) != EOF)) { if (!isidchar(c)) { Ungetc(c, s); break; } Putc(c, arg); } if (Len(arg)) Append(args, arg); Delete(arg); } else { Seek(s, -1, SEEK_CUR); } lenargs = Len(args); if ((!args) || (!lenargs)) { /* This is not a defined() operator. */ Append(ns, id); state = 0; break; } for (i = 0; i < lenargs; i++) { DOH *o = Getitem(args, i); if (!Getattr(symbols, o)) { break; } } if (i < lenargs) Putc('0', ns); else Putc('1', ns); Delete(args); } else { Append(ns, id); } state = 0; break; } else if (Equal(kpp_LINE, id)) { Printf(ns, "%d", macro_level > 0 ? macro_start_line : Getline(s)); state = 0; break; } else if (Equal(kpp_FILE, id)) { String *fn = Copy(macro_level > 0 ? macro_start_file : Getfile(s)); Replaceall(fn, "\\", "\\\\"); Printf(ns, "\"%s\"", fn); Delete(fn); state = 0; break; } else if (Equal(kpp_hash_if, id) || Equal(kpp_hash_elif, id)) { expand_defined_operator = 1; Append(ns, id); } else if (Equal("#ifdef", id) || Equal("#ifndef", id)) { /* Evaluate the defined-ness check and substitute `#if 0` or `#if 1`. */ int allow = 0; skip_whitespace(s, 0); c = Getc(s); if (isidchar(c)) { DOH *arg = NewStringEmpty(); Putc(c, arg); while (((c = Getc(s)) != EOF)) { if (!isidchar(c)) { Ungetc(c, s); break; } Putc(c, arg); } if (Equal("#ifdef", id)) { if (Getattr(symbols, arg)) allow = 1; } else { if (!Getattr(symbols, arg)) allow = 1; } Delete(arg); } else { Ungetc(c, s); Swig_error(Getfile(s), Getline(s), "Missing identifier for %s.\n", id); } if (allow) { Append(ns, "#if 1"); } else { Append(ns, "#if 0"); } } else if ((m = Getattr(symbols, id))) { /* See if the macro is defined in the preprocessor symbol table */ DOH *args = 0; DOH *e; int macro_additional_lines = 0; /* See if the macro expects arguments */ if (Getattr(m, kpp_args)) { /* Yep. We need to go find the arguments and do a substitution */ int line = Getline(s); args = find_args(s, 1, id); macro_additional_lines = Getline(s) - line; assert(macro_additional_lines >= 0); } else { args = 0; } e = expand_macro(id, args, s); if (e) { Append(ns, e); } while (macro_additional_lines--) { Putc('\n', ns); } Delete(e); Delete(args); } else { Append(ns, id); } state = 0; } break; case 10: if (c == '/') state = 11; else if (c == '*') state = 12; else { Ungetc(c, s); state = 0; break; } Putc(c, ns); break; case 11: /* in C++ comment */ Putc(c, ns); if (c == '\n') { expand_defined_operator = 0; state = 0; } break; case 12: /* in C comment */ Putc(c, ns); if (c == '*') state = 13; break; case 13: Putc(c, ns); if (c == '/') state = 0; else if (c != '*') state = 12; break; default: state = 0; break; } } /* Identifier at the end */ if (state == 2 || state == 4) { /* See if this is the special "defined" operator */ if (Equal(kpp_defined, id)) { Swig_error(Getfile(s), Getline(s), "No arguments given to defined()\n"); } else if (Equal(kpp_LINE, id)) { Printf(ns, "%d", macro_level > 0 ? macro_start_line : Getline(s)); } else if (Equal(kpp_FILE, id)) { String *fn = Copy(macro_level > 0 ? macro_start_file : Getfile(s)); Replaceall(fn, "\\", "\\\\"); Printf(ns, "\"%s\"", fn); Delete(fn); } else if ((m = Getattr(symbols, id))) { /* Yes. There is a macro here */ /* If it expects arguments, they must come from `line_file` */ DOH *args = 0; DOH *e; int macro_additional_lines = 0; /* See if the macro expects arguments */ if (Getattr(m, kpp_args) && line_file) { /* Yep. We need to go find the arguments and do a substitution */ int line = Getline(line_file); args = find_args(line_file, 1, id); macro_additional_lines = Getline(line_file) - line; assert(macro_additional_lines >= 0); } else { args = 0; } e = expand_macro(id, args, s); if (e) { Append(ns, e); } while (macro_additional_lines--) { Putc('\n', ns); } Delete(e); Delete(args); } else { Append(ns, id); } } Delete(id); return ns; } /* ----------------------------------------------------------------------------- * int checkpp_id(DOH *s) * * Checks the string s to see if it contains any unresolved identifiers. This * function contains the heuristic that determines whether or not a macro * definition passes through the preprocessor as a constant declaration. * ----------------------------------------------------------------------------- */ static int checkpp_id(DOH *s) { int c; int hastok = 0; Scanner *scan = id_scan; Seek(s, 0, SEEK_SET); Scanner_clear(scan); s = Copy(s); Seek(s, SEEK_SET, 0); Scanner_push(scan, s); while ((c = Scanner_token(scan))) { hastok = 1; if ((c == SWIG_TOKEN_ID) || (c == SWIG_TOKEN_LBRACE) || (c == SWIG_TOKEN_RBRACE)) return 1; } if (!hastok) return 1; return 0; } /* addline(). Utility function for adding lines to a chunk */ static void addline(DOH *s1, DOH *s2, int allow) { if (allow) { Append(s1, s2); } else { int len = Len(s2); for (int i = 0; i < len; ++i) { if (Char(s2)[i] == '\n') Putc('\n', s1); } } } static void add_chunk(DOH *ns, DOH *chunk, int allow) { DOH *echunk; Seek(chunk, 0, SEEK_SET); if (allow) { echunk = Preprocessor_replace(chunk, NULL); addline(ns, echunk, allow); Delete(echunk); } else { addline(ns, chunk, 0); } Clear(chunk); } /* push/pop_imported(): helper functions for defining and undefining SWIGIMPORTED (when %importing a file). */ static void push_imported(void) { if (imported_depth == 0) { Preprocessor_define("SWIGIMPORTED 1", 0); } ++imported_depth; } static void pop_imported(void) { --imported_depth; if (imported_depth == 0) { Preprocessor_undef("SWIGIMPORTED"); } } /* ----------------------------------------------------------------------------- * Preprocessor_parse() * * Parses the string s. Returns a new string containing the preprocessed version. * * Parsing rules : * 1. Lines starting with # are C preprocessor directives * 2. Macro expansion inside strings is not allowed * 3. All code inside false conditionals is changed to blank lines * 4. Code in %{, %} is not parsed because it may need to be * included inline (with all preprocessor directives included). * ----------------------------------------------------------------------------- */ String *Preprocessor_parse(String *s) { String *ns; /* New string containing the preprocessed text */ String *chunk, *decl; Hash *symbols; String *id = 0, *value = 0, *comment = 0; int i, state, e, c; int start_line = 0; int allow = 1; int level = 0; int dlevel = 0; int filelevel = 0; int mask = 0; int start_level = 0; int cpp_lines = 0; int cond_lines[256]; /* Blow away all carriage returns */ Replace(s, "\015", "", DOH_REPLACE_ANY); ns = NewStringEmpty(); /* Return result */ decl = NewStringEmpty(); id = NewStringEmpty(); value = NewStringEmpty(); comment = NewStringEmpty(); chunk = NewStringEmpty(); copy_location(s, chunk); copy_location(s, ns); symbols = Getattr(cpp, kpp_symbols); state = 0; while ((c = Getc(s)) != EOF) { switch (state) { case 0: /* Initial state - in first column */ /* Look for C preprocessor directives. Otherwise, go directly to state 1 */ if (c == '#') { copy_location(s, chunk); add_chunk(ns, chunk, allow); cpp_lines = 1; state = 40; } else if (isspace(c)) { Putc(c, chunk); skip_whitespace(s, chunk); } else { state = 1; Ungetc(c, s); } break; case 1: { /* Non-preprocessor directive */ /* Look for SWIG directives */ state1: if (c == '%') { state = 100; break; } Putc(c, chunk); if (c == '\n') state = 0; else if (c == '\"') { start_line = Getline(s); if (skip_tochar(s, '\"', chunk) < 0) { Swig_error(Getfile(s), start_line, "Unterminated string constant\n"); } } else if (c == '\'') { start_line = Getline(s); if (skip_tochar(s, '\'', chunk) < 0) { Swig_error(Getfile(s), start_line, "Unterminated character constant\n"); } } else if (c == '/') state = 30; /* Comment */ break; } case 30: /* Possibly a comment string of some sort */ start_line = Getline(s); if (c == '/') state = 31; else if (c == '*') state = 32; else { state = 1; goto state1; /* Process this character the same as if it wasn't preceded by a `/`. */ } Putc(c, chunk); break; case 31: Putc(c, chunk); if (c == '\n') state = 0; break; case 32: Putc(c, chunk); if (c == '*') state = 33; break; case 33: Putc(c, chunk); if (c == '/') state = 1; else if (c != '*') state = 32; break; case 40: /* Start of a C preprocessor directive */ if (c == '\n') { Putc('\n', chunk); state = 0; } else if (isspace(c)) { state = 40; } else { /* Got the start of a preprocessor directive */ Ungetc(c, s); Clear(id); copy_location(s, id); state = 41; } break; case 41: /* Build up the name of the preprocessor directive */ if ((isspace(c) || (!isidchar(c)))) { Clear(value); Clear(comment); if (c == '\n') { Ungetc(c, s); state = 50; } else { state = 42; if (!isspace(c)) { Ungetc(c, s); } } copy_location(s, value); break; } Putc(c, id); break; case 42: /* Strip any leading space after the preprocessor directive (before preprocessor value) */ if (isspace(c)) { if (c == '\n') { Ungetc(c, s); state = 50; } break; } state = 43; /* FALL THRU */ case 43: /* Get preprocessor value */ if (c == '\n') { Ungetc(c, s); state = 50; } else if (c == '/') { state = 45; } else if (c == '\"') { Putc(c, value); skip_tochar_or_eol(s, '\"', value); } else if (c == '\'') { Putc(c, value); skip_tochar_or_eol(s, '\'', value); } else { Putc(c, value); if (c == '\\') state = 44; } break; case 44: if (c == '\n') { Putc(c, value); cpp_lines++; } else { Ungetc(c, s); } state = 43; break; /* States 45-48 are used to remove, but retain comments from macro values. The comments will be placed in the output in an alternative form */ case 45: if (c == '/') state = 46; else if (c == '*') state = 47; else if (c == '\n') { Putc('/', value); Ungetc(c, s); state = 50; } else { Putc('/', value); Putc(c, value); state = 43; } break; case 46: /* in C++ comment */ if (c == '\n') { Ungetc(c, s); state = 50; } else Putc(c, comment); break; case 47: /* in C comment */ if (c == '*') state = 48; else Putc(c, comment); break; case 48: if (c == '/') state = 43; else if (c == '*') Putc(c, comment); else { Putc('*', comment); Putc(c, comment); state = 47; } break; case 50: /* Check for various preprocessor directives */ Chop(value); if (Equal(id, kpp_define)) { if (allow) { DOH *m, *v, *v1; Seek(value, 0, SEEK_SET); m = Preprocessor_define(value, 0); if ((m) && !(Getattr(m, kpp_args))) { v = Copy(Getattr(m, kpp_value)); copy_location(m, v); if (Len(v)) { Swig_error_silent(1); v1 = Preprocessor_replace(v, NULL); Swig_error_silent(0); /* Printf(stdout,"checking '%s'\n", v1); */ if (!checkpp_id(v1)) { if (Len(comment) == 0) Printf(ns, "%%constant %s = %s;\n", Getattr(m, kpp_name), v1); else Printf(ns, "%%constant %s = %s; /*%s*/\n", Getattr(m, kpp_name), v1, comment); cpp_lines--; } Delete(v1); } Delete(v); } } } else if (Equal(id, kpp_undef)) { if (allow) Preprocessor_undef(value); } else if (Equal(id, kpp_ifdef)) { cond_lines[level] = Getline(id); level++; if (allow) { start_level = level; if (Len(value) > 0) { /* See if the identifier is in the hash table */ if (!Getattr(symbols, value)) allow = 0; } else { Swig_error(Getfile(s), Getline(id), "Missing identifier for #ifdef.\n"); allow = 0; } mask = 1; } } else if (Equal(id, kpp_ifndef)) { cond_lines[level] = Getline(id); level++; if (allow) { start_level = level; if (Len(value) > 0) { /* See if the identifier is in the hash table */ if (Getattr(symbols, value)) allow = 0; } else { Swig_error(Getfile(s), Getline(id), "Missing identifier for #ifndef.\n"); allow = 0; } mask = 1; } } else if (Equal(id, kpp_else)) { if (level <= 0) { Swig_error(Getfile(s), Getline(id), "Misplaced #else.\n"); } else { cond_lines[level - 1] = Getline(id); if (Len(value) != 0) Swig_warning(WARN_PP_UNEXPECTED_TOKENS, Getfile(s), Getline(id), "Unexpected tokens after #else directive.\n"); if (allow) { allow = 0; mask = 0; } else if (level == start_level) { allow = 1 * mask; } } } else if (Equal(id, kpp_endif)) { level--; if (level < 0) { Swig_error(Getfile(id), Getline(id), "Extraneous #endif.\n"); level = 0; } else { if (level < start_level) { if (Len(value) != 0) Swig_warning(WARN_PP_UNEXPECTED_TOKENS, Getfile(s), Getline(id), "Unexpected tokens after #endif directive.\n"); allow = 1; start_level--; } } } else if (Equal(id, kpp_if)) { cond_lines[level] = Getline(id); level++; if (allow) { int val; String *sval; expand_defined_operator = 1; sval = Preprocessor_replace(value, NULL); start_level = level; Seek(sval, 0, SEEK_SET); /* Printf(stdout,"Evaluating '%s'\n", sval); */ if (Len(sval) > 0) { val = Preprocessor_expr(sval, &e); if (e) { const char *msg = Preprocessor_expr_error(); Seek(value, 0, SEEK_SET); Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Could not evaluate expression '%s'\n", value); if (msg) Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "%s\n", msg); allow = 0; } else { if (val == 0) allow = 0; } } else { Swig_error(Getfile(s), Getline(id), "Missing expression for #if.\n"); allow = 0; } expand_defined_operator = 0; mask = 1; } } else if (Equal(id, kpp_elif)) { if (level == 0) { Swig_error(Getfile(s), Getline(id), "Misplaced #elif.\n"); } else { cond_lines[level - 1] = Getline(id); if (allow) { allow = 0; mask = 0; } else if (level == start_level) { int val; String *sval; expand_defined_operator = 1; sval = Preprocessor_replace(value, NULL); Seek(sval, 0, SEEK_SET); if (Len(sval) > 0) { val = Preprocessor_expr(sval, &e); if (e) { const char *msg = Preprocessor_expr_error(); Seek(value, 0, SEEK_SET); Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Could not evaluate expression '%s'\n", value); if (msg) Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "%s\n", msg); allow = 0; } else { if (val) allow = 1 * mask; else allow = 0; } } else { Swig_error(Getfile(s), Getline(id), "Missing expression for #elif.\n"); allow = 0; } expand_defined_operator = 0; } } } else if (Equal(id, kpp_warning)) { if (allow) { Swig_warning(WARN_PP_CPP_WARNING, Getfile(s), Getline(id), "CPP #warning, \"%s\".\n", value); } } else if (Equal(id, kpp_error)) { if (allow) { if (error_as_warning) { Swig_warning(WARN_PP_CPP_ERROR, Getfile(s), Getline(id), "CPP #error \"%s\".\n", value); } else { Swig_error(Getfile(s), Getline(id), "CPP #error \"%s\". Use the -cpperraswarn option to continue swig processing.\n", value); } } } else if (Equal(id, kpp_line)) { } else if (Equal(id, kpp_include)) { if (((include_all) || (import_all)) && (allow)) { String *s1, *s2, *fn; String *dirname; int sysfile = 0; if (include_all && import_all) { Swig_warning(WARN_PP_INCLUDEALL_IMPORTALL, Getfile(s), Getline(id), "Both includeall and importall are defined: using includeall.\n"); import_all = 0; } Seek(value, 0, SEEK_SET); fn = get_filename(value, &sysfile); s1 = cpp_include(fn, sysfile); if (s1) { if (include_all) Printf(ns, "%%includefile \"%s\" %%beginfile\n", Swig_filename_escape(Swig_last_file())); else if (import_all) { Printf(ns, "%%importfile \"%s\" %%beginfile\n", Swig_filename_escape(Swig_last_file())); push_imported(); } /* See if the filename has a directory component */ dirname = Swig_file_dirname(Swig_last_file()); if (sysfile || !Len(dirname)) { Delete(dirname); dirname = 0; } if (dirname) { int len = Len(dirname); Delslice(dirname, len - 1, len); /* Kill trailing directory delimiter */ Swig_push_directory(dirname); } s2 = Preprocessor_parse(s1); addline(ns, s2, allow); Append(ns, "%endoffile"); if (dirname) { Swig_pop_directory(); } if (import_all) { pop_imported(); } Delete(s2); Delete(dirname); Delete(s1); } Delete(fn); } } else if (Equal(id, kpp_pragma)) { if (Strncmp(value, "SWIG ", 5) == 0) { char *c = Char(value) + 5; while (*c && (isspace((int) *c))) c++; if (*c) { if (strncmp(c, "nowarn=", 7) == 0) { String *val = NewString(c + 7); String *nowarn = Preprocessor_replace(val, NULL); Swig_warnfilter(nowarn, 1); Delete(nowarn); Delete(val); } else if (strncmp(c, "cpperraswarn=", 13) == 0) { error_as_warning = atoi(c + 13); } else { Swig_error(Getfile(s), Getline(id), "Unknown SWIG pragma: %s\n", c); } } } } else if (Equal(id, kpp_level)) { Swig_error(Getfile(s), Getline(id), "cpp debug: level = %d, startlevel = %d\n", level, start_level); } else if (Equal(id, "")) { /* Null directive */ } else if (is_digits(id)) { /* A gcc linemarker of the form '# linenum filename flags' (resulting from running gcc -E) */ } else { /* Ignore unknown preprocessor directives which are inside an inactive * conditional (github issue #394). */ if (allow) Swig_error(Getfile(s), Getline(id), "Unknown SWIG preprocessor directive: %s (if this is a block of target language code, delimit it with %%{ and %%})\n", id); } for (i = 0; i < cpp_lines; i++) Putc('\n', ns); state = 0; break; /* SWIG directives */ case 100: /* %{,%} block */ if (c == '{') { start_line = Getline(s); copy_location(s, chunk); add_chunk(ns, chunk, allow); Putc('%', chunk); Putc(c, chunk); state = 105; } /* %#cpp - an embedded C preprocessor directive (we strip off the %) */ else if (c == '#') { add_chunk(ns, chunk, allow); Putc(c, chunk); state = 107; } else if (isidentifier(c)) { Clear(decl); Putc('%', decl); Putc(c, decl); state = 110; } else { Putc('%', chunk); Putc(c, chunk); state = 1; } break; case 105: Putc(c, chunk); if (c == '%') state = 106; break; case 106: Putc(c, chunk); if (c == '}') { state = 1; addline(ns, chunk, allow); Clear(chunk); copy_location(s, chunk); } else { state = 105; } break; case 107: Putc(c, chunk); if (c == '\n') { addline(ns, chunk, allow); Clear(chunk); state = 0; } else if (c == '\\') { state = 108; } break; case 108: Putc(c, chunk); state = 107; break; case 110: if (!isidchar(c)) { Ungetc(c, s); /* Look for common SWIG directives */ if (Equal(decl, kpp_dinclude) || Equal(decl, kpp_dimport)) { /* Got some kind of file inclusion directive, eg: %import(option1="value1") "filename" */ if (allow) { DOH *s1, *s2, *fn, *opt; String *options_whitespace = NewStringEmpty(); String *filename_whitespace = NewStringEmpty(); int sysfile = 0; skip_whitespace(s, options_whitespace); opt = get_options(s); skip_whitespace(s, filename_whitespace); fn = get_filename(s, &sysfile); s1 = cpp_include(fn, sysfile); if (s1) { String *dirname; copy_location(s, chunk); add_chunk(ns, chunk, allow); Printf(ns, "%sfile%s%s%s\"%s\" %%beginfile\n", decl, options_whitespace, opt, filename_whitespace, Swig_filename_escape(Swig_last_file())); if (Equal(decl, kpp_dimport)) { push_imported(); } dirname = Swig_file_dirname(Swig_last_file()); if (sysfile || !Len(dirname)) { Delete(dirname); dirname = 0; } if (dirname) { int len = Len(dirname); Delslice(dirname, len - 1, len); /* Kill trailing directory delimiter */ Swig_push_directory(dirname); } s2 = Preprocessor_parse(s1); if (dirname) { Swig_pop_directory(); } if (Equal(decl, kpp_dimport)) { pop_imported(); } addline(ns, s2, allow); Append(ns, "%endoffile"); Delete(s2); Delete(dirname); Delete(s1); } Delete(fn); Delete(filename_whitespace); Delete(options_whitespace); } state = 1; } else if (Equal(decl, kpp_dbeginfile)) { /* Got an internal directive marking the beginning of an included file: %beginfile ... %endoffile */ filelevel++; start_line = Getline(s); copy_location(s, chunk); add_chunk(ns, chunk, allow); Append(chunk, decl); state = 120; } else if (Equal(decl, kpp_dline)) { /* Got a line directive */ state = 1; } else if (Equal(decl, kpp_ddefine)) { /* Got a define directive */ dlevel++; copy_location(s, chunk); add_chunk(ns, chunk, allow); Clear(value); copy_location(s, value); state = 150; } else { Append(chunk, decl); state = 1; } } else { Putc(c, decl); } break; /* Searching for the end of a %beginfile block */ case 120: Putc(c, chunk); if (c == '%') { const char *bf = "beginfile"; const char *ef = "endoffile"; char statement[10]; int i = 0; for (i = 0; i < 9;) { c = Getc(s); Putc(c, chunk); statement[i++] = (char)c; if (strncmp(statement, bf, i) && strncmp(statement, ef, i)) break; } c = Getc(s); Ungetc(c, s); if ((i == 9) && (isspace(c))) { if (strncmp(statement, bf, i) == 0) { ++filelevel; } else if (strncmp(statement, ef, i) == 0) { --filelevel; if (!filelevel) { /* Reached end of included file */ addline(ns, chunk, allow); Clear(chunk); copy_location(s, chunk); state = 1; } } } } break; /* Searching for the end of a %define statement */ case 150: Putc(c, value); if (c == '%') { const char *ed = "enddef"; const char *df = "define"; char statement[7]; int i = 0; for (i = 0; i < 6;) { c = Getc(s); Putc(c, value); statement[i++] = (char)c; if (strncmp(statement, ed, i) && strncmp(statement, df, i)) break; } c = Getc(s); Ungetc(c, s); if ((i == 6) && (isspace(c))) { if (strncmp(statement, df, i) == 0) { ++dlevel; } else { if (strncmp(statement, ed, i) == 0) { --dlevel; if (!dlevel) { /* Got the macro */ for (i = 0; i < 7; i++) { Delitem(value, DOH_END); } if (allow) { Seek(value, 0, SEEK_SET); Preprocessor_define(value, 1); } addline(ns, value, 0); state = 0; } } } } } break; default: Printf(stderr, "cpp: Invalid parser state %d\n", state); Exit(EXIT_FAILURE); } } while (level > 0) { Swig_error(Getfile(s), cond_lines[level - 1], "Missing #endif for conditional starting here\n"); level--; } if (state == 120) { Swig_error(Getfile(s), start_line, "Missing %%endoffile for file inclusion block starting here\n"); } if (state == 150) { Seek(value, 0, SEEK_SET); Swig_error(Getfile(s), Getline(value), "Missing %%enddef for macro starting here\n", Getline(value)); } if ((state >= 105) && (state < 107)) { Swig_error(Getfile(s), start_line, "Unterminated %%{ ... %%} block\n"); } if ((state >= 30) && (state < 40)) { Swig_error(Getfile(s), start_line, "Unterminated comment\n"); } copy_location(s, chunk); add_chunk(ns, chunk, allow); /* DelScope(scp); */ Delete(decl); Delete(id); Delete(value); Delete(comment); Delete(chunk); return ns; } swig-4.4.0/Source/Preprocessor/.deps/0000775000175000017500000000000015075470612017316 5ustar williamwilliamswig-4.4.0/Source/Makefile.in0000664000175000017500000014441715075470576015710 0ustar williamwilliam# Makefile.in generated by automake 1.16.5 from Makefile.am. # @configure_input@ # Copyright (C) 1994-2021 Free Software Foundation, Inc. # This Makefile.in is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. @SET_MAKE@ VPATH = @srcdir@ am__is_gnu_make = { \ if test -z '$(MAKELEVEL)'; then \ false; \ elif test -n '$(MAKE_HOST)'; then \ true; \ elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ true; \ else \ false; \ fi; \ } am__make_running_with_option = \ case $${target_option-} in \ ?) ;; \ *) echo "am__make_running_with_option: internal error: invalid" \ "target option '$${target_option-}' specified" >&2; \ exit 1;; \ esac; \ has_opt=no; \ sane_makeflags=$$MAKEFLAGS; \ if $(am__is_gnu_make); then \ sane_makeflags=$$MFLAGS; \ else \ case $$MAKEFLAGS in \ *\\[\ \ ]*) \ bs=\\; \ sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ esac; \ fi; \ skip_next=no; \ strip_trailopt () \ { \ flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ }; \ for flg in $$sane_makeflags; do \ test $$skip_next = yes && { skip_next=no; continue; }; \ case $$flg in \ *=*|--*) continue;; \ -*I) strip_trailopt 'I'; skip_next=yes;; \ -*I?*) strip_trailopt 'I';; \ -*O) strip_trailopt 'O'; skip_next=yes;; \ -*O?*) strip_trailopt 'O';; \ -*l) strip_trailopt 'l'; skip_next=yes;; \ -*l?*) strip_trailopt 'l';; \ -[dEDm]) skip_next=yes;; \ -[JT]) skip_next=yes;; \ esac; \ case $$flg in \ *$$target_option*) has_opt=yes; break;; \ esac; \ done; \ test $$has_opt = yes am__make_dryrun = (target_option=n; $(am__make_running_with_option)) am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) pkgdatadir = $(datadir)/@PACKAGE@ pkgincludedir = $(includedir)/@PACKAGE@ pkglibdir = $(libdir)/@PACKAGE@ pkglibexecdir = $(libexecdir)/@PACKAGE@ am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd install_sh_DATA = $(install_sh) -c -m 644 install_sh_PROGRAM = $(install_sh) -c install_sh_SCRIPT = $(install_sh) -c INSTALL_HEADER = $(INSTALL_DATA) transform = $(program_transform_name) NORMAL_INSTALL = : PRE_INSTALL = : POST_INSTALL = : NORMAL_UNINSTALL = : PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ bin_PROGRAMS = eswig$(EXEEXT) subdir = Source ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = \ $(top_srcdir)/Tools/config/ac_compile_warnings.m4 \ $(top_srcdir)/Tools/config/ac_define_dir.m4 \ $(top_srcdir)/Tools/config/ax_boost_base.m4 \ $(top_srcdir)/Tools/config/ax_compare_version.m4 \ $(top_srcdir)/Tools/config/ax_path_generic.m4 \ $(top_srcdir)/Tools/config/m4_ax_cxx_compile_stdcxx.m4 \ $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/Source/Include/swigconfig.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) am__dirstamp = $(am__leading_dot)dirstamp am_eswig_OBJECTS = CParse/cscanner.$(OBJEXT) CParse/parser.$(OBJEXT) \ CParse/templ.$(OBJEXT) CParse/util.$(OBJEXT) \ DOH/base.$(OBJEXT) DOH/file.$(OBJEXT) DOH/fio.$(OBJEXT) \ DOH/hash.$(OBJEXT) DOH/list.$(OBJEXT) DOH/memory.$(OBJEXT) \ DOH/string.$(OBJEXT) DOH/void.$(OBJEXT) \ Doxygen/csharpdoc.$(OBJEXT) Doxygen/doxyentity.$(OBJEXT) \ Doxygen/doxyparser.$(OBJEXT) Doxygen/doxytranslator.$(OBJEXT) \ Doxygen/javadoc.$(OBJEXT) Doxygen/pydoc.$(OBJEXT) \ Modules/allocate.$(OBJEXT) Modules/contract.$(OBJEXT) \ Modules/c.$(OBJEXT) Modules/csharp.$(OBJEXT) \ Modules/d.$(OBJEXT) Modules/directors.$(OBJEXT) \ Modules/emit.$(OBJEXT) Modules/go.$(OBJEXT) \ Modules/guile.$(OBJEXT) Modules/interface.$(OBJEXT) \ Modules/java.$(OBJEXT) Modules/javascript.$(OBJEXT) \ Modules/lang.$(OBJEXT) Modules/lua.$(OBJEXT) \ Modules/main.$(OBJEXT) Modules/nested.$(OBJEXT) \ Modules/ocaml.$(OBJEXT) Modules/octave.$(OBJEXT) \ Modules/overload.$(OBJEXT) Modules/perl5.$(OBJEXT) \ Modules/php.$(OBJEXT) Modules/python.$(OBJEXT) \ Modules/r.$(OBJEXT) Modules/ruby.$(OBJEXT) \ Modules/scilab.$(OBJEXT) Modules/swigmain.$(OBJEXT) \ Modules/tcl8.$(OBJEXT) Modules/typepass.$(OBJEXT) \ Modules/utils.$(OBJEXT) Modules/xml.$(OBJEXT) \ Preprocessor/cpp.$(OBJEXT) Preprocessor/expr.$(OBJEXT) \ Swig/cwrap.$(OBJEXT) Swig/deprecate.$(OBJEXT) \ Swig/error.$(OBJEXT) Swig/extend.$(OBJEXT) \ Swig/fragment.$(OBJEXT) Swig/getopt.$(OBJEXT) \ Swig/include.$(OBJEXT) Swig/misc.$(OBJEXT) \ Swig/naming.$(OBJEXT) Swig/parms.$(OBJEXT) \ Swig/scanner.$(OBJEXT) Swig/stype.$(OBJEXT) \ Swig/symbol.$(OBJEXT) Swig/tree.$(OBJEXT) \ Swig/typemap.$(OBJEXT) Swig/typeobj.$(OBJEXT) \ Swig/typesys.$(OBJEXT) Swig/wrapfunc.$(OBJEXT) eswig_OBJECTS = $(am_eswig_OBJECTS) eswig_LDADD = $(LDADD) AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false am__v_P_1 = : AM_V_GEN = $(am__v_GEN_@AM_V@) am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) am__v_GEN_0 = @echo " GEN " $@; am__v_GEN_1 = AM_V_at = $(am__v_at_@AM_V@) am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) am__v_at_0 = @ am__v_at_1 = DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/Tools/config/depcomp am__maybe_remake_depfiles = depfiles am__depfiles_remade = CParse/$(DEPDIR)/cscanner.Po \ CParse/$(DEPDIR)/parser.Po CParse/$(DEPDIR)/templ.Po \ CParse/$(DEPDIR)/util.Po DOH/$(DEPDIR)/base.Po \ DOH/$(DEPDIR)/file.Po DOH/$(DEPDIR)/fio.Po \ DOH/$(DEPDIR)/hash.Po DOH/$(DEPDIR)/list.Po \ DOH/$(DEPDIR)/memory.Po DOH/$(DEPDIR)/string.Po \ DOH/$(DEPDIR)/void.Po Doxygen/$(DEPDIR)/csharpdoc.Po \ Doxygen/$(DEPDIR)/doxyentity.Po \ Doxygen/$(DEPDIR)/doxyparser.Po \ Doxygen/$(DEPDIR)/doxytranslator.Po \ Doxygen/$(DEPDIR)/javadoc.Po Doxygen/$(DEPDIR)/pydoc.Po \ Modules/$(DEPDIR)/allocate.Po Modules/$(DEPDIR)/c.Po \ Modules/$(DEPDIR)/contract.Po Modules/$(DEPDIR)/csharp.Po \ Modules/$(DEPDIR)/d.Po Modules/$(DEPDIR)/directors.Po \ Modules/$(DEPDIR)/emit.Po Modules/$(DEPDIR)/go.Po \ Modules/$(DEPDIR)/guile.Po Modules/$(DEPDIR)/interface.Po \ Modules/$(DEPDIR)/java.Po Modules/$(DEPDIR)/javascript.Po \ Modules/$(DEPDIR)/lang.Po Modules/$(DEPDIR)/lua.Po \ Modules/$(DEPDIR)/main.Po Modules/$(DEPDIR)/nested.Po \ Modules/$(DEPDIR)/ocaml.Po Modules/$(DEPDIR)/octave.Po \ Modules/$(DEPDIR)/overload.Po Modules/$(DEPDIR)/perl5.Po \ Modules/$(DEPDIR)/php.Po Modules/$(DEPDIR)/python.Po \ Modules/$(DEPDIR)/r.Po Modules/$(DEPDIR)/ruby.Po \ Modules/$(DEPDIR)/scilab.Po Modules/$(DEPDIR)/swigmain.Po \ Modules/$(DEPDIR)/tcl8.Po Modules/$(DEPDIR)/typepass.Po \ Modules/$(DEPDIR)/utils.Po Modules/$(DEPDIR)/xml.Po \ Preprocessor/$(DEPDIR)/cpp.Po Preprocessor/$(DEPDIR)/expr.Po \ Swig/$(DEPDIR)/cwrap.Po Swig/$(DEPDIR)/deprecate.Po \ Swig/$(DEPDIR)/error.Po Swig/$(DEPDIR)/extend.Po \ Swig/$(DEPDIR)/fragment.Po Swig/$(DEPDIR)/getopt.Po \ Swig/$(DEPDIR)/include.Po Swig/$(DEPDIR)/misc.Po \ Swig/$(DEPDIR)/naming.Po Swig/$(DEPDIR)/parms.Po \ Swig/$(DEPDIR)/scanner.Po Swig/$(DEPDIR)/stype.Po \ Swig/$(DEPDIR)/symbol.Po Swig/$(DEPDIR)/tree.Po \ Swig/$(DEPDIR)/typemap.Po Swig/$(DEPDIR)/typeobj.Po \ Swig/$(DEPDIR)/typesys.Po Swig/$(DEPDIR)/wrapfunc.Po am__mv = mv -f COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) AM_V_CC = $(am__v_CC_@AM_V@) am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) am__v_CC_0 = @echo " CC " $@; am__v_CC_1 = CCLD = $(CC) LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ AM_V_CCLD = $(am__v_CCLD_@AM_V@) am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) AM_V_CXX = $(am__v_CXX_@AM_V@) am__v_CXX_ = $(am__v_CXX_@AM_DEFAULT_V@) am__v_CXX_0 = @echo " CXX " $@; am__v_CXX_1 = CXXLD = $(CXX) CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \ -o $@ AM_V_CXXLD = $(am__v_CXXLD_@AM_V@) am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) am__v_CXXLD_0 = @echo " CXXLD " $@; am__v_CXXLD_1 = SOURCES = $(eswig_SOURCES) DIST_SOURCES = $(eswig_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ *) (install-info --version) >/dev/null 2>&1;; \ esac am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) # Read a list of newline-separated strings from the standard input, # and print each of them once, without duplicates. Input order is # *not* preserved. am__uniquify_input = $(AWK) '\ BEGIN { nonempty = 0; } \ { items[$$0] = 1; nonempty = 1; } \ END { if (nonempty) { for (i in items) print i; }; } \ ' # Make sure the list of sources is unique. This is necessary because, # e.g., the same source file might be shared among _SOURCES variables # for different programs/libraries. am__define_uniq_tagged_files = \ list='$(am__tagged_files)'; \ unique=`for i in $$list; do \ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ done | $(am__uniquify_input)` am__DIST_COMMON = $(srcdir)/Makefile.in \ $(top_srcdir)/Tools/config/depcomp README DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) ACLOCAL = @ACLOCAL@ ADB = @ADB@ AMTAR = @AMTAR@ AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ ANDROID = @ANDROID@ ANT = @ANT@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ AWK = @AWK@ BISON = @BISON@ BOOST_CPPFLAGS = @BOOST_CPPFLAGS@ BOOST_LDFLAGS = @BOOST_LDFLAGS@ CAMLP4 = @CAMLP4@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CCSHARED = @CCSHARED@ CFLAGS = @CFLAGS@ CPP = @CPP@ CPPFLAGS = @CPPFLAGS@ CSCOPE = @CSCOPE@ CSHARPCFLAGS = @CSHARPCFLAGS@ CSHARPCILINTERPRETER = @CSHARPCILINTERPRETER@ CSHARPCILINTERPRETER_FLAGS = @CSHARPCILINTERPRETER_FLAGS@ CSHARPCOMPILER = @CSHARPCOMPILER@ CSHARPCONVERTPATH = @CSHARPCONVERTPATH@ CSHARPDYNAMICLINKING = @CSHARPDYNAMICLINKING@ CSHARPLIBRARYPREFIX = @CSHARPLIBRARYPREFIX@ CSHARPSO = @CSHARPSO@ CTAGS = @CTAGS@ CXX = @CXX@ CXXDEPMODE = @CXXDEPMODE@ CXXFLAGS = @CXXFLAGS@ CXXSHARED = @CXXSHARED@ CXX_LDSHARED = @CXX_LDSHARED@ CYGPATH_W = @CYGPATH_W@ C_LDSHARED = @C_LDSHARED@ C_SO = @C_SO@ D2COMPILER = @D2COMPILER@ DEFS = @DEFS@ DEPDIR = @DEPDIR@ DLIBPREFIX = @DLIBPREFIX@ ECHO_C = @ECHO_C@ ECHO_N = @ECHO_N@ ECHO_T = @ECHO_T@ EGREP = @EGREP@ ENABLE_CCACHE = @ENABLE_CCACHE@ ETAGS = @ETAGS@ EXAMPLE_LIBS = @EXAMPLE_LIBS@ EXEEXT = @EXEEXT@ EXTRA_CLEAN = @EXTRA_CLEAN@ GCCGO = @GCCGO@ GCCGOOPT = @GCCGOOPT@ GO = @GO@ GO1 = @GO1@ GO12 = @GO12@ GO13 = @GO13@ GO15 = @GO15@ GOC = @GOC@ GOGCC = @GOGCC@ GOOPT = @GOOPT@ GOVERSIONOPTION = @GOVERSIONOPTION@ GREP = @GREP@ GUILE = @GUILE@ GUILE_CFLAGS = @GUILE_CFLAGS@ GUILE_CONFIG = @GUILE_CONFIG@ GUILE_LIBS = @GUILE_LIBS@ GUILE_SO = @GUILE_SO@ HAVE_CXX11 = @HAVE_CXX11@ HAVE_CXX14 = @HAVE_CXX14@ HAVE_CXX17 = @HAVE_CXX17@ HAVE_CXX20 = @HAVE_CXX20@ INSTALL = @INSTALL@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ JAVA = @JAVA@ JAVAC = @JAVAC@ JAVACFLAGS = @JAVACFLAGS@ JAVACXXSHARED = @JAVACXXSHARED@ JAVAC_OPTIONS = @JAVAC_OPTIONS@ JAVADYNAMICLINKING = @JAVADYNAMICLINKING@ JAVAFLAGS = @JAVAFLAGS@ JAVAINC = @JAVAINC@ JAVALDSHARED = @JAVALDSHARED@ JAVALIBRARYPREFIX = @JAVALIBRARYPREFIX@ JAVASO = @JAVASO@ JAVA_SKIP_DOXYGEN_TEST_CASES = @JAVA_SKIP_DOXYGEN_TEST_CASES@ JAVA_VERSION_MAJOR = @JAVA_VERSION_MAJOR@ JSCENABLED = @JSCENABLED@ JSCOREDYNAMICLINKING = @JSCOREDYNAMICLINKING@ JSCOREINC = @JSCOREINC@ JSCOREVERSION = @JSCOREVERSION@ JSINTERPRETERCXX = @JSINTERPRETERCXX@ JSINTERPRETERLINKFLAGS = @JSINTERPRETERLINKFLAGS@ JSNAPIENABLED = @JSNAPIENABLED@ JSV8DYNAMICLINKING = @JSV8DYNAMICLINKING@ JSV8ENABLED = @JSV8ENABLED@ JSV8INC = @JSV8INC@ LDFLAGS = @LDFLAGS@ LDSHARED = @LDSHARED@ LIBC = @LIBC@ LIBM = @LIBM@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ LINKFORSHARED = @LINKFORSHARED@ LTLIBOBJS = @LTLIBOBJS@ LUABIN = @LUABIN@ LUADYNAMICLINKING = @LUADYNAMICLINKING@ LUAFLAGS = @LUAFLAGS@ LUALINK = @LUALINK@ LUA_SO = @LUA_SO@ MAKEINFO = @MAKEINFO@ MKDIR_P = @MKDIR_P@ NDKBUILD = @NDKBUILD@ NODEGYP = @NODEGYP@ NODEJS = @NODEJS@ NODENAPI_DIR = @NODENAPI_DIR@ NODENPM = @NODENPM@ OBJEXT = @OBJEXT@ OCAMLC = @OCAMLC@ OCAMLDLGEN = @OCAMLDLGEN@ OCAMLFIND = @OCAMLFIND@ OCAMLMKTOP = @OCAMLMKTOP@ OCTAVE = @OCTAVE@ OCTAVE_CPPFLAGS = @OCTAVE_CPPFLAGS@ OCTAVE_CXXFLAGS = @OCTAVE_CXXFLAGS@ OCTAVE_LDFLAGS = @OCTAVE_LDFLAGS@ OCTAVE_SO = @OCTAVE_SO@ PACKAGE = @PACKAGE@ PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ PACKAGE_NAME = @PACKAGE_NAME@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ PCHINCLUDEARG = @PCHINCLUDEARG@ PCHINCLUDEEXT = @PCHINCLUDEEXT@ PCHSUPPORT = @PCHSUPPORT@ PCRE2_CFLAGS = @PCRE2_CFLAGS@ PCRE2_CONFIG = @PCRE2_CONFIG@ PCRE2_LIBS = @PCRE2_LIBS@ PERL = @PERL@ PERL5CCCDLFLAGS = @PERL5CCCDLFLAGS@ PERL5CCDLFLAGS = @PERL5CCDLFLAGS@ PERL5CCFLAGS = @PERL5CCFLAGS@ PERL5DYNAMICLINKING = @PERL5DYNAMICLINKING@ PERL5EXT = @PERL5EXT@ PERL5LDFLAGS = @PERL5LDFLAGS@ PERL5LIB = @PERL5LIB@ PHP = @PHP@ PHPINC = @PHPINC@ PHP_SO = @PHP_SO@ PKG_CONFIG = @PKG_CONFIG@ PLATCFLAGS = @PLATCFLAGS@ PLATCXXFLAGS = @PLATCXXFLAGS@ PY3CONFIG = @PY3CONFIG@ PY3INCLUDE = @PY3INCLUDE@ PY3LIB = @PY3LIB@ PY3LINK = @PY3LINK@ PYABI3AUDIT = @PYABI3AUDIT@ PYCODESTYLE = @PYCODESTYLE@ PYINCLUDE = @PYINCLUDE@ PYLIB = @PYLIB@ PYLINK = @PYLINK@ PYTHON = @PYTHON@ PYTHON3 = @PYTHON3@ PYTHON3DYNAMICLINKING = @PYTHON3DYNAMICLINKING@ PYTHONDYNAMICLINKING = @PYTHONDYNAMICLINKING@ PYTHON_SO = @PYTHON_SO@ RBIN = @RBIN@ ROOT_DIR = @ROOT_DIR@ RPATH = @RPATH@ RUBY = @RUBY@ RUBYCCDLFLAGS = @RUBYCCDLFLAGS@ RUBYDYNAMICLINKING = @RUBYDYNAMICLINKING@ RUBYINCLUDE = @RUBYINCLUDE@ RUBYLIB = @RUBYLIB@ RUBYLINK = @RUBYLINK@ RUBYSO = @RUBYSO@ SCILAB = @SCILAB@ SCILABINCLUDE = @SCILABINCLUDE@ SCILABOPT = @SCILABOPT@ SCILAB_VERSION = @SCILAB_VERSION@ SED = @SED@ SET_MAKE = @SET_MAKE@ SHELL = @SHELL@ SHELL_PATH_SEP = @SHELL_PATH_SEP@ SKIP_ANDROID = @SKIP_ANDROID@ SKIP_C = @SKIP_C@ SKIP_CSHARP = @SKIP_CSHARP@ SKIP_D = @SKIP_D@ SKIP_GO = @SKIP_GO@ SKIP_GUILE = @SKIP_GUILE@ SKIP_JAVA = @SKIP_JAVA@ SKIP_JAVASCRIPT = @SKIP_JAVASCRIPT@ SKIP_LUA = @SKIP_LUA@ SKIP_OCAML = @SKIP_OCAML@ SKIP_OCTAVE = @SKIP_OCTAVE@ SKIP_PERL5 = @SKIP_PERL5@ SKIP_PHP = @SKIP_PHP@ SKIP_PYTHON = @SKIP_PYTHON@ SKIP_R = @SKIP_R@ SKIP_RUBY = @SKIP_RUBY@ SKIP_SCILAB = @SKIP_SCILAB@ SKIP_TCL = @SKIP_TCL@ SO = @SO@ STRIP = @STRIP@ SWIG_LIB = @SWIG_LIB@ SWIG_LIB_INSTALL = @SWIG_LIB_INSTALL@ SWIG_LIB_PREINST = @SWIG_LIB_PREINST@ SWIG_LIB_SET = @SWIG_LIB_SET@ TCLCXXSHARED = @TCLCXXSHARED@ TCLDYNAMICLINKING = @TCLDYNAMICLINKING@ TCLINCLUDE = @TCLINCLUDE@ TCLLDSHARED = @TCLLDSHARED@ TCLLIB = @TCLLIB@ TCLLINK = @TCLLINK@ TCL_SO = @TCL_SO@ TRYLINKINGWITHCXX = @TRYLINKINGWITHCXX@ VERSION = @VERSION@ abs_builddir = @abs_builddir@ abs_srcdir = @abs_srcdir@ abs_top_builddir = @abs_top_builddir@ abs_top_srcdir = @abs_top_srcdir@ ac_aux_dir = @ac_aux_dir@ ac_ct_CC = @ac_ct_CC@ ac_ct_CXX = @ac_ct_CXX@ am__include = @am__include@ am__leading_dot = @am__leading_dot@ am__quote = @am__quote@ am__tar = @am__tar@ am__untar = @am__untar@ bindir = @bindir@ build = @build@ build_alias = @build_alias@ build_cpu = @build_cpu@ build_os = @build_os@ build_vendor = @build_vendor@ builddir = @builddir@ datadir = @datadir@ datarootdir = @datarootdir@ docdir = @docdir@ dvidir = @dvidir@ exec_prefix = @exec_prefix@ host = @host@ host_alias = @host_alias@ host_cpu = @host_cpu@ host_os = @host_os@ host_vendor = @host_vendor@ htmldir = @htmldir@ includedir = @includedir@ infodir = @infodir@ install_sh = @install_sh@ libdir = @libdir@ libexecdir = @libexecdir@ localedir = @localedir@ localstatedir = @localstatedir@ mandir = @mandir@ mkdir_p = @mkdir_p@ oldincludedir = @oldincludedir@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ subdirs = @subdirs@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ # subdir-objects generates object files using the directory structure of the source files. AUTOMAKE_OPTIONS = foreign nostdinc subdir-objects 1.7.2 SOURCE_DIR = $(top_srcdir)/Source BUILD_SOURCE_DIR = $(top_builddir)/Source AM_CPPFLAGS = -I$(BUILD_SOURCE_DIR)/Include \ -I$(BUILD_SOURCE_DIR)/CParse \ -I$(SOURCE_DIR)/Include \ -I$(SOURCE_DIR)/DOH \ -I$(SOURCE_DIR)/CParse \ -I$(SOURCE_DIR)/Doxygen \ -I$(SOURCE_DIR)/Preprocessor \ -I$(SOURCE_DIR)/Swig \ -I$(SOURCE_DIR)/Modules AM_CXXFLAGS = AM_YFLAGS = -d -Wall -Werror BUILT_SOURCES = CParse/parser.c CParse/parser.h eswig_SOURCES = CParse/cscanner.c \ CParse/parser.c \ CParse/templ.c \ CParse/util.c \ DOH/base.c \ DOH/file.c \ DOH/fio.c \ DOH/hash.c \ DOH/list.c \ DOH/memory.c \ DOH/string.c \ DOH/void.c \ Doxygen/csharpdoc.cxx \ Doxygen/csharpdoc.h \ Doxygen/doxyentity.cxx \ Doxygen/doxyentity.h \ Doxygen/doxyparser.cxx \ Doxygen/doxyparser.h \ Doxygen/doxytranslator.cxx \ Doxygen/doxytranslator.h \ Doxygen/javadoc.cxx \ Doxygen/javadoc.h \ Doxygen/pydoc.cxx \ Doxygen/pydoc.h \ Modules/allocate.cxx \ Modules/contract.cxx \ Modules/c.cxx \ Modules/csharp.cxx \ Modules/d.cxx \ Modules/directors.cxx \ Modules/emit.cxx \ Modules/go.cxx \ Modules/guile.cxx \ Modules/interface.cxx \ Modules/java.cxx \ Modules/javascript.cxx \ Modules/lang.cxx \ Modules/lua.cxx \ Modules/main.cxx \ Modules/nested.cxx \ Modules/ocaml.cxx \ Modules/octave.cxx \ Modules/overload.cxx \ Modules/perl5.cxx \ Modules/php.cxx \ Modules/python.cxx \ Modules/r.cxx \ Modules/ruby.cxx \ Modules/scilab.cxx \ Modules/swigmain.cxx \ Modules/tcl8.cxx \ Modules/typepass.cxx \ Modules/utils.cxx \ Modules/xml.cxx \ Preprocessor/cpp.c \ Preprocessor/expr.c \ Swig/cwrap.c \ Swig/deprecate.c \ Swig/error.c \ Swig/extend.c \ Swig/fragment.c \ Swig/getopt.c \ Swig/include.c \ Swig/misc.c \ Swig/naming.c \ Swig/parms.c \ Swig/scanner.c \ Swig/stype.c \ Swig/symbol.c \ Swig/tree.c \ Swig/typemap.c \ Swig/typeobj.c \ Swig/typesys.c \ Swig/wrapfunc.c # Beautify the code. # Note that this works well on C code, but does some odd joining of lines for C++ code. # Compiling with -DNDEBUG and no optimisations will allow one to do a binary diff of the # swig executable as a way of checking before and after the 'beautifying'. # Single files can be beautified with the beautify-file target, eg: 'make beautify-file INDENTFILE=chosenfile.c' SWIGTYPEDEFS = -T bool -T File -T DohObjInfo -T Parm -T Language -T List -T TargetLanguageModule -T Typetab -T ModuleFactory -T ErrorMessageFormat -T Symtab -T Hash -T Scanner -T String -T DohBase -T Node -T String_or_char -T SwigType -T Dispatcher -T Wrapper -T DohStringMethods -T DohFileMethods -T DohListMethods -T DohHashMethods -T DOH -T DohIterator -T ParmList -T FILE -T HashNode -T DOHObj_or_char -T DOHFile -T DOHString -T DOHString_or_char -T UpcallData -T DoxygenEntity -T string INDENTBAKSDIR = ../IndentBaks all: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) all-am .SUFFIXES: .SUFFIXES: .c .cxx .o .obj $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) @for dep in $?; do \ case '$(am__configure_deps)' in \ *$$dep*) \ ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ && { if test -f $@; then exit 0; else break; fi; }; \ exit 1;; \ esac; \ done; \ echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign Source/Makefile'; \ $(am__cd) $(top_srcdir) && \ $(AUTOMAKE) --foreign Source/Makefile Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status @case '$?' in \ *config.status*) \ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ *) \ echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ esac; $(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh $(top_srcdir)/configure: $(am__configure_deps) cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh $(ACLOCAL_M4): $(am__aclocal_m4_deps) cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh $(am__aclocal_m4_deps): install-binPROGRAMS: $(bin_PROGRAMS) @$(NORMAL_INSTALL) @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ if test -n "$$list"; then \ echo " $(MKDIR_P) '$(DESTDIR)$(bindir)'"; \ $(MKDIR_P) "$(DESTDIR)$(bindir)" || exit 1; \ fi; \ for p in $$list; do echo "$$p $$p"; done | \ sed 's/$(EXEEXT)$$//' | \ while read p p1; do if test -f $$p \ ; then echo "$$p"; echo "$$p"; else :; fi; \ done | \ sed -e 'p;s,.*/,,;n;h' \ -e 's|.*|.|' \ -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \ sed 'N;N;N;s,\n, ,g' | \ $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \ { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \ if ($$2 == $$4) files[d] = files[d] " " $$1; \ else { print "f", $$3 "/" $$4, $$1; } } \ END { for (d in files) print "f", d, files[d] }' | \ while read type dir files; do \ if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \ test -z "$$files" || { \ echo " $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(bindir)$$dir'"; \ $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(bindir)$$dir" || exit $$?; \ } \ ; done uninstall-binPROGRAMS: @$(NORMAL_UNINSTALL) @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ files=`for p in $$list; do echo "$$p"; done | \ sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \ -e 's/$$/$(EXEEXT)/' \ `; \ test -n "$$list" || exit 0; \ echo " ( cd '$(DESTDIR)$(bindir)' && rm -f" $$files ")"; \ cd "$(DESTDIR)$(bindir)" && rm -f $$files clean-binPROGRAMS: -test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS) CParse/$(am__dirstamp): @$(MKDIR_P) CParse @: > CParse/$(am__dirstamp) CParse/$(DEPDIR)/$(am__dirstamp): @$(MKDIR_P) CParse/$(DEPDIR) @: > CParse/$(DEPDIR)/$(am__dirstamp) CParse/cscanner.$(OBJEXT): CParse/$(am__dirstamp) \ CParse/$(DEPDIR)/$(am__dirstamp) CParse/parser.$(OBJEXT): CParse/$(am__dirstamp) \ CParse/$(DEPDIR)/$(am__dirstamp) CParse/templ.$(OBJEXT): CParse/$(am__dirstamp) \ CParse/$(DEPDIR)/$(am__dirstamp) CParse/util.$(OBJEXT): CParse/$(am__dirstamp) \ CParse/$(DEPDIR)/$(am__dirstamp) DOH/$(am__dirstamp): @$(MKDIR_P) DOH @: > DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp): @$(MKDIR_P) DOH/$(DEPDIR) @: > DOH/$(DEPDIR)/$(am__dirstamp) DOH/base.$(OBJEXT): DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp) DOH/file.$(OBJEXT): DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp) DOH/fio.$(OBJEXT): DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp) DOH/hash.$(OBJEXT): DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp) DOH/list.$(OBJEXT): DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp) DOH/memory.$(OBJEXT): DOH/$(am__dirstamp) \ DOH/$(DEPDIR)/$(am__dirstamp) DOH/string.$(OBJEXT): DOH/$(am__dirstamp) \ DOH/$(DEPDIR)/$(am__dirstamp) DOH/void.$(OBJEXT): DOH/$(am__dirstamp) DOH/$(DEPDIR)/$(am__dirstamp) Doxygen/$(am__dirstamp): @$(MKDIR_P) Doxygen @: > Doxygen/$(am__dirstamp) Doxygen/$(DEPDIR)/$(am__dirstamp): @$(MKDIR_P) Doxygen/$(DEPDIR) @: > Doxygen/$(DEPDIR)/$(am__dirstamp) Doxygen/csharpdoc.$(OBJEXT): Doxygen/$(am__dirstamp) \ Doxygen/$(DEPDIR)/$(am__dirstamp) Doxygen/doxyentity.$(OBJEXT): Doxygen/$(am__dirstamp) \ Doxygen/$(DEPDIR)/$(am__dirstamp) Doxygen/doxyparser.$(OBJEXT): Doxygen/$(am__dirstamp) \ Doxygen/$(DEPDIR)/$(am__dirstamp) Doxygen/doxytranslator.$(OBJEXT): Doxygen/$(am__dirstamp) \ Doxygen/$(DEPDIR)/$(am__dirstamp) Doxygen/javadoc.$(OBJEXT): Doxygen/$(am__dirstamp) \ Doxygen/$(DEPDIR)/$(am__dirstamp) Doxygen/pydoc.$(OBJEXT): Doxygen/$(am__dirstamp) \ Doxygen/$(DEPDIR)/$(am__dirstamp) Modules/$(am__dirstamp): @$(MKDIR_P) Modules @: > Modules/$(am__dirstamp) Modules/$(DEPDIR)/$(am__dirstamp): @$(MKDIR_P) Modules/$(DEPDIR) @: > Modules/$(DEPDIR)/$(am__dirstamp) Modules/allocate.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/contract.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/c.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/csharp.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/d.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/directors.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/emit.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/go.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/guile.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/interface.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/java.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/javascript.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/lang.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/lua.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/main.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/nested.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/ocaml.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/octave.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/overload.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/perl5.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/php.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/python.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/r.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/ruby.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/scilab.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/swigmain.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/tcl8.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/typepass.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/utils.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Modules/xml.$(OBJEXT): Modules/$(am__dirstamp) \ Modules/$(DEPDIR)/$(am__dirstamp) Preprocessor/$(am__dirstamp): @$(MKDIR_P) Preprocessor @: > Preprocessor/$(am__dirstamp) Preprocessor/$(DEPDIR)/$(am__dirstamp): @$(MKDIR_P) Preprocessor/$(DEPDIR) @: > Preprocessor/$(DEPDIR)/$(am__dirstamp) Preprocessor/cpp.$(OBJEXT): Preprocessor/$(am__dirstamp) \ Preprocessor/$(DEPDIR)/$(am__dirstamp) Preprocessor/expr.$(OBJEXT): Preprocessor/$(am__dirstamp) \ Preprocessor/$(DEPDIR)/$(am__dirstamp) Swig/$(am__dirstamp): @$(MKDIR_P) Swig @: > Swig/$(am__dirstamp) Swig/$(DEPDIR)/$(am__dirstamp): @$(MKDIR_P) Swig/$(DEPDIR) @: > Swig/$(DEPDIR)/$(am__dirstamp) Swig/cwrap.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/deprecate.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/error.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/extend.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/fragment.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/getopt.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/include.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/misc.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/naming.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/parms.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/scanner.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/stype.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/symbol.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/tree.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/typemap.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/typeobj.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/typesys.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) Swig/wrapfunc.$(OBJEXT): Swig/$(am__dirstamp) \ Swig/$(DEPDIR)/$(am__dirstamp) eswig$(EXEEXT): $(eswig_OBJECTS) $(eswig_DEPENDENCIES) $(EXTRA_eswig_DEPENDENCIES) @rm -f eswig$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(eswig_OBJECTS) $(eswig_LDADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) -rm -f CParse/*.$(OBJEXT) -rm -f DOH/*.$(OBJEXT) -rm -f Doxygen/*.$(OBJEXT) -rm -f Modules/*.$(OBJEXT) -rm -f Preprocessor/*.$(OBJEXT) -rm -f Swig/*.$(OBJEXT) distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@CParse/$(DEPDIR)/cscanner.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@CParse/$(DEPDIR)/parser.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@CParse/$(DEPDIR)/templ.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@CParse/$(DEPDIR)/util.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/base.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/file.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/fio.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/hash.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/list.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/memory.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/string.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@DOH/$(DEPDIR)/void.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Doxygen/$(DEPDIR)/csharpdoc.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Doxygen/$(DEPDIR)/doxyentity.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Doxygen/$(DEPDIR)/doxyparser.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Doxygen/$(DEPDIR)/doxytranslator.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Doxygen/$(DEPDIR)/javadoc.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Doxygen/$(DEPDIR)/pydoc.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/allocate.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/c.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/contract.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/csharp.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/d.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/directors.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/emit.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/go.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/guile.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/interface.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/java.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/javascript.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/lang.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/lua.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/main.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/nested.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/ocaml.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/octave.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/overload.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/perl5.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/php.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/python.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/r.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/ruby.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/scilab.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/swigmain.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/tcl8.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/typepass.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/utils.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Modules/$(DEPDIR)/xml.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Preprocessor/$(DEPDIR)/cpp.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Preprocessor/$(DEPDIR)/expr.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/cwrap.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/deprecate.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/error.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/extend.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/fragment.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/getopt.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/include.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/misc.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/naming.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/parms.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/scanner.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/stype.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/symbol.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/tree.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/typemap.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/typeobj.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/typesys.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@Swig/$(DEPDIR)/wrapfunc.Po@am__quote@ # am--include-marker $(am__depfiles_remade): @$(MKDIR_P) $(@D) @echo '# dummy' >$@-t && $(am__mv) $@-t $@ am--depfiles: $(am__depfiles_remade) .c.o: @am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ @am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ @am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po @AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< .c.obj: @am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ @am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ @am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po @AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` .cxx.o: @am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ @am__fastdepCXX_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po @AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ $< .cxx.obj: @am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ @am__fastdepCXX_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po @AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` ID: $(am__tagged_files) $(am__define_uniq_tagged_files); mkid -fID $$unique tags: tags-am TAGS: tags tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) set x; \ here=`pwd`; \ $(am__define_uniq_tagged_files); \ shift; \ if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ test -n "$$unique" || unique=$$empty_fix; \ if test $$# -gt 0; then \ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ "$$@" $$unique; \ else \ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ $$unique; \ fi; \ fi ctags: ctags-am CTAGS: ctags ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) $(am__define_uniq_tagged_files); \ test -z "$(CTAGS_ARGS)$$unique" \ || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ $$unique GTAGS: here=`$(am__cd) $(top_builddir) && pwd` \ && $(am__cd) $(top_srcdir) \ && gtags -i $(GTAGS_ARGS) "$$here" cscopelist: cscopelist-am cscopelist-am: $(am__tagged_files) list='$(am__tagged_files)'; \ case "$(srcdir)" in \ [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ *) sdir=$(subdir)/$(srcdir) ;; \ esac; \ for i in $$list; do \ if test -f "$$i"; then \ echo "$(subdir)/$$i"; \ else \ echo "$$sdir/$$i"; \ fi; \ done >> $(top_builddir)/cscope.files distclean-tags: -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags distdir: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) distdir-am distdir-am: $(DISTFILES) @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ list='$(DISTFILES)'; \ dist_files=`for file in $$list; do echo $$file; done | \ sed -e "s|^$$srcdirstrip/||;t" \ -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ case $$dist_files in \ */*) $(MKDIR_P) `echo "$$dist_files" | \ sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ sort -u` ;; \ esac; \ for file in $$dist_files; do \ if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ if test -d $$d/$$file; then \ dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ if test -d "$(distdir)/$$file"; then \ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ fi; \ if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ fi; \ cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ else \ test -f "$(distdir)/$$file" \ || cp -p $$d/$$file "$(distdir)/$$file" \ || exit 1; \ fi; \ done check-am: all-am check: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) check-am all-am: Makefile $(PROGRAMS) all-local installdirs: for dir in "$(DESTDIR)$(bindir)"; do \ test -z "$$dir" || $(MKDIR_P) "$$dir"; \ done install: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) install-am install-exec: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data: install-data-am uninstall: uninstall-am install-am: all-am @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am installcheck: installcheck-am install-strip: if test -z '$(STRIP)'; then \ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ install; \ else \ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ fi mostlyclean-generic: clean-generic: distclean-generic: -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) -rm -f CParse/$(DEPDIR)/$(am__dirstamp) -rm -f CParse/$(am__dirstamp) -rm -f DOH/$(DEPDIR)/$(am__dirstamp) -rm -f DOH/$(am__dirstamp) -rm -f Doxygen/$(DEPDIR)/$(am__dirstamp) -rm -f Doxygen/$(am__dirstamp) -rm -f Modules/$(DEPDIR)/$(am__dirstamp) -rm -f Modules/$(am__dirstamp) -rm -f Preprocessor/$(DEPDIR)/$(am__dirstamp) -rm -f Preprocessor/$(am__dirstamp) -rm -f Swig/$(DEPDIR)/$(am__dirstamp) -rm -f Swig/$(am__dirstamp) maintainer-clean-generic: @echo "This command is intended for maintainers to use" @echo "it deletes files that may require special tools to rebuild." -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES) clean: clean-am clean-am: clean-binPROGRAMS clean-generic clean-local mostlyclean-am distclean: distclean-am -rm -f CParse/$(DEPDIR)/cscanner.Po -rm -f CParse/$(DEPDIR)/parser.Po -rm -f CParse/$(DEPDIR)/templ.Po -rm -f CParse/$(DEPDIR)/util.Po -rm -f DOH/$(DEPDIR)/base.Po -rm -f DOH/$(DEPDIR)/file.Po -rm -f DOH/$(DEPDIR)/fio.Po -rm -f DOH/$(DEPDIR)/hash.Po -rm -f DOH/$(DEPDIR)/list.Po -rm -f DOH/$(DEPDIR)/memory.Po -rm -f DOH/$(DEPDIR)/string.Po -rm -f DOH/$(DEPDIR)/void.Po -rm -f Doxygen/$(DEPDIR)/csharpdoc.Po -rm -f Doxygen/$(DEPDIR)/doxyentity.Po -rm -f Doxygen/$(DEPDIR)/doxyparser.Po -rm -f Doxygen/$(DEPDIR)/doxytranslator.Po -rm -f Doxygen/$(DEPDIR)/javadoc.Po -rm -f Doxygen/$(DEPDIR)/pydoc.Po -rm -f Modules/$(DEPDIR)/allocate.Po -rm -f Modules/$(DEPDIR)/c.Po -rm -f Modules/$(DEPDIR)/contract.Po -rm -f Modules/$(DEPDIR)/csharp.Po -rm -f Modules/$(DEPDIR)/d.Po -rm -f Modules/$(DEPDIR)/directors.Po -rm -f Modules/$(DEPDIR)/emit.Po -rm -f Modules/$(DEPDIR)/go.Po -rm -f Modules/$(DEPDIR)/guile.Po -rm -f Modules/$(DEPDIR)/interface.Po -rm -f Modules/$(DEPDIR)/java.Po -rm -f Modules/$(DEPDIR)/javascript.Po -rm -f Modules/$(DEPDIR)/lang.Po -rm -f Modules/$(DEPDIR)/lua.Po -rm -f Modules/$(DEPDIR)/main.Po -rm -f Modules/$(DEPDIR)/nested.Po -rm -f Modules/$(DEPDIR)/ocaml.Po -rm -f Modules/$(DEPDIR)/octave.Po -rm -f Modules/$(DEPDIR)/overload.Po -rm -f Modules/$(DEPDIR)/perl5.Po -rm -f Modules/$(DEPDIR)/php.Po -rm -f Modules/$(DEPDIR)/python.Po -rm -f Modules/$(DEPDIR)/r.Po -rm -f Modules/$(DEPDIR)/ruby.Po -rm -f Modules/$(DEPDIR)/scilab.Po -rm -f Modules/$(DEPDIR)/swigmain.Po -rm -f Modules/$(DEPDIR)/tcl8.Po -rm -f Modules/$(DEPDIR)/typepass.Po -rm -f Modules/$(DEPDIR)/utils.Po -rm -f Modules/$(DEPDIR)/xml.Po -rm -f Preprocessor/$(DEPDIR)/cpp.Po -rm -f Preprocessor/$(DEPDIR)/expr.Po -rm -f Swig/$(DEPDIR)/cwrap.Po -rm -f Swig/$(DEPDIR)/deprecate.Po -rm -f Swig/$(DEPDIR)/error.Po -rm -f Swig/$(DEPDIR)/extend.Po -rm -f Swig/$(DEPDIR)/fragment.Po -rm -f Swig/$(DEPDIR)/getopt.Po -rm -f Swig/$(DEPDIR)/include.Po -rm -f Swig/$(DEPDIR)/misc.Po -rm -f Swig/$(DEPDIR)/naming.Po -rm -f Swig/$(DEPDIR)/parms.Po -rm -f Swig/$(DEPDIR)/scanner.Po -rm -f Swig/$(DEPDIR)/stype.Po -rm -f Swig/$(DEPDIR)/symbol.Po -rm -f Swig/$(DEPDIR)/tree.Po -rm -f Swig/$(DEPDIR)/typemap.Po -rm -f Swig/$(DEPDIR)/typeobj.Po -rm -f Swig/$(DEPDIR)/typesys.Po -rm -f Swig/$(DEPDIR)/wrapfunc.Po -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-local distclean-tags dvi: dvi-am dvi-am: html: html-am html-am: info: info-am info-am: install-data-am: install-dvi: install-dvi-am install-dvi-am: install-exec-am: install-binPROGRAMS install-html: install-html-am install-html-am: install-info: install-info-am install-info-am: install-man: install-pdf: install-pdf-am install-pdf-am: install-ps: install-ps-am install-ps-am: installcheck-am: maintainer-clean: maintainer-clean-am -rm -f CParse/$(DEPDIR)/cscanner.Po -rm -f CParse/$(DEPDIR)/parser.Po -rm -f CParse/$(DEPDIR)/templ.Po -rm -f CParse/$(DEPDIR)/util.Po -rm -f DOH/$(DEPDIR)/base.Po -rm -f DOH/$(DEPDIR)/file.Po -rm -f DOH/$(DEPDIR)/fio.Po -rm -f DOH/$(DEPDIR)/hash.Po -rm -f DOH/$(DEPDIR)/list.Po -rm -f DOH/$(DEPDIR)/memory.Po -rm -f DOH/$(DEPDIR)/string.Po -rm -f DOH/$(DEPDIR)/void.Po -rm -f Doxygen/$(DEPDIR)/csharpdoc.Po -rm -f Doxygen/$(DEPDIR)/doxyentity.Po -rm -f Doxygen/$(DEPDIR)/doxyparser.Po -rm -f Doxygen/$(DEPDIR)/doxytranslator.Po -rm -f Doxygen/$(DEPDIR)/javadoc.Po -rm -f Doxygen/$(DEPDIR)/pydoc.Po -rm -f Modules/$(DEPDIR)/allocate.Po -rm -f Modules/$(DEPDIR)/c.Po -rm -f Modules/$(DEPDIR)/contract.Po -rm -f Modules/$(DEPDIR)/csharp.Po -rm -f Modules/$(DEPDIR)/d.Po -rm -f Modules/$(DEPDIR)/directors.Po -rm -f Modules/$(DEPDIR)/emit.Po -rm -f Modules/$(DEPDIR)/go.Po -rm -f Modules/$(DEPDIR)/guile.Po -rm -f Modules/$(DEPDIR)/interface.Po -rm -f Modules/$(DEPDIR)/java.Po -rm -f Modules/$(DEPDIR)/javascript.Po -rm -f Modules/$(DEPDIR)/lang.Po -rm -f Modules/$(DEPDIR)/lua.Po -rm -f Modules/$(DEPDIR)/main.Po -rm -f Modules/$(DEPDIR)/nested.Po -rm -f Modules/$(DEPDIR)/ocaml.Po -rm -f Modules/$(DEPDIR)/octave.Po -rm -f Modules/$(DEPDIR)/overload.Po -rm -f Modules/$(DEPDIR)/perl5.Po -rm -f Modules/$(DEPDIR)/php.Po -rm -f Modules/$(DEPDIR)/python.Po -rm -f Modules/$(DEPDIR)/r.Po -rm -f Modules/$(DEPDIR)/ruby.Po -rm -f Modules/$(DEPDIR)/scilab.Po -rm -f Modules/$(DEPDIR)/swigmain.Po -rm -f Modules/$(DEPDIR)/tcl8.Po -rm -f Modules/$(DEPDIR)/typepass.Po -rm -f Modules/$(DEPDIR)/utils.Po -rm -f Modules/$(DEPDIR)/xml.Po -rm -f Preprocessor/$(DEPDIR)/cpp.Po -rm -f Preprocessor/$(DEPDIR)/expr.Po -rm -f Swig/$(DEPDIR)/cwrap.Po -rm -f Swig/$(DEPDIR)/deprecate.Po -rm -f Swig/$(DEPDIR)/error.Po -rm -f Swig/$(DEPDIR)/extend.Po -rm -f Swig/$(DEPDIR)/fragment.Po -rm -f Swig/$(DEPDIR)/getopt.Po -rm -f Swig/$(DEPDIR)/include.Po -rm -f Swig/$(DEPDIR)/misc.Po -rm -f Swig/$(DEPDIR)/naming.Po -rm -f Swig/$(DEPDIR)/parms.Po -rm -f Swig/$(DEPDIR)/scanner.Po -rm -f Swig/$(DEPDIR)/stype.Po -rm -f Swig/$(DEPDIR)/symbol.Po -rm -f Swig/$(DEPDIR)/tree.Po -rm -f Swig/$(DEPDIR)/typemap.Po -rm -f Swig/$(DEPDIR)/typeobj.Po -rm -f Swig/$(DEPDIR)/typesys.Po -rm -f Swig/$(DEPDIR)/wrapfunc.Po -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic mostlyclean: mostlyclean-am mostlyclean-am: mostlyclean-compile mostlyclean-generic pdf: pdf-am pdf-am: ps: ps-am ps-am: uninstall-am: uninstall-binPROGRAMS .MAKE: all check install install-am install-exec install-strip .PHONY: CTAGS GTAGS TAGS all all-am all-local am--depfiles check \ check-am clean clean-binPROGRAMS clean-generic clean-local \ cscopelist-am ctags ctags-am distclean distclean-compile \ distclean-generic distclean-local distclean-tags distdir dvi \ dvi-am html html-am info info-am install install-am \ install-binPROGRAMS install-data install-data-am install-dvi \ install-dvi-am install-exec install-exec-am install-html \ install-html-am install-info install-info-am install-man \ install-pdf install-pdf-am install-ps install-ps-am \ install-strip installcheck installcheck-am installdirs \ maintainer-clean maintainer-clean-generic mostlyclean \ mostlyclean-compile mostlyclean-generic pdf pdf-am ps ps-am \ tags tags-am uninstall uninstall-am uninstall-binPROGRAMS .PRECIOUS: Makefile # Use GNU make's grouped targets to avoid repeated bison execution for # each target. CParse/parser.c CParse/parser.h &: CParse/parser.y $(BISON) $(AM_YFLAGS) $(YFLAGS) --output=CParse/parser.c $(srcdir)/CParse/parser.y # The executable is copied to the root directory for installation and running the test-suite. # This occurs on each invocation of make and is a step towards providing support for multiple # build directories. all-local: eswig@EXEEXT@ cp -f $(top_builddir)/Source/eswig@EXEEXT@ $(top_builddir)/swig@EXEEXT@ clean-local: rm -f $(top_builddir)/swig@EXEEXT@ rm -f core @EXTRA_CLEAN@ distclean-local: rm -f $(top_builddir)/Source/Include/swigconfig.h rm -f $(top_builddir)/Source/Include/stamp-h1 beautify: rm -rf $(INDENTBAKSDIR) mkdir $(INDENTBAKSDIR) mkdir $(INDENTBAKSDIR)/CParse mkdir $(INDENTBAKSDIR)/DOH mkdir $(INDENTBAKSDIR)/Modules mkdir $(INDENTBAKSDIR)/Preprocessor mkdir $(INDENTBAKSDIR)/Swig mkdir $(INDENTBAKSDIR)/Include (csources=`find . -name "*.c"` && \ hsources=`find . -name "*.h"` && \ cxxsources=`find . -name "*.cxx"` && \ for file in $$csources $$hsources $$cxxsources; do \ $(MAKE) beautify-file INDENTFILE=$$file; \ done; ) beautify-file: test -e $(INDENTBAKSDIR) || (echo $(INDENTBAKSDIR) directory does not exist && exit 1;) test -n "$(INDENTFILE)" || (echo INDENTFILE not defined && exit 1;) test -e $(INDENTFILE) || (echo File does not exist: $(INDENTFILE) && exit 1;) cp $(INDENTFILE) $(INDENTBAKSDIR)/$(INDENTFILE); indent -kr --honour-newlines --line-length160 --indent-level2 --braces-on-func-def-line --leave-optional-blank-lines $(SWIGTYPEDEFS) $(INDENTFILE) -o $(INDENTFILE).tmp; cat $(INDENTFILE).tmp | sed -e 's/const const /const /' > $(INDENTFILE); rm $(INDENTFILE).tmp; # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. .NOEXPORT: swig-4.4.0/Source/Swig/0000775000175000017500000000000015075470607014534 5ustar williamwilliamswig-4.4.0/Source/Swig/typeobj.c0000664000175000017500000010774115075443613016363 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * typeobj.c * * This file provides functions for constructing, manipulating, and testing * type objects. Type objects are merely the raw low-level representation * of C++ types. They do not incorporate high-level type system features * like typedef, namespaces, etc. * ----------------------------------------------------------------------------- */ #include "swig.h" #include #include /* ----------------------------------------------------------------------------- * Synopsis * * This file provides a collection of low-level functions for constructing and * manipulating C++ data types. In SWIG, C++ datatypes are encoded as simple * text strings. This representation is compact, easy to debug, and easy to read. * * General idea: * * Types are represented by a base type (e.g., "int") and a collection of * type operators applied to the base (e.g., pointers, arrays, etc...). * * Encoding: * * Types are encoded as strings of type constructors such as follows: * * String Encoding C Example * --------------- --------- * p.p.int int ** * a(300).a(400).int int [300][400] * p.q(const).char char const * * * All type constructors are denoted by a trailing '.': * * 'p.' = Pointer (*) * 'r.' = Reference or ref-qualifier (&) * 'z.' = Rvalue reference or ref-qualifier (&&) * 'v.' = Variadic (...) * 'a(n).' = Array of size n [n] * 'f(..,..).' = Function with arguments (args) * 'q(str).' = Qualifier, such as const or volatile (cv-qualifier) * 'm(cls).' = Pointer to member (cls::*) * * The complete type representation for varargs is: * 'v(...)' * * The encoding follows the order that you might describe a type in words. * For example "p.a(200).int" is "A pointer to array of int's" and * "p.q(const).char" is "a pointer to a const char". * * This representation of types is fairly convenient because ordinary string * operations can be used for type manipulation. For example, a type could be * formed by combining two strings such as the following: * * "p.p." + "a(400).int" = "p.p.a(400).int" * * For C++, typenames may be parameterized using <(...)>. Here are some * examples: * * String Encoding C++ Example * --------------- ------------ * p.vector<(int)> vector * * r.foo<(int,p.double)> foo & * * Contents of this file: * * Most of this functions in this file pertain to the low-level manipulation * of type objects. There are constructor functions like this: * * SwigType_add_pointer() * SwigType_add_reference() * SwigType_add_rvalue_reference() * SwigType_add_variadic() * SwigType_add_array() * * These are used to build new types. There are also functions to undo these * operations. For example: * * SwigType_del_pointer() * SwigType_del_reference() * SwigType_del_rvalue_reference() * SwigType_del_variadic() * SwigType_del_array() * * In addition, there are query functions * * SwigType_ispointer() * SwigType_isreference() * SwigType_isrvalue_reference() * SwigType_isvariadic() * SwigType_isarray() * * Finally, there are some data extraction functions that can be used to * extract array dimensions, template arguments, and so forth. * * It is very important for developers to realize that the functions in this * module do *NOT* incorporate higher-level type system features like typedef. * For example, you could have C code like this: * * typedef int *intptr; * * In this case, a SwigType of type 'intptr' will be treated as a simple type and * functions like SwigType_ispointer() will evaluate as false. It is strongly * advised that developers use the TypeSys_* interface to check types in a more * reliable manner. * ----------------------------------------------------------------------------- */ /* The next few functions are utility functions used in the construction and management of types */ /* ----------------------------------------------------------------------------- * static element_size() * * This utility function finds the size of a single type element in a type string. * Type elements are always delimited by periods, but may be nested with * parentheses. A nested element is always handled as a single item. * * Returns the integer size of the element (which can be used to extract a * substring, to chop the element off, or for other purposes). * ----------------------------------------------------------------------------- */ static int element_size(char *c) { int nparen; char *s = c; while (*c) { if (*c == '.') { c++; return (int) (c - s); } else if (*c == '(') { nparen = 1; c++; while (*c) { if (*c == '(') nparen++; if (*c == ')') { nparen--; if (nparen == 0) break; } c++; } } if (*c) c++; } return (int) (c - s); } /* ----------------------------------------------------------------------------- * SwigType_del_element() * * Deletes one type element from the type. * ----------------------------------------------------------------------------- */ SwigType *SwigType_del_element(SwigType *t) { int sz = element_size(Char(t)); Delslice(t, 0, sz); return t; } /* ----------------------------------------------------------------------------- * SwigType_pop() * * Pop one type element off the type. * For example: * t in: q(const).p.Integer * t out: p.Integer * result: q(const). * ----------------------------------------------------------------------------- */ SwigType *SwigType_pop(SwigType *t) { SwigType *result; char *c; int sz; c = Char(t); if (!*c) return 0; sz = element_size(c); result = NewStringWithSize(c, sz); Delslice(t, 0, sz); c = Char(t); if (*c == '.') { Delitem(t, 0); } return result; } /* ----------------------------------------------------------------------------- * SwigType_last() * * Return the last element of the given (partial) type. * For example: * t: q(const).p. * result: p. * ----------------------------------------------------------------------------- */ SwigType *SwigType_last(SwigType *t) { SwigType *result; char *c; char *last; int sz = 0; if (!t) return 0; /* Find the last element */ c = Char(t); last = 0; while (*c) { last = c; sz = element_size(c); c = c + sz; if (*c == '.') { c++; sz++; } } /* Extract the last element */ if (last) { result = NewStringWithSize(last, sz); } else { result = 0; } return result; } /* ----------------------------------------------------------------------------- * SwigType_parm() * * Returns the parameter of an operator as a string * ----------------------------------------------------------------------------- */ String *SwigType_parm(const SwigType *t) { char *start, *c; int nparens = 0; c = Char(t); while (*c && (*c != '(') && (*c != '.')) c++; if (!*c || (*c == '.')) return 0; c++; start = c; while (*c) { if (*c == ')') { if (nparens == 0) break; nparens--; } else if (*c == '(') { nparens++; } c++; } return NewStringWithSize(start, (int) (c - start)); } /* ----------------------------------------------------------------------------- * SwigType_split() * * Splits a type into its component parts and returns a list of string. * The component parts of SwigType are split by '.'. * ----------------------------------------------------------------------------- */ List *SwigType_split(const SwigType *t) { String *item; List *list; char *c; int len; c = Char(t); list = NewList(); while (*c) { len = element_size(c); item = NewStringWithSize(c, len); Append(list, item); Delete(item); c = c + len; if (*c == '.') c++; } return list; } /* ----------------------------------------------------------------------------- * SwigType_parmlist() * * Splits a comma separated list of SwigType * parameters into its component parts * The input is expected to contain the parameter list within () brackets * Returns 0 if no argument list in the input, ie there are no round brackets () * Returns an empty List if there are no parameters in the () brackets * For example: * * Foo(std::string,p.f().Bar<(int,double)>) * * returns 2 SwigType * elements in the list: * std::string * p.f().Bar<(int,double)> * ----------------------------------------------------------------------------- */ List *SwigType_parmlist(const SwigType *p) { String *item = 0; List *list; char *c; char *itemstart; int size; assert(p); c = Char(p); while (*c && (*c != '(') && (*c != '.')) c++; if (!*c) return 0; assert(*c != '.'); /* p is expected to contain sub elements of a type */ c++; list = NewList(); itemstart = c; while (*c) { if (*c == ',') { size = (int) (c - itemstart); item = NewStringWithSize(itemstart, size); Append(list, item); Delete(item); itemstart = c + 1; } else if (*c == '(') { int nparens = 1; c++; while (*c) { if (*c == '(') nparens++; if (*c == ')') { nparens--; if (nparens == 0) break; } c++; } } else if (*c == ')') { break; } if (*c) c++; } size = (int) (c - itemstart); if (size > 0) { item = NewStringWithSize(itemstart, size); Append(list, item); } Delete(item); return list; } /* ----------------------------------------------------------------------------- * Pointers * * SwigType_add_pointer() * SwigType_del_pointer() * SwigType_ispointer() * * Add, remove, and test if a type is a pointer. The deletion and query * functions take into account qualifiers (if any). * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_pointer(SwigType *t) { Insert(t, 0, "p."); return t; } SwigType *SwigType_del_pointer(SwigType *t) { char *c, *s; c = Char(t); s = c; /* Skip qualifiers, if any */ if (strncmp(c, "q(", 2) == 0) { c = strchr(c, '.'); assert(c); c++; } if (strncmp(c, "p.", 2)) { Printf(stderr, "Internal error: SwigType_del_pointer applied to non-pointer type %s.\n", t); Exit(EXIT_FAILURE); } Delslice(t, 0, (int)((c - s) + 2)); return t; } int SwigType_ispointer(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); /* Skip qualifiers, if any */ if (strncmp(c, "q(", 2) == 0) { c = strchr(c, '.'); if (!c) return 0; c++; } if (strncmp(c, "p.", 2) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * References * * SwigType_add_reference() * SwigType_del_reference() * SwigType_isreference() * * Add, remove, and test if a type is a reference. The deletion and query * functions take into account qualifiers (if any). * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_reference(SwigType *t) { Insert(t, 0, "r."); return t; } SwigType *SwigType_del_reference(SwigType *t) { char *c = Char(t); if (strncmp(c, "r.", 2)) { Printf(stderr, "Internal error: SwigType_del_reference applied to non-reference type %s.\n", t); Exit(EXIT_FAILURE); } Delslice(t, 0, 2); return t; } int SwigType_isreference(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "r.", 2) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * Rvalue References * * SwigType_add_rvalue_reference() * SwigType_del_rvalue_reference() * SwigType_isrvalue_reference() * * Add, remove, and test if a type is a rvalue reference. The deletion and query * functions take into account qualifiers (if any). * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_rvalue_reference(SwigType *t) { Insert(t, 0, "z."); return t; } SwigType *SwigType_del_rvalue_reference(SwigType *t) { char *c = Char(t); if (strncmp(c, "z.", 2)) { Printf(stderr, "Internal error: SwigType_del_rvalue_reference() applied to non-rvalue-reference type %s.\n", t); Exit(EXIT_FAILURE); } Delslice(t, 0, 2); return t; } int SwigType_isrvalue_reference(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "z.", 2) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * Variadic * * SwigType_add_variadic() * SwigType_del_variadic() * SwigType_isvariadic() * * Add, remove, and test if a type is a variadic. The deletion and query * functions take into account qualifiers (if any). * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_variadic(SwigType *t) { Insert(t, 0, "v."); return t; } SwigType *SwigType_del_variadic(SwigType *t) { char *c = Char(t); if (strncmp(c, "v.", 2)) { Printf(stderr, "Internal error: SwigType_del_variadic applied to non-variadic type %s.\n", t); Exit(EXIT_FAILURE); } Delslice(t, 0, 2); return t; } int SwigType_isvariadic(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "v.", 2) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * Qualifiers * * SwigType_add_qualifier() * SwigType_del_qualifier() * SwigType_is_qualifier() * * Adds type qualifiers like "const" and "volatile". When multiple qualifiers * are added to a type, they are combined together into a single qualifier. * Repeated qualifications have no effect. Moreover, the order of qualifications * is alphabetical---meaning that "const volatile" and "volatile const" are * stored in exactly the same way as "q(const volatile)". * 'qual' can be a list of multiple qualifiers in any order, separated by spaces. * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_qualifier(SwigType *t, const_String_or_char_ptr qual) { List *qlist; String *allq, *newq; int i, sz; const char *cqprev = 0; const char *c = Char(t); const char *cqual = Char(qual); /* if 't' has no qualifiers and 'qual' is a single qualifier, simply add it */ if ((strncmp(c, "q(", 2) != 0) && (strchr(cqual, ' ') == 0)) { String *temp = NewStringf("q(%s).", cqual); Insert(t, 0, temp); Delete(temp); return t; } /* create string of all qualifiers */ if (strncmp(c, "q(", 2) == 0) { allq = SwigType_parm(t); Append(allq, " "); SwigType_del_element(t); /* delete old qualifier list from 't' */ } else { allq = NewStringEmpty(); } Append(allq, qual); /* create list of all qualifiers from string */ qlist = Split(allq, ' ', INT_MAX); Delete(allq); /* sort in alphabetical order */ SortList(qlist, Strcmp); /* create new qualifier string from unique elements of list */ sz = Len(qlist); newq = NewString("q("); for (i = 0; i < sz; ++i) { String *q = Getitem(qlist, i); const char *cq = Char(q); if (cqprev == 0 || strcmp(cqprev, cq) != 0) { if (i > 0) { Append(newq, " "); } Append(newq, q); cqprev = cq; } } Append(newq, ")."); Delete(qlist); /* replace qualifier string with new one */ Insert(t, 0, newq); Delete(newq); return t; } SwigType *SwigType_del_qualifier(SwigType *t) { char *c = Char(t); int check = strncmp(c, "q(", 2); assert(check == 0); (void)check; Delslice(t, 0, element_size(c)); return t; } int SwigType_isqualifier(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "q(", 2) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * Function Pointers * ----------------------------------------------------------------------------- */ int SwigType_isfunctionpointer(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "p.f(", 4) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * SwigType_functionpointer_decompose * * Decompose the function pointer into the parameter list and the return type * t - input and on completion contains the return type * returns the function's parameters * ----------------------------------------------------------------------------- */ SwigType *SwigType_functionpointer_decompose(SwigType *t) { String *p; assert(SwigType_isfunctionpointer(t)); p = SwigType_pop(t); Delete(p); p = SwigType_pop(t); return p; } /* ----------------------------------------------------------------------------- * Member Pointers * * SwigType_add_memberpointer() * SwigType_del_memberpointer() * SwigType_ismemberpointer() * * Add, remove, and test for C++ pointer to members. * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_memberpointer(SwigType *t, const_String_or_char_ptr name) { String *temp = NewStringf("m(%s).", name); Insert(t, 0, temp); Delete(temp); return t; } SwigType *SwigType_del_memberpointer(SwigType *t) { char *c = Char(t); int check = strncmp(c, "m(", 2); assert(check == 0); (void)check; Delslice(t, 0, element_size(c)); return t; } int SwigType_ismemberpointer(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "m(", 2) == 0) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * Arrays * * SwigType_add_array() * SwigType_del_array() * SwigType_isarray() * * Utility functions: * * SwigType_array_ndim() - Calculate number of array dimensions. * SwigType_array_getdim() - Get array dimension * SwigType_array_setdim() - Set array dimension * SwigType_array_type() - Return array type * SwigType_pop_arrays() - Remove all arrays * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_array(SwigType *t, const_String_or_char_ptr size) { String *temp = NewString("a("); Append(temp, size); Append(temp, ")."); Insert(t, 0, temp); Delete(temp); return t; } SwigType *SwigType_del_array(SwigType *t) { char *c = Char(t); if (strncmp(c, "a(", 2)) { Printf(stderr, "Internal error: SwigType_del_array() applied to non-array type %s.\n", t); Exit(EXIT_FAILURE); } Delslice(t, 0, element_size(c)); return t; } int SwigType_isarray(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "a(", 2) == 0) { return 1; } return 0; } /* * SwigType_prefix_is_simple_1D_array * * Determine if the type is a 1D array type that is treated as a pointer within SWIG * eg Foo[], Foo[3] return true, but Foo[3][3], Foo*[], Foo*[3], Foo**[] return false */ int SwigType_prefix_is_simple_1D_array(const SwigType *t) { char *c = Char(t); if (c && (strncmp(c, "a(", 2) == 0)) { c = strchr(c, '.'); if (c) return (*(++c) == 0); } return 0; } /* Remove all arrays */ SwigType *SwigType_pop_arrays(SwigType *t) { String *ta; assert(SwigType_isarray(t)); ta = NewStringEmpty(); while (SwigType_isarray(t)) { SwigType *td = SwigType_pop(t); Append(ta, td); Delete(td); } return ta; } /* Return number of array dimensions */ int SwigType_array_ndim(const SwigType *t) { int ndim = 0; char *c = Char(t); while (c && (strncmp(c, "a(", 2) == 0)) { c = strchr(c, '.'); if (c) { c++; ndim++; } } return ndim; } /* Get nth array dimension */ String *SwigType_array_getdim(const SwigType *t, int n) { char *c = Char(t); while (c && (strncmp(c, "a(", 2) == 0) && (n > 0)) { c = strchr(c, '.'); if (c) { c++; n--; } } if (n == 0) { String *dim = SwigType_parm(c); if (SwigType_istemplate(dim)) { String *ndim = SwigType_namestr(dim); Delete(dim); dim = ndim; } return dim; } return 0; } /* Replace nth array dimension */ void SwigType_array_setdim(SwigType *t, int n, const_String_or_char_ptr rep) { String *result = 0; char temp; char *start; char *c = Char(t); start = c; if (strncmp(c, "a(", 2)) { Printf(stderr, "Internal error: SwigType_array_type applied to non-array type %s.\n", t); Exit(EXIT_FAILURE); } while (c && (strncmp(c, "a(", 2) == 0) && (n > 0)) { c = strchr(c, '.'); if (c) { c++; n--; } } if (n == 0) { temp = *c; *c = 0; result = NewString(start); Printf(result, "a(%s)", rep); *c = temp; c = strchr(c, '.'); Append(result, c); } Clear(t); Append(t, result); Delete(result); } /* Return base type of an array */ SwigType *SwigType_array_type(const SwigType *ty) { SwigType *t; t = Copy(ty); while (SwigType_isarray(t)) { Delete(SwigType_pop(t)); } return t; } /* ----------------------------------------------------------------------------- * Functions * * SwigType_add_function() * SwigType_function_parms_only() * SwigType_isfunction() * SwigType_pop_function() * * Add, remove, and test for function types. * ----------------------------------------------------------------------------- */ /* ----------------------------------------------------------------------------- * SwigType_function_parms_only() * * Creates a comma separated list of SwigType strings from parms * ----------------------------------------------------------------------------- */ SwigType *SwigType_function_parms_only(ParmList *parms) { Parm *p; SwigType *t = NewString(""); for (p = parms; p; p = nextSibling(p)) { if (p != parms) Putc(',', t); Append(t, Getattr(p, "type")); } return t; } /* ----------------------------------------------------------------------------- * SwigType_add_function() * * Returns the function type, t, constructed from the parameters, parms * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_function(SwigType *t, ParmList *parms) { SwigType *fparms = SwigType_function_parms_only(parms); Insert(fparms, 0, "f("); Append(fparms, ")."); Insert(t, 0, fparms); Delete(fparms); return t; } /* ----------------------------------------------------------------------------- * SwigType_pop_function() * * Pop and return the function from the input type leaving the function's return * type, if any. * For example: * t in: q(const).f().p. * t out: p. * result: q(const).f(). * ----------------------------------------------------------------------------- */ SwigType *SwigType_pop_function(SwigType *t) { SwigType *f = 0; SwigType *g = 0; char *c = Char(t); if (strncmp(c, "r.", 2) == 0 || strncmp(c, "z.", 2) == 0) { /* Remove ref-qualifier */ f = SwigType_pop(t); c = Char(t); } if (strncmp(c, "q(", 2) == 0) { /* Remove cv-qualifier */ String *qual = SwigType_pop(t); if (f) { SwigType_push(qual, f); Delete(f); } f = qual; c = Char(t); } if (strncmp(c, "f(", 2)) { Printf(stderr, "Internal error. SwigType_pop_function applied to non-function type %s.\n", t); Exit(EXIT_FAILURE); } g = SwigType_pop(t); if (f) SwigType_push(g, f); Delete(f); return g; } /* ----------------------------------------------------------------------------- * SwigType_pop_function_qualifiers() * * Pop and return the function qualifiers from the input type leaving the rest of * function declaration. Returns NULL if no qualifiers. * For example: * t in: r.q(const).f().p. * t out: f().p. * result: r.q(const) * ----------------------------------------------------------------------------- */ SwigType *SwigType_pop_function_qualifiers(SwigType *t) { SwigType *qualifiers = 0; char *c = Char(t); if (strncmp(c, "r.", 2) == 0 || strncmp(c, "z.", 2) == 0) { /* Remove ref-qualifier */ String *qual = SwigType_pop(t); qualifiers = qual; c = Char(t); } if (strncmp(c, "q(", 2) == 0) { /* Remove cv-qualifier */ String *qual = SwigType_pop(t); if (qualifiers) { SwigType_push(qual, qualifiers); Delete(qualifiers); } qualifiers = qual; } assert(Strncmp(t, "f(", 2) == 0); return qualifiers; } int SwigType_isfunction(const SwigType *t) { char *c; if (!t) { return 0; } c = Char(t); if (strncmp(c, "r.", 2) == 0 || strncmp(c, "z.", 2) == 0) { /* Might be a function with a ref-qualifier, skip over */ c += 2; if (!*c) return 0; } if (strncmp(c, "q(", 2) == 0) { /* Might be a function with a cv-qualifier, skip over */ c = strchr(c, '.'); if (c) c++; else return 0; } if (strncmp(c, "f(", 2) == 0) { return 1; } return 0; } /* Create a list of parameters from the type t, using the file_line_node Node for * file and line numbering for the parameters */ ParmList *SwigType_function_parms(const SwigType *t, Node *file_line_node) { List *l = SwigType_parmlist(t); Hash *p, *pp = 0, *firstp = 0; Iterator o; for (o = First(l); o.item; o = Next(o)) { p = file_line_node ? NewParm(o.item, 0, file_line_node) : NewParmWithoutFileLineInfo(o.item, 0); if (!firstp) firstp = p; if (pp) { set_nextSibling(pp, p); Delete(p); } pp = p; } Delete(l); return firstp; } int SwigType_isvarargs(const SwigType *t) { if (Strcmp(t, "v(...)") == 0) return 1; return 0; } /* ----------------------------------------------------------------------------- * Templates * * SwigType_add_template() * * Template handling. * ----------------------------------------------------------------------------- */ /* ----------------------------------------------------------------------------- * SwigType_add_template() * * Adds a template to a type. This template is encoded in the SWIG type * mechanism and produces a string like this: * * vector ----> "vector<(p.int)>" * ----------------------------------------------------------------------------- */ SwigType *SwigType_add_template(SwigType *t, ParmList *parms) { Parm *p; Append(t, "<("); for (p = parms; p; p = nextSibling(p)) { String *v; if (Getattr(p, "default")) continue; if (p != parms) Append(t, ","); v = Getattr(p, "value"); if (v) { Append(t, v); } else { Append(t, Getattr(p, "type")); } } Append(t, ")>"); return t; } /* ----------------------------------------------------------------------------- * SwigType_templateprefix() * * Returns the prefix before the first template definition. * Returns the type unmodified if not a template. * For example: * * Foo<(p.int)>::bar => Foo * r.q(const).Foo<(p.int)>::bar => r.q(const).Foo * Foo => Foo * ----------------------------------------------------------------------------- */ String *SwigType_templateprefix(const SwigType *t) { const char *s = Char(t); const char *c = strstr(s, "<("); return c ? NewStringWithSize(s, (int)(c - s)) : NewString(s); } /* ----------------------------------------------------------------------------- * SwigType_templatesuffix() * * Returns text after a template substitution. Used to handle scope names * for example: * * Foo<(p.int)>::bar * * returns "::bar" * ----------------------------------------------------------------------------- */ String *SwigType_templatesuffix(const SwigType *t) { const char *c, *d; c = Char(t); d = c + strlen(c); while (d > c) { if ((*c == '<') && (*(c + 1) == '(')) { int nest = 1; c++; while ((d > c) && nest) { if (*c == '<' && *(c + 1) == '(') nest++; if (*c == '>' && *(c - 1) == ')') nest--; c++; } return NewString(c); } c++; } return NewStringEmpty(); } /* ----------------------------------------------------------------------------- * SwigType_istemplate_templateprefix() * * Combines SwigType_istemplate and SwigType_templateprefix efficiently into one function. * Returns the prefix before the first template definition. * Returns NULL if not a template. * For example: * * Foo<(p.int)>::bar => Foo * r.q(const).Foo<(p.int)>::bar => r.q(const).Foo * Foo => NULL * ----------------------------------------------------------------------------- */ String *SwigType_istemplate_templateprefix(const SwigType *t) { const char *s = Char(t); const char *c = strstr(s, "<("); return c ? NewStringWithSize(s, (int)(c - s)) : 0; } /* ----------------------------------------------------------------------------- * SwigType_istemplate_only_templateprefix() * * Similar to SwigType_istemplate_templateprefix() but only returns the template * prefix if the type is just the template and not a subtype/symbol within the template. * Returns NULL if not a template or is a template with a symbol within the template. * For example: * * Foo<(p.int)> => Foo * Foo<(p.int)>::bar => NULL * r.q(const).Foo<(p.int)> => r.q(const).Foo * r.q(const).Foo<(p.int)>::bar => NULL * Foo => NULL * ----------------------------------------------------------------------------- */ String *SwigType_istemplate_only_templateprefix(const SwigType *t) { int len = Len(t); const char *s = Char(t); if (len >= 4 && strcmp(s + len - 2, ")>") == 0) { const char *c = strstr(s, "<("); return c ? NewStringWithSize(s, (int)(c - s)) : 0; } else { return 0; } } /* ----------------------------------------------------------------------------- * SwigType_templateargs() * * Returns the template arguments * For example: * * Foo<(p.int)>::bar * * returns "<(p.int)>" * ----------------------------------------------------------------------------- */ String *SwigType_templateargs(const SwigType *t) { const char *c, *d; const char *start; c = Char(t); d = c + strlen(c); while (d > c) { if ((*c == '<') && (*(c + 1) == '(')) { int nest = 1; start = c; c++; while ((d > c) && nest) { if (*c == '<' && *(c + 1) == '(') nest++; if (*c == '>' && *(c - 1) == ')') nest--; c++; } return NewStringWithSize(start, (int)(c - start)); } c++; } return 0; } /* ----------------------------------------------------------------------------- * SwigType_istemplate() * * Tests a type to see if it includes template parameters * ----------------------------------------------------------------------------- */ int SwigType_istemplate(const SwigType *t) { char *ct = Char(t); ct = strstr(ct, "<("); if (ct && (strstr(ct + 2, ")>"))) return 1; return 0; } /* ----------------------------------------------------------------------------- * SwigType_base() * * This function returns the base of a type. For example, if you have a * type "p.p.int", the function would return "int". * ----------------------------------------------------------------------------- */ SwigType *SwigType_base(const SwigType *t) { char *c; char *lastop = 0; c = Char(t); lastop = c; /* Search for the last type constructor separator '.' */ while (*c) { if (*c == '.') { if (*(c + 1)) { lastop = c + 1; } c++; continue; } if (*c == '<' && *(c + 1) == '(') { /* start of template parameters --- the remainder is part of the base */ break; } if (*c == '(') { /* Skip over params */ int nparen = 1; c++; while ((*c) && (nparen > 0)) { if (*c == '(') nparen++; else if (*c == ')') nparen--; c++; } if (nparen) break; continue; } c++; } return NewString(lastop); } /* ----------------------------------------------------------------------------- * SwigType_prefix() * * Returns the prefix of a datatype. For example, the prefix of the * type "p.p.int" is "p.p.". * ----------------------------------------------------------------------------- */ String *SwigType_prefix(const SwigType *t) { char *c, *d; String *r = 0; c = Char(t); d = c + strlen(c); /* Check for a type constructor */ if ((d > c) && (*(d - 1) == '.')) d--; while (d > c) { d--; if (*d == '>' && (d > c) && *(d - 1) == ')') { /* skip over template parameters */ int nest = 1; d--; d--; while ((d > c) && (nest)) { if (*d == '>' && *(d - 1) == ')') nest++; if (*d == '<' && *(d + 1) == '(') nest--; d--; } } if (*d == ')') { /* Skip over params */ int nparen = 1; d--; while ((d > c) && (nparen)) { if (*d == ')') nparen++; if (*d == '(') nparen--; d--; } } if (*d == '.') { char x = *(d + 1); *(d + 1) = 0; r = NewString(c); *(d + 1) = x; return r; } } return NewStringEmpty(); } /* ----------------------------------------------------------------------------- * SwigType_strip_qualifiers() * * Strip all qualifiers from a type and return a new type * ----------------------------------------------------------------------------- */ SwigType *SwigType_strip_qualifiers(const SwigType *t) { static Hash *memoize_stripped = 0; SwigType *r; List *l; Iterator ei; if (!memoize_stripped) memoize_stripped = NewHash(); r = Getattr(memoize_stripped, t); if (r) return Copy(r); l = SwigType_split(t); r = NewStringEmpty(); for (ei = First(l); ei.item; ei = Next(ei)) { if (SwigType_isqualifier(ei.item)) continue; Append(r, ei.item); } Delete(l); { String *key, *value; key = Copy(t); value = Copy(r); Setattr(memoize_stripped, key, value); Delete(key); Delete(value); } return r; } /* ----------------------------------------------------------------------------- * SwigType_strip_single_qualifier() * * If the type contains a qualifier, strip one qualifier and return a new type. * The left most qualifier is stripped first (when viewed as C source code) but * this is the equivalent to the right most qualifier using SwigType notation. * Example: * r.q(const).p.q(const).int => r.q(const).p.int * r.q(const).p.int => r.p.int * r.p.int => r.p.int * ----------------------------------------------------------------------------- */ SwigType *SwigType_strip_single_qualifier(const SwigType *t) { static Hash *memoize_stripped = 0; SwigType *r = 0; List *l; int numitems; if (!memoize_stripped) memoize_stripped = NewHash(); r = Getattr(memoize_stripped, t); if (r) return Copy(r); l = SwigType_split(t); numitems = Len(l); if (numitems >= 2) { int item; /* iterate backwards from last but one item */ for (item = numitems - 2; item >= 0; --item) { String *subtype = Getitem(l, item); if (SwigType_isqualifier(subtype)) { Iterator it; Delitem(l, item); r = NewStringEmpty(); for (it = First(l); it.item; it = Next(it)) { Append(r, it.item); } break; } } } if (!r) r = Copy(t); Delete(l); { String *key, *value; key = Copy(t); value = Copy(r); Setattr(memoize_stripped, key, value); Delete(key); Delete(value); } return r; } swig-4.4.0/Source/Swig/typesys.c0000664000175000017500000020052515075443613016421 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * typesys.c * * SWIG type system management. These functions are used to manage * the C++ type system including typenames, typedef, type scopes, * inheritance, and namespaces. Generation of support code for the * run-time type checker is also handled here. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" /* ----------------------------------------------------------------------------- * Synopsis * * The purpose of this module is to manage type names and scoping issues related * to the C++ type system. The primary use is tracking typenames through typedef * and inheritance. * * New typenames are introduced by typedef, class, and enum declarations. * Each type is declared in a scope. This is either the global scope, a * class, or a namespace. For example: * * typedef int A; // Typename A, in global scope * namespace Foo { * typedef int A; // Typename A, in scope Foo:: * } * class Bar { // Typename Bar, in global scope * typedef int A; // Typename A, in scope Bar:: * } * * To manage scopes, the type system is constructed as a tree of hash tables. Each * hash table contains the following attributes: * * "name" - Scope name * "qname" - Fully qualified typename * "typetab" - Type table containing typenames and typedef information * For a given key in the typetab table, the value is a fully * qualified name if not pointing to itself. * "symtab" - Hash table of symbols defined in a scope * "inherit" - List of inherited scopes * "parent" - Parent scope * * The contents of these tables can be viewed for debugging using the -debug-typedef * option which calls SwigType_print_scope(). * * Typedef information is stored in the "typetab" hash table. For example, * if you have these declarations: * * typedef int A; * typedef A B; * typedef B *C; * * typetab in scope '' contains: * "A" : "int" * "B" : "A" * "C" : "p.B" * * To resolve a type back to its root type, one repeatedly expands on the type base. * For example: * * C *[40] ---> a(40).p.C (string type representation, see stype.c) * ---> a(40).p.p.B (C --> p.B) * ---> a(40).p.p.A (B --> A) * ---> a(40).p.p.int (A --> int) * * * Using declarations are stored in the "typetab" hash table. For example, * * namespace NN { * struct SS {}; * } * namespace N { * struct S {}; * using NN::SS; * } * using N::S; * * typetab in scope '' contains: * "S" : "N::S" * * and typetab in scope 'N' contains: * "SS" : "NN::SS" * "S" : "S" * * * For inheritance, SWIG tries to resolve types back to the base class. For instance, if * you have this: * * class Foo { * public: * typedef int Integer; * }; * struct Bar : public Foo { * void blah(Integer x); * }; * * In this case typetab in scope '' contains: * "Foo" : "Foo" * "Bar" : "Bar" * and scope 'Foo' contains: * "Integer" : "int" * and scope 'Bar' inherits from 'Foo' but is empty (observe that blah is not a scope or typedef) * * The argument type of Bar::blah will be set to Foo::Integer. * * * The scope-inheritance mechanism is used to manage C++ using directives. * * namespace XX { * class CC {}; * } * namespace X { * class C {}; * using namespace XX; * } * using namespace X; * * typetab in scope '' inherits from 'X' * typetab in scope 'X' inherits from 'XX' and contains: * "C" : "C" * typetab in scope 'XX' contains: * "CC" : "CC" * * * The scope-inheritance mechanism is used to manage C++ namespace aliases. * For example, if you have this: * * namespace Foo { * typedef int Integer; * } * * namespace F = Foo; * * In this case, F is defined as a scope that "inherits" from Foo. Internally, * F will merely be an empty scope that points to Foo. SWIG will never * place new type information into a namespace alias---attempts to do so * will generate a warning message (in the parser) and will place information into * Foo instead. * *----------------------------------------------------------------------------- */ static Typetab *current_scope = 0; /* Current type scope */ static Hash *current_typetab = 0; /* Current type table */ static Hash *current_symtab = 0; /* Current symbol table */ static Typetab *global_scope = 0; /* The global scope */ static Hash *scopes = 0; /* Hash table containing fully qualified scopes */ /* Performance optimization */ #define SWIG_TYPEDEF_RESOLVE_CACHE static Hash *typedef_resolve_cache = 0; static Hash *typedef_all_cache = 0; static Hash *typedef_qualified_cache = 0; static Typetab *SwigType_find_scope(Typetab *s, const SwigType *nameprefix); /* common attribute keys, to avoid calling find_key all the times */ /* Enable this one if your language fully support SwigValueWrapper. Leaving at '0' keeps the old swig behavior, which is not always safe, but is well known. Setting at '1' activates the new scheme, which is always safe but it requires all the typemaps to be ready for that. */ static int value_wrapper_mode = 0; int Swig_value_wrapper_mode(int mode) { value_wrapper_mode = mode; return mode; } static void flush_cache(void) { typedef_resolve_cache = 0; typedef_all_cache = 0; typedef_qualified_cache = 0; } /* Initialize the scoping system */ void SwigType_typesystem_init(void) { if (global_scope) Delete(global_scope); if (scopes) Delete(scopes); current_scope = NewHash(); global_scope = current_scope; Setattr(current_scope, "name", ""); /* No name for global scope */ current_typetab = NewHash(); Setattr(current_scope, "typetab", current_typetab); current_symtab = 0; scopes = NewHash(); Setattr(scopes, "", current_scope); } /* ----------------------------------------------------------------------------- * SwigType_typedef() * * Defines a new typedef in the current scope. Returns -1 if the type name is * already defined. * ----------------------------------------------------------------------------- */ int SwigType_typedef(const SwigType *type, const_String_or_char_ptr name) { /* Printf(stdout, "typedef %s %s\n", type, name); */ if (Getattr(current_typetab, name)) return -1; /* Already defined */ if (Strcmp(type, name) == 0) { /* Can't typedef a name to itself */ return 0; } /* Check if 'type' is already a scope. If so, we create an alias in the type system for it. This is needed to make strange nested scoping problems work correctly. */ { Typetab *t = SwigType_find_scope(current_scope, type); if (t) { SwigType_new_scope(name); SwigType_inherit_scope(t); SwigType_pop_scope(); } } Setattr(current_typetab, name, type); flush_cache(); return 0; } /* ----------------------------------------------------------------------------- * SwigType_typedef_class() * * Defines a class in the current scope. * ----------------------------------------------------------------------------- */ int SwigType_typedef_class(const_String_or_char_ptr name) { String *cname; /* Printf(stdout,"class : '%s'\n", name); */ if (Getattr(current_typetab, name)) return -1; /* Already defined */ cname = NewString(name); Setmeta(cname, "class", "1"); Setattr(current_typetab, cname, cname); Delete(cname); flush_cache(); return 0; } /* ----------------------------------------------------------------------------- * SwigType_scope_name() * * Returns the qualified scope name of a type table * ----------------------------------------------------------------------------- */ static String *SwigType_scope_name(Typetab *ttab) { String *qname = NewString(Getattr(ttab, "name")); ttab = Getattr(ttab, "parent"); while (ttab) { String *pname = Getattr(ttab, "name"); if (Len(pname)) { Insert(qname, 0, "::"); Insert(qname, 0, pname); } ttab = Getattr(ttab, "parent"); } return qname; } /* ----------------------------------------------------------------------------- * SwigType_new_scope() * * Creates a new scope * ----------------------------------------------------------------------------- */ void SwigType_new_scope(const_String_or_char_ptr name) { Typetab *s; Hash *ttab; String *qname; if (!name) { name = ""; } s = NewHash(); Setattr(s, "name", name); Setattr(s, "parent", current_scope); ttab = NewHash(); Setattr(s, "typetab", ttab); /* Build fully qualified name */ qname = SwigType_scope_name(s); #if 1 { /* TODO: only do with templates? What happens with non-templates with code below? */ String *stripped_qname; stripped_qname = SwigType_remove_global_scope_prefix(qname); /* Use fully qualified name for hash key without unary scope prefix, qname may contain unary scope */ Setattr(scopes, stripped_qname, s); Setattr(s, "qname", qname); /* Printf(stdout, "SwigType_new_scope stripped %s %s\n", qname, stripped_qname); */ Delete(stripped_qname); } #else Printf(stdout, "SwigType_new_scope %s\n", qname); Setattr(scopes, qname, s); Setattr(s, "qname", qname); #endif Delete(qname); current_scope = s; current_typetab = ttab; current_symtab = 0; flush_cache(); } /* ----------------------------------------------------------------------------- * SwigType_inherit_scope() * * Makes the current scope inherit from another scope. This is used for both * C++ class inheritance, namespaces, and namespace aliases. * ----------------------------------------------------------------------------- */ void SwigType_inherit_scope(Typetab *scope) { List *inherits; int i, len; inherits = Getattr(current_scope, "inherit"); if (!inherits) { inherits = NewList(); Setattr(current_scope, "inherit", inherits); Delete(inherits); } assert(scope != current_scope); len = Len(inherits); for (i = 0; i < len; i++) { Node *n = Getitem(inherits, i); if (n == scope) return; } Append(inherits, scope); } /* ----------------------------------------------------------------------------- * SwigType_scope_alias() * * Creates a scope-alias. * ----------------------------------------------------------------------------- */ void SwigType_scope_alias(String *aliasname, Typetab *ttab) { String *q; /* Printf(stdout,"alias: '%s' '%p'\n", aliasname, ttab); */ q = SwigType_scope_name(current_scope); if (Len(q)) { Append(q, "::"); } Append(q, aliasname); Setattr(scopes, q, ttab); flush_cache(); } /* ----------------------------------------------------------------------------- * SwigType_using_scope() * * Import another scope into this scope. * ----------------------------------------------------------------------------- */ void SwigType_using_scope(Typetab *scope) { SwigType_inherit_scope(scope); { List *ulist; int i, len; ulist = Getattr(current_scope, "using"); if (!ulist) { ulist = NewList(); Setattr(current_scope, "using", ulist); Delete(ulist); } assert(scope != current_scope); len = Len(ulist); for (i = 0; i < len; i++) { Typetab *n = Getitem(ulist, i); if (n == scope) return; } Append(ulist, scope); } flush_cache(); } /* ----------------------------------------------------------------------------- * SwigType_pop_scope() * * Pop off the last scope and perform a merge operation. Returns the hash * table for the scope that was popped off. * ----------------------------------------------------------------------------- */ Typetab *SwigType_pop_scope(void) { Typetab *t, *old = current_scope; t = Getattr(current_scope, "parent"); if (!t) t = global_scope; current_scope = t; current_typetab = Getattr(t, "typetab"); current_symtab = Getattr(t, "symtab"); flush_cache(); return old; } /* ----------------------------------------------------------------------------- * SwigType_set_scope() * * Set the scope. Returns the old scope. * ----------------------------------------------------------------------------- */ Typetab *SwigType_set_scope(Typetab *t) { Typetab *old = current_scope; if (!t) t = global_scope; current_scope = t; current_typetab = Getattr(t, "typetab"); current_symtab = Getattr(t, "symtab"); flush_cache(); return old; } /* ----------------------------------------------------------------------------- * SwigType_attach_symtab() * * Attaches a symbol table to a type scope * ----------------------------------------------------------------------------- */ void SwigType_attach_symtab(Symtab *sym) { Setattr(current_scope, "symtab", sym); current_symtab = sym; } /* ----------------------------------------------------------------------------- * SwigType_print_scope() * * Debugging function for printing out current scope * ----------------------------------------------------------------------------- */ void SwigType_print_scope(void) { Hash *ttab; Iterator i, j; Printf(stdout, "SCOPES start =======================================\n"); for (i = First(scopes); i.key; i = Next(i)) { Printf(stdout, "-------------------------------------------------------------\n"); ttab = Getattr(i.item, "typetab"); Printf(stdout, "Type scope '%s' (%p)\n", i.key, i.item); { List *inherit = Getattr(i.item, "inherit"); if (inherit) { Iterator j; for (j = First(inherit); j.item; j = Next(j)) { Printf(stdout, " Inherits from '%s' (%p)\n", Getattr(j.item, "qname"), j.item); } } } for (j = First(ttab); j.key; j = Next(j)) { Printf(stdout, "%40s -> %s\n", j.key, j.item); } } Printf(stdout, "SCOPES finish =======================================\n"); } static Typetab *SwigType_find_scope(Typetab *s, const SwigType *nameprefix) { Typetab *ss; Typetab *s_orig = s; String *nnameprefix = 0; static int check_parent = 1; int is_template = 0; if (Getmark(s)) return 0; Setmark(s, 1); is_template = SwigType_istemplate(nameprefix); if (is_template) { nnameprefix = SwigType_typedef_resolve_all(nameprefix); nameprefix = nnameprefix; } ss = s; while (ss) { String *full; String *qname = Getattr(ss, "qname"); if (qname) { full = NewStringf("%s::%s", qname, nameprefix); if (Strncmp(full, "enum ", 5) == 0) { Delslice(full, 0, 5); } } else { full = NewString(nameprefix); } s = Getattr(scopes, full); if (!s && is_template) { /* try look up scope with all the unary scope operators within the template parameter list removed */ SwigType *full_stripped = SwigType_remove_global_scope_prefix(full); s = Getattr(scopes, full_stripped); Delete(full_stripped); } Delete(full); if (s) { if (nnameprefix) Delete(nnameprefix); Setmark(s_orig, 0); return s; } if (!s) { /* Check inheritance */ List *inherit; inherit = Getattr(ss, "using"); if (inherit) { Typetab *ttab; int i, len; len = Len(inherit); for (i = 0; i < len; i++) { int oldcp = check_parent; ttab = Getitem(inherit, i); check_parent = 0; s = SwigType_find_scope(ttab, nameprefix); check_parent = oldcp; if (s) { if (nnameprefix) Delete(nnameprefix); Setmark(s_orig, 0); return s; } } } } if (!check_parent) break; ss = Getattr(ss, "parent"); } if (nnameprefix) Delete(nnameprefix); Setmark(s_orig, 0); return 0; } /* ----------------------------------------------------------------------------- * typedef_resolve() * * Resolves a typedef and returns a new type string. Returns 0 if there is no * typedef mapping. base is a name without qualification. * Internal function. * ----------------------------------------------------------------------------- */ static Typetab *resolved_scope = 0; /* Internal function */ static SwigType *_typedef_resolve(Typetab *s, String *base, int look_parent) { Hash *ttab; SwigType *type = 0; List *inherit; Typetab *parent; /* if (!s) return 0; *//* now is checked below */ /* Printf(stdout,"Typetab %s : %s\n", Getattr(s,"name"), base); */ if (!Getmark(s)) { Setmark(s, 1); ttab = Getattr(s, "typetab"); type = Getattr(ttab, base); if (type) { resolved_scope = s; Setmark(s, 0); } else { /* Hmmm. Not found in my scope. It could be in an inherited scope */ inherit = Getattr(s, "inherit"); if (inherit) { int i, len; len = Len(inherit); for (i = 0; i < len; i++) { type = _typedef_resolve(Getitem(inherit, i), base, 0); if (type) { Setmark(s, 0); break; } } } if (!type) { /* Hmmm. Not found in my scope. check parent */ if (look_parent) { parent = Getattr(s, "parent"); type = parent ? _typedef_resolve(parent, base, 1) : 0; } } Setmark(s, 0); } } return type; } /* ----------------------------------------------------------------------------- * template_parameters_resolve() * * For use with templates only. Attempts to resolve one template parameter. * * If one of the template parameters can be resolved, the type is returned with * just the one parameter resolved and the remaining parameters left as is. * If none of the template parameters can be resolved, zero is returned. * ----------------------------------------------------------------------------- */ static String *template_parameters_resolve(const String *base) { List *tparms; String *suffix; String *type; int i, sz; int rep = 0; type = SwigType_templateprefix(base); suffix = SwigType_templatesuffix(base); Append(type, "<("); tparms = SwigType_parmlist(base); sz = Len(tparms); for (i = 0; i < sz; i++) { SwigType *tpr; SwigType *tp = Getitem(tparms, i); if (!rep) { tpr = SwigType_typedef_resolve(tp); } else { tpr = 0; } if (tpr) { Append(type, tpr); Delete(tpr); rep = 1; } else { Append(type, tp); } if ((i + 1) < sz) Append(type, ","); } if (rep) { Append(type, ")>"); Append(type, suffix); } else { Delete(type); type = 0; } Delete(suffix); Delete(tparms); return type; } static SwigType *typedef_resolve(Typetab *s, String *base) { return _typedef_resolve(s, base, 1); } /* ----------------------------------------------------------------------------- * SwigType_typedef_resolve() * * Given a type declaration, this function looks to reduce/resolve the type via a * typedef (including via C++ using declarations). * * If it is able to find a typedef, the resolved type is returned. If no typedef * is found NULL is returned. The type name is resolved in the current scope. * The type returned is not always fully qualified for the global scope, it is * valid for use in the current scope. If the current scope is global scope, a * fully qualified type should be returned. * * Some additional notes are in Doc/Manual/Extending.html. * ----------------------------------------------------------------------------- */ /* #define SWIG_DEBUG */ SwigType *SwigType_typedef_resolve(const SwigType *t) { String *base; String *type = 0; String *r = 0; Typetab *s; Hash *ttab; String *namebase = 0; String *nameprefix = 0, *rnameprefix = 0; int newtype = 0; resolved_scope = 0; #ifdef SWIG_TYPEDEF_RESOLVE_CACHE if (!typedef_resolve_cache) { typedef_resolve_cache = NewHash(); } r = Getattr(typedef_resolve_cache, t); if (r) { resolved_scope = Getmeta(r, "scope"); return Copy(r); } #endif base = SwigType_base(t); #ifdef SWIG_DEBUG Printf(stdout, "base = '%s' t='%s'\n", base, t); #endif if (SwigType_issimple(base)) { s = current_scope; ttab = current_typetab; if (strncmp(Char(base), "::", 2) == 0) { s = global_scope; ttab = Getattr(s, "typetab"); Delitem(base, 0); Delitem(base, 0); } /* Do a quick check in the local scope */ type = Getattr(ttab, base); if (type) { resolved_scope = s; } if (!type) { /* Didn't find in this scope. We need to do a little more searching */ if (Swig_scopename_check(base)) { /* A qualified name. */ Swig_scopename_split(base, &nameprefix, &namebase); #ifdef SWIG_DEBUG Printf(stdout, "nameprefix = '%s'\n", nameprefix); #endif if (nameprefix) { rnameprefix = SwigType_typedef_resolve(nameprefix); if(rnameprefix != NULL) { #ifdef SWIG_DEBUG Printf(stdout, "nameprefix '%s' is a typedef to '%s'\n", nameprefix, rnameprefix); #endif type = Copy(namebase); Insert(type, 0, "::"); Insert(type, 0, rnameprefix); if (Strncmp(type, "enum ", 5) == 0) { Delslice(type, 0, 5); } if (Strncmp(type, "::", 2) == 0) { Delslice(type, 0, 2); } newtype = 1; } else { /* Name had a prefix on it. See if we can locate the proper scope for it */ String *rnameprefix = template_parameters_resolve(nameprefix); nameprefix = rnameprefix ? Copy(rnameprefix) : nameprefix; Delete(rnameprefix); s = SwigType_find_scope(s, nameprefix); /* Couldn't locate a scope for the type. */ if (!s) { Delete(base); Delete(namebase); Delete(nameprefix); r = 0; goto return_result; } /* Try to locate the name starting in the scope */ #ifdef SWIG_DEBUG Printf(stdout, "namebase = '%s'\n", namebase); #endif type = typedef_resolve(s, namebase); if (type && resolved_scope) { /* we need to look for the resolved type, this will also fix the resolved_scope if 'type' and 'namebase' are declared in different scopes */ String *rtype = 0; rtype = typedef_resolve(resolved_scope, type); if (rtype) type = rtype; } #ifdef SWIG_DEBUG Printf(stdout, "%s type = '%s'\n", Getattr(s, "name"), type); #endif if ((type) && (!Swig_scopename_check(type)) && resolved_scope) { Typetab *rtab = resolved_scope; String *qname = Getattr(resolved_scope, "qname"); /* If qualified *and* the typename is defined from the resolved scope, we qualify */ if ((qname) && typedef_resolve(resolved_scope, type)) { type = Copy(type); Insert(type, 0, "::"); Insert(type, 0, qname); #ifdef SWIG_DEBUG Printf(stdout, "qual %s \n", type); #endif newtype = 1; } resolved_scope = rtab; } } } else { /* Name is unqualified. */ type = typedef_resolve(s, base); } } else { /* Name is unqualified. */ type = typedef_resolve(s, base); } } if (!type && SwigType_istemplate(base)) { String *tprefix = SwigType_templateprefix(base); String *rtprefix = SwigType_typedef_resolve(tprefix); /* We're looking for a using declaration on the template prefix to resolve the template prefix * in another scope. Using declaration do not have template parameters. */ if (rtprefix && !SwigType_istemplate(rtprefix)) { String *tsuffix = SwigType_templatesuffix(base); String *targs = SwigType_templateargs(base); type = NewString(rtprefix); newtype = 1; Append(type, targs); Append(type, tsuffix); Delete(targs); Delete(tsuffix); Delete(rtprefix); } Delete(tprefix); } if (type && (Equal(base, type))) { if (newtype) Delete(type); Delete(base); Delete(namebase); Delete(nameprefix); r = 0; goto return_result; } /* If the type is a template, and no typedef was found, we need to check the template arguments one by one to see if they can be resolved. */ if (!type && SwigType_istemplate(base)) { newtype = 1; type = template_parameters_resolve(base); } Delete(namebase); Delete(nameprefix); } else { if (SwigType_isfunction(base)) { List *parms; int i, sz; int rep = 0; type = NewString("f("); newtype = 1; parms = SwigType_parmlist(base); sz = Len(parms); for (i = 0; i < sz; i++) { SwigType *tpr; SwigType *tp = Getitem(parms, i); if (!rep) { tpr = SwigType_typedef_resolve(tp); } else { tpr = 0; } if (tpr) { Append(type, tpr); Delete(tpr); rep = 1; } else { Append(type, tp); } if ((i + 1) < sz) Append(type, ","); } Append(type, ")."); Delete(parms); if (!rep) { Delete(type); type = 0; } } else if (SwigType_ismemberpointer(base)) { String *rt; String *mtype = SwigType_parm(base); rt = SwigType_typedef_resolve(mtype); if (rt) { type = NewStringf("m(%s).", rt); newtype = 1; Delete(rt); } Delete(mtype); } else { type = 0; } } r = SwigType_prefix(t); if (!type) { if (r && Len(r)) { char *cr = Char(r); if ((strstr(cr, "f(") || (strstr(cr, "m(")))) { SwigType *rt = SwigType_typedef_resolve(r); if (rt) { Delete(r); Append(rt, base); Delete(base); r = rt; goto return_result; } } } Delete(r); Delete(base); r = 0; goto return_result; } Delete(base); /* If 'type' is an array, then the right-most qualifier in 'r' should be added to 'type' after the array qualifier, so that given a(7).q(volatile).double myarray // typedef volatile double[7] myarray; the type q(const).myarray // const myarray becomes a(7).q(const volatile).double // const volatile double[7] and NOT q(const).a(7).q(volatile).double // non-sensical type */ if (r && Len(r) && SwigType_isarray(type)) { List *r_elem; String *r_qual; int r_sz; r_elem = SwigType_split(r); r_sz = Len(r_elem); r_qual = Getitem(r_elem, r_sz-1); if (SwigType_isqualifier(r_qual)) { String *new_r; String *new_type; List *type_elem; String *type_qual; String *r_qual_arg; int i, type_sz; type_elem = SwigType_split(type); type_sz = Len(type_elem); for (i = 0; i < type_sz; ++i) { String *e = Getitem(type_elem, i); if (!SwigType_isarray(e)) break; } type_qual = Copy(Getitem(type_elem, i)); r_qual_arg = SwigType_parm(r_qual); SwigType_add_qualifier(type_qual, r_qual_arg); Delete(r_qual_arg); Setitem(type_elem, i, type_qual); new_r = NewStringEmpty(); for (i = 0; i < r_sz-1; ++i) { Append(new_r, Getitem(r_elem, i)); } new_type = NewStringEmpty(); for (i = 0; i < type_sz; ++i) { Append(new_type, Getitem(type_elem, i)); } #ifdef SWIG_DEBUG Printf(stdout, "r+type='%s%s' new_r+new_type='%s%s'\n", r, type, new_r, new_type); #endif Delete(r); r = new_r; newtype = 1; type = new_type; Delete(type_elem); } Delete(r_elem); } Append(r, type); if (newtype) { Delete(type); } return_result: #ifdef SWIG_TYPEDEF_RESOLVE_CACHE { String *key = NewString(t); if (r) { SwigType *r1; Setattr(typedef_resolve_cache, key, r); Setmeta(r, "scope", resolved_scope); r1 = Copy(r); Delete(r); r = r1; } Delete(key); } #endif return r; } /* ----------------------------------------------------------------------------- * SwigType_typedef_resolve_all() * * Fully resolve a type down to its most basic datatype * ----------------------------------------------------------------------------- */ SwigType *SwigType_typedef_resolve_all(const SwigType *t) { SwigType *n; SwigType *r; int count = 0; /* Check to see if the typedef resolve has been done before by checking the cache */ if (!typedef_all_cache) { typedef_all_cache = NewHash(); } r = Getattr(typedef_all_cache, t); if (r) { return Copy(r); } #ifdef SWIG_DEBUG Printf(stdout, "SwigType_typedef_resolve_all start ... %s\n", t); #endif /* Recursively resolve the typedef */ r = NewString(t); while ((n = SwigType_typedef_resolve(r))) { Delete(r); r = n; if (++count >= 512) { Swig_error(Getfile(t), Getline(t), "Recursive typedef detected resolving '%s' to '%s' to '%s' and so on...\n", SwigType_str(t, 0), SwigType_str(SwigType_typedef_resolve(t), 0), SwigType_str(SwigType_typedef_resolve(SwigType_typedef_resolve(t)), 0)); break; } } /* Add the typedef to the cache for next time it is looked up */ { String *key; SwigType *rr = Copy(r); key = NewString(t); Setattr(typedef_all_cache, key, rr); Delete(key); Delete(rr); } #ifdef SWIG_DEBUG Printf(stdout, "SwigType_typedef_resolve_all end === %s => %s\n", t, r); #endif return r; } /* ----------------------------------------------------------------------------- * SwigType_typedef_qualified() * * Given a type declaration, this function tries to fully qualify it so that the * resulting type can be used in the global scope. The type name is resolved in * the current scope. * * It provides a fully qualified name, not necessarily a fully expanded name. * When a using declaration or using directive is found the type may not be fully * expanded, but it will be resolved and fully qualified for use in the global scope. * * This function is for looking up scopes to qualify a type. It does not resolve * C typedefs, it just qualifies them. See SwigType_typedef_resolve for resolving. * * If the unary scope operator (::) is used as a prefix to the type to denote global * scope, it is left in place. * ----------------------------------------------------------------------------- */ SwigType *SwigType_typedef_qualified(const SwigType *t) { List *elements; String *result; int i, len; if (!typedef_qualified_cache) typedef_qualified_cache = NewHash(); result = Getattr(typedef_qualified_cache, t); if (result) { String *rc = Copy(result); return rc; } result = NewStringEmpty(); elements = SwigType_split(t); len = Len(elements); for (i = 0; i < len; i++) { String *ty = 0; String *e = Getitem(elements, i); if (SwigType_issimple(e)) { if (!SwigType_istemplate(e)) { String *isenum = 0; if (SwigType_isenum(e)) { isenum = NewString("enum "); ty = NewString(Char(e) + 5); e = ty; } resolved_scope = 0; if (typedef_resolve(current_scope, e) && resolved_scope) { /* resolved_scope contains the scope that actually resolved the symbol */ String *qname = Getattr(resolved_scope, "qname"); if (qname) { Insert(e, 0, "::"); Insert(e, 0, qname); } } else { if (Swig_scopename_check(e)) { String *qlast; String *qname; Swig_scopename_split(e, &qname, &qlast); if (qname) { String *tqname = SwigType_typedef_qualified(qname); Clear(e); Printf(e, "%s::%s", tqname, qlast); Delete(qname); Delete(tqname); } Delete(qlast); /* Automatic template instantiation might go here??? */ } else { /* It's a bare name. It's entirely possible, that the name is part of a namespace. We'll check this by unrolling out of the current scope */ Typetab *cs = current_scope; if (cs) { Typetab *found_scope = SwigType_find_scope(cs, e); if (found_scope) { String *qs = SwigType_scope_name(found_scope); Clear(e); Append(e, qs); Delete(qs); } } } } if (isenum) { Insert(e, 0, isenum); Delete(isenum); } } else { /* Template. We need to qualify template parameters as well as the template itself */ String *tprefix, *qprefix; String *tsuffix; Iterator pi; Parm *p; List *parms; ty = Swig_symbol_template_deftype(e, current_symtab); e = ty; parms = SwigType_parmlist(e); tprefix = SwigType_templateprefix(e); tsuffix = SwigType_templatesuffix(e); qprefix = SwigType_typedef_qualified(tprefix); Append(qprefix, "<("); pi = First(parms); while ((p = pi.item)) { /* TODO: the logic here should be synchronised with that in symbol_template_qualify() in symbol.c */ String *qt = SwigType_typedef_qualified(p); if (Equal(qt, p)) { /* && (!Swig_scopename_check(qt))) */ /* No change in value. It is entirely possible that the parameter is an integer value. If there is a symbol table associated with this scope, we're going to check for this */ if (current_symtab) { Node *lastnode = 0; String *value = Copy(p); while (1) { Node *n = Swig_symbol_clookup(value, current_symtab); if (n == lastnode) break; lastnode = n; if (n) { char *ntype = Char(nodeType(n)); if (strcmp(ntype, "enumitem") == 0) { /* An enum item. Generate a fully qualified name */ String *qn = Swig_symbol_qualified(n); if (Len(qn)) { Append(qn, "::"); Append(qn, Getattr(n, "name")); Delete(value); value = qn; continue; } else { Delete(qn); break; } } else if ((strcmp(ntype, "cdecl") == 0) && (Getattr(n, "value"))) { Delete(value); value = Copy(Getattr(n, "value")); continue; } } break; } Append(qprefix, value); Delete(value); } else { Append(qprefix, p); } } else { Append(qprefix, qt); } Delete(qt); pi = Next(pi); if (pi.item) { Append(qprefix, ","); } } Append(qprefix, ")>"); Append(qprefix, tsuffix); Delete(tsuffix); Clear(e); Append(e, qprefix); Delete(tprefix); Delete(qprefix); Delete(parms); } Append(result, e); Delete(ty); } else if (SwigType_isfunction(e)) { List *parms = SwigType_parmlist(e); String *s = NewString("f("); Iterator pi; pi = First(parms); while (pi.item) { String *pq = SwigType_typedef_qualified(pi.item); Append(s, pq); Delete(pq); pi = Next(pi); if (pi.item) { Append(s, ","); } } Append(s, ")."); Append(result, s); Delete(s); Delete(parms); } else if (SwigType_isarray(e)) { String *ndim; String *dim = SwigType_parm(e); ndim = Swig_symbol_string_qualify(dim, 0); Printf(result, "a(%s).", ndim); Delete(dim); Delete(ndim); } else { Append(result, e); } } Delete(elements); { String *key, *cresult; key = NewString(t); cresult = NewString(result); Setattr(typedef_qualified_cache, key, cresult); Delete(key); Delete(cresult); } return result; } /* ----------------------------------------------------------------------------- * SwigType_istypedef() * * Checks a typename to see if it is a typedef. * ----------------------------------------------------------------------------- */ int SwigType_istypedef(const SwigType *t) { String *type; type = SwigType_typedef_resolve(t); if (type) { Delete(type); return 1; } else { return 0; } } /* ----------------------------------------------------------------------------- * SwigType_typedef_using() * * Processes a 'using' declaration to import types from one scope into another. * Name is a qualified name like A::B. * ----------------------------------------------------------------------------- */ int SwigType_typedef_using(const_String_or_char_ptr name) { String *base; String *td; String *prefix; Typetab *s; Typetab *tt = 0; String *defined_name = 0; /* Printf(stdout, "using %s\n", name); */ if (!Swig_scopename_check(name)) return -1; /* Not properly qualified */ base = Swig_scopename_last(name); /* See if the base is already defined in this scope */ if (Getattr(current_typetab, base)) { Delete(base); return -1; } /* See if the using name is a scope */ /* tt = SwigType_find_scope(current_scope,name); Printf(stdout,"tt = %p, name = '%s'\n", tt, name); */ /* We set up a typedef B --> A::B */ Setattr(current_typetab, base, name); /* Find the scope name where the symbol is defined */ td = SwigType_typedef_resolve(name); /* Printf(stdout,"td = '%s' %p\n", td, resolved_scope); */ if (resolved_scope) { defined_name = Getattr(resolved_scope, "qname"); if (defined_name) { defined_name = Copy(defined_name); Append(defined_name, "::"); Append(defined_name, base); /* Printf(stdout,"defined_name = '%s'\n", defined_name); */ tt = SwigType_find_scope(current_scope, defined_name); } } if (td) Delete(td); /* Figure out the scope the using directive refers to */ { prefix = Swig_scopename_prefix(name); if (prefix) { s = SwigType_find_scope(current_scope, prefix); if (s) { Hash *ttab = Getattr(s, "typetab"); if (!Getattr(ttab, base) && defined_name) { Setattr(ttab, base, defined_name); } } } } if (tt) { /* Using directive had its own scope. We need to create a new scope for it */ SwigType_new_scope(base); SwigType_inherit_scope(tt); SwigType_pop_scope(); } if (defined_name) Delete(defined_name); Delete(prefix); Delete(base); return 0; } /* ----------------------------------------------------------------------------- * SwigType_isclass() * * Determines if a type defines a class or not. A class is defined by * its type-table entry maps to itself. Note: a pointer to a class is not * a class. * ----------------------------------------------------------------------------- */ int SwigType_isclass(const SwigType *t) { SwigType *qty, *qtys; int isclass = 0; qty = SwigType_typedef_resolve_all(t); qtys = SwigType_strip_qualifiers(qty); if (SwigType_issimple(qtys)) { String *td = SwigType_typedef_resolve(qtys); if (td) { Delete(td); } if (resolved_scope) { isclass = 1; } /* Hmmm. Not a class. If a template, it might be uninstantiated */ if (!isclass) { String *tp = SwigType_istemplate_templateprefix(qtys); if (tp && Strcmp(tp, t) != 0) { isclass = SwigType_isclass(tp); } Delete(tp); } } Delete(qty); Delete(qtys); return isclass; } /* ----------------------------------------------------------------------------- * SwigType_type() * * Returns an integer code describing the datatype. This is only used for * compatibility with SWIG1.1 language modules and is likely to go away once * everything is based on typemaps. * ----------------------------------------------------------------------------- */ int SwigType_type(const SwigType *t) { char *c; /* Check for the obvious stuff */ c = Char(t); if (strncmp(c, "p.", 2) == 0) { if (SwigType_type(c + 2) == T_CHAR) return T_STRING; else if (SwigType_type(c + 2) == T_WCHAR) return T_WSTRING; else return T_POINTER; } if (strncmp(c, "a(", 2) == 0) return T_ARRAY; if (strncmp(c, "r.", 2) == 0) return T_REFERENCE; if (strncmp(c, "z.", 2) == 0) return T_RVALUE_REFERENCE; if (strncmp(c, "m(", 2) == 0) return T_MPOINTER; if (strncmp(c, "q(", 2) == 0) { while (*c && (*c != '.')) c++; if (*c) return SwigType_type(c + 1); Printf(stderr, "*** Internal error: Invalid type string '%s'\n", t); Exit(EXIT_FAILURE); } if (strncmp(c, "f(", 2) == 0) return T_FUNCTION; /* Look for basic types */ if (strcmp(c, "int") == 0) return T_INT; if (strcmp(c, "long") == 0) return T_LONG; if (strcmp(c, "short") == 0) return T_SHORT; if (strcmp(c, "unsigned") == 0) return T_UINT; if (strcmp(c, "unsigned short") == 0) return T_USHORT; if (strcmp(c, "unsigned long") == 0) return T_ULONG; if (strcmp(c, "unsigned int") == 0) return T_UINT; if (strcmp(c, "char") == 0) return T_CHAR; if (strcmp(c, "signed char") == 0) return T_SCHAR; if (strcmp(c, "unsigned char") == 0) return T_UCHAR; if (strcmp(c, "wchar_t") == 0) return T_WCHAR; if (strcmp(c, "float") == 0) return T_FLOAT; if (strcmp(c, "double") == 0) return T_DOUBLE; if (strcmp(c, "long double") == 0) return T_LONGDOUBLE; if (!cparse_cplusplus && (strcmp(c, "float _Complex") == 0)) return T_FLTCPLX; if (!cparse_cplusplus && (strcmp(c, "double _Complex") == 0)) return T_DBLCPLX; if (!cparse_cplusplus && (strcmp(c, "_Complex") == 0)) return T_COMPLEX; if (strcmp(c, "void") == 0) return T_VOID; if (strcmp(c, "bool") == 0) return T_BOOL; if (strcmp(c, "long long") == 0) return T_LONGLONG; if (strcmp(c, "unsigned long long") == 0) return T_ULONGLONG; if (strncmp(c, "enum ", 5) == 0) return T_INT; if (strcmp(c, "auto") == 0) return T_AUTO; if (strcmp(c, "v(...)") == 0) return T_VARARGS; /* Hmmm. Unknown type */ if (SwigType_istypedef(t)) { int r; SwigType *nt = SwigType_typedef_resolve(t); r = SwigType_type(nt); Delete(nt); return r; } return T_USER; } /* ----------------------------------------------------------------------------- * SwigType_alttype() * * Returns the alternative value type needed in C++ for class value * types. When swig is not sure about using a plain $ltype value, * since the class doesn't have a default constructor, or it can't be * assigned, you will get back 'SwigValueWrapper<(type)>'. * * This is the default behavior unless: * * 1.- swig detects a default_constructor and 'allocate:default_constructor' * attribute. * * 2.- swig doesn't mark 'type' as non-assignable. * * 3.- the user specifies that the value wrapper is not needed by using * %feature("novaluewrapper") like so: * * %feature("novaluewrapper") MyOpaqueClass; * class MyOpaqueClass; * * The user can also force the use of the value wrapper with * %feature("valuewrapper"). * ----------------------------------------------------------------------------- */ SwigType *SwigType_alttype(const SwigType *t, int local_tmap) { Node *n; SwigType *w = 0; int use_wrapper = 0; SwigType *td = 0; if (!cparse_cplusplus) return 0; if (value_wrapper_mode == 0) { /* old partial use of SwigValueTypes, it can fail for opaque types */ if (local_tmap) return 0; if (SwigType_isclass(t)) { SwigType *ftd = SwigType_typedef_resolve_all(t); td = SwigType_strip_qualifiers(ftd); Delete(ftd); n = Swig_symbol_clookup(td, 0); if (n) { if (GetFlag(n, "feature:valuewrapper")) { use_wrapper = 1; } else { if (Checkattr(n, "nodeType", "class") && (!Getattr(n, "allocate:default_constructor") || (Getattr(n, "allocate:noassign")))) { use_wrapper = !GetFlag(n, "feature:novaluewrapper") || GetFlag(n, "feature:nodefault"); } } } else { if (SwigType_issimple(td) && SwigType_istemplate(td)) { use_wrapper = 1; } } } } else { /* safe use of SwigValueTypes, it can fail with some typemaps */ SwigType *ftd = SwigType_typedef_resolve_all(t); td = SwigType_strip_qualifiers(ftd); Delete(ftd); if (SwigType_type(td) == T_USER) { use_wrapper = 1; n = Swig_symbol_clookup(td, 0); if (n) { if ((Checkattr(n, "nodeType", "class") && !Getattr(n, "allocate:noassign") && (Getattr(n, "allocate:default_constructor"))) || (GetFlag(n, "feature:novaluewrapper"))) { use_wrapper = GetFlag(n, "feature:valuewrapper"); } } } } if (use_wrapper) { w = NewStringf("SwigValueWrapper<(%s)>", td); } Delete(td); return w; } /* ---------------------------------------------------------------------------- * * * * WARNING * * * *** * *** * Don't even think about modifying anything below this line unless you *** * are completely on top of *EVERY* subtle aspect of the C++ type system *** * and you are prepared to suffer endless hours of agony trying to *** * debug the SWIG run-time type checker after you break it. *** * ------------------------------------------------------------------------- */ /* ----------------------------------------------------------------------------- * SwigType_remember() * * This function "remembers" a datatype that was used during wrapper code generation * so that a type-checking table can be generated later on. It is up to the language * modules to actually call this function--it is not done automatically. * * Type tracking is managed through two separate hash tables. The hash 'r_mangled' * is mapping between mangled type names (used in the target language) and * fully-resolved C datatypes used in the source input. The second hash 'r_resolved' * is the inverse mapping that maps fully-resolved C datatypes to all of the mangled * names in the scripting languages. For example, consider the following set of * typedef declarations: * * typedef double Real; * typedef double Float; * typedef double Point[3]; * * Now, suppose that the types 'double *', 'Real *', 'Float *', 'double[3]', and * 'Point' were used in an interface file and "remembered" using this function. * The hash tables would look like this: * * r_mangled { * _p_double : [ p.double, a(3).double ] * _p_Real : [ p.double ] * _p_Float : [ p.double ] * _Point : [ a(3).double ] * * r_resolved { * p.double : [ _p_double, _p_Real, _p_Float ] * a(3).double : [ _p_double, _Point ] * } * * Together these two hash tables can be used to determine type-equivalency between * mangled typenames. To do this, we view the two hash tables as a large graph and * compute the transitive closure. * ----------------------------------------------------------------------------- */ static Hash *r_mangled = 0; /* Hash mapping mangled types to fully resolved types */ static Hash *r_resolved = 0; /* Hash mapping resolved types to mangled types */ static Hash *r_ltype = 0; /* Hash mapping mangled names to their local c type */ static Hash *r_clientdata = 0; /* Hash mapping resolved types to client data */ static Hash *r_mangleddata = 0; /* Hash mapping mangled types to client data */ static Hash *r_remembered = 0; /* Hash of types we remembered already */ static void (*r_tracefunc) (const SwigType *t, String *mangled, String *clientdata) = 0; void SwigType_remember_mangleddata(String *mangled, const_String_or_char_ptr clientdata) { if (!r_mangleddata) { r_mangleddata = NewHash(); } Setattr(r_mangleddata, mangled, clientdata); } void SwigType_remember_clientdata(const SwigType *t, const_String_or_char_ptr clientdata) { String *mt; SwigType *lt; Hash *h; SwigType *fr; SwigType *qr; String *tkey; String *cd; Hash *lthash; if (!r_mangled) { r_mangled = NewHash(); r_resolved = NewHash(); r_ltype = NewHash(); r_clientdata = NewHash(); r_remembered = NewHash(); } { String *last; last = Getattr(r_remembered, t); if (last && (Cmp(last, clientdata) == 0)) return; } tkey = Copy(t); cd = clientdata ? NewString(clientdata) : NewStringEmpty(); Setattr(r_remembered, tkey, cd); Delete(tkey); Delete(cd); mt = SwigType_manglestr(t); /* Create mangled string */ if (r_tracefunc) { (*r_tracefunc) (t, mt, (String *) clientdata); } if (SwigType_istypedef(t)) { lt = Copy(t); } else { lt = SwigType_ltype(t); } lthash = Getattr(r_ltype, mt); if (!lthash) { lthash = NewHash(); Setattr(r_ltype, mt, lthash); } Setattr(lthash, lt, "1"); Delete(lt); fr = SwigType_typedef_resolve_all(t); /* Create fully resolved type */ qr = SwigType_typedef_qualified(fr); Delete(fr); /* Added to deal with possible table bug */ fr = SwigType_strip_qualifiers(qr); Delete(qr); /*Printf(stdout,"t = '%s'\n", t); Printf(stdout,"fr= '%s'\n\n", fr); */ h = Getattr(r_mangled, mt); if (!h) { h = NewHash(); Setattr(r_mangled, mt, h); Delete(h); } Setattr(h, fr, mt); h = Getattr(r_resolved, fr); if (!h) { h = NewHash(); Setattr(r_resolved, fr, h); Delete(h); } Setattr(h, mt, fr); if (clientdata) { String *cd = Getattr(r_clientdata, fr); if (cd) { if (Strcmp(clientdata, cd) != 0) { Printf(stderr, "*** Internal error. Inconsistent clientdata for type '%s'\n", SwigType_str(fr, 0)); Printf(stderr, "*** '%s' != '%s'\n", clientdata, cd); assert(0); } } else { String *cstr = NewString(clientdata); Setattr(r_clientdata, fr, cstr); Delete(cstr); } } /* If the remembered type is a reference, we also remember the pointer version. This is to prevent odd problems with mixing pointers and references--especially when different functions are using different typenames (via typedef). */ if (SwigType_isreference(t)) { SwigType *tt = Copy(t); SwigType_del_reference(tt); SwigType_add_pointer(tt); SwigType_remember_clientdata(tt, clientdata); } else if (SwigType_isrvalue_reference(t)) { SwigType *tt = Copy(t); SwigType_del_rvalue_reference(tt); SwigType_add_pointer(tt); SwigType_remember_clientdata(tt, clientdata); } } void SwigType_remember(const SwigType *ty) { SwigType_remember_clientdata(ty, 0); } void (*SwigType_remember_trace(void (*tf) (const SwigType *, String *, String *))) (const SwigType *, String *, String *) { void (*o) (const SwigType *, String *, String *) = r_tracefunc; r_tracefunc = tf; return o; } /* ----------------------------------------------------------------------------- * SwigType_equivalent_mangle() * * Return a list of all of the mangled typenames that are equivalent to another * mangled name. This works as follows: For each fully qualified C datatype * in the r_mangled hash entry, we collect all of the mangled names from the * r_resolved hash and combine them together in a list (removing duplicate entries). * ----------------------------------------------------------------------------- */ static List *SwigType_equivalent_mangle(String *ms, Hash *checked, Hash *found) { List *l; Hash *h; Hash *ch; Hash *mh; if (found) { h = found; } else { h = NewHash(); } if (checked) { ch = checked; } else { ch = NewHash(); } if (Getattr(ch, ms)) goto check_exit; /* Already checked this type */ Setattr(h, ms, "1"); Setattr(ch, ms, "1"); mh = Getattr(r_mangled, ms); if (mh) { Iterator ki; ki = First(mh); while (ki.key) { Hash *rh; if (Getattr(ch, ki.key)) { ki = Next(ki); continue; } Setattr(ch, ki.key, "1"); rh = Getattr(r_resolved, ki.key); if (rh) { Iterator rk; rk = First(rh); while (rk.key) { Setattr(h, rk.key, "1"); SwigType_equivalent_mangle(rk.key, ch, h); rk = Next(rk); } } ki = Next(ki); } } check_exit: if (!found) { l = Keys(h); Delete(h); Delete(ch); return l; } else { return 0; } } /* ----------------------------------------------------------------------------- * SwigType_clientdata_collect() * * Returns the clientdata field for a mangled type-string. * ----------------------------------------------------------------------------- */ static String *SwigType_clientdata_collect(String *ms) { Hash *mh; String *clientdata = 0; if (r_mangleddata) { clientdata = Getattr(r_mangleddata, ms); if (clientdata) return clientdata; } mh = Getattr(r_mangled, ms); if (mh) { Iterator ki; ki = First(mh); while (ki.key) { clientdata = Getattr(r_clientdata, ki.key); if (clientdata) break; ki = Next(ki); } } return clientdata; } /* ----------------------------------------------------------------------------- * SwigType_inherit() * * Record information about inheritance. We keep a hash table that keeps * a mapping between base classes and all of the classes that are derived * from them. * * subclass is a hash that maps base-classes to all of the classes derived from them. * * derived - name of derived class * base - name of base class * cast - additional casting code when casting from derived to base * conversioncode - if set, overrides the default code in the function when casting * from derived to base * ----------------------------------------------------------------------------- */ static Hash *subclass = 0; static Hash *conversions = 0; void SwigType_inherit(String *derived, String *base, String *cast, String *conversioncode) { Hash *h; String *dd = 0; String *bb = 0; if (!subclass) subclass = NewHash(); /* Printf(stdout,"'%s' --> '%s' '%s'\n", derived, base, cast); */ if (SwigType_istemplate(derived)) { String *ty = SwigType_typedef_resolve_all(derived); dd = SwigType_typedef_qualified(ty); derived = dd; Delete(ty); } if (SwigType_istemplate(base)) { String *ty = SwigType_typedef_resolve_all(base); bb = SwigType_typedef_qualified(ty); base = bb; Delete(ty); } /* Printf(stdout,"'%s' --> '%s' '%s'\n", derived, base, cast); */ h = Getattr(subclass, base); if (!h) { h = NewHash(); Setattr(subclass, base, h); Delete(h); } if (!Getattr(h, derived)) { Hash *c = NewHash(); if (cast) Setattr(c, "cast", cast); if (conversioncode) Setattr(c, "convcode", conversioncode); Setattr(h, derived, c); Delete(c); } Delete(dd); Delete(bb); } /* ----------------------------------------------------------------------------- * SwigType_issubtype() * * Determines if a t1 is a subtype of t2, ie, is t1 derived from t2 * ----------------------------------------------------------------------------- */ int SwigType_issubtype(const SwigType *t1, const SwigType *t2) { SwigType *ft1, *ft2; String *b1, *b2; Hash *h; int r = 0; if (!subclass) return 0; ft1 = SwigType_typedef_resolve_all(t1); ft2 = SwigType_typedef_resolve_all(t2); b1 = SwigType_base(ft1); b2 = SwigType_base(ft2); h = Getattr(subclass, b2); if (h) { if (Getattr(h, b1)) { r = 1; } } Delete(ft1); Delete(ft2); Delete(b1); Delete(b2); /* Printf(stdout, "issubtype(%s,%s) --> %d\n", t1, t2, r); */ return r; } /* ----------------------------------------------------------------------------- * SwigType_inherit_equiv() * * Modify the type table to handle C++ inheritance * ----------------------------------------------------------------------------- */ static void SwigType_inherit_equiv(File *out) { String *ckey; String *prefix, *base; String *mprefix, *mkey; Hash *sub; Hash *rh; List *rlist; List *r_resolved_sorted_keys; Iterator rk, bk, ck; if (!conversions) conversions = NewHash(); if (!subclass) subclass = NewHash(); r_resolved_sorted_keys = SortedKeys(r_resolved, Strcmp); rk = First(r_resolved_sorted_keys); while (rk.item) { List *sub_sorted_keys; /* rkey is a fully qualified type. We strip all of the type constructors off of it just to get the base */ base = SwigType_base(rk.item); /* Check to see whether the base is recorded in the subclass table */ sub = Getattr(subclass, base); Delete(base); if (!sub) { rk = Next(rk); continue; } /* This type has subclasses. We now need to walk through these subtypes and generate pointer conversion functions */ rh = Getattr(r_resolved, rk.item); rlist = NewList(); for (ck = First(rh); ck.key; ck = Next(ck)) { Append(rlist, ck.key); } /* Printf(stdout,"rk.item = '%s'\n", rk.item); Printf(stdout,"rh = %p '%s'\n", rh,rh); */ sub_sorted_keys = SortedKeys(sub, Strcmp); bk = First(sub_sorted_keys); while (bk.item) { prefix = SwigType_prefix(rk.item); Append(prefix, bk.item); /* Printf(stdout,"set %p = '%s' : '%s'\n", rh, SwigType_manglestr(prefix),prefix); */ mprefix = SwigType_manglestr(prefix); Setattr(rh, mprefix, prefix); mkey = SwigType_manglestr(rk.item); ckey = NewStringf("%s+%s", mprefix, mkey); if (!Getattr(conversions, ckey)) { String *convname = NewStringf("%sTo%s", mprefix, mkey); String *lkey = SwigType_lstr(rk.item, 0); String *lprefix = SwigType_lstr(prefix, 0); Hash *subhash = Getattr(sub, bk.item); String *convcode = Getattr(subhash, "convcode"); if (convcode) { char *newmemoryused = Strstr(convcode, "newmemory"); /* see if newmemory parameter is used in order to avoid unused parameter warnings */ String *fn = Copy(convcode); Replaceall(fn, "$from", "x"); Printf(out, "static void *%s(void *x, int *%s) {", convname, newmemoryused ? "newmemory" : "SWIGUNUSEDPARM(newmemory)"); Printf(out, "%s", fn); } else { String *cast = Getattr(subhash, "cast"); Printf(out, "static void *%s(void *x, int *SWIGUNUSEDPARM(newmemory)) {", convname); Printf(out, "\n return (void *)((%s) ", lkey); if (cast) Printf(out, "%s", cast); Printf(out, " ((%s) x));\n", lprefix); } Printf(out, "}\n"); Setattr(conversions, ckey, convname); Delete(ckey); Delete(lkey); Delete(lprefix); /* This inserts conversions for typedefs */ { Hash *r = Getattr(r_resolved, prefix); if (r) { Iterator rrk; rrk = First(r); while (rrk.key) { Iterator rlk; String *rkeymangle; /* Make sure this name equivalence is not due to inheritance */ if (Cmp(prefix, Getattr(r, rrk.key)) == 0) { rkeymangle = Copy(mkey); ckey = NewStringf("%s+%s", rrk.key, rkeymangle); if (!Getattr(conversions, ckey)) { Setattr(conversions, ckey, convname); } Delete(ckey); for (rlk = First(rlist); rlk.item; rlk = Next(rlk)) { ckey = NewStringf("%s+%s", rrk.key, rlk.item); Setattr(conversions, ckey, convname); Delete(ckey); } Delete(rkeymangle); /* This is needed to pick up other alternative names for the same type. Needed to make templates work */ Setattr(rh, rrk.key, rrk.item); } rrk = Next(rrk); } } } Delete(convname); } Delete(prefix); Delete(mprefix); Delete(mkey); bk = Next(bk); } Delete(sub_sorted_keys); rk = Next(rk); Delete(rlist); } Delete(r_resolved_sorted_keys); } /* ----------------------------------------------------------------------------- * SwigType_type_table() * * Generate the type-table for the type-checker. * ----------------------------------------------------------------------------- */ void SwigType_emit_type_table(File *f_forward, File *f_table) { Iterator ki; String *types, *table, *cast, *cast_init, *cast_temp; Hash *imported_types; List *mangled_list; List *table_list = NewList(); int i = 0; if (!r_mangled) { r_mangled = NewHash(); r_resolved = NewHash(); } Printf(f_table, "\n/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */\n\n"); SwigType_inherit_equiv(f_table); /*#define DEBUG 1*/ #ifdef DEBUG Printf(stdout, "---r_mangled---\n"); Swig_print(r_mangled, 2); Printf(stdout, "---r_resolved---\n"); Swig_print(r_resolved, 2); Printf(stdout, "---r_ltype---\n"); Swig_print(r_ltype, 2); Printf(stdout, "---subclass---\n"); Swig_print(subclass, 2); Printf(stdout, "---conversions---\n"); Swig_print(conversions, 2); Printf(stdout, "---r_clientdata---\n"); Swig_print(r_clientdata, 2); #endif table = NewStringEmpty(); types = NewStringEmpty(); cast = NewStringEmpty(); cast_init = NewStringEmpty(); imported_types = NewHash(); Printf(table, "static swig_type_info *swig_type_initial[] = {\n"); Printf(cast_init, "static swig_cast_info *swig_cast_initial[] = {\n"); Printf(f_forward, "\n/* -------- TYPES TABLE (BEGIN) -------- */\n\n"); mangled_list = SortedKeys(r_mangled, Strcmp); for (ki = First(mangled_list); ki.item; ki = Next(ki)) { List *el; Iterator ei; String *nt; String *ln; String *rn; const String *cd; Hash *lthash; Iterator ltiter; Hash *nthash; String *cast_temp_conv; String *resolved_lstr = 0; List *ntlist; cast_temp = NewStringEmpty(); cast_temp_conv = NewStringEmpty(); Printv(types, "static swig_type_info _swigt_", ki.item, " = {", NIL); Append(table_list, ki.item); Printf(cast_temp, "static swig_cast_info _swigc_%s[] = {", ki.item); i++; cd = SwigType_clientdata_collect(ki.item); if (!cd) cd = "0"; lthash = Getattr(r_ltype, ki.item); nt = 0; nthash = NewHash(); ltiter = First(lthash); while (ltiter.key) { SwigType *lt = ltiter.key; SwigType *rt = SwigType_typedef_resolve_all(lt); /* we save the original type and the fully resolved version */ ln = SwigType_lstr(lt, 0); rn = SwigType_lstr(rt, 0); if (Equal(ln, rn)) { Setattr(nthash, ln, "1"); } else { Setattr(nthash, rn, "1"); Setattr(nthash, ln, "1"); } if (!resolved_lstr) { resolved_lstr = Copy(rn); } else if (Len(rn) < Len(resolved_lstr)) { Delete(resolved_lstr); resolved_lstr = Copy(rn); } if (SwigType_istemplate(rt)) { String *dt = Swig_symbol_template_deftype(rt, 0); String *dn = SwigType_lstr(dt, 0); if (!Equal(dn, rn) && !Equal(dn, ln)) { Setattr(nthash, dn, "1"); } Delete(dt); Delete(dn); } Delete(rt); Delete(rn); Delete(ln); ltiter = Next(ltiter); } /* now build nt */ ntlist = SortedKeys(nthash, Strcmp); ltiter = First(ntlist); nt = 0; while (ltiter.item) { if (!Equal(resolved_lstr, ltiter.item)) { if (nt) { Printf(nt, "|%s", ltiter.item); } else { nt = NewString(ltiter.item); } } ltiter = Next(ltiter); } /* Last in list is a resolved type used by SWIG_TypePrettyName. * There can be more than one resolved type and the chosen one is simply the * shortest in length, arguably the most user friendly/readable. */ if (nt) { Printf(nt, "|%s", resolved_lstr); } else { nt = NewString(resolved_lstr); } Delete(ntlist); Delete(nthash); Delete(resolved_lstr); Printf(types, "\"%s\", \"%s\", 0, 0, (void*)%s, 0};\n", ki.item, nt, cd); el = SwigType_equivalent_mangle(ki.item, 0, 0); SortList(el, Strcmp); for (ei = First(el); ei.item; ei = Next(ei)) { String *ckey; String *conv; ckey = NewStringf("%s+%s", ei.item, ki.item); conv = Getattr(conversions, ckey); if (conv) { Printf(cast_temp_conv, " {&_swigt_%s, %s, 0, 0},", ei.item, conv); } else { Printf(cast_temp, " {&_swigt_%s, 0, 0, 0},", ei.item); } Delete(ckey); if (!Getattr(r_mangled, ei.item) && !Getattr(imported_types, ei.item)) { Printf(types, "static swig_type_info _swigt_%s = {\"%s\", 0, 0, 0, 0, 0};\n", ei.item, ei.item); Append(table_list, ei.item); Printf(cast, "static swig_cast_info _swigc_%s[] = {{&_swigt_%s, 0, 0, 0},{0, 0, 0, 0}};\n", ei.item, ei.item); i++; Setattr(imported_types, ei.item, "1"); } } Delete(el); Printf(cast, "%s%s{0, 0, 0, 0}};\n", cast_temp, cast_temp_conv); Delete(cast_temp_conv); Delete(cast_temp); Delete(nt); } /* print the tables in the proper order */ SortList(table_list, Strcmp); i = 0; for (ki = First(table_list); ki.item; ki = Next(ki)) { Printf(f_forward, "#define SWIGTYPE%s swig_types[%d]\n", ki.item, i++); Printf(table, " &_swigt_%s,\n", ki.item); Printf(cast_init, " _swigc_%s,\n", ki.item); } if (i == 0) { /* empty arrays are not allowed by ISO C */ Printf(table, " NULL\n"); Printf(cast_init, " NULL\n"); } Delete(table_list); Delete(mangled_list); Printf(table, "};\n"); Printf(cast_init, "};\n"); Printf(f_table, "%s\n", types); Printf(f_table, "%s\n", table); Printf(f_table, "%s\n", cast); Printf(f_table, "%s\n", cast_init); Printf(f_table, "\n/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (END) -------- */\n\n"); Printf(f_forward, "static swig_type_info *swig_types[%d];\n", i + 1); Printf(f_forward, "static swig_module_info swig_module = {swig_types, %d, 0, 0, 0, 0};\n", i); Printf(f_forward, "#define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name)\n"); Printf(f_forward, "#define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name)\n"); Printf(f_forward, "\n/* -------- TYPES TABLE (END) -------- */\n\n"); Delete(types); Delete(table); Delete(cast); Delete(cast_init); Delete(imported_types); } swig-4.4.0/Source/Swig/symbol.c0000664000175000017500000021111315075443613016201 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * symbol.c * * This file implements the SWIG symbol table. See details below. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" #include /* #define SWIG_DEBUG*/ /* ----------------------------------------------------------------------------- * Synopsis * * This module provides symbol table management for all of SWIG. In previous * releases, the management of symbols was rather haphazard. This module tries * to correct that. * * All symbols are associated with simple identifiers. For example, here are some * declarations that generate symbol table entries: * * decl symbol * -------------- ------------ * void foo(int); foo * int x; x * typedef int *blah; blah * * Associated with each symbol is a Hash table that can contain any set of * attributes that make sense for that object. For example: * * typedef int *blah; ----> "name" : 'blah' * "type" : 'int' * "decl" : 'p.' * "storage" : 'typedef' * * In some cases, the symbol table needs to manage overloaded entries. For instance, * overloaded functions. In this case, a linked list is built. The "sym:nextSibling" * attribute is reserved to hold a link to the next entry. For example: * * int foo(int); --> "name" : "foo" "name" : "foo" * int foo(int,double); "type" : "int" "type" : "int" * "decl" : "f(int)." "decl" : "f(int,double)." * ... ... * "sym:nextSibling" : --------> "sym:nextSibling": --------> ... * * When more than one symbol has the same name, the symbol declarator is * used to detect duplicates. For example, in the above case, foo(int) and * foo(int,double) are different because their "decl" attribute is different. * However, if a third declaration "foo(int)" was made, it would generate a * conflict (due to having a declarator that matches a previous entry). * * Structures and classes: * * C/C++ symbol tables are normally managed in a few different spaces. The * most visible namespace is reserved for functions, variables, typedef, enum values * and such. In C, a separate tag-space is reserved for 'struct name', 'class name', * and 'union name' declarations. In SWIG, a single namespace is used for everything * this means that certain incompatibilities will arise with some C programs. For instance: * * struct Foo { * ... * } * * int Foo(); // Error. Name clash. Works in C though * * Due to the unified namespace for structures, special handling is performed for * the following: * * typedef struct Foo { * * } Foo; * * In this case, the symbol table contains an entry for the structure itself. The * typedef is left out of the symbol table. * * Target language vs C: * * The symbol tables are normally managed *in the namespace of the target language*. * This means that name-collisions can be resolved using %rename and related * directives. A quirk of this is that sometimes the symbol tables need to * be used for C type resolution as well. To handle this, each symbol table * also has a C-symbol table lurking behind the scenes. This is used to locate * symbols in the C namespace. However, this symbol table is not used for error * reporting nor is it used for anything else during code generation. * * Symbol table structure: * * Symbol tables themselves are a special kind of node that is organized just like * a normal parse tree node. Symbol tables are organized in a tree that can be * traversed using the SWIG-DOM API. The following attributes names are reserved. * * name -- Name of the scope defined by the symbol table (if any) * This name is the C-scope name and is not affected by * %renaming operations * symtab -- Hash table mapping identifiers to nodes. * csymtab -- Hash table mapping C identifiers to nodes. * * Reserved attributes on symbol objects: * * When a symbol is placed in the symbol table, the following attributes * are set: * * sym:name -- Symbol name * sym:nextSibling -- Next symbol (if overloaded) * sym:previousSibling -- Previous symbol (if overloaded) * sym:symtab -- Symbol table object holding the symbol * sym:overloaded -- Set to the first symbol if overloaded * * These names are modeled after XML namespaces. In particular, every attribute * pertaining to symbol table management is prefaced by the "sym:" prefix. * * An example dump of the parse tree showing symbol table entries for the * following code should clarify this: * * namespace OuterNamespace { * namespace InnerNamespace { * class Class { * }; * struct Struct { * int Var; * }; * } * } * * +++ namespace ---------------------------------------- * | sym:name - "OuterNamespace" * | symtab - 0xa064bf0 * | sym:symtab - 0xa041690 * | sym:overname - "__SWIG_0" * * +++ namespace ---------------------------------------- * | sym:name - "InnerNamespace" * | symtab - 0xa064cc0 * | sym:symtab - 0xa064bf0 * | sym:overname - "__SWIG_0" * * +++ class ---------------------------------------- * | sym:name - "Class" * | symtab - 0xa064d80 * | sym:symtab - 0xa064cc0 * | sym:overname - "__SWIG_0" * | * +++ class ---------------------------------------- * | sym:name - "Struct" * | symtab - 0xa064f00 * | sym:symtab - 0xa064cc0 * | sym:overname - "__SWIG_0" * * +++ cdecl ---------------------------------------- * | sym:name - "Var" * | sym:symtab - 0xa064f00 * | sym:overname - "__SWIG_0" * | * * * Each class and namespace has its own scope and thus a new symbol table (sym) * is created. The sym attribute is only set for the first entry in the symbol * table. The sym:symtab entry points to the symbol table in which the symbol * exists, so for example, Struct is in the scope OuterNamespace::InnerNamespace * so sym:symtab points to this symbol table (0xa064cc0). * * ----------------------------------------------------------------------------- */ static Hash *current = 0; /* The current symbol table hash */ static Hash *ccurrent = 0; /* The current c symbol table hash */ static Hash *current_symtab = 0; /* Current symbol table node */ static Hash *symtabs = 0; /* Hash of all symbol tables by fully-qualified name */ static Hash *global_scope = 0; /* Global scope */ static int use_inherit = 1; /* common attribute keys, to avoid calling find_key all the times */ /* ----------------------------------------------------------------------------- * Swig_symbol_print_tables() * * Debug display of symbol tables * ----------------------------------------------------------------------------- */ void Swig_symbol_print_tables(Symtab *symtab) { if (!symtab) symtab = current_symtab; Printf(stdout, "SYMBOL TABLES start =======================================\n"); Swig_print_tree(symtab); Printf(stdout, "SYMBOL TABLES finish =======================================\n"); } /* ----------------------------------------------------------------------------- * Swig_symbol_print_tables_summary() * * Debug summary display of all symbol tables by fully-qualified name * ----------------------------------------------------------------------------- */ void Swig_symbol_print_tables_summary(void) { Printf(stdout, "SYMBOL TABLES SUMMARY start =======================================\n"); Swig_print_node(symtabs); Printf(stdout, "SYMBOL TABLES SUMMARY finish =======================================\n"); } /* ----------------------------------------------------------------------------- * symbol_print_symbols() * ----------------------------------------------------------------------------- */ static void symbol_print_symbols(const char *symboltabletype, const char *nextSibling) { Node *table = symtabs; Iterator ki = First(table); int show_pointers = 0; while (ki.key) { String *k = ki.key; Printf(stdout, "===================================================\n"); Printf(stdout, "%s -\n", k); { Symtab *symtab = Getattr(Getattr(table, k), symboltabletype); Iterator it = First(symtab); while (it.key) { String *symname = it.key; Printf(stdout, " %s (%s)", symname, nodeType(it.item)); if (show_pointers) Printf(stdout, " %p", it.item); Printf(stdout, "\n"); { Node *sibling = Getattr(it.item, nextSibling); while (sibling) { Printf(stdout, " %s (%s)", symname, nodeType(sibling)); if (show_pointers) Printf(stdout, " %p", sibling); Printf(stdout, "\n"); sibling = Getattr(sibling, nextSibling); } } it = Next(it); } } ki = Next(ki); } } /* ----------------------------------------------------------------------------- * Swig_symbol_print_symbols() * * Debug display of all the target language symbols * ----------------------------------------------------------------------------- */ void Swig_symbol_print_symbols(void) { Printf(stdout, "SYMBOLS start =======================================\n"); symbol_print_symbols("symtab", "sym:nextSibling"); Printf(stdout, "SYMBOLS finish =======================================\n"); } /* ----------------------------------------------------------------------------- * Swig_symbol_print_csymbols() * * Debug display of all the C symbols * ----------------------------------------------------------------------------- */ void Swig_symbol_print_csymbols(void) { Printf(stdout, "CSYMBOLS start =======================================\n"); symbol_print_symbols("csymtab", "csym:nextSibling"); Printf(stdout, "CSYMBOLS finish =======================================\n"); } /* ----------------------------------------------------------------------------- * Swig_symbol_init() * * Create a new symbol table object * ----------------------------------------------------------------------------- */ void Swig_symbol_init(void) { current = NewHash(); current_symtab = NewHash(); ccurrent = NewHash(); set_nodeType(current_symtab, "symboltable"); Setattr(current_symtab, "symtab", current); Delete(current); Setattr(current_symtab, "csymtab", ccurrent); Delete(ccurrent); /* Set the global scope */ symtabs = NewHash(); Setattr(symtabs, "", current_symtab); Delete(current_symtab); global_scope = current_symtab; } /* ----------------------------------------------------------------------------- * Swig_symbol_setscopename() * * Set the C scopename of the current symbol table. * ----------------------------------------------------------------------------- */ void Swig_symbol_setscopename(const_String_or_char_ptr name) { String *qname; /* assert(!Getattr(current_symtab,"name")); */ Setattr(current_symtab, "name", name); /* Set nested scope in parent */ qname = Swig_symbol_qualifiedscopename(current_symtab); /* Save a reference to this scope */ Setattr(symtabs, qname, current_symtab); Delete(qname); } /* ----------------------------------------------------------------------------- * Swig_symbol_getscopename() * * Get the C scopename of the current symbol table * ----------------------------------------------------------------------------- */ String *Swig_symbol_getscopename(void) { return Getattr(current_symtab, "name"); } /* ----------------------------------------------------------------------------- * Swig_symbol_getscope() * * Given a fully qualified C scopename, this function returns a symbol table * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_getscope(const_String_or_char_ptr name) { if (!symtabs) return 0; if (Equal("::", (const_String_or_char_ptr ) name)) name = ""; return Getattr(symtabs, name); } /* ----------------------------------------------------------------------------- * Swig_symbol_qualifiedscopename() * * Get the fully qualified C scopename of a symbol table. Note, this only pertains * to the C/C++ scope name. It is not affected by renaming. * ----------------------------------------------------------------------------- */ String *Swig_symbol_qualifiedscopename(Symtab *symtab) { String *result = 0; Hash *parent; String *name; if (!symtab) symtab = current_symtab; parent = Getattr(symtab, "parentNode"); if (parent) { result = Swig_symbol_qualifiedscopename(parent); } name = Getattr(symtab, "name"); if (name) { if (!result) { result = NewStringEmpty(); } if (Len(result)) { Printv(result, "::", name, NIL); } else { Append(result, name); } } return result; } /* ----------------------------------------------------------------------------- * Swig_symbol_qualified_language_scopename() * * Get the fully qualified C scopename of a symbol table but using a language * specific separator for the scopenames. Basically the same as * Swig_symbol_qualifiedscopename() but using the different separator. * ----------------------------------------------------------------------------- */ String *Swig_symbol_qualified_language_scopename(Symtab *n) { /* TODO: fix for %rename to work */ String *result = Swig_symbol_qualifiedscopename(n); Replaceall(result, "::", NSPACE_SEPARATOR); return result; } /* ----------------------------------------------------------------------------- * Swig_symbol_newscope() * * Create a new scope. Returns the newly created scope. * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_newscope(void) { Hash *n; Hash *hsyms, *h; hsyms = NewHash(); h = NewHash(); set_nodeType(h, "symboltable"); Setattr(h, "symtab", hsyms); Delete(hsyms); set_parentNode(h, current_symtab); n = lastChild(current_symtab); if (!n) { set_firstChild(current_symtab, h); } else { set_nextSibling(n, h); Delete(h); } set_lastChild(current_symtab, h); current = hsyms; ccurrent = NewHash(); Setattr(h, "csymtab", ccurrent); Delete(ccurrent); current_symtab = h; return h; } /* ----------------------------------------------------------------------------- * Swig_symbol_setscope() * * Set the current scope. Returns the previous current scope. * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_setscope(Symtab *sym) { Symtab *ret = current_symtab; current_symtab = sym; current = Getattr(sym, "symtab"); assert(current); ccurrent = Getattr(sym, "csymtab"); assert(ccurrent); return ret; } /* ----------------------------------------------------------------------------- * Swig_symbol_popscope() * * Pop out of the current scope. Returns the popped scope and sets the * scope to the parent scope. * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_popscope(void) { Hash *h = current_symtab; current_symtab = Getattr(current_symtab, "parentNode"); assert(current_symtab); current = Getattr(current_symtab, "symtab"); assert(current); ccurrent = Getattr(current_symtab, "csymtab"); assert(ccurrent); return h; } /* ----------------------------------------------------------------------------- * Swig_symbol_global_scope() * * Return the symbol table for the global scope. * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_global_scope(void) { return global_scope; } /* ----------------------------------------------------------------------------- * Swig_symbol_current() * * Return the current symbol table. * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_current(void) { return current_symtab; } /* ----------------------------------------------------------------------------- * Swig_symbol_alias() * * Makes an alias for a symbol in the global symbol table. * Primarily for namespace aliases such as 'namespace X = Y;'. * ----------------------------------------------------------------------------- */ void Swig_symbol_alias(const_String_or_char_ptr aliasname, Symtab *s) { String *qname = Swig_symbol_qualifiedscopename(current_symtab); if (qname) { Printf(qname, "::%s", aliasname); } else { qname = NewString(aliasname); } if (!Getattr(symtabs, qname)) { Setattr(symtabs, qname, s); } Delete(qname); } /* ----------------------------------------------------------------------------- * Swig_symbol_inherit() * * Inherit symbols from another scope. Primarily for C++ inheritance and * for using directives, such as 'using namespace X;' * but not for using declarations, such as 'using X::A;'. * ----------------------------------------------------------------------------- */ void Swig_symbol_inherit(Symtab *s) { int i, ilen; List *inherit = Getattr(current_symtab, "inherit"); if (!inherit) { inherit = NewList(); Setattr(current_symtab, "inherit", inherit); Delete(inherit); } if (s == current_symtab) { Swig_warning(WARN_PARSE_REC_INHERITANCE, Getfile(s), Getline(s), "Recursive scope inheritance of '%s'.\n", Getattr(s, "name")); return; } assert(s != current_symtab); ilen = Len(inherit); for (i = 0; i < ilen; i++) { Node *n = Getitem(inherit, i); if (n == s) return; /* Already inherited */ } Append(inherit, s); } /* ----------------------------------------------------------------------------- * Swig_symbol_cadd() * * Adds a node to the C symbol table only. * ----------------------------------------------------------------------------- */ void Swig_symbol_cadd(const_String_or_char_ptr name, Node *n) { Node *append = 0; Node *cn; /* There are a few options for weak symbols. A "weak" symbol is any symbol that can be replaced by another symbol in the C symbol table. An example would be a forward class declaration. A forward class sits in the symbol table until a real class declaration comes along. Certain symbols are marked as "sym:typename". These are important symbols related to the C++ type-system and take precedence in the C symbol table. An example might be code like this: template T foo(T x); int foo(int); In this case, the template is marked with "sym:typename" so that it stays in the C symbol table (so that it can be expanded using %template). */ if (!name) return; if (SwigType_istemplate(name)) { String *cname = NewString(name); String *dname = Swig_symbol_template_deftype(cname, 0); if (!Equal(dname, name)) { /* Add another symbol with all template default arguments expanded, eg * * template struct X {}; * %template(XInt) X; * * then name=X, and dname=X so add X here too. */ Swig_symbol_cadd(dname, n); } Delete(dname); Delete(cname); } #ifdef SWIG_DEBUG Printf(stderr, "symbol_cadd %s %p\n", name, n); #endif cn = Getattr(ccurrent, name); if (cn && (Getattr(cn, "sym:typename"))) { /* The node in the C symbol table is a typename. Do nothing */ /* We might append the symbol at the end */ append = n; } else if (cn && (Getattr(cn, "sym:weak"))) { /* The node in the symbol table is weak. Replace it */ if (checkAttribute(cn, "nodeType", "template") && checkAttribute(cn, "templatetype", "classforward")) { /* The node is a template classforward declaration, and the default template parameters here take precedence. */ ParmList *pc = Getattr(cn, "templateparms"); ParmList *pn = Getattr(n, "templateparms"); #ifdef SWIG_DEBUG Printf(stderr, "found template classforward %s\n", Getattr(cn, "name")); #endif while (pc && pn) { String *value = Getattr(pc, "value"); if (value) { #ifdef SWIG_DEBUG Printf(stderr, "add default template value %s %s\n", Getattr(pc, "name"), value); #endif Setattr(pn, "value", value); } pc = nextSibling(pc); pn = nextSibling(pn); } Setattr(n, "templateparms", Getattr(cn, "templateparms")); } Setattr(ccurrent, name, n); } else if (cn && (Getattr(n, "sym:weak"))) { /* The node being added is weak. Don't worry about it */ } else if (cn && (Getattr(n, "sym:typename"))) { /* The node being added is a typename. We definitely add it */ Setattr(ccurrent, name, n); append = cn; } else if (cn && (Checkattr(cn, "nodeType", "templateparm"))) { Swig_error(Getfile(n), Getline(n), "Declaration of '%s' shadows template parameter,\n", name); Swig_error(Getfile(cn), Getline(cn), "previous template parameter declaration '%s'.\n", name); return; } else if (cn) { append = n; } else if (!cn) { /* No conflict. Add the symbol */ Setattr(ccurrent, name, n); } /* Multiple entries in the C symbol table. We append to the symbol table */ if (append) { Node *fn, *pn = 0; cn = Getattr(ccurrent, name); fn = cn; while (fn) { pn = fn; if (fn == append) { /* already added. Bail */ return; } fn = Getattr(fn, "csym:nextSibling"); } if (pn) { Setattr(pn, "csym:nextSibling", append); } } /* Special typedef handling. When a typedef node is added to the symbol table, we might have to add a type alias. This would occur if the typedef mapped to another scope in the system. For example: class Foo { }; typedef Foo OtherFoo; In this case, OtherFoo becomes an alias for Foo. */ { Node *td = n; while (td && ((Equal(nodeType(td), "cdecl") && Checkattr(td, "storage", "typedef")) || (Equal(nodeType(td), "using") && !Getattr(n, "namespace")))) { SwigType *type; Node *td1; int using_not_typedef = Equal(nodeType(td), "using"); type = Copy(Getattr(td, using_not_typedef ? "uname" : "type")); SwigType_push(type, Getattr(td, "decl")); td1 = Swig_symbol_clookup(type, 0); /* Fix pathetic case #1214313: class Foo { }; typedef Foo FooBar; class CBaz { public: typedef FooBar Foo; }; ie, when Foo -> FooBar -> Foo, jump one scope up when possible. */ if (td1) { String *st = 0; String *sn = Getattr(td, "name"); if (Equal(nodeType(td1), "cdecl") && Checkattr(td1, "storage", "typedef")) st = Getattr(td1, "type"); else if (Equal(nodeType(td1), "using") && !Getattr(td1, "namespace")) st = Getattr(td1, "uname"); if (st && sn && Equal(st, sn)) { Symtab *sc = Getattr(current_symtab, "parentNode"); if (sc) td1 = Swig_symbol_clookup(type, sc); } } Delete(type); if (td1 == td) break; td = td1; if (td) { Symtab *st = Getattr(td, "symtab"); if (st) { Swig_symbol_alias(Getattr(n, "name"), st); break; } } } } } /* ----------------------------------------------------------------------------- * Swig_symbol_add() * * Adds a node to the symbol table. Returns the node itself if successfully * added. Otherwise, it returns the symbol table entry of the conflicting node. * * Also places the symbol in a behind-the-scenes C symbol table. This is needed * for namespace support, type resolution, and other issues. * ----------------------------------------------------------------------------- */ static Node *symbol_add(const_String_or_char_ptr symname, Node *n) { Hash *c, *cl = 0; SwigType *decl, *ndecl; String *cstorage, *nstorage; int nt = 0, ct = 0; int pn = 0; int u1 = 0, u2 = 0; String *name, *overname; /* See if the node has a name. If so, we place in the C symbol table for this scope. We don't worry about overloading here---the primary purpose of this is to record information for type/name resolution for later. Conflicts in C namespaces are errors, but these will be caught by the C++ compiler when compiling the wrapper code */ /* There are a few options for weak symbols. A "weak" symbol is any symbol that can be replaced by another symbol in the C symbol table. An example would be a forward class declaration. A forward class sits in the symbol table until a real class declaration comes along. Certain symbols are marked as "sym:typename". These are important symbols related to the C++ type-system and take precedence in the C symbol table. An example might be code like this: template T foo(T x); int foo(int); In this case, the template is marked with "sym:typename" so that it stays in the C symbol table (so that it can be expanded using %template). */ name = Getattr(n, "name"); if (name && Len(name)) { Swig_symbol_cadd(name, n); } /* No symbol name defined. We return. */ if (!symname) { return n; } /* If node is ignored. We don't proceed any further */ if (GetFlag(n, "feature:ignore")) return n; /* See if the symbol already exists in the table */ c = Getattr(current, symname); /* Check for a weak symbol. A weak symbol is allowed to be in the symbol table, but is silently overwritten by other symbols. An example would be a forward class declaration. For instance: class Foo; In this case, "Foo" sits in the symbol table. However, the definition of Foo would replace the entry if it appeared later. */ if (c && Getattr(c, "sym:weak")) { c = 0; } if (c) { /* There is a symbol table conflict. There are a few cases to consider here: (1) A conflict between a class/enum and a typedef declaration is okay. In this case, the symbol table entry is set to the class/enum declaration itself, not the typedef. (2) A conflict between namespaces is okay--namespaces are open (3) Otherwise, overloading is only allowed for functions (4) This special case is okay: a class template instantiated with same name as the template's name */ /* Check for namespaces */ String *ntype = Getattr(n, "nodeType"); if ((Equal(ntype, Getattr(c, "nodeType"))) && ((Equal(ntype, "namespace")))) { Node *cl, *pcl = 0; cl = c; while (cl) { pcl = cl; cl = Getattr(cl, "sym:nextSibling"); } Setattr(pcl, "sym:nextSibling", n); Setattr(n, "sym:symtab", current_symtab); Setattr(n, "sym:name", symname); Setattr(n, "sym:previousSibling", pcl); return n; } /* Special case: class template instantiated with same name as the template's name eg: %template(X) X; */ if (Equal(nodeType(c), "template")) { String *nt1 = Getattr(c, "templatetype"); String *nt2 = nodeType(n); if (Equal(nt1, "class") && Equal(nt1, nt2)) { if (Getattr(n, "template")) { /* Finally check that another %template with same name doesn't already exist */ if (!Getattr(c, "sym:nextSibling")) { Setattr(c, "sym:nextSibling", n); Setattr(n, "sym:symtab", current_symtab); Setattr(n, "sym:name", symname); Setattr(n, "sym:previousSibling", c); return n; } } } } if (Getattr(n, "allows_typedef")) nt = 1; if (Getattr(c, "allows_typedef")) ct = 1; if (nt || ct) { Node *td, *other; String *s; /* At least one of the nodes allows typedef overloading. Make sure that both don't--this would be a conflict */ if (nt && ct) return c; /* Figure out which node allows the typedef */ if (nt) { td = n; other = c; } else { td = c; other = n; } /* Make sure the other node is a typedef */ s = Getattr(other, "storage"); if (!s || (!Equal(s, "typedef"))) return c; /* No. This is a conflict */ /* Hmmm. This appears to be okay. Make sure the symbol table refers to the allow_type node */ Setattr(n, "sym:symtab", current_symtab); Setattr(n, "sym:name", symname); if (td == n) { Setattr(current, symname, td); } return n; } decl = Getattr(c, "decl"); ndecl = Getattr(n, "decl"); { String *nt1, *nt2; nt1 = Getattr(n, "nodeType"); if (Equal(nt1, "template")) nt1 = Getattr(n, "templatetype"); nt2 = Getattr(c, "nodeType"); if (Equal(nt2, "template")) nt2 = Getattr(c, "templatetype"); if (Equal(nt1, "using")) u1 = 1; if (Equal(nt2, "using")) u2 = 1; if ((!Equal(nt1, nt2)) && !(u1 || u2)) return c; } if (!(u1 || u2)) { if ((!SwigType_isfunction(decl)) || (!SwigType_isfunction(ndecl))) { /* Symbol table conflict */ return c; } } /* Hmmm. Declarator seems to indicate that this is a function */ /* Look at storage class to see if compatible */ cstorage = Getattr(c, "storage"); nstorage = Getattr(n, "storage"); /* If either one is declared as typedef, forget it. We're hosed */ if (Cmp(cstorage, "typedef") == 0) { return c; } if (Cmp(nstorage, "typedef") == 0) { return c; } /* Okay. Walk down the list of symbols and see if we get a declarator match */ { String *nt = Getattr(n, "nodeType"); int n_template = Equal(nt, "template") && Checkattr(n, "templatetype", "cdecl"); int n_plain_cdecl = Equal(nt, "cdecl"); Node *cn = c; pn = 0; while (cn) { decl = Getattr(cn, "decl"); if (!(u1 || u2)) { if (Cmp(ndecl, decl) == 0) { /* Declarator conflict */ /* Now check we don't have a non-templated function overloaded by a templated function with same params, * eg void foo(); template void foo(); */ String *cnt = Getattr(cn, "nodeType"); int cn_template = Equal(cnt, "template") && Checkattr(cn, "templatetype", "cdecl"); int cn_plain_cdecl = Equal(cnt, "cdecl"); if (!((n_template && cn_plain_cdecl) || (cn_template && n_plain_cdecl))) { /* found a conflict */ return cn; } } } cl = cn; cn = Getattr(cn, "sym:nextSibling"); pn++; } } /* Well, we made it this far. Guess we can drop the symbol in place */ Setattr(n, "sym:symtab", current_symtab); Setattr(n, "sym:name", symname); /* Printf(stdout,"%s %p\n", Getattr(n,"sym:overname"), current_symtab); */ assert(!Getattr(n, "sym:overname")); overname = NewStringf("__SWIG_%d", pn); Setattr(n, "sym:overname", overname); /*Printf(stdout,"%s %s %s\n", symname, Getattr(n,"decl"), Getattr(n,"sym:overname")); */ Setattr(cl, "sym:nextSibling", n); Setattr(n, "sym:previousSibling", cl); Setattr(cl, "sym:overloaded", c); Setattr(n, "sym:overloaded", c); Delete(overname); return n; } /* No conflict. Just add it */ Setattr(n, "sym:symtab", current_symtab); Setattr(n, "sym:name", symname); /* Printf(stdout,"%s\n", Getattr(n,"sym:overname")); */ overname = NewStringf("__SWIG_%d", pn); Setattr(n, "sym:overname", overname); Delete(overname); /* Printf(stdout,"%s %s %s\n", symname, Getattr(n,"decl"), Getattr(n,"sym:overname")); */ Setattr(current, symname, n); return n; } Node *Swig_symbol_add(const_String_or_char_ptr symname, Node *n) { Node *nn = symbol_add(symname, n); /* Always set the symtab to have correct scope in case of error reporting */ if (!Getattr(n, "sym:symtab")) Setattr(n, "sym:symtab", current_symtab); return nn; } /* ----------------------------------------------------------------------------- * Swig_symbol_conflict_warn() * * Issue warnings for node n if it conflicts with node c after calling * Swig_symbol_add(). * ----------------------------------------------------------------------------- */ void Swig_symbol_conflict_warn(Node *n, Node *c, const String *symname, int inclass) { String *e = NewStringEmpty(); String *en = NewStringEmpty(); String *ec = NewStringEmpty(); String *symname_stripped = SwigType_templateprefix(symname); String *n_name_stripped = SwigType_templateprefix(Getattr(n, "name")); String *c_name_stripped = SwigType_templateprefix(Getattr(c, "name")); int redefined = Swig_need_redefined_warn(n, c, inclass); String *n_name_decl = Swig_name_decl(n); String *c_name_decl = Swig_name_decl(c); if (redefined) { Printf(en, "Redefinition of identifier '%s'", symname_stripped); Printf(ec, "previous definition of '%s'", symname_stripped); } else { Printf(en, "Redundant redeclaration of identifier '%s'", symname_stripped); Printf(ec, "previous declaration of '%s'", symname_stripped); } if (!Equal(symname_stripped, n_name_stripped)) Printf(en, " (Renamed from '%s')", SwigType_namestr(n_name_stripped)); if (!Equal(symname_stripped, c_name_stripped)) Printf(ec, " (Renamed from '%s')", SwigType_namestr(c_name_stripped)); if (!Equal(n_name_stripped, n_name_decl)) Printf(en, " as %s", n_name_decl); if (!Equal(c_name_stripped, c_name_decl)) Printf(ec, " as %s", c_name_decl); Printf(en, " ignored,"); Printf(ec, "."); SWIG_WARN_NODE_BEGIN(n); if (redefined) { Swig_warning(WARN_PARSE_REDEFINED, Getfile(n), Getline(n), "%s\n", en); Swig_warning(WARN_PARSE_REDEFINED, Getfile(c), Getline(c), "%s\n", ec); } else if (!Strstr(Getattr(n, "storage"), "friend") && !Strstr(Getattr(c, "storage"), "friend")) { Swig_warning(WARN_PARSE_REDUNDANT, Getfile(n), Getline(n), "%s\n", en); Swig_warning(WARN_PARSE_REDUNDANT, Getfile(c), Getline(c), "%s\n", ec); } SWIG_WARN_NODE_END(n); Printf(e, "%s:%d:%s\n%s:%d:%s\n", Getfile(n), Getline(n), en, Getfile(c), Getline(c), ec); Setattr(n, "error", e); Delete(c_name_decl); Delete(n_name_decl); Delete(symname_stripped); Delete(c_name_stripped); Delete(n_name_stripped); Delete(e); Delete(en); Delete(ec); } /* ----------------------------------------------------------------------------- * symbol_lookup() * * Internal function to handle fully qualified symbol table lookups. This * works from the symbol table supplied in symtab and unwinds its way out * towards the global scope. * * This function operates in the C namespace, not the target namespace. * * The checkfunc function is an optional callback that can be used to verify a particular * symbol match. This is only used in some of the more exotic parts of SWIG. For instance, * verifying that a class hierarchy implements all pure virtual methods. * ----------------------------------------------------------------------------- */ static Node *_symbol_lookup(const String *name, Symtab *symtab, Node *(*checkfunc) (Node *n)) { Node *n; List *inherit; Hash *sym = Getattr(symtab, "csymtab"); if (Getmark(symtab)) return 0; Setmark(symtab, 1); n = Getattr(sym, name); #ifdef SWIG_DEBUG Printf(stderr, "symbol_look %s %p %p %s\n", name, n, symtab, Getattr(symtab, "name")); #endif if (n) { /* if checkfunc is defined. Call it to determine a match */ if (checkfunc) { Node *cn = checkfunc(n); if (cn) { Setmark(symtab, 0); /* Note that checkfunc can return n != cn, where cn could be a node further down the csym linked list starting at n */ return cn; } } else { Setmark(symtab, 0); return n; } } if (!n && SwigType_istemplate(name)) { String *dname = 0; Setmark(symtab, 0); dname = Swig_symbol_template_deftype(name, symtab); if (!Equal(dname, name)) { n = _symbol_lookup(dname, symtab, checkfunc); } Delete(dname); if (n) return n; Setmark(symtab, 1); } inherit = Getattr(symtab, "inherit"); if (inherit && use_inherit) { int i, len; len = Len(inherit); for (i = 0; i < len; i++) { n = _symbol_lookup(name, Getitem(inherit, i), checkfunc); if (n) { Setmark(symtab, 0); return n; } } } Setmark(symtab, 0); return 0; } static Node *symbol_lookup(const_String_or_char_ptr name, Symtab *symtab, Node *(*checkfunc) (Node *n)) { Node *n = 0; if (DohCheck(name)) { n = _symbol_lookup(name, symtab, checkfunc); } else { String *sname = NewString(name); n = _symbol_lookup(sname, symtab, checkfunc); Delete(sname); } return n; } /* ----------------------------------------------------------------------------- * symbol_lookup_qualified() * ----------------------------------------------------------------------------- */ static Node *symbol_lookup_qualified(const_String_or_char_ptr name, Symtab *symtab, const String *prefix, int local, Node *(*checkfunc) (Node *n)) { /* This is a little funky, we search by fully qualified names */ if (!symtab) return 0; if (!prefix) { Node *n; String *bname = 0; String *prefix = 0; Swig_scopename_split(name, &prefix, &bname); n = symbol_lookup_qualified(bname, symtab, prefix, local, checkfunc); Delete(bname); Delete(prefix); return n; } else { Symtab *st; Node *n = 0; /* Make qualified name of current scope */ String *qalloc = 0; String *qname = Swig_symbol_qualifiedscopename(symtab); const String *cqname; if (qname) { if (Len(qname)) { if (prefix && Len(prefix)) { Printv(qname, "::", prefix, NIL); } } else { Append(qname, prefix); } qalloc = qname; cqname = qname; } else { cqname = prefix; } st = Getattr(symtabs, cqname); /* Found a scope match */ if (st) { if (!name) { if (qalloc) Delete(qalloc); return st; } n = symbol_lookup(name, st, checkfunc); } if (qalloc) Delete(qalloc); if (!n) { if (!local) { Node *pn = Getattr(symtab, "parentNode"); if (pn) n = symbol_lookup_qualified(name, pn, prefix, local, checkfunc); /* Check inherited scopes */ if (!n) { List *inherit = Getattr(symtab, "inherit"); if (inherit && use_inherit) { int i, len; len = Len(inherit); for (i = 0; i < len; i++) { Node *prefix_node = symbol_lookup(prefix, Getitem(inherit, i), checkfunc); if (prefix_node) { Node *prefix_symtab = Getattr(prefix_node, "symtab"); if (prefix_symtab) { n = symbol_lookup(name, prefix_symtab, checkfunc); break; } } } } } } else { n = 0; } } return n; } } /* ----------------------------------------------------------------------------- * Swig_symbol_clookup() * * Look up a symbol in the symbol table. This uses the C name, not scripting * names. Note: If we come across a using declaration, we follow it to * to get the real node. Any using directives are also followed (but this is * implemented in symbol_lookup()). * ----------------------------------------------------------------------------- */ Node *Swig_symbol_clookup(const_String_or_char_ptr name, Symtab *n) { Hash *hsym = 0; Node *s = 0; if (!n) { hsym = current_symtab; } else { if (!Checkattr(n, "nodeType", "symboltable")) { n = Getattr(n, "sym:symtab"); } assert(n); if (n) { hsym = n; } } if (Swig_scopename_check(name)) { char *cname = Char(name); if (strncmp(cname, "::", 2) == 0) { String *nname = NewString(cname + 2); if (Swig_scopename_check(nname)) { s = symbol_lookup_qualified(nname, global_scope, 0, 0, 0); } else { s = symbol_lookup(nname, global_scope, 0); } Delete(nname); } else { String *prefix = Swig_scopename_prefix(name); if (prefix) { s = symbol_lookup_qualified(name, hsym, 0, 0, 0); Delete(prefix); if (!s) { return 0; } } } } if (!s) { while (hsym) { s = symbol_lookup(name, hsym, 0); if (s) break; hsym = Getattr(hsym, "parentNode"); if (!hsym) break; } } /* Check if s is a 'using' node */ while (s && Checkattr(s, "nodeType", "using")) { if (Getattr(s, "csym:nextSibling")) { /* overloaded using declarations and method declarations - don't chase the using declarations up the inheritance hierarchy */ break; } else { String *uname = Getattr(s, "uname"); Symtab *un = Getattr(s, "sym:symtab"); Node *ss = (!Equal(name, uname) || (un != n)) ? Swig_symbol_clookup(uname, un) : 0; /* avoid infinity loop */ if (!ss) { SWIG_WARN_NODE_BEGIN(s); Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", SwigType_namestr(uname)); SWIG_WARN_NODE_END(s); } s = ss; } } return s; } /* ----------------------------------------------------------------------------- * Swig_symbol_clookup_check() * * This function is identical to Swig_symbol_clookup() except that it * accepts a callback function that is invoked to determine a symbol match. * The purpose of this function is to support complicated algorithms that need * to examine multiple definitions of the same symbol that might appear in an * inheritance hierarchy. * ----------------------------------------------------------------------------- */ Node *Swig_symbol_clookup_check(const_String_or_char_ptr name, Symtab *n, Node *(*checkfunc) (Node *n)) { Hash *hsym = 0; Node *s = 0; if (!n) { hsym = current_symtab; } else { if (!Checkattr(n, "nodeType", "symboltable")) { n = Getattr(n, "sym:symtab"); } assert(n); if (n) { hsym = n; } } if (Swig_scopename_check(name)) { char *cname = Char(name); if (strncmp(cname, "::", 2) == 0) { String *nname = NewString(cname + 2); if (Swig_scopename_check(nname)) { s = symbol_lookup_qualified(nname, global_scope, 0, 0, checkfunc); } else { s = symbol_lookup(nname, global_scope, checkfunc); } Delete(nname); } else { String *prefix = Swig_scopename_prefix(name); if (prefix) { s = symbol_lookup_qualified(name, hsym, 0, 0, checkfunc); Delete(prefix); if (!s) { return 0; } } } } if (!s) { while (hsym) { s = symbol_lookup(name, hsym, checkfunc); if (s) break; hsym = Getattr(hsym, "parentNode"); if (!hsym) break; } } /* Check if s is a 'using' node */ while (s && Checkattr(s, "nodeType", "using")) { if (Getattr(s, "csym:nextSibling")) { /* overloaded using declarations and method declarations - don't chase the using declarations up the inheritance hierarchy */ break; } else { String *uname = Getattr(s, "uname"); Symtab *un = Getattr(s, "sym:symtab"); Node *ss = Swig_symbol_clookup_check(uname, un, checkfunc); if (!ss && !checkfunc) { SWIG_WARN_NODE_BEGIN(s); Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", SwigType_namestr(uname)); SWIG_WARN_NODE_END(s); } s = ss; } } return s; } /* ----------------------------------------------------------------------------- * Swig_symbol_clookup_local() * * Same as Swig_symbol_clookup but parent nodes are not searched, that is, just * this symbol table is searched. * ----------------------------------------------------------------------------- */ Node *Swig_symbol_clookup_local(const_String_or_char_ptr name, Symtab *n) { Hash *hsym; Node *s = 0; if (!n) { hsym = current_symtab; } else { if (!Checkattr(n, "nodeType", "symboltable")) { n = Getattr(n, "sym:symtab"); } assert(n); hsym = n; } if (Swig_scopename_check(name)) { char *cname = Char(name); if (strncmp(cname, "::", 2) == 0) { String *nname = NewString(cname + 2); if (Swig_scopename_check(nname)) { s = symbol_lookup_qualified(nname, global_scope, 0, 0, 0); } else { s = symbol_lookup(nname, global_scope, 0); } Delete(nname); } else { s = symbol_lookup_qualified(name, hsym, 0, 0, 0); } } if (!s) { s = symbol_lookup(name, hsym, 0); } /* Check if s is a 'using' node */ while (s && Checkattr(s, "nodeType", "using")) { if (Getattr(s, "csym:nextSibling")) { /* overloaded using declarations and method declarations - don't chase the using declarations up the inheritance hierarchy */ break; } else { String *uname = Getattr(s, "uname"); Symtab *un = Getattr(s, "sym:symtab"); Node *ss = Swig_symbol_clookup_local(uname, un); if (!ss) { SWIG_WARN_NODE_BEGIN(s); Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", SwigType_namestr(uname)); SWIG_WARN_NODE_END(s); } s = ss; } } return s; } /* ----------------------------------------------------------------------------- * Swig_symbol_clookup_local_check() * ----------------------------------------------------------------------------- */ Node *Swig_symbol_clookup_local_check(const_String_or_char_ptr name, Symtab *n, Node *(*checkfunc) (Node *)) { Hash *hsym; Node *s = 0; if (!n) { hsym = current_symtab; } else { if (!Checkattr(n, "nodeType", "symboltable")) { n = Getattr(n, "sym:symtab"); } assert(n); hsym = n; } if (Swig_scopename_check(name)) { char *cname = Char(name); if (strncmp(cname, "::", 2) == 0) { String *nname = NewString(cname + 2); if (Swig_scopename_check(nname)) { s = symbol_lookup_qualified(nname, global_scope, 0, 0, checkfunc); } else { s = symbol_lookup(nname, global_scope, checkfunc); } Delete(nname); } else { s = symbol_lookup_qualified(name, hsym, 0, 0, checkfunc); } } if (!s) { s = symbol_lookup(name, hsym, checkfunc); } /* Check if s is a 'using' node */ while (s && Checkattr(s, "nodeType", "using")) { if (Getattr(s, "csym:nextSibling")) { /* overloaded using declarations and method declarations - don't chase the using declarations up the inheritance hierarchy */ break; } else { String *uname = Getattr(s, "uname"); Symtab *un = Getattr(s, "sym:symtab"); Node *ss = Swig_symbol_clookup_local_check(uname, un, checkfunc); if (!ss && !checkfunc) { SWIG_WARN_NODE_BEGIN(s); Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(s), Getline(s), "Nothing known about '%s'.\n", SwigType_namestr(uname)); SWIG_WARN_NODE_END(s); } s = ss; } } return s; } /* ----------------------------------------------------------------------------- * Swig_symbol_clookup_no_inherit() * * Symbol lookup like Swig_symbol_clookup but does not follow using directives. * Using declarations are followed. * ----------------------------------------------------------------------------- */ Node *Swig_symbol_clookup_no_inherit(const_String_or_char_ptr name, Symtab *n) { Node *s = 0; assert(use_inherit==1); use_inherit = 0; s = Swig_symbol_clookup(name, n); use_inherit = 1; return s; } /* ----------------------------------------------------------------------------- * Swig_symbol_cscope() * * Look up a scope name. * ----------------------------------------------------------------------------- */ Symtab *Swig_symbol_cscope(const_String_or_char_ptr name, Symtab *symtab) { char *cname = Char(name); if (strncmp(cname, "::", 2) == 0) return symbol_lookup_qualified(0, global_scope, name, 0, 0); return symbol_lookup_qualified(0, symtab, name, 0, 0); } /* ----------------------------------------------------------------------------- * Swig_symbol_remove() * * Remove a symbol. If the symbol is an overloaded function and the symbol removed * is not the last in the list of overloaded functions, then the overloaded * names (sym:overname attribute) are changed to start from zero, eg __SWIG_0. * ----------------------------------------------------------------------------- */ void Swig_symbol_remove(Node *n) { Symtab *symtab; String *symname; Node *symprev; Node *symnext; Node *fixovername = 0; symtab = Getattr(n, "sym:symtab"); /* Get symbol table object */ symtab = Getattr(symtab, "symtab"); /* Get actual hash table of symbols */ symname = Getattr(n, "sym:name"); symprev = Getattr(n, "sym:previousSibling"); symnext = Getattr(n, "sym:nextSibling"); /* If previous symbol, just fix the links */ if (symprev) { if (symnext) { Setattr(symprev, "sym:nextSibling", symnext); fixovername = symprev; /* fix as symbol to remove is somewhere in the middle of the linked list */ } else { Delattr(symprev, "sym:nextSibling"); } } else { /* If no previous symbol, see if there is a next symbol */ if (symnext) { Setattr(symtab, symname, symnext); fixovername = symnext; /* fix as symbol to remove is at head of linked list */ } else { if (symname) Delattr(symtab, symname); } } if (symnext) { if (symprev) { Setattr(symnext, "sym:previousSibling", symprev); } else { Delattr(symnext, "sym:previousSibling"); } } Delattr(n, "sym:symtab"); Delattr(n, "sym:previousSibling"); Delattr(n, "sym:nextSibling"); Delattr(n, "csym:nextSibling"); Delattr(n, "sym:overname"); Delattr(n, "csym:previousSibling"); Delattr(n, "sym:overloaded"); Swig_symbol_fix_overname(fixovername); } /* ----------------------------------------------------------------------------- * Swig_symbol_fix_overname() * * Fix/update the sym:overname attribute for all the overloaded names. * The sym:overname attributes are changed to start from zero, eg __SWIG_0. * Call this when the linked lists for overloaded methods are modified. * ----------------------------------------------------------------------------- */ void Swig_symbol_fix_overname(Node *n) { if (n) { Node *nn = n; Node *head = n; int pn = 0; /* find head of linked list */ while (nn) { head = nn; nn = Getattr(nn, "sym:previousSibling"); } /* adjust all the sym:overname strings to start from 0 and increment by one */ nn = head; while (nn) { String *overname = NewStringf("__SWIG_%d", pn); Delattr(nn, "sym:overname"); Setattr(nn, "sym:overname", overname); Delete(overname); pn++; nn = Getattr(nn, "sym:nextSibling"); } } } /* ----------------------------------------------------------------------------- * Swig_symbol_qualified() * * Return the qualified name of a symbol * ----------------------------------------------------------------------------- */ String *Swig_symbol_qualified(Node *n) { Hash *symtab; if (Checkattr(n, "nodeType", "symboltable")) { symtab = n; } else { symtab = Getattr(n, "sym:symtab"); } if (!symtab) return NewStringEmpty(); #ifdef SWIG_DEBUG Printf(stderr, "symbol_qscope %s %p %s\n", Getattr(n, "name"), symtab, Getattr(symtab, "name")); #endif return Swig_symbol_qualifiedscopename(symtab); } /* ----------------------------------------------------------------------------- * Swig_symbol_isoverloaded() * * Check if a symbol is overloaded. Returns the first symbol if so. * ----------------------------------------------------------------------------- */ Node *Swig_symbol_isoverloaded(Node *n) { return Getattr(n, "sym:overloaded"); } /* ----------------------------------------------------------------------------- * symbol_template_qualify() * * Internal function to create a fully qualified type name for templates * ----------------------------------------------------------------------------- */ /* This cache produces problems with OSS, don't active it */ /* #define SWIG_TEMPLATE_QUALIFY_CACHE */ static SwigType *symbol_template_qualify(const SwigType *e, Symtab *st) { String *tprefix, *tsuffix; SwigType *qprefix; String *targs; List *targslist; Node *tempn; Symtab *tscope; Iterator ti; #ifdef SWIG_TEMPLATE_QUALIFY_CACHE static Hash *qualify_cache = 0; String *scopetype = st ? NewStringf("%s::%s", Getattr(st, "name"), e) : NewStringf("%s::%s", Swig_symbol_getscopename(), e); if (!qualify_cache) { qualify_cache = NewHash(); } if (scopetype) { String *cres = Getattr(qualify_cache, scopetype); if (cres) { Delete(scopetype); return Copy(cres); } } #endif tprefix = SwigType_templateprefix(e); tsuffix = SwigType_templatesuffix(e); qprefix = Swig_symbol_type_qualify(tprefix, st); targs = SwigType_templateargs(e); targslist = SwigType_parmlist(targs); tempn = Swig_symbol_clookup_local(tprefix, st); tscope = tempn ? Getattr(tempn, "sym:symtab") : 0; Append(qprefix, "<("); for (ti = First(targslist); ti.item;) { String *vparm; /* TODO: the logic here should be synchronised with that in SwigType_typedef_qualified() */ /* TODO: ti.item might be a non-type parameter possibly within (), eg: (std::is_integral_v<(A)>||std::is_same_v<(A,node_t)>) */ String *qparm = Swig_symbol_type_qualify(ti.item, st); if (tscope && (tscope != st)) { String *ty = Swig_symbol_type_qualify(qparm, tscope); Delete(qparm); qparm = ty; } vparm = Swig_symbol_template_param_eval(qparm, st); Append(qprefix, vparm); ti = Next(ti); if (ti.item) { Putc(',', qprefix); } Delete(qparm); Delete(vparm); } Append(qprefix, ")>"); Append(qprefix, tsuffix); Delete(tprefix); Delete(tsuffix); Delete(targs); Delete(targslist); #ifdef SWIG_DEBUG Printf(stderr, "symbol_temp_qual %s %s\n", e, qprefix); #endif #ifdef SWIG_TEMPLATE_QUALIFY_CACHE Setattr(qualify_cache, scopetype, qprefix); Delete(scopetype); #endif return qprefix; } static Node *symbol_no_constructor(Node *n) { return Checkattr(n, "nodeType", "constructor") ? 0 : n; } static Node *symbol_is_template(Node *n) { return Checkattr(n, "nodeType", "template") ? n : 0; } /* ----------------------------------------------------------------------------- * Swig_symbol_type_qualify() * * Create a fully qualified type name * Note: Does not resolve a constructor if passed in as the 'type'. * ----------------------------------------------------------------------------- */ SwigType *Swig_symbol_type_qualify(const SwigType *t, Symtab *st) { List *elements; String *result = NewStringEmpty(); int i, len; char *c = Char(t); if (strncmp(c, "::", 2) == 0) { Append(result, t); return result; } elements = SwigType_split(t); len = Len(elements); for (i = 0; i < len; i++) { String *e = Getitem(elements, i); if (SwigType_issimple(e)) { /* Note: the unary scope operator (::) is being removed from the template parameters here. */ Node *n = Swig_symbol_clookup_check(e, st, symbol_no_constructor); if (n) { String *name = Getattr(n, "name"); Clear(e); Append(e, name); #ifdef SWIG_DEBUG Printf(stderr, "symbol_qual_ei %d %s %s %p\n", i, name, e, st); #endif if (!Swig_scopename_check(name)) { String *qname = Swig_symbol_qualified(n); if (qname && Len(qname)) { Insert(e, 0, "::"); Insert(e, 0, qname); } #ifdef SWIG_DEBUG Printf(stderr, "symbol_qual_sc %d %s %s %p\n", i, qname, e, st); #endif Delete(qname); } } else if (SwigType_istemplate(e)) { SwigType *ty = symbol_template_qualify(e, st); Clear(e); Append(e, ty); Delete(ty); } if (strncmp(Char(e), "::", 2) == 0) { Delitem(e, 0); Delitem(e, 0); } Append(result, e); } else if (SwigType_isfunction(e)) { List *parms = SwigType_parmlist(e); String *s = NewString("f("); Iterator pi = First(parms); while (pi.item) { String *pf = Swig_symbol_type_qualify(pi.item, st); Append(s, pf); pi = Next(pi); if (pi.item) { Append(s, ","); } Delete(pf); } Append(s, ")."); Append(result, s); Delete(parms); Delete(s); } else { Append(result, e); } } Delete(elements); #ifdef SWIG_DEBUG Printf(stderr, "symbol_qualify %s %s %p %s\n", t, result, st, st ? Getattr(st, "name") : 0); #endif return result; } /* ----------------------------------------------------------------------------- * Swig_symbol_template_reduce() * Resolves template parameter types * For example: * typedef int Int; * typedef Int Integer; * with input: * Foo<(Int,Integer)> * returns: * Foo<(int,int)> * ----------------------------------------------------------------------------- */ static SwigType *Swig_symbol_template_reduce(SwigType *qt, Symtab *ntab) { Parm *p; String *templateargs = SwigType_templateargs(qt); List *parms = SwigType_parmlist(templateargs); Iterator pi = First(parms); String *tprefix = SwigType_templateprefix(qt); String *tsuffix = SwigType_templatesuffix(qt); String *qprefix = SwigType_typedef_qualified(tprefix); Append(qprefix, "<("); while ((p = pi.item)) { String *np; String *tp = Swig_symbol_typedef_reduce(p, ntab); String *qp = Swig_symbol_type_qualify(tp, ntab); Node *n = Swig_symbol_clookup(qp, ntab); if (n) { String *qual = Swig_symbol_qualified(n); np = Copy(Getattr(n, "name")); Delete(tp); tp = np; if (qual && Len(qual)) { Insert(np, 0, "::"); Insert(np, 0, qual); } Delete(qual); } else { np = qp; } Append(qprefix, np); pi = Next(pi); if (pi.item) { Append(qprefix, ","); } Delete(qp); Delete(tp); } Append(qprefix, ")>"); Append(qprefix, tsuffix); Delete(parms); Delete(tprefix); Delete(tsuffix); Delete(templateargs); return qprefix; } /* ----------------------------------------------------------------------------- * Swig_symbol_typedef_reduce() * * Chase a typedef through symbol tables looking for a match. * ----------------------------------------------------------------------------- */ SwigType *Swig_symbol_typedef_reduce(const SwigType *ty, Symtab *tab) { SwigType *prefix, *base; Node *n; String *nt; base = SwigType_base(ty); prefix = SwigType_prefix(ty); n = Swig_symbol_clookup(base, tab); if (!n) { if (SwigType_istemplate(base)) { SwigType *qt = Swig_symbol_template_reduce(base, tab); Append(prefix, qt); Delete(qt); #ifdef SWIG_DEBUG Printf(stderr, "symbol_reduce (a) %s %s\n", ty, prefix); #endif Delete(base); return prefix; } else { Delete(prefix); #ifdef SWIG_DEBUG Printf(stderr, "symbol_reduce (b) %s %s\n", ty, ty); #endif return Copy(ty); } } nt = Getattr(n, "nodeType"); if (Equal(nt, "using")) { String *uname = Getattr(n, "uname"); if (uname) { n = Swig_symbol_clookup(base, Getattr(n, "sym:symtab")); if (!n) { Delete(base); Delete(prefix); #ifdef SWIG_DEBUG Printf(stderr, "symbol_reduce (c) %s %s\n", ty, ty); #endif return Copy(ty); } } } if (Equal(nt, "cdecl")) { String *storage = Getattr(n, "storage"); if (storage && (Equal(storage, "typedef"))) { SwigType *decl; SwigType *rt; SwigType *qt; Symtab *ntab; SwigType *nt = Copy(Getattr(n, "type")); /* Fix for case 'typedef struct Hello hello;' */ { const char *dclass[3] = { "struct ", "union ", "class " }; int i; char *c = Char(nt); for (i = 0; i < 3; i++) { if (strstr(c, dclass[i]) == c) { Replace(nt, dclass[i], "", DOH_REPLACE_FIRST); } } } decl = Getattr(n, "decl"); if (decl) { SwigType_push(nt, decl); } SwigType_push(nt, prefix); Delete(base); Delete(prefix); ntab = Getattr(n, "sym:symtab"); rt = Swig_symbol_typedef_reduce(nt, ntab); qt = Swig_symbol_type_qualify(rt, ntab); if (SwigType_istemplate(qt)) { SwigType *qtr = Swig_symbol_template_reduce(qt, ntab); Delete(qt); qt = qtr; } Delete(nt); Delete(rt); #ifdef SWIG_DEBUG Printf(stderr, "symbol_reduce (d) %s %s\n", qt, ty); #endif return qt; } } Delete(base); Delete(prefix); #ifdef SWIG_DEBUG Printf(stderr, "symbol_reduce (e) %s %s\n", ty, ty); #endif return Copy(ty); } /* ----------------------------------------------------------------------------- * Swig_symbol_string_qualify() * * This function takes a string and looks for identifiers. Identifiers are * then qualified according to scope rules. This function is used in a number * of settings including expression evaluation, scoping of conversion operators, * and so forth. * ----------------------------------------------------------------------------- */ String *Swig_symbol_string_qualify(String *s, Symtab *st) { int have_id = 0; String *id = NewStringEmpty(); String *r = NewStringEmpty(); char *c = Char(s); int first_char = 1; while (*c) { if (isalpha((int) *c) || (*c == '_') || (*c == ':') || (*c == '~' && first_char) || (isdigit((int) *c) && !first_char)) { Putc(*c, id); have_id = 1; } else { if (have_id) { String *qid = Swig_symbol_type_qualify(id, st); Append(r, qid); Clear(id); Delete(qid); have_id = 0; } Putc(*c, r); } first_char = (*c == ':'); c++; } if (have_id) { String *qid = Swig_symbol_type_qualify(id, st); Append(r, qid); Delete(qid); } Delete(id); return r; } /* ----------------------------------------------------------------------------- * Swig_symbol_template_defargs() * * Apply default arg from generic template default args * Returns a parameter list which contains missing default arguments (if any) * Note side effects: parms will also contain the extra parameters in its list * (but only if non-zero). * ----------------------------------------------------------------------------- */ ParmList *Swig_symbol_template_defargs(Parm *parms, Parm *targs, Symtab *tscope, Symtab *tsdecl) { ParmList *expandedparms = parms; if (Len(parms) < Len(targs)) { Parm *lp = parms; Parm *p = lp; Parm *tp = targs; while (p && tp) { p = nextSibling(p); tp = nextSibling(tp); if (p) lp = p; } while (tp) { String *value = Getattr(tp, "value"); if (value) { Parm *cp; Parm *ta = targs; Parm *p = parms; SwigType *nt = Swig_symbol_string_qualify(value, tsdecl); SwigType *ntq = 0; #ifdef SWIG_DEBUG Printf(stderr, "value %s %s %s\n", value, nt, tsdecl ? Getattr(tsdecl, "name") : tsdecl); #endif while (p && ta) { String *name = Getattr(ta, "name"); String *pvalue = Getattr(p, "value"); String *value = pvalue ? pvalue : Getattr(p, "type"); String *ttq = Swig_symbol_type_qualify(value, tscope); /* value = SwigType_typedef_resolve_all(value); */ Replaceid(nt, name, ttq); p = nextSibling(p); ta = nextSibling(ta); Delete(ttq); } ntq = Swig_symbol_type_qualify(nt, tsdecl); if (SwigType_istemplate(ntq)) { String *ty = Swig_symbol_template_deftype(ntq, tscope); Delete(ntq); ntq = ty; } cp = NewParmWithoutFileLineInfo(ntq, 0); if (lp) { set_nextSibling(lp, cp); Delete(cp); } else { expandedparms = cp; } lp = cp; tp = nextSibling(tp); Delete(nt); Delete(ntq); } else { tp = 0; } } } return expandedparms; } /* ----------------------------------------------------------------------------- * Swig_symbol_template_deftype() * * Apply default args to generic template type * Return input type with template args expanded to include default template args * ----------------------------------------------------------------------------- */ #define SWIG_TEMPLATE_DEFTYPE_CACHE SwigType *Swig_symbol_template_deftype(const SwigType *type, Symtab *tscope) { String *result = NewStringEmpty(); List *elements = SwigType_split(type); int len = Len(elements); int i; #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE static Hash *s_cache = 0; Hash *scope_cache; /* The lookup depends on the current scope and potential namespace qualification. Looking up x in namespace y is not the same as looking up x::y in outer scope. -> we use a 2-level hash: first scope and then symbol. */ String *scope_name = tscope ? Swig_symbol_qualifiedscopename(tscope) : Swig_symbol_qualifiedscopename(current_symtab); String *type_name = tscope ? NewStringf("%s::%s", Getattr(tscope, "name"), type) : NewStringf("%s::%s", Swig_symbol_getscopename(), type); if (!scope_name) scope_name = NewString("::"); if (!s_cache) { s_cache = NewHash(); } scope_cache = Getattr(s_cache, scope_name); if (scope_cache) { String *cres = Getattr(scope_cache, type_name); if (cres) { Append(result, cres); #ifdef SWIG_DEBUG Printf(stderr, "cached deftype %s(%s) -> %s\n", type, scope_name, result); #endif Delete(type_name); Delete(scope_name); return result; } } else { scope_cache = NewHash(); Setattr(s_cache, scope_name, scope_cache); Delete(scope_name); } #endif #ifdef SWIG_DEBUG Printf(stderr, "finding deftype %s\n", type); #endif for (i = 0; i < len; i++) { String *e = Getitem(elements, i); if (SwigType_isfunction(e)) { String *s = NewString("f("); List *parms = SwigType_parmlist(e); Iterator pi = First(parms); while (pi.item) { String *pf = SwigType_istemplate(e) ? Swig_symbol_template_deftype(pi.item, tscope) : Swig_symbol_type_qualify(pi.item, tscope); Append(s, pf); pi = Next(pi); if (pi.item) { Append(s, ","); } Delete(pf); } Append(s, ")."); Append(result, s); Delete(s); Delete(parms); } else if (SwigType_istemplate(e)) { String *prefix = SwigType_prefix(e); String *base = SwigType_base(e); String *tprefix = SwigType_templateprefix(base); String *targs = SwigType_templateargs(base); String *tsuffix = SwigType_templatesuffix(base); ParmList *tparms = SwigType_function_parms(targs, 0); Node *tempn = Swig_symbol_clookup_local_check(tprefix, tscope, symbol_is_template); if (!tempn && tsuffix && Len(tsuffix)) { tempn = Swig_symbol_clookup_check(tprefix, 0, symbol_is_template); } #ifdef SWIG_DEBUG Printf(stderr, "deftype type %s %s %d\n", e, tprefix, (long) tempn); #endif if (tempn) { ParmList *tnargs = Getattr(tempn, "templateparms"); ParmList *expandedparms; Parm *p; Symtab *tsdecl = Getattr(tempn, "sym:symtab"); #ifdef SWIG_DEBUG Printf(stderr, "deftype type %s %s %s\n", tprefix, targs, tsuffix); #endif Append(tprefix, "<("); expandedparms = Swig_symbol_template_defargs(tparms, tnargs, tscope, tsdecl); p = expandedparms; tscope = tsdecl; while (p) { SwigType *ptype = Getattr(p, "type"); SwigType *ttr = ptype ? ptype : Getattr(p, "value"); SwigType *ttf = Swig_symbol_type_qualify(ttr, tscope); SwigType *ttq = Swig_symbol_template_param_eval(ttf, tscope); #ifdef SWIG_DEBUG Printf(stderr, "arg type %s\n", ttq); #endif if (SwigType_istemplate(ttq)) { SwigType *ttd = Swig_symbol_template_deftype(ttq, tscope); Delete(ttq); ttq = ttd; #ifdef SWIG_DEBUG Printf(stderr, "arg deftype %s\n", ttq); #endif } Append(tprefix, ttq); p = nextSibling(p); if (p) Putc(',', tprefix); Delete(ttf); Delete(ttq); } Append(tprefix, ")>"); Append(tprefix, tsuffix); Append(prefix, tprefix); #ifdef SWIG_DEBUG Printf(stderr, "deftype %s %s \n", type, tprefix); #endif Append(result, prefix); } else { Append(result, e); } Delete(prefix); Delete(base); Delete(tprefix); Delete(tsuffix); Delete(targs); Delete(tparms); } else { Append(result, e); } } Delete(elements); #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE Setattr(scope_cache, type_name, result); Delete(type_name); #endif return result; } SwigType *Swig_symbol_template_param_eval(const SwigType *p, Symtab *symtab) { String *value = Copy(p); Node *lastnode = 0; while (1) { Node *n = Swig_symbol_clookup(value, symtab); if (n == lastnode) break; lastnode = n; if (n) { String *nt = Getattr(n, "nodeType"); if (Equal(nt, "enumitem")) { /* An enum item. Generate a fully qualified name */ String *qn = Swig_symbol_qualified(n); if (qn && Len(qn)) { Append(qn, "::"); Append(qn, Getattr(n, "name")); Delete(value); value = qn; continue; } else { Delete(qn); break; } } else if ((Equal(nt, "cdecl"))) { String *nv = Getattr(n, "value"); if (nv) { Delete(value); value = Copy(nv); continue; } } } break; } return value; } /* ----------------------------------------------------------------------------- * Swig_symbol_isvalid() * * Checks that s is a valid C symbol * ----------------------------------------------------------------------------- */ int Swig_symbol_isvalid(const String *s) { int valid = 0; const char *c = Char(s); if (c) { valid = isalpha((int)*c) || (*c == '_'); while (valid && *++c) { valid = isalnum((int)*c) || (*c == '_'); } } return valid; } swig-4.4.0/Source/Swig/naming.c0000664000175000017500000015574015075443613016162 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * naming.c * * Functions for generating various kinds of names during code generation. * * Swig_name_register is used to register a format string for generating names. * The format string makes use of the following format specifiers: * * %c - class name is substituted * %f - function name is substituted * %m - member name is substituted * %n - namespace is substituted * %v - variable name is substituted * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" #include /* Hash table containing naming data */ static Hash *naming_hash = 0; #if 0 #define SWIG_DEBUG #endif /* ----------------------------------------------------------------------------- * Swig_name_register() * * Register a new naming format. * ----------------------------------------------------------------------------- */ void Swig_name_register(const_String_or_char_ptr method, const_String_or_char_ptr format) { if (!naming_hash) naming_hash = NewHash(); Setattr(naming_hash, method, format); } void Swig_name_unregister(const_String_or_char_ptr method) { if (naming_hash) { Delattr(naming_hash, method); } } /* Return naming format for the specified method or the default format if none was explicitly registered */ static String* get_naming_format_for(const char *method, const char *def_format) { String* f = naming_hash ? Getattr(naming_hash, method) : NULL; return f ? Copy(f) : NewString(def_format); } static int name_mangle(String *r) { char *c; int special; special = 0; Replaceall(r, "::", "_"); c = Char(r); while (*c) { if (!isalnum((int) *c) && (*c != '_')) { special = 1; switch (*c) { case '+': *c = 'a'; break; case '-': *c = 's'; break; case '*': *c = 'm'; break; case '/': *c = 'd'; break; case '<': *c = 'l'; break; case '>': *c = 'g'; break; case '=': *c = 'e'; break; case ',': *c = 'c'; break; case '(': *c = 'p'; break; case ')': *c = 'P'; break; case '[': *c = 'b'; break; case ']': *c = 'B'; break; case '^': *c = 'x'; break; case '&': *c = 'A'; break; case '|': *c = 'o'; break; case '~': *c = 'n'; break; case '!': *c = 'N'; break; case '%': *c = 'M'; break; case '.': *c = 'f'; break; case '?': *c = 'q'; break; default: *c = '_'; break; } } c++; } if (special) Append(r, "___"); return special; } /* ----------------------------------------------------------------------------- * replace_nspace() * * Mangles in the namespace from nspace by replacing %n in name if nspace feature required. * ----------------------------------------------------------------------------- */ static void replace_nspace(String *name, const_String_or_char_ptr nspace) { if (nspace) { String *namspace = NewStringf("%s_", nspace); Replaceall(namspace, NSPACE_SEPARATOR, "_"); Replace(name, "%n", namspace, DOH_REPLACE_ANY); Delete(namspace); } else { Replace(name, "%n", "", DOH_REPLACE_ANY); } } /* ----------------------------------------------------------------------------- * Swig_name_mangle_type() * * Same as Swig_name_mangle_string, but converting internal SwigType * to a human * readable string of the type (for templates). Simplifies a type that is a * template to the default template if possible. * ----------------------------------------------------------------------------- */ String *Swig_name_mangle_type(const SwigType *s) { String *mangled = 0; String *b = Copy(s); if (SwigType_istemplate(b)) { String *st = Swig_symbol_template_deftype(b, 0); String *sq = Swig_symbol_type_qualify(st, 0); String *t = SwigType_namestr(sq); Delete(st); Delete(sq); Delete(b); b = t; } mangled = Swig_name_mangle_string(b); Delete(b); return mangled; } /* ----------------------------------------------------------------------------- * Swig_name_type() * * Returns the name of a type. * ----------------------------------------------------------------------------- */ String *Swig_name_type(const_String_or_char_ptr tname) { String *r, *s; String* f = naming_hash ? Getattr(naming_hash, "type") : NULL; /* Don't bother doing anything else if there is no special naming format. */ if (f) { s = Copy(f); Replace(s, "%c", tname, DOH_REPLACE_ANY); } else { s = (String*)tname; } r = Swig_name_mangle_string(s); if (s != tname) Delete(s); return r; } /* ----------------------------------------------------------------------------- * Swig_name_mangle_string() * * Take a string and mangle it by stripping all non-valid C identifier * characters. * * This routine skips unnecessary blank spaces, therefore mangling * 'char *' and 'char*', 'std::pair' and * 'std::pair', produce the same result. * * However, note that 'long long' and 'long_long' produce different * mangled strings. * * The mangling method still is not 'perfect', for example std::pair and * std_pair return the same mangling. This is just a little better * than before, but it seems to be enough for most of the purposes. * * Having a perfect mangling will break some examples and code which * assume, for example, that A::get_value will be mangled as * A_get_value. * ----------------------------------------------------------------------------- */ String *Swig_name_mangle_string(const String *s) { String *result = NewStringEmpty(); int space = 0; int state = 0; char *pc, *cb; pc = cb = Char(s); while (*pc) { char c = *pc; if (isalnum((int) c) || (c == '_')) { state = 1; if (space && (space == state)) { Append(result, "_SS_"); } space = 0; Printf(result, "%c", (int) c); } else { if (isspace((int) c)) { space = state; ++pc; continue; } else { state = 3; space = 0; } switch (c) { case '.': if ((cb != pc) && (*(pc - 1) == 'p')) { Append(result, "_"); ++pc; continue; } else { c = 'f'; } break; case ':': if (*(pc + 1) == ':') { Append(result, "_"); ++pc; ++pc; continue; } break; case '*': c = 'm'; break; case '&': c = 'A'; break; case '<': c = 'l'; break; case '>': c = 'g'; break; case '=': c = 'e'; break; case ',': c = 'c'; break; case '(': c = 'p'; break; case ')': c = 'P'; break; case '[': c = 'b'; break; case ']': c = 'B'; break; case '^': c = 'x'; break; case '|': c = 'o'; break; case '~': c = 'n'; break; case '!': c = 'N'; break; case '%': c = 'M'; break; case '?': c = 'q'; break; case '+': c = 'a'; break; case '-': c = 's'; break; case '/': c = 'd'; break; default: break; } if (isalpha((int) c)) { Printf(result, "_S%c_", (int) c); } else { Printf(result, "_S%02X_", (int) c); } } ++pc; } return result; } /* ----------------------------------------------------------------------------- * Swig_name_wrapper() * * Returns the name of a wrapper function. * ----------------------------------------------------------------------------- */ String *Swig_name_wrapper(const_String_or_char_ptr fname) { String *r = get_naming_format_for("wrapper", "_wrap_%f"); Replace(r, "%f", fname, DOH_REPLACE_ANY); name_mangle(r); return r; } /* ----------------------------------------------------------------------------- * Swig_name_member() * * Returns the name of a class method. * ----------------------------------------------------------------------------- */ String *Swig_name_member(const_String_or_char_ptr nspace, const_String_or_char_ptr classname, const_String_or_char_ptr membername) { String *r; String *rclassname; String *rmembername; char *cname; rclassname = SwigType_namestr(classname); rmembername = SwigType_namestr(membername); r = get_naming_format_for("member", "%n%c_%m"); cname = Char(rclassname); if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) { cname = strchr(cname, ' ') + 1; } replace_nspace(r, nspace); Replace(r, "%c", cname, DOH_REPLACE_ANY); Replace(r, "%m", rmembername, DOH_REPLACE_ANY); /* name_mangle(r); */ Delete(rclassname); Delete(rmembername); return r; } /* ----------------------------------------------------------------------------- * Swig_name_get() * * Returns the name of the accessor function used to get a variable. * ----------------------------------------------------------------------------- */ String *Swig_name_get(const_String_or_char_ptr nspace, const_String_or_char_ptr vname) { String *r = get_naming_format_for("get", "%n%v_get"); #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_get: '%s'\n", vname); #endif replace_nspace(r, nspace); Replace(r, "%v", vname, DOH_REPLACE_ANY); /* name_mangle(r); */ return r; } /* ----------------------------------------------------------------------------- * Swig_name_set() * * Returns the name of the accessor function used to set a variable. * ----------------------------------------------------------------------------- */ String *Swig_name_set(const_String_or_char_ptr nspace, const_String_or_char_ptr vname) { String *r = get_naming_format_for("set", "%n%v_set"); replace_nspace(r, nspace); Replace(r, "%v", vname, DOH_REPLACE_ANY); /* name_mangle(r); */ return r; } /* Common implementation of all Swig_name_() functions below. */ static String *make_full_name_for(const char *method, const char *def_format, const_String_or_char_ptr nspace, const_String_or_char_ptr classname) { String *r; String *rclassname; char *cname; rclassname = SwigType_namestr(classname); r = get_naming_format_for(method, def_format); cname = Char(rclassname); if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) { cname = strchr(cname, ' ') + 1; } replace_nspace(r, nspace); Replace(r, "%c", cname, DOH_REPLACE_ANY); Delete(rclassname); return r; } /* ----------------------------------------------------------------------------- * Swig_name_construct() * * Returns the name of the accessor function used to create an object. * ----------------------------------------------------------------------------- */ String *Swig_name_construct(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) { return make_full_name_for("construct", "new_%n%c", nspace, classname); } /* ----------------------------------------------------------------------------- * Swig_name_copyconstructor() * * Returns the name of the accessor function used to copy an object. * ----------------------------------------------------------------------------- */ String *Swig_name_copyconstructor(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) { return make_full_name_for("copy", "copy_%n%c", nspace, classname); } /* ----------------------------------------------------------------------------- * Swig_name_destroy() * * Returns the name of the accessor function used to destroy an object. * ----------------------------------------------------------------------------- */ String *Swig_name_destroy(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) { return make_full_name_for("destroy", "delete_%n%c", nspace, classname); } /* ----------------------------------------------------------------------------- * Swig_name_disown() * * Returns the name of the accessor function used to disown an object. * ----------------------------------------------------------------------------- */ String *Swig_name_disown(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) { return make_full_name_for("disown", "disown_%n%c", nspace, classname); } /* ----------------------------------------------------------------------------- * Swig_name_object_set() * * Sets an object associated with a name and optional declarators. * ----------------------------------------------------------------------------- */ void Swig_name_object_set(Hash *namehash, String *name, SwigType *decl, DOH *object) { DOH *n; #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_object_set: '%s', '%s'\n", name, decl); #endif n = Getattr(namehash, name); if (!n) { n = NewHash(); Setattr(namehash, name, n); Delete(n); } /* Add an object based on the declarator value */ if (!decl) { Setattr(n, "start", object); } else { SwigType *cd = Copy(decl); Setattr(n, cd, object); Delete(cd); } } /* ----------------------------------------------------------------------------- * Swig_name_object_get() * * Return an object associated with an optional class prefix, name, and * declarator. This function operates according to name matching rules * described for the %rename directive in the SWIG manual. * ----------------------------------------------------------------------------- */ static DOH *get_object(Hash *n, String *decl) { DOH *rn = 0; if (!n) return 0; if (decl) { rn = Getattr(n, decl); } else { rn = Getattr(n, "start"); } return rn; } static DOH *name_object_get(Hash *namehash, String *tname, SwigType *decl, SwigType *ncdecl) { DOH *rn = 0; Hash *n = Getattr(namehash, tname); if (n) { rn = get_object(n, decl); if ((!rn) && ncdecl) rn = get_object(n, ncdecl); if (!rn) rn = get_object(n, 0); } return rn; } DOH *Swig_name_object_get(Hash *namehash, String *prefix, String *name, SwigType *decl) { String *tname = NewStringEmpty(); DOH *rn = 0; char *ncdecl = 0; if (!namehash) return 0; /* DB: This removed to more tightly control feature/name matching */ /* if ((decl) && (SwigType_isqualifier(decl))) { ncdecl = strchr(Char(decl),'.'); ncdecl++; } */ #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_object_get: '%s' '%s', '%s'\n", prefix, name, decl); #endif /* Perform a class-based lookup (if class prefix supplied) */ if (prefix) { if (Len(prefix)) { Printf(tname, "%s::%s", prefix, name); rn = name_object_get(namehash, tname, decl, ncdecl); if (!rn) { String *cls = Swig_scopename_last(prefix); if (!Equal(cls, prefix)) { Clear(tname); Printf(tname, "*::%s::%s", cls, name); rn = name_object_get(namehash, tname, decl, ncdecl); } Delete(cls); } /* Lookup a name within a templated-based class */ if (!rn) { String *t_name = SwigType_istemplate_templateprefix(prefix); if (t_name) { Clear(tname); Printf(tname, "%s::%s", t_name, name); rn = name_object_get(namehash, tname, decl, ncdecl); Delete(t_name); } } /* Lookup a template-based name within a class */ if (!rn) { String *t_name = SwigType_istemplate_templateprefix(name); if (t_name) rn = Swig_name_object_get(namehash, prefix, t_name, decl); Delete(t_name); } } /* A wildcard-based class lookup */ if (!rn) { Clear(tname); Printf(tname, "*::%s", name); rn = name_object_get(namehash, tname, decl, ncdecl); } } else { /* Lookup in the global namespace only */ Clear(tname); Printf(tname, "::%s", name); rn = name_object_get(namehash, tname, decl, ncdecl); } /* Catch-all */ if (!rn) { rn = name_object_get(namehash, name, decl, ncdecl); } if (!rn && Swig_scopename_check(name)) { String *nprefix = 0; String *nlast = 0; Swig_scopename_split(name, &nprefix, &nlast); rn = name_object_get(namehash, nlast, decl, ncdecl); Delete(nlast); Delete(nprefix); } Delete(tname); #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_object_get: found %d\n", rn ? 1 : 0); #endif return rn; } /* ----------------------------------------------------------------------------- * Swig_name_object_inherit() * * Implements name-based inheritance scheme. * ----------------------------------------------------------------------------- */ void Swig_name_object_inherit(Hash *namehash, String *base, String *derived) { Iterator ki; Hash *derh; String *bprefix; String *dprefix; char *cbprefix; int plen; if (!namehash) return; /* Temporary hash holding all the entries we add while we iterate over namehash itself as we can't modify the latter while iterating over it. */ derh = NULL; bprefix = NewStringf("%s::", base); dprefix = NewStringf("%s::", derived); cbprefix = Char(bprefix); plen = (int)strlen(cbprefix); for (ki = First(namehash); ki.key; ki = Next(ki)) { char *k = Char(ki.key); if (strncmp(k, cbprefix, plen) == 0) { /* Copy, adjusting name, this element to the derived hash. */ Iterator oi; String *nkey = NewStringf("%s%s", dprefix, k + plen); Hash *n = ki.item; Hash *newh; /* Don't overwrite an existing value for the derived class, if any. */ newh = Getattr(namehash, nkey); if (!newh) { if (!derh) derh = NewHash(); newh = NewHash(); Setattr(derh, nkey, newh); Delete(newh); } for (oi = First(n); oi.key; oi = Next(oi)) { if (!Getattr(newh, oi.key)) { String *ci = Copy(oi.item); Setattr(newh, oi.key, ci); Delete(ci); } } Delete(nkey); } } /* Merge the contents of derived hash into the main hash. */ if (derh) { for (ki = First(derh); ki.key; ki = Next(ki)) { Setattr(namehash, ki.key, ki.item); } } Delete(bprefix); Delete(dprefix); Delete(derh); } /* ----------------------------------------------------------------------------- * merge_features() * * Given a hash, this function merges the features in the hash into the node. * ----------------------------------------------------------------------------- */ static void merge_features(Hash *features, Node *n) { Iterator ki; if (!features) return; for (ki = First(features); ki.key; ki = Next(ki)) { String *ci = Copy(ki.item); Setattr(n, ki.key, ci); Delete(ci); } } /* ----------------------------------------------------------------------------- * Swig_features_get() * * Attaches any features in the features hash to the node that matches * the declaration, decl. * ----------------------------------------------------------------------------- */ static void features_get(Hash *features, const String *tname, SwigType *decl, SwigType *ncdecl, Node *node) { Node *n = Getattr(features, tname); #ifdef SWIG_DEBUG Printf(stdout, " features_get: %s\n", tname); #endif if (n) { merge_features(get_object(n, 0), node); if (ncdecl) merge_features(get_object(n, ncdecl), node); merge_features(get_object(n, decl), node); } } void Swig_features_get(Hash *features, String *prefix, String *name, SwigType *decl, Node *node) { char *ncdecl = 0; String *rdecl = 0; String *rname = 0; if (!features) return; /* MM: This removed to more tightly control feature/name matching */ /* if ((decl) && (SwigType_isqualifier(decl))) { ncdecl = strchr(Char(decl),'.'); ncdecl++; } */ /* very specific hack for template constructors/destructors */ if (name && SwigType_istemplate(name)) { String *nodetype = nodeType(node); if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) { String *nprefix = 0; String *nlast = 0; String *tprefix; Swig_scopename_split(name, &nprefix, &nlast); tprefix = SwigType_templateprefix(nlast); Delete(nlast); if (Len(nprefix)) { Append(nprefix, "::"); Append(nprefix, tprefix); Delete(tprefix); rname = nprefix; } else { rname = tprefix; Delete(nprefix); } rdecl = Copy(decl); Replaceall(rdecl, name, rname); decl = rdecl; name = rname; } } #ifdef SWIG_DEBUG Printf(stdout, "Swig_features_get: '%s' '%s' '%s'\n", prefix, name, decl); #endif /* Global features */ features_get(features, "", 0, 0, node); if (name) { String *tname = NewStringEmpty(); /* add features for 'root' template */ String *dname = SwigType_istemplate_templateprefix(name); if (dname) { features_get(features, dname, decl, ncdecl, node); } /* Catch-all */ features_get(features, name, decl, ncdecl, node); /* Perform a class-based lookup (if class prefix supplied) */ if (prefix) { /* A class-generic feature */ if (Len(prefix)) { Printf(tname, "%s::", prefix); features_get(features, tname, decl, ncdecl, node); } /* A wildcard-based class lookup */ Clear(tname); Printf(tname, "*::%s", name); features_get(features, tname, decl, ncdecl, node); /* A specific class lookup */ if (Len(prefix)) { /* A template-based class lookup */ String *tprefix = SwigType_istemplate_templateprefix(prefix); if (tprefix) { Clear(tname); Printf(tname, "%s::%s", tprefix, name); features_get(features, tname, decl, ncdecl, node); } Clear(tname); Printf(tname, "%s::%s", prefix, name); features_get(features, tname, decl, ncdecl, node); Delete(tprefix); } } else { /* Lookup in the global namespace only */ Clear(tname); Printf(tname, "::%s", name); features_get(features, tname, decl, ncdecl, node); } Delete(tname); Delete(dname); } if (name && SwigType_istemplate(name)) { /* add features for complete template type */ String *dname = Swig_symbol_template_deftype(name, 0); if (!Equal(dname, name)) { Swig_features_get(features, prefix, dname, decl, node); } Delete(dname); } if (rname) Delete(rname); if (rdecl) Delete(rdecl); } /* ----------------------------------------------------------------------------- * Swig_feature_set() * * Sets a feature name and value. Also sets optional feature attributes as * passed in by featureattribs. Optional feature attributes are given a full name * concatenating the feature name plus ':' plus the attribute name. * ----------------------------------------------------------------------------- */ void Swig_feature_set(Hash *features, const_String_or_char_ptr name, SwigType *decl, const_String_or_char_ptr featurename, const_String_or_char_ptr value, Hash *featureattribs) { Hash *n; Hash *fhash; #ifdef SWIG_DEBUG Printf(stdout, "Swig_feature_set: '%s' '%s' '%s' '%s'\n", name, decl, featurename, value); #endif n = Getattr(features, name); if (!n) { n = NewHash(); Setattr(features, name, n); Delete(n); } if (!decl) { fhash = Getattr(n, "start"); if (!fhash) { fhash = NewHash(); Setattr(n, "start", fhash); Delete(fhash); } } else { fhash = Getattr(n, decl); if (!fhash) { String *cdecl_ = Copy(decl); fhash = NewHash(); Setattr(n, cdecl_, fhash); Delete(cdecl_); Delete(fhash); } } if (value) { Setattr(fhash, featurename, value); } else { Delattr(fhash, featurename); } { /* Add in the optional feature attributes */ Hash *attribs = featureattribs; while (attribs) { String *attribname = Getattr(attribs, "name"); String *featureattribname = NewStringf("%s:%s", featurename, attribname); if (value) { String *attribvalue = Getattr(attribs, "value"); Setattr(fhash, featureattribname, attribvalue); } else { Delattr(fhash, featureattribname); } attribs = nextSibling(attribs); Delete(featureattribname); } } if (name && SwigType_istemplate(name)) { String *dname = Swig_symbol_template_deftype(name, 0); if (Strcmp(dname, name)) { Swig_feature_set(features, dname, decl, featurename, value, featureattribs); } Delete(dname); } } /* ----------------------------------------------------------------------------- * The rename/namewarn engine * * Code below was in parser.y for a while * ----------------------------------------------------------------------------- */ static Hash *namewarn_hash = 0; static Hash *name_namewarn_hash(void) { if (!namewarn_hash) namewarn_hash = NewHash(); return namewarn_hash; } static Hash *rename_hash = 0; static Hash *name_rename_hash(void) { if (!rename_hash) rename_hash = NewHash(); return rename_hash; } static List *namewarn_list = 0; static List *name_namewarn_list(void) { if (!namewarn_list) namewarn_list = NewList(); return namewarn_list; } static List *rename_list = 0; static List *name_rename_list(void) { if (!rename_list) rename_list = NewList(); return rename_list; } /* ----------------------------------------------------------------------------- * int need_name_warning(Node *n) * * Detects if a node needs name warnings * * ----------------------------------------------------------------------------- */ static int need_name_warning(Node *n) { int need = 1; /* We don't use name warnings for: - class forwards, no symbol is generated at the target language. - template declarations, only for real instances using %template(name). - typedefs, have no effect at the target language. - using declarations and using directives, have no effect at the target language. */ if (checkAttribute(n, "nodeType", "classforward")) { need = 0; } else if (checkAttribute(n, "nodeType", "using")) { need = 0; } else if (checkAttribute(n, "storage", "typedef")) { need = 0; } else if (Getattr(n, "hidden")) { need = 0; } else if (Getattr(n, "ignore")) { need = 0; } else if (Getattr(n, "templatetype")) { need = 0; } else if (GetFlag(n, "parsing_template_declaration")) { need = 0; } return need; } /* ----------------------------------------------------------------------------- * int Swig_need_redefined_warn() * * Detects when a redefined object needs a warning * * ----------------------------------------------------------------------------- */ static int nodes_are_equivalent(Node *a, Node *b, int a_inclass) { /* they must have the same type */ String *ta = nodeType(a); String *tb = nodeType(b); if (!Equal(ta, tb)) { if (!(Equal(ta, "using") && Equal(tb, "cdecl"))) { return 0; } } if (Equal(ta, "cdecl") || Equal(ta, "constructor")) { /* both cdecl or constructor case */ /* typedef */ String *a_storage = Getattr(a, "storage"); String *b_storage = Getattr(b, "storage"); if ((Cmp(a_storage, "typedef") == 0) || (Cmp(b_storage, "typedef") == 0)) { if (Cmp(a_storage, b_storage) == 0) { String *a_type = (Getattr(a, "type")); String *b_type = (Getattr(b, "type")); if (Cmp(a_type, b_type) == 0) return 1; } return 0; } /* static functions */ if (Swig_storage_isstatic(a) || Swig_storage_isstatic(b)) { if (Cmp(a_storage, b_storage) != 0) return 0; } /* friend methods */ if (!a_inclass || Strstr(a_storage, "friend")) { /* check declaration */ String *a_decl = (Getattr(a, "decl")); String *b_decl = (Getattr(b, "decl")); if (Cmp(a_decl, b_decl) == 0) { /* check return type */ String *a_type = (Getattr(a, "type")); String *b_type = (Getattr(b, "type")); if (Cmp(a_type, b_type) == 0) { /* check parameters */ Parm *ap = (Getattr(a, "parms")); Parm *bp = (Getattr(b, "parms")); while (ap && bp) { SwigType *at = Getattr(ap, "type"); SwigType *bt = Getattr(bp, "type"); if (Cmp(at, bt) != 0) return 0; ap = nextSibling(ap); bp = nextSibling(bp); } if (ap || bp) { return 0; } else { Node *a_template = Getattr(a, "template"); Node *b_template = Getattr(b, "template"); /* Not equivalent if one is a template instantiation (via %template) and the other is a non-templated function */ if ((a_template && !b_template) || (!a_template && b_template)) return 0; } return 1; } } } } else if (Equal(ta, "using")) { /* using and cdecl case */ String *b_storage = Getattr(b, "storage"); if (Equal(b_storage, "typedef")) { String *a_name = Getattr(a, "name"); String *b_name = Getattr(b, "name"); if (Equal(a_name, b_name)) return 1; } } else { /* both %constant case */ String *a_storage = Getattr(a, "storage"); String *b_storage = Getattr(b, "storage"); if ((Cmp(a_storage, "%constant") == 0) || (Cmp(b_storage, "%constant") == 0)) { if (Cmp(a_storage, b_storage) == 0) { String *a_type = (Getattr(a, "type")); String *b_type = (Getattr(b, "type")); if ((Cmp(a_type, b_type) == 0) && (Cmp(Getattr(a, "value"), Getattr(b, "value")) == 0)) return 1; } return 0; } if (Equal(ta, "template") && Equal(tb, "template")) { if (Strstr(a_storage, "friend") || Strstr(b_storage, "friend")) return 1; } } return 0; } int Swig_need_redefined_warn(Node *a, Node *b, int InClass) { String *a_name = Getattr(a, "name"); String *b_name = Getattr(b, "name"); String *a_symname = Getattr(a, "sym:name"); String *b_symname = Getattr(b, "sym:name"); /* always send a warning if a 'rename' is involved */ if ((a_symname && !Equal(a_symname, a_name)) || (b_symname && !Equal(b_symname, b_name))) { if (!Equal(a_name, b_name)) { return 1; } } return !nodes_are_equivalent(a, b, InClass); } /* ----------------------------------------------------------------------------- * int Swig_need_protected(Node* n) * * Detects when we need to fully register the protected member. * This is basically any protected members when the allprotected mode is set. * Otherwise we take just the protected virtual methods and non-static methods * (potentially virtual methods) as well as constructors/destructors. * Also any "using" statements in a class may potentially be virtual. * ----------------------------------------------------------------------------- */ int Swig_need_protected(Node *n) { String *nodetype = nodeType(n); if (checkAttribute(n, "access", "protected")) { if ((Equal(nodetype, "cdecl"))) { if (Swig_director_mode() && Swig_director_protected_mode() && Swig_all_protected_mode()) { return 1; } if (SwigType_isfunction(Getattr(n, "decl"))) { String *storage = Getattr(n, "storage"); /* The function is declared virtual, or it has no storage. This eliminates typedef, static etc. */ return !storage || Equal(storage, "virtual"); } } else if (Equal(nodetype, "constructor") || Equal(nodetype, "destructor")) { return 1; } else if (Equal(nodetype, "using") && !Getattr(n, "namespace")) { return 1; } } return 0; } /* ----------------------------------------------------------------------------- * void name_nameobj_add() * * Add nameobj (rename/namewarn) * * ----------------------------------------------------------------------------- */ static List *make_attrlist(const char *ckey) { List *list = NewList(); const char *cattr = strchr(ckey, '$'); if (cattr) { String *nattr; const char *rattr = strchr(++cattr, '$'); while (rattr) { nattr = NewStringWithSize(cattr, (int)(rattr - cattr)); Append(list, nattr); Delete(nattr); cattr = rattr + 1; rattr = strchr(cattr, '$'); } nattr = NewString(cattr); Append(list, nattr); Delete(nattr); } else { Append(list, "nodeType"); } return list; } static void name_object_attach_keys(const char *keys[], Hash *nameobj) { Node *kw = nextSibling(nameobj); List *matchlist = 0; while (kw) { Node *next = nextSibling(kw); String *kname = Getattr(kw, "name"); char *ckey = kname ? Char(kname) : 0; if (ckey) { const char **rkey; int isnotmatch = 0; int isregexmatch = 0; if ((strncmp(ckey, "match", 5) == 0) || (isnotmatch = (strncmp(ckey, "notmatch", 8) == 0)) || (isregexmatch = (strncmp(ckey, "regexmatch", 10) == 0)) || (isnotmatch = isregexmatch = (strncmp(ckey, "notregexmatch", 13) == 0))) { Hash *mi = NewHash(); List *attrlist = make_attrlist(ckey); if (!matchlist) matchlist = NewList(); Setattr(mi, "value", Getattr(kw, "value")); Setattr(mi, "attrlist", attrlist); if (isnotmatch) SetFlag(mi, "notmatch"); if (isregexmatch) SetFlag(mi, "regexmatch"); Delete(attrlist); Append(matchlist, mi); Delete(mi); removeNode(kw); } else { for (rkey = keys; *rkey != 0; ++rkey) { if (strcmp(ckey, *rkey) == 0) { Setattr(nameobj, *rkey, Getattr(kw, "value")); removeNode(kw); } } } } kw = next; } if (matchlist) { Setattr(nameobj, "matchlist", matchlist); Delete(matchlist); } } static void name_nameobj_add(Hash *name_hash, List *name_list, String *prefix, String *name, SwigType *decl, Hash *nameobj) { String *nname = 0; if (name && Len(name)) { String *target_fmt = Getattr(nameobj, "targetfmt"); nname = prefix ? NewStringf("%s::%s", prefix, name) : NewString(name); if (target_fmt) { String *tmp = NewStringf(target_fmt, nname); Delete(nname); nname = tmp; } } if (!nname || !Len(nname) || Getattr(nameobj, "fullname") || /* any of these options trigger a 'list' nameobj */ Getattr(nameobj, "sourcefmt") || Getattr(nameobj, "matchlist") || Getattr(nameobj, "regextarget")) { if (decl) Setattr(nameobj, "decl", decl); if (nname && Len(nname)) Setattr(nameobj, "targetname", nname); /* put the new nameobj at the beginning of the list, such that the last inserted rule take precedence */ Insert(name_list, 0, nameobj); } else { /* here we add an old 'hash' nameobj, simple and fast */ Swig_name_object_set(name_hash, nname, decl, nameobj); } Delete(nname); } /* ----------------------------------------------------------------------------- * int name_match_nameobj() * * Apply and check the nameobj's math list to the node * * ----------------------------------------------------------------------------- */ static DOH *get_lattr(Node *n, List *lattr) { DOH *res = 0; int ilen = Len(lattr); int i; for (i = 0; n && (i < ilen); ++i) { String *nattr = Getitem(lattr, i); res = Getattr(n, nattr); #ifdef SWIG_DEBUG if (!res) { Printf(stdout, "missing %s %s %s\n", nattr, Getattr(n, "name"), Getattr(n, "member")); } else { Printf(stdout, "lattr %d %s %s\n", i, nattr, DohIsString(res) ? res : Getattr(res, "name")); } #endif n = res; } return res; } #ifdef HAVE_PCRE #define PCRE2_CODE_UNIT_WIDTH 8 #include static int name_regexmatch_value(Node *n, String *pattern, String *s) { pcre2_code *compiled_pat; PCRE2_UCHAR err[256]; int errornum; size_t errpos; int rc; pcre2_match_data *match_data = 0; compiled_pat = pcre2_compile((PCRE2_SPTR8)Char(pattern), PCRE2_ZERO_TERMINATED, 0, &errornum, &errpos, NULL); if (!compiled_pat) { pcre2_get_error_message (errornum, err, sizeof err); Swig_error("SWIG", Getline(n), "Invalid regex \"%s\": compilation failed at %d: %s\n", Char(pattern), errpos, err); Exit(EXIT_FAILURE); } match_data = pcre2_match_data_create_from_pattern (compiled_pat, NULL); rc = pcre2_match(compiled_pat, (PCRE2_SPTR8)Char(s), PCRE2_ZERO_TERMINATED, 0, 0, match_data, 0); pcre2_code_free(compiled_pat); pcre2_match_data_free(match_data); if (rc == PCRE2_ERROR_NOMATCH) return 0; if (rc < 0 ) { Swig_error("SWIG", Getline(n), "Matching \"%s\" against regex \"%s\" failed: %d\n", Char(s), Char(pattern), rc); Exit(EXIT_FAILURE); } return 1; } #else /* !HAVE_PCRE */ static int name_regexmatch_value(Node *n, String *pattern, String *s) { (void)pattern; (void)s; Swig_error("SWIG", Getline(n), "PCRE regex matching is not available in this SWIG build.\n"); Exit(EXIT_FAILURE); return 0; } #endif /* HAVE_PCRE/!HAVE_PCRE */ static int name_match_value(String *mvalue, String *value) { #if defined(SWIG_USE_SIMPLE_MATCHOR) int match = 0; char *cvalue = Char(value); char *cmvalue = Char(mvalue); char *sep = strchr(cmvalue, '|'); while (sep && !match) { match = strncmp(cvalue, cmvalue, sep - cmvalue) == 0; #ifdef SWIG_DEBUG Printf(stdout, "match_value: %s %s %d\n", cvalue, cmvalue, match); #endif cmvalue = sep + 1; sep = strchr(cmvalue, '|'); } if (!match) { match = strcmp(cvalue, cmvalue) == 0; #ifdef SWIG_DEBUG Printf(stdout, "match_value: %s %s %d\n", cvalue, cmvalue, match); #endif } return match; #else return Equal(mvalue, value); #endif } static int name_match_nameobj(Hash *rn, Node *n) { int match = 1; List *matchlist = Getattr(rn, "matchlist"); #ifdef SWIG_DEBUG Printf(stdout, "name_match_nameobj: %s\n", Getattr(n, "name")); #endif if (matchlist) { int ilen = Len(matchlist); int i; for (i = 0; match && (i < ilen); ++i) { Node *mi = Getitem(matchlist, i); List *lattr = Getattr(mi, "attrlist"); String *nval = get_lattr(n, lattr); int notmatch = GetFlag(mi, "notmatch"); int regexmatch = GetFlag(mi, "regexmatch"); match = 0; if (nval) { String *kwval = Getattr(mi, "value"); match = regexmatch ? name_regexmatch_value(n, kwval, nval) : name_match_value(kwval, nval); #ifdef SWIG_DEBUG Printf(stdout, "val %s %s %d %d \n", nval, kwval, match, ilen); #endif } if (notmatch) match = !match; } } #ifdef SWIG_DEBUG Printf(stdout, "name_match_nameobj: %d\n", match); #endif return match; } /* ----------------------------------------------------------------------------- * Hash *name_nameobj_lget() * * Get a nameobj (rename/namewarn) from the list of filters * * ----------------------------------------------------------------------------- */ static Hash *name_nameobj_lget(List *namelist, Node *n, String *prefix, String *name, String *decl) { Hash *res = 0; if (namelist) { int len = Len(namelist); int i; int match = 0; for (i = 0; !match && (i < len); i++) { Hash *rn = Getitem(namelist, i); String *rdecl = Getattr(rn, "decl"); if (rdecl && (!decl || !Equal(rdecl, decl))) { continue; } else if (name_match_nameobj(rn, n)) { String *tname = Getattr(rn, "targetname"); if (tname) { String *sfmt = Getattr(rn, "sourcefmt"); String *sname = 0; int fullname = GetFlag(rn, "fullname"); int regextarget = GetFlag(rn, "regextarget"); if (sfmt) { if (fullname && prefix) { String *pname = NewStringf("%s::%s", prefix, name); sname = NewStringf(sfmt, pname); Delete(pname); } else { sname = NewStringf(sfmt, name); } } else { if (fullname && prefix) { sname = NewStringf("%s::%s", prefix, name); } else { sname = name; DohIncref(name); } } match = regextarget ? name_regexmatch_value(n, tname, sname) : name_match_value(tname, sname); Delete(sname); } else { /* Applying the renaming rule may fail if it contains a %(regex)s expression that doesn't match the given name. */ String *sname = NewStringf(Getattr(rn, "name"), name); if (sname) { if (Len(sname)) match = 1; Delete(sname); } } } if (match) { res = rn; break; } } } return res; } /* ----------------------------------------------------------------------------- * Swig_name_namewarn_add * * Add a namewarn objects * * ----------------------------------------------------------------------------- */ void Swig_name_namewarn_add(String *prefix, String *name, SwigType *decl, Hash *namewrn) { const char *namewrn_keys[] = { "rename", "error", "fullname", "sourcefmt", "targetfmt", 0 }; name_object_attach_keys(namewrn_keys, namewrn); name_nameobj_add(name_namewarn_hash(), name_namewarn_list(), prefix, name, decl, namewrn); } /* ----------------------------------------------------------------------------- * Hash *name_namewarn_get() * * Return the namewarn object, if there is one. * * ----------------------------------------------------------------------------- */ static Hash *name_namewarn_get(Node *n, String *prefix, String *name, SwigType *decl) { if (!namewarn_hash && !namewarn_list) return 0; if (n) { /* Return in the obvious cases */ if (!name || !need_name_warning(n)) { return 0; } else { String *access = Getattr(n, "access"); int is_public = !access || Equal(access, "public"); if (!is_public && !Swig_need_protected(n)) { return 0; } } } if (name) { /* Check to see if the name is in the hash */ Hash *wrn = Swig_name_object_get(name_namewarn_hash(), prefix, name, decl); if (wrn && !name_match_nameobj(wrn, n)) wrn = 0; if (!wrn) { wrn = name_nameobj_lget(name_namewarn_list(), n, prefix, name, decl); } if (wrn && Getattr(wrn, "error")) { if (n) { Swig_error(Getfile(n), Getline(n), "%s\n", Getattr(wrn, "name")); } else { Swig_error(cparse_file, cparse_line, "%s\n", Getattr(wrn, "name")); } } return wrn; } else { return 0; } } /* ----------------------------------------------------------------------------- * String *Swig_name_warning() * * Return the name warning, if there is one. * * ----------------------------------------------------------------------------- */ String *Swig_name_warning(Node *n, String *prefix, String *name, SwigType *decl) { Hash *wrn = name_namewarn_get(n, prefix, name, decl); return (name && wrn) ? Getattr(wrn, "name") : 0; } /* ----------------------------------------------------------------------------- * Swig_name_rename_add() * * Manage the rename objects * * ----------------------------------------------------------------------------- */ static void single_rename_add(String *prefix, String *name, SwigType *decl, Hash *newname) { name_nameobj_add(name_rename_hash(), name_rename_list(), prefix, name, decl, newname); } /* Add a new rename. Works much like new_feature including default argument handling. */ void Swig_name_rename_add(String *prefix, String *name, SwigType *decl, Hash *newname, ParmList *declaratorparms) { ParmList *declparms = declaratorparms; const char *rename_keys[] = { "fullname", "sourcefmt", "targetfmt", "continue", "regextarget", 0 }; name_object_attach_keys(rename_keys, newname); /* Add the name */ single_rename_add(prefix, name, decl, newname); /* Add extra names if there are default parameters in the parameter list */ if (decl) { int constqualifier = SwigType_isconst(decl); while (declparms) { if (ParmList_has_defaultargs(declparms)) { /* Create a parameter list for the new rename by copying all but the last (defaulted) parameter */ ParmList *newparms = CopyParmListMax(declparms,ParmList_len(declparms)-1); /* Create new declaration - with the last parameter removed */ SwigType *newdecl = Copy(decl); Delete(SwigType_pop_function(newdecl)); /* remove the old parameter list from newdecl */ SwigType_add_function(newdecl, newparms); if (constqualifier) SwigType_add_qualifier(newdecl, "const"); single_rename_add(prefix, name, newdecl, newname); declparms = newparms; Delete(newdecl); } else { declparms = 0; } } } } /* Create a name for the given node applying rename/namewarn if needed */ static String *apply_rename(Node* n, String *newname, int fullname, String *prefix, String *name) { String *result = 0; if (newname && Len(newname)) { if (Strcmp(newname, "$ignore") == 0) { /* $ignore doesn't apply to parameters and while it's rare to explicitly write %ignore directives for them they could be caught by a wildcard ignore using regex match, just ignore the attempt to ignore them in this case */ if (!Equal(nodeType(n), "parm")) result = Copy(newname); } else { char *cnewname = Char(newname); if (cnewname) { int destructor = name && (*(Char(name)) == '~'); String *fmt = newname; /* use name as a fmt, but avoid C++ "%" and "%=" operators */ if (Len(newname) > 1 && strchr(cnewname, '%') && !(strcmp(cnewname, "%=") == 0)) { if (fullname && prefix) { result = NewStringf(fmt, prefix, name); } else { result = NewStringf(fmt, name); } } else { result = Copy(newname); } if (destructor && result && (*(Char(result)) != '~')) { Insert(result, 0, "~"); } } } } return result; } /* ----------------------------------------------------------------------------- * String *Swig_name_make() * * Make a name after applying all the rename/namewarn objects * * ----------------------------------------------------------------------------- */ String *Swig_name_make(Node *n, String *prefix, const_String_or_char_ptr cname, SwigType *decl, String *oldname) { String *nname = 0; String *result = 0; String *name = NewString(cname); Hash *wrn = 0; String *rdecl = 0; String *rname = 0; /* very specific hack for template constructors/destructors */ #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_make: looking for %s %s %s %s\n", prefix, name, decl, oldname); #endif if (name && n && SwigType_istemplate(name)) { String *nodetype = nodeType(n); if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) { String *nprefix = 0; String *nlast = 0; String *tprefix; Swig_scopename_split(name, &nprefix, &nlast); tprefix = SwigType_templateprefix(nlast); Delete(nlast); if (Len(nprefix)) { Append(nprefix, "::"); Append(nprefix, tprefix); Delete(tprefix); rname = nprefix; } else { rname = tprefix; Delete(nprefix); } rdecl = Copy(decl); Replaceall(rdecl, name, rname); #ifdef SWIG_DEBUG Printf(stdout, "SWIG_name_make: use new name %s %s : %s %s\n", name, decl, rname, rdecl); #endif decl = rdecl; Delete(name); name = rname; } } if (rename_hash || rename_list || namewarn_hash || namewarn_list) { Hash *rn = Swig_name_object_get(name_rename_hash(), prefix, name, decl); if (!rn || !name_match_nameobj(rn, n)) { rn = name_nameobj_lget(name_rename_list(), n, prefix, name, decl); if (rn) { String *sfmt = Getattr(rn, "sourcefmt"); int fullname = GetFlag(rn, "fullname"); if (fullname && prefix) { String *sname = NewStringf("%s::%s", prefix, name); Delete(name); name = sname; prefix = 0; } if (sfmt) { String *sname = NewStringf(sfmt, name); Delete(name); name = sname; } } } if (rn) { String *newname = Getattr(rn, "name"); int fullname = GetFlag(rn, "fullname"); result = apply_rename(n, newname, fullname, prefix, name); } if (result && !Equal(result, name)) { /* operators in C++ allow aliases, we look for them */ char *cresult = Char(result); if (cresult && (strncmp(cresult, "operator ", 9) == 0)) { String *nresult = Swig_name_make(n, prefix, result, decl, oldname); if (!Equal(nresult, result)) { Delete(result); result = nresult; } else { Delete(nresult); } } } nname = result ? result : name; wrn = name_namewarn_get(n, prefix, nname, decl); if (wrn) { String *rename = Getattr(wrn, "rename"); if (rename) { String *msg = Getattr(wrn, "name"); int fullname = GetFlag(wrn, "fullname"); if (result) Delete(result); result = apply_rename(n, rename, fullname, prefix, name); if ((msg) && (Len(msg))) { if (!Getmeta(nname, "already_warned")) { String* suffix = 0; if (Strcmp(result, "$ignore") == 0) { suffix = NewStringf(": ignoring '%s'\n", name); } else if (Strcmp(result, name) != 0) { suffix = NewStringf(", renaming to '%s'\n", result); } else { /* No rename was performed */ suffix = NewString("\n"); } if (n) { /* Parameter renaming is not fully implemented. Mainly because there is no C/C++ syntax to * for %rename to fully qualify a function's parameter name from outside the function. Hence it * is not possible to implemented targeted warning suppression on one parameter in one function. */ int suppress_parameter_rename_warning = Equal(nodeType(n), "parm"); if (!suppress_parameter_rename_warning) { SWIG_WARN_NODE_BEGIN(n); Swig_warning(0, Getfile(n), Getline(n), "%s%s", msg, suffix); SWIG_WARN_NODE_END(n); } } else { Swig_warning(0, Getfile(name), Getline(name), "%s%s", msg, suffix); } Setmeta(nname, "already_warned", "1"); Delete(suffix); } } } } } if (!result || !Len(result)) { if (result) Delete(result); if (oldname) { result = NewString(oldname); } else { result = NewString(cname); } } Delete(name); #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_make: result '%s' '%s'\n", cname, result); #endif return result; } /* ----------------------------------------------------------------------------- * void Swig_name_inherit() * * Inherit namewarn, rename, and feature objects * * ----------------------------------------------------------------------------- */ void Swig_name_inherit(String *base, String *derived) { /* Printf(stdout,"base = '%s', derived = '%s'\n", base, derived); */ Swig_name_object_inherit(name_rename_hash(), base, derived); Swig_name_object_inherit(name_namewarn_hash(), base, derived); Swig_name_object_inherit(Swig_cparse_features(), base, derived); } /* ----------------------------------------------------------------------------- * Swig_inherit_base_symbols() * ----------------------------------------------------------------------------- */ void Swig_inherit_base_symbols(List *bases) { if (bases) { Iterator s; for (s = First(bases); s.item; s = Next(s)) { Symtab *st = Getattr(s.item, "symtab"); if (st) { Setfile(st, Getfile(s.item)); Setline(st, Getline(s.item)); Swig_symbol_inherit(st); } } Delete(bases); } } /* ----------------------------------------------------------------------------- * Swig_make_inherit_list() * ----------------------------------------------------------------------------- */ List *Swig_make_inherit_list(String *clsname, List *names, String *Namespaceprefix) { int i, ilen; String *derived; List *bases = NewList(); if (Namespaceprefix) derived = NewStringf("%s::%s", Namespaceprefix, clsname); else derived = NewString(clsname); ilen = Len(names); for (i = 0; i < ilen; i++) { String *base; String *n = Getitem(names, i); /* Try to figure out where this symbol is */ Node *s = Swig_symbol_clookup(n, 0); if (s) { while (s && (Strcmp(nodeType(s), "class") != 0)) { /* Not a class. Could be a typedef though. */ String *storage = Getattr(s, "storage"); if (storage && (Strcmp(storage, "typedef") == 0)) { String *nn = Getattr(s, "type"); s = Swig_symbol_clookup(nn, Getattr(s, "sym:symtab")); } else { break; } } if (s && ((Strcmp(nodeType(s), "class") == 0) || (Strcmp(nodeType(s), "template") == 0))) { String *q = Swig_symbol_qualified(s); Append(bases, s); if (q) { base = NewStringf("%s::%s", q, Getattr(s, "name")); Delete(q); } else { base = NewString(Getattr(s, "name")); } } else { base = NewString(n); } } else { base = NewString(n); } if (base) { Swig_name_inherit(base, derived); Delete(base); } } return bases; } /* ----------------------------------------------------------------------------- * void Swig_name_str() * * Return a stringified version of a C/C++ symbol from a node. * The node passed in is expected to be a function, constructor, destructor or * variable. Some example return values: * "MyNameSpace::MyTemplate::~MyTemplate" * "MyNameSpace::ABC::ABC" * "MyNameSpace::ABC::constmethod" * "MyNameSpace::ABC::variablename" * * ----------------------------------------------------------------------------- */ String *Swig_name_str(Node *n) { String *qname; String *qualifier = Swig_symbol_qualified(n); String *name = Swig_scopename_last(Getattr(n, "name")); if (qualifier) qualifier = SwigType_namestr(qualifier); /* Very specific hack for template constructors/destructors */ if (SwigType_istemplate(name)) { String *nodetype = nodeType(n); if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) { String *nprefix = 0; String *nlast = 0; String *tprefix; Swig_scopename_split(name, &nprefix, &nlast); tprefix = SwigType_templateprefix(nlast); Delete(nlast); Delete(nprefix); Delete(name); name = tprefix; } } qname = NewString(""); if (qualifier && Len(qualifier) > 0) Printf(qname, "%s::", qualifier); Printf(qname, "%s", SwigType_str(name, 0)); Delete(name); Delete(qualifier); return qname; } /* ----------------------------------------------------------------------------- * void Swig_name_decl() * * Return a stringified version of a C/C++ declaration without the return type. * The node passed in is usually a function, constructor, destructor. * Other nodes result in a simple fully qualified string of the symbol. * Some example return values: * "MyNameSpace::MyTemplate::~MyTemplate()" * "MyNameSpace::ABC::ABC(int,double)" * "MyNameSpace::ABC::constmethod(int) const" * "MyNameSpace::ABC::refqualifiermethod(int) const &" * "MyNameSpace::ABC::variablename" * "MyNameSpace::ABC::MyClass" * ----------------------------------------------------------------------------- */ String *Swig_name_decl(Node *n) { String *qname; String *decl; String *nodetype = nodeType(n); qname = Swig_name_str(n); decl = NewStringf("%s", qname); if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor") || Equal(nodetype, "cdecl"))) { String *d = Getattr(n, "decl"); if (SwigType_isfunction(d)) { SwigType *decl_temp = Copy(d); SwigType *qualifiers = SwigType_pop_function_qualifiers(decl_temp); Printv(decl, "(", ParmList_errorstr(Getattr(n, "parms")), ")", NIL); if (qualifiers) { String *qualifiers_string = SwigType_str(qualifiers, 0); Printv(decl, " ", qualifiers_string, NIL); Delete(qualifiers_string); } Delete(decl_temp); } } Delete(qname); return decl; } /* ----------------------------------------------------------------------------- * void Swig_name_fulldecl() * * Return a stringified version of a C/C++ declaration including the return type. * The node passed in is expected to be a function, constructor or destructor. * Some example return values: * "MyNameSpace::MyTemplate::~MyTemplate()" * "MyNameSpace::ABC::ABC(int,double)" * "int * MyNameSpace::ABC::constmethod(int) const" * * ----------------------------------------------------------------------------- */ String *Swig_name_fulldecl(Node *n) { String *decl = Swig_name_decl(n); String *type = Getattr(n, "type"); String *nodetype = nodeType(n); String *fulldecl; /* add on the return type */ if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) { fulldecl = decl; } else { String *t = SwigType_str(type, 0); fulldecl = NewStringf("%s %s", t, decl); Delete(decl); Delete(t); } return fulldecl; } swig-4.4.0/Source/Swig/getopt.c0000664000175000017500000000664015075443613016205 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * getopt.c * * Handles the parsing of command line options. This is particularly nasty * compared to other utilities given that command line options can potentially * be read by many different modules within SWIG. Thus, in order to make sure * there are no unrecognized options, each module is required to "mark" * the options that it uses. Afterwards, we can make a quick scan to make * sure there are no unmarked options. * * TODO: * Should have cleaner error handling in general. * ----------------------------------------------------------------------------- */ #include "swig.h" static char **args; static int numargs; static int *marked; /* ----------------------------------------------------------------------------- * Swig_init_args() * * Initialize the argument list handler. * ----------------------------------------------------------------------------- */ void Swig_init_args(int argc, char **argv) { assert(argc > 0); assert(argv); numargs = argc; args = argv; marked = (int *) Calloc(numargs, sizeof(int)); marked[0] = 1; } /* ----------------------------------------------------------------------------- * Swig_mark_arg() * * Marks an argument as being parsed. * ----------------------------------------------------------------------------- */ void Swig_mark_arg(int n) { assert(marked); assert((n >= 0) && (n < numargs)); marked[n] = 1; } /* ----------------------------------------------------------------------------- * Swig_check_marked() * * Checks to see if argument has been picked up. * ----------------------------------------------------------------------------- */ int Swig_check_marked(int n) { assert((n >= 0) && (n < numargs)); return marked[n]; } /* ----------------------------------------------------------------------------- * Swig_check_options() * * Checkers for unprocessed command line options and errors. * ----------------------------------------------------------------------------- */ void Swig_check_options(int check_input) { int error = 0; int i; int max = check_input ? numargs - 1 : numargs; assert(marked); for (i = 1; i < max; i++) { if (!marked[i]) { Printf(stderr, "swig error : Unrecognized option %s\n", args[i]); error = 1; } } if (error) { Printf(stderr, "Use 'swig -help' for available options.\n"); Exit(EXIT_FAILURE); } if (check_input && marked[numargs - 1]) { Printf(stderr, "Must specify an input file. Use -help for available options.\n"); Exit(EXIT_FAILURE); } } /* ----------------------------------------------------------------------------- * Swig_arg_error() * * Generates a generic error message and exits. * ----------------------------------------------------------------------------- */ void Swig_arg_error(void) { Printf(stderr, "SWIG : Unable to parse command line options.\n"); Printf(stderr, "Use 'swig -help' for available options.\n"); Exit(EXIT_FAILURE); } swig-4.4.0/Source/Swig/misc.c0000664000175000017500000012126215075443613015634 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * misc.c * * Miscellaneous functions that don't really fit anywhere else. * ----------------------------------------------------------------------------- */ #include "swig.h" #include #include #include #include #include #include #ifdef _WIN32 #include #ifndef S_ISDIR #define S_ISDIR(mode) (((mode) & S_IFDIR) == S_IFDIR) #endif #endif static char *fake_version = 0; /* ----------------------------------------------------------------------------- * Swig_copy_string() * * Duplicate a NULL-terminate string given as a char *. * ----------------------------------------------------------------------------- */ char *Swig_copy_string(const char *s) { char *c = 0; if (s) { c = (char *) Malloc(strlen(s) + 1); strcpy(c, s); } return c; } /* ----------------------------------------------------------------------------- * Swig_set_fakeversion() * * Version string override * ----------------------------------------------------------------------------- */ void Swig_set_fakeversion(const char *version) { fake_version = Swig_copy_string(version); } /* ----------------------------------------------------------------------------- * Swig_package_version() * * Return the package string containing the version number * ----------------------------------------------------------------------------- */ const char *Swig_package_version(void) { return fake_version ? fake_version : PACKAGE_VERSION; } /* ----------------------------------------------------------------------------- * Swig_package_version_hex() * * Return the package version in hex format "0xAABBCC" such as "0x040200" for 4.2.0 * ----------------------------------------------------------------------------- */ String *Swig_package_version_hex(void) { String *package_version = NewString(Swig_package_version()); char *token = strtok(Char(package_version), "."); String *vers = NewString("SWIG_VERSION 0x"); int count = 0; while (token) { int len = (int)strlen(token); assert(len == 1 || len == 2); Printf(vers, "%s%s", (len == 1) ? "0" : "", token); token = strtok(NULL, "."); count++; } Delete(package_version); assert(count == 3); /* Check version format is correct */ return vers; } /* ----------------------------------------------------------------------------- * Swig_obligatory_macros() * * Generates the SWIG_VERSION and SWIGXXX macros where XXX is the target language * name (must be provided uppercase). * ----------------------------------------------------------------------------- */ void Swig_obligatory_macros(String *f_runtime, const char *language) { String *version_hex = Swig_package_version_hex(); Printf(f_runtime, "\n\n"); Printf(f_runtime, "#define %s\n", version_hex); Printf(f_runtime, "#define SWIG%s\n", language); Delete(version_hex); } /* ----------------------------------------------------------------------------- * Swig_banner() * * Emits the SWIG identifying banner for the C/C++ wrapper file. * ----------------------------------------------------------------------------- */ void Swig_banner(File *f) { Printf(f, "/* ----------------------------------------------------------------------------\n"); Swig_banner_target_lang(f, " *"); Printf(f, " * ----------------------------------------------------------------------------- */\n"); } /* ----------------------------------------------------------------------------- * Swig_banner_target_lang() * * Emits a SWIG identifying banner in the target language * ----------------------------------------------------------------------------- */ void Swig_banner_target_lang(File *f, const_String_or_char_ptr commentchar) { Printf(f, "%s This file was automatically generated by SWIG (https://www.swig.org).\n", commentchar); Printf(f, "%s Version %s\n", commentchar, Swig_package_version()); Printf(f, "%s\n", commentchar); Printf(f, "%s Do not make changes to this file unless you know what you are doing - modify\n", commentchar); Printf(f, "%s the SWIG interface file instead.\n", commentchar); } /* ----------------------------------------------------------------------------- * Swig_strip_c_comments() * * Return a new string with C comments stripped from the input string. NULL is * returned if there aren't any comments. * ----------------------------------------------------------------------------- */ String *Swig_strip_c_comments(const String *s) { const char *c = Char(s); const char *comment_begin = 0; const char *comment_end = 0; String *stripped = 0; while (*c) { if (!comment_begin && *c == '/') { ++c; if (!*c) break; if (*c == '*') comment_begin = c-1; } else if (comment_begin && !comment_end && *c == '*') { ++c; if (*c == '/') { comment_end = c; break; } } ++c; } if (comment_begin && comment_end) { int size = (int)(comment_begin - Char(s)); String *stripmore = 0; stripped = NewStringWithSize(s, size); Printv(stripped, comment_end + 1, NIL); do { stripmore = Swig_strip_c_comments(stripped); if (stripmore) { Delete(stripped); stripped = stripmore; } } while (stripmore); } return stripped; } /* ----------------------------------------------------------------------------- * is_directory() * ----------------------------------------------------------------------------- */ static int is_directory(String *directory) { int last = Len(directory) - 1; int statres; struct stat st; char *dir = Char(directory); if (dir[last] == SWIG_FILE_DELIMITER[0]) { /* remove trailing slash - can cause S_ISDIR to fail on Windows, at least */ dir[last] = 0; statres = stat(dir, &st); dir[last] = SWIG_FILE_DELIMITER[0]; } else { statres = stat(dir, &st); } return (statres == 0 && S_ISDIR(st.st_mode)); } /* ----------------------------------------------------------------------------- * Swig_new_subdirectory() * * Create the subdirectory only if the basedirectory already exists as a directory. * basedirectory can be empty to indicate current directory but not NULL. * ----------------------------------------------------------------------------- */ String *Swig_new_subdirectory(String *basedirectory, String *subdirectory) { String *error = 0; int current_directory = Len(basedirectory) == 0; if (current_directory || is_directory(basedirectory)) { Iterator it; String *dir = NewString(basedirectory); List *subdirs = Split(subdirectory, SWIG_FILE_DELIMITER[0], INT_MAX); for (it = First(subdirs); it.item; it = Next(it)) { int result; String *subdirectory = it.item; Printf(dir, "%s", subdirectory); #ifdef _WIN32 result = _mkdir(Char(dir)); #else result = mkdir(Char(dir), 0777); #endif if (result != 0 && errno != EEXIST) { error = NewStringf("Cannot create directory %s: %s", dir, strerror(errno)); break; } if (!is_directory(dir)) { error = NewStringf("Cannot create directory %s: it may already exist but not be a directory", dir); break; } Printf(dir, SWIG_FILE_DELIMITER); } } else { error = NewStringf("Cannot create subdirectory %s under the base directory %s. Either the base does not exist as a directory or it is not readable.", subdirectory, basedirectory); } return error; } /* ----------------------------------------------------------------------------- * Swig_filename_correct() * * Corrects filename paths by removing duplicate delimiters and on non-unix * systems use the correct delimiter across the whole name. * ----------------------------------------------------------------------------- */ void Swig_filename_correct(String *filename) { int network_path = 0; if (Len(filename) >= 2) { const char *fname = Char(filename); if (fname[0] == '\\' && fname[1] == '\\') network_path = 1; if (fname[0] == '/' && fname[1] == '/') network_path = 1; } #if defined(_WIN32) /* accept Unix path separator on non-Unix systems */ Replaceall(filename, "/", SWIG_FILE_DELIMITER); #endif #if defined(__CYGWIN__) /* accept Windows path separator in addition to Unix path separator */ Replaceall(filename, "\\", SWIG_FILE_DELIMITER); #endif /* remove all duplicate file name delimiters */ while (Replaceall(filename, SWIG_FILE_DELIMITER SWIG_FILE_DELIMITER, SWIG_FILE_DELIMITER)) { } /* Network paths can start with a double slash on Windows - unremove the duplicate slash we just removed */ if (network_path) Insert(filename, 0, SWIG_FILE_DELIMITER); } /* ----------------------------------------------------------------------------- * Swig_filename_escape() * * Escapes backslashes in filename - for Windows * ----------------------------------------------------------------------------- */ String *Swig_filename_escape(String *filename) { String *adjusted_filename = Copy(filename); Swig_filename_correct(adjusted_filename); #if defined(_WIN32) /* Note not on Cygwin else filename is displayed with double '/' */ Replaceall(adjusted_filename, "\\", "\\\\"); #endif return adjusted_filename; } /* ----------------------------------------------------------------------------- * Swig_filename_escape() * * Escapes spaces in filename - for Makefiles * ----------------------------------------------------------------------------- */ String *Swig_filename_escape_space(String *filename) { String *adjusted_filename = Copy(filename); Swig_filename_correct(adjusted_filename); Replaceall(adjusted_filename, " ", "\\ "); return adjusted_filename; } /* ----------------------------------------------------------------------------- * Swig_filename_unescape() * * Remove double backslash escaping in filename - for Windows * ----------------------------------------------------------------------------- */ void Swig_filename_unescape(String *filename) { (void)filename; #if defined(_WIN32) Replaceall(filename, "\\\\", "\\"); #endif } /* ----------------------------------------------------------------------------- * Swig_storage_isextern() * * Determine if the storage class specifier is extern (but not externc) * ----------------------------------------------------------------------------- */ int Swig_storage_isextern(Node *n) { const String *storage = Getattr(n, "storage"); return storage ? Strcmp(storage, "extern") == 0 || Strncmp(storage, "extern ", 7) == 0 : 0; } /* ----------------------------------------------------------------------------- * Swig_storage_isexternc() * * Determine if the storage class specifier is externc (but not plain extern) * ----------------------------------------------------------------------------- */ int Swig_storage_isexternc(Node *n) { const String *storage = Getattr(n, "storage"); return storage ? Strcmp(storage, "externc") == 0 || Strncmp(storage, "externc ", 8) == 0 : 0; } /* ----------------------------------------------------------------------------- * Swig_storage_isstatic_custom() * * Determine if the storage class specifier is static * ----------------------------------------------------------------------------- */ int Swig_storage_isstatic_custom(Node *n, const_String_or_char_ptr storage_name) { const String *storage = Getattr(n, storage_name); return storage ? Strncmp(storage, "static", 6) == 0 : 0; } /* ----------------------------------------------------------------------------- * Swig_storage_isstatic() * * Determine if the storage class specifier is static * ----------------------------------------------------------------------------- */ int Swig_storage_isstatic(Node *n) { return Swig_storage_isstatic_custom(n, "storage"); } /* ----------------------------------------------------------------------------- * Swig_string_escape() * * Takes a string object and produces a string with escape codes added to it. * Octal escaping is used. The result is used for literal strings and characters * in C/C++ and also Java, Python, R and Ruby. * * The result is suitable for wrapping in single or double quotes to form a * character or string literal. * * Note that it's not safe to concatenate the results of escaping two strings * - you need to concatenate first and escape second. The problem case is * when the first string ends with a character which gets escaped using one or * two octal digits and the second string starts with a character which is an * octal digit. * ----------------------------------------------------------------------------- */ String *Swig_string_escape(String *s) { String *ns = NewStringEmpty(); int c = Getc(s); while (c != EOF) { if (c == '\n') { Printf(ns, "\\n"); } else if (c == '\r') { Printf(ns, "\\r"); } else if (c == '\t') { Printf(ns, "\\t"); } else if (c == '\\') { Printf(ns, "\\\\"); } else if (c == '\'') { Printf(ns, "\\'"); } else if (c == '\"') { Printf(ns, "\\\""); } else if (c >= 32 && c < 127) { Putc(c, ns); } else { int next_c = Getc(s); assert(c >= 0); if (next_c >= '0' && next_c < '8') { /* We need to emit 3 octal digits. */ Printf(ns, "\\%03o", c); } else { Printf(ns, "\\%o", c); } c = next_c; continue; } c = Getc(s); } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_csharpescape() * * Takes a string object and produces a string with escape codes added to it * suitable for use as a C# string or character literal. * ----------------------------------------------------------------------------- */ static String *Swig_string_csharpescape(String *s) { String *ns; int c; ns = NewStringEmpty(); while ((c = Getc(s)) != EOF) { if (c == '\n') { Printf(ns, "\\n"); } else if (c == '\r') { Printf(ns, "\\r"); } else if (c == '\t') { Printf(ns, "\\t"); } else if (c == '\\') { Printf(ns, "\\\\"); } else if (c == '\'') { Printf(ns, "\\'"); } else if (c == '\"') { Printf(ns, "\\\""); } else if (c >= 32 && c < 127) { Putc(c, ns); } else { assert(c >= 0); // Emit 4 hex digits in case the next character is a hex digit. Printf(ns, "\\x%04X", c); } } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_goescape() * * Takes a string object and produces a string with escape codes added to it * suitable for use as a Go string or character literal. * ----------------------------------------------------------------------------- */ static String *Swig_string_goescape(String *s) { String *ns; int c; ns = NewStringEmpty(); while ((c = Getc(s)) != EOF) { if (c == '\n') { Printf(ns, "\\n"); } else if (c == '\r') { Printf(ns, "\\r"); } else if (c == '\t') { Printf(ns, "\\t"); } else if (c == '\\') { Printf(ns, "\\\\"); } else if (c >= 32 && c < 127 && c != '\'' && c != '"') { Putc(c, ns); } else { // In Go, \' isn't valid in a double quoted string, while \" isn't valid // in a single quoted rune, so to avoid needing two different escaping // functions we always escape both using hex escapes. assert(c >= 0); Printf(ns, "\\x%02x", c); } } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_upper() * * Takes a string object and returns a copy that is uppercase * ----------------------------------------------------------------------------- */ String *Swig_string_upper(String *s) { String *ns; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { Putc(toupper(c), ns); } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_lower() * * Takes a string object and returns a copy that is lowercase * ----------------------------------------------------------------------------- */ String *Swig_string_lower(String *s) { String *ns; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { Putc(tolower(c), ns); } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_title() * * Takes a string object and returns a copy that is lowercase with first letter * capitalized * ----------------------------------------------------------------------------- */ String *Swig_string_title(String *s) { String *ns; int first = 1; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { Putc(first ? toupper(c) : tolower(c), ns); first = 0; } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_ccase() * * Takes a string object and returns a copy that is lowercase with the first * letter capitalized and the one following '_', which are removed. * * camel_case -> CamelCase * camelCase -> CamelCase * ----------------------------------------------------------------------------- */ static String *Swig_string_ccase(String *s) { String *ns; int first = 1; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { if (c == '_') { first = 1; continue; } Putc(first ? toupper(c) : c, ns); first = 0; } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_lccase() * * Takes a string object and returns a copy with the character after * each '_' capitalised, and the '_' removed. The first character is * also forced to lowercase. * * camel_case -> camelCase * CamelCase -> camelCase * ----------------------------------------------------------------------------- */ static String *Swig_string_lccase(String *s) { String *ns; int first = 1; int after_underscore = 0; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { if (c == '_') { after_underscore = 1; continue; } if (first) { Putc(tolower(c), ns); first = 0; } else { Putc(after_underscore ? toupper(c) : c, ns); } after_underscore = 0; } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_ucase() * * This is the reverse case of ccase, ie * * CamelCase -> camel_case * get2D -> get_2d * asFloat2 -> as_float2 * ----------------------------------------------------------------------------- */ static String *Swig_string_ucase(String *s) { String *ns; int c; int lastC = 0; int nextC = 0; int underscore = 0; ns = NewStringEmpty(); /* We insert a underscore when: 1. Lower case char followed by upper case char getFoo > get_foo; getFOo > get_foo; GETFOO > getfoo 2. Number preceded by char and not end of string get2D > get_2d; get22D > get_22d; GET2D > get_2d but: asFloat2 > as_float2 */ Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { nextC = Getc(s); Ungetc(nextC, s); if (isdigit(c) && isalpha(lastC) && nextC != EOF) underscore = 1; else if (isupper(c) && isalpha(lastC) && !isupper(lastC)) underscore = 1; lastC = c; if (underscore) { Putc('_', ns); underscore = 0; } Putc(tolower(c), ns); } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_first_upper() * * Make the first character in the string uppercase, leave all the * rest the same. This is used by the Ruby module to provide backwards * compatibility with the old way of naming classes and constants. For * more info see the Ruby documentation. * * firstUpper -> FirstUpper * ----------------------------------------------------------------------------- */ static String *Swig_string_first_upper(String *s) { String *ns = NewStringEmpty(); char *cs = Char(s); if (cs && cs[0] != 0) { Putc(toupper((int)cs[0]), ns); Append(ns, cs + 1); } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_first_lower() * * Make the first character in the string lowercase, leave all the * rest the same. This is used by the Ruby module to provide backwards * compatibility with the old way of naming classes and constants. For * more info see the Ruby documentation. * * firstLower -> FirstLower * ----------------------------------------------------------------------------- */ static String *Swig_string_first_lower(String *s) { String *ns = NewStringEmpty(); char *cs = Char(s); if (cs && cs[0] != 0) { Putc(tolower((int)cs[0]), ns); Append(ns, cs + 1); } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_schemify() * * Replace underscores with dashes, to make identifiers look nice to Schemers. * * under_scores -> under-scores * ----------------------------------------------------------------------------- */ static String *Swig_string_schemify(String *s) { String *ns = NewString(s); Replaceall(ns, "_", "-"); return ns; } static String *string_mangle(String *s) { return Swig_name_mangle_string(s); } /* ----------------------------------------------------------------------------- * Swig_scopename_split() * * Take a qualified name like "A::B::C" and splits off the last name. * In this case, returns "C" as last and "A::B" as prefix. * Always returns non NULL for last, but prefix may be NULL if there is no prefix. * ----------------------------------------------------------------------------- */ void Swig_scopename_split(const String *s, String **rprefix, String **rlast) { char *tmp = Char(s); char *c = tmp; char *cc = c; char *co = 0; if (!strstr(c, "::")) { *rprefix = 0; *rlast = Copy(s); } co = strstr(cc, "operator "); if (co) { if (co == cc) { *rprefix = 0; *rlast = Copy(s); return; } else { *rprefix = NewStringWithSize(cc, (int)(co - cc - 2)); *rlast = NewString(co); return; } } while (*c) { if ((*c == ':') && (*(c + 1) == ':')) { cc = c; c += 2; } else { if (*c == '<') { int level = 1; c++; while (*c && level) { if (*c == '<') level++; if (*c == '>') level--; c++; } } else { c++; } } } if (cc != tmp) { *rprefix = NewStringWithSize(tmp, (int)(cc - tmp)); *rlast = NewString(cc + 2); return; } else { *rprefix = 0; *rlast = Copy(s); } } /* ----------------------------------------------------------------------------- * Swig_scopename_prefix() * * Take a qualified name like "A::B::C" and return the scope name. * In this case, "A::B". Returns NULL if there is no base. * ----------------------------------------------------------------------------- */ String *Swig_scopename_prefix(const String *s) { char *tmp = Char(s); char *c = tmp; char *cc = c; char *co = 0; if (!strstr(c, "::")) return 0; co = strstr(cc, "operator "); if (co) { if (co == cc) { return 0; } else { String *prefix = NewStringWithSize(cc, (int)(co - cc - 2)); return prefix; } } while (*c) { if ((*c == ':') && (*(c + 1) == ':')) { cc = c; c += 2; } else { if (*c == '<') { int level = 1; c++; while (*c && level) { if (*c == '<') level++; if (*c == '>') level--; c++; } } else { c++; } } } if (cc != tmp) { return NewStringWithSize(tmp, (int)(cc - tmp)); } else { return 0; } } /* ----------------------------------------------------------------------------- * Swig_scopename_last() * * Take a qualified name like "A::B::C" and returns the last. In this * case, "C". * ----------------------------------------------------------------------------- */ String *Swig_scopename_last(const String *s) { char *tmp = Char(s); char *c = tmp; char *cc = c; char *co = 0; if (!strstr(c, "::")) return NewString(s); co = strstr(cc, "operator "); if (co) { return NewString(co); } while (*c) { if ((*c == ':') && (*(c + 1) == ':')) { c += 2; cc = c; } else { if (*c == '<') { int level = 1; c++; while (*c && level) { if (*c == '<') level++; if (*c == '>') level--; c++; } } else { c++; } } } return NewString(cc); } /* ----------------------------------------------------------------------------- * Swig_scopename_first() * * Take a qualified name like "A::B::C" and returns the first scope name. * In this case, "A". Returns NULL if there is no base. * ----------------------------------------------------------------------------- */ String *Swig_scopename_first(const String *s) { char *tmp = Char(s); char *c = tmp; char *co = 0; if (!strstr(c, "::")) return 0; co = strstr(c, "operator "); if (co) { if (co == c) { return 0; } } else { co = c + Len(s); } while (*c && (c != co)) { if ((*c == ':') && (*(c + 1) == ':')) { break; } else { if (*c == '<') { int level = 1; c++; while (*c && level) { if (*c == '<') level++; if (*c == '>') level--; c++; } } else { c++; } } } if (*c && (c != tmp)) { return NewStringWithSize(tmp, (int)(c - tmp)); } else { return 0; } } /* ----------------------------------------------------------------------------- * Swig_scopename_suffix() * * Take a qualified name like "A::B::C" and returns the suffix. * In this case, "B::C". Returns NULL if there is no suffix. * ----------------------------------------------------------------------------- */ String *Swig_scopename_suffix(const String *s) { char *tmp = Char(s); char *c = tmp; char *co = 0; if (!strstr(c, "::")) return 0; co = strstr(c, "operator "); if (co) { if (co == c) return 0; } while (*c) { if ((*c == ':') && (*(c + 1) == ':')) { break; } else { if (*c == '<') { int level = 1; c++; while (*c && level) { if (*c == '<') level++; if (*c == '>') level--; c++; } } else { c++; } } } if (*c && (c != tmp)) { return NewString(c + 2); } else { return 0; } } /* ----------------------------------------------------------------------------- * Swig_scopename_tolist() * * Take a qualified scope name like "A::B::C" and convert it to a list. * In this case, return a list of 3 elements "A", "B", "C". * Returns an empty list if the input is empty. * ----------------------------------------------------------------------------- */ List *Swig_scopename_tolist(const String *s) { List *scopes = NewList(); String *name = Len(s) == 0 ? 0 : NewString(s); while (name) { String *last = 0; String *prefix = 0; Swig_scopename_split(name, &prefix, &last); Insert(scopes, 0, last); Delete(last); Delete(name); name = prefix; } Delete(name); return scopes; } /* ----------------------------------------------------------------------------- * Swig_scopename_isvalid() * * Checks that s is a valid scopename (C++ namespace scope) * ----------------------------------------------------------------------------- */ int Swig_scopename_isvalid(const String *s) { List *scopes = Swig_scopename_tolist(s); int valid = 0; Iterator si; for (si = First(scopes); si.item; si = Next(si)) { String *subscope = si.item; valid = subscope && Len(subscope) > 0; if (valid) valid = Swig_symbol_isvalid(subscope); if (!valid) break; } return valid; } /* ----------------------------------------------------------------------------- * Swig_scopename_check() * * Checks to see if a name is qualified with a scope name, examples: * foo -> 0 * ::foo -> 1 * foo::bar -> 1 * foo< ::bar > -> 0 * ----------------------------------------------------------------------------- */ int Swig_scopename_check(const String *s) { char *c = Char(s); char *co = strstr(c, "operator "); if (co) { if (co == c) return 0; } if (!strstr(c, "::")) return 0; while (*c) { if ((*c == ':') && (*(c + 1) == ':')) { return 1; } else { if (*c == '<') { int level = 1; c++; while (*c && level) { if (*c == '<') level++; if (*c == '>') level--; c++; } } else { c++; } } } return 0; } /* ----------------------------------------------------------------------------- * Swig_string_command() * * Feature removed in SWIG 4.1.0. * ----------------------------------------------------------------------------- */ static String *Swig_string_command(String *s) { Swig_error("SWIG", Getline(s), "Command encoder no longer supported - use regex encoder instead, command:%s\n", s); Exit(EXIT_FAILURE); return 0; } /* ----------------------------------------------------------------------------- * Swig_string_strip() * * Strip given prefix from identifiers * * Printf(stderr,"%(strip:[wx])s","wxHello") -> Hello * ----------------------------------------------------------------------------- */ static String *Swig_string_strip(String *s) { String *ns; if (!Len(s)) { ns = NewString(s); } else { const char *cs = Char(s); const char *ce = Strchr(cs, ']'); if (*cs != '[' || !ce) { ns = NewString(s); } else { String *fmt = NewStringf("%%.%ds", ce-cs-1); String *prefix = NewStringf(fmt, cs+1); if (0 == Strncmp(ce+1, prefix, Len(prefix))) { ns = NewString(ce+1+Len(prefix)); } else { ns = NewString(ce+1); } } } return ns; } /* ----------------------------------------------------------------------------- * Swig_string_rstrip() * * Strip given suffix from identifiers * * Printf(stderr,"%(rstrip:[Cls])s","HelloCls") -> Hello * ----------------------------------------------------------------------------- */ static String *Swig_string_rstrip(String *s) { String *ns; int len = Len(s); if (!len) { ns = NewString(s); } else { const char *cs = Char(s); const char *ce = Strchr(cs, ']'); if (*cs != '[' || !ce) { ns = NewString(s); } else { String *fmt = NewStringf("%%.%ds", ce-cs-1); String *suffix = NewStringf(fmt, cs+1); int suffix_len = Len(suffix); if (0 == Strncmp(cs+len-suffix_len, suffix, suffix_len)) { int copy_len = len-suffix_len-(int)(ce+1-cs); ns = NewStringWithSize(ce+1, copy_len); } else { ns = NewString(ce+1); } } } return ns; } /* ----------------------------------------------------------------------------- * Swig_offset_string() * * Insert number tabs before each new line in s * ----------------------------------------------------------------------------- */ void Swig_offset_string(String *s, int number) { char *res, *p, *end, *start; /* count a number of lines in s */ int lines = 1; int len = Len(s); if (len == 0) return; start = strchr(Char(s), '\n'); while (start) { ++lines; start = strchr(start + 1, '\n'); } /* do not count pending new line */ if ((Char(s))[len-1] == '\n') --lines; /* allocate a temporary storage for a padded string */ res = (char*)Malloc(len + lines * number * 2 + 1); res[len + lines * number * 2] = 0; /* copy lines to res, prepending tabs to each line */ p = res; /* output pointer */ start = Char(s); /* start of a current line */ end = strchr(start, '\n'); /* end of a current line */ while (end) { memset(p, ' ', number*2); p += number*2; memcpy(p, start, end - start + 1); p += end - start + 1; start = end + 1; end = strchr(start, '\n'); } /* process the last line */ if (*start) { memset(p, ' ', number*2); p += number*2; strcpy(p, start); } /* replace 's' contents with 'res' */ Clear(s); Append(s, res); Free(res); } #ifdef HAVE_PCRE #define PCRE2_CODE_UNIT_WIDTH 8 #include static int split_regex_pattern_subst(String *s, String **pattern, String **subst, const char **input) { const char *pats, *pate; const char *subs, *sube; /* Locate the search pattern */ const char *p = Char(s); if (*p++ != '/') goto err_out; pats = p; p = strchr(p, '/'); if (!p) goto err_out; pate = p; /* Locate the substitution string */ subs = ++p; p = strchr(p, '/'); if (!p) goto err_out; sube = p; *pattern = NewStringWithSize(pats, (int)(pate - pats)); *subst = NewStringWithSize(subs, (int)(sube - subs)); *input = p + 1; return 1; err_out: Swig_error("SWIG", Getline(s), "Invalid regex substitution: '%s'.\n", s); Exit(EXIT_FAILURE); return 0; } /* This function copies len characters from src to dst, possibly applying case conversions to them: if convertCase is 1, to upper case and if it is -1, to lower * case. If convertNextOnly is 1, only a single character is converted (and convertCase is reset), otherwise all of them are. */ static void copy_with_maybe_case_conversion(String *dst, const char *src, int len, int *convertCase, int convertNextOnly) { /* Deal with the trivial cases first. */ if (!len) return; if (!*convertCase) { Write(dst, src, len); return; } /* If we must convert only the first character, do it and write the rest at once. */ if (convertNextOnly) { int src_char = *src; Putc(*convertCase == 1 ? toupper(src_char) : tolower(src_char), dst); *convertCase = 0; if (len > 1) { Write(dst, src + 1, len - 1); } } else { /* We need to convert all characters. */ int i; for (i = 0; i < len; i++, src++) { int src_char = *src; Putc(*convertCase == 1 ? toupper(src_char) : tolower(src_char), dst); } } } static String *replace_captures(int num_captures, const char *input, String *subst, size_t captures[], String *pattern, String *s) { int convertCase = 0, convertNextOnly = 0; String *result = NewStringEmpty(); const char *p = Char(subst); while (*p) { /* Copy part without substitutions */ const char *q = strchr(p, '\\'); if (!q) { copy_with_maybe_case_conversion(result, p, (int)strlen(p), &convertCase, convertNextOnly); break; } copy_with_maybe_case_conversion(result, p, (int)(q - p), &convertCase, convertNextOnly); p = q + 1; /* Handle substitution */ if (*p == '\0') { Putc('\\', result); } else if (isdigit((unsigned char)*p)) { int group = *p++ - '0'; if (group < num_captures) { int l = (int)captures[group*2], r = (int)captures[group*2 + 1]; if (l != -1) { copy_with_maybe_case_conversion(result, input + l, r - l, &convertCase, convertNextOnly); } } else { Swig_error("SWIG", Getline(s), "PCRE capture replacement failed while matching \"%s\" using \"%s\" - request for group %d is greater than the number of captures %d.\n", Char(pattern), input, group, num_captures-1); } } else { /* Handle Perl-like case conversion escapes. */ switch (*p) { case 'u': convertCase = 1; convertNextOnly = 1; break; case 'U': convertCase = 1; convertNextOnly = 0; break; case 'l': convertCase = -1; convertNextOnly = 1; break; case 'L': convertCase = -1; convertNextOnly = 0; break; case 'E': convertCase = 0; break; default: Swig_error("SWIG", Getline(s), "Unrecognized escape character '%c' in the replacement string \"%s\".\n", *p, Char(subst)); } p++; } } return result; } /* ----------------------------------------------------------------------------- * Swig_string_regex() * * Executes a regular expression substitution. For example: * * Printf(stderr,"gsl%(regex:/GSL_(.*)_/\\1/)s", "GSL_Hello_") -> gslHello * ----------------------------------------------------------------------------- */ static String *Swig_string_regex(String *s) { const int pcre_options = 0; String *res = 0; pcre2_code *compiled_pat = 0; const char *input; PCRE2_UCHAR pcre_error[256]; int pcre_errornum; size_t pcre_errorpos; String *pattern = 0, *subst = 0; size_t *captures = 0; pcre2_match_data *match_data = 0; if (split_regex_pattern_subst(s, &pattern, &subst, &input)) { int rc; compiled_pat = pcre2_compile( (PCRE2_SPTR8)Char(pattern), PCRE2_ZERO_TERMINATED, pcre_options, &pcre_errornum, &pcre_errorpos, NULL); if (!compiled_pat) { pcre2_get_error_message (pcre_errornum, pcre_error, sizeof pcre_error); Swig_error("SWIG", Getline(s), "PCRE compilation failed: '%s' in '%s':%i.\n", pcre_error, Char(pattern), pcre_errorpos); Exit(EXIT_FAILURE); } match_data = pcre2_match_data_create_from_pattern (compiled_pat, NULL); rc = pcre2_match(compiled_pat, (PCRE2_SPTR8)input, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL); captures = pcre2_get_ovector_pointer (match_data); if (rc >= 0) { res = replace_captures(rc, input, subst, captures, pattern, s); } else if (rc != PCRE2_ERROR_NOMATCH) { Swig_error("SWIG", Getline(s), "PCRE execution failed: error %d while matching \"%s\" using \"%s\".\n", rc, Char(pattern), input); Exit(EXIT_FAILURE); } } Delete(pattern); Delete(subst); pcre2_code_free(compiled_pat); pcre2_match_data_free(match_data); return res ? res : NewStringEmpty(); } String *Swig_pcre_version(void) { int len = pcre2_config(PCRE2_CONFIG_VERSION, NULL); char *buf = Malloc(len); String *result; pcre2_config(PCRE2_CONFIG_VERSION, buf); result = NewStringf("PCRE2 Version: %s", buf); Free(buf); return result; } #else String *Swig_string_regex(String *s) { Swig_error("SWIG", Getline(s), "PCRE regex support not enabled in this SWIG build.\n"); Exit(EXIT_FAILURE); return 0; } String *Swig_pcre_version(void) { return NewStringf("PCRE not used"); } #endif /* ------------------------------------------------------------ * Swig_is_generated_overload() * Check if the function is an automatically generated * overload created because a method has default parameters. * ------------------------------------------------------------ */ int Swig_is_generated_overload(Node *n) { Node *base_method = Getattr(n, "sym:overloaded"); Node *default_args = Getattr(n, "defaultargs"); return ((base_method != NULL) && (default_args != NULL) && (base_method == default_args)); } /* ----------------------------------------------------------------------------- * Swig_item_in_list() * * If the input item is in the list, return the item. * Note: uses DohCmp for comparisons so for a List of String *, Strcmp is ultimately * used for item comparisons to determine if a string is in the list. * ----------------------------------------------------------------------------- */ Node *Swig_item_in_list(List *list, const DOH *item) { Node *found_item = 0; if (list) { Iterator it; for (it = First(list); it.item; it = Next(it)) { if (DohCmp(item, it.item) == 0) { found_item = it.item; break; } } } return found_item; } /* ----------------------------------------------------------------------------- * Swig_init() * * Initialize the SWIG core * ----------------------------------------------------------------------------- */ void Swig_init(void) { /* Set some useful string encoding methods */ DohEncoding("escape", Swig_string_escape); DohEncoding("csharpescape", Swig_string_csharpescape); DohEncoding("goescape", Swig_string_goescape); DohEncoding("upper", Swig_string_upper); DohEncoding("lower", Swig_string_lower); DohEncoding("title", Swig_string_title); DohEncoding("ctitle", Swig_string_ccase); DohEncoding("lctitle", Swig_string_lccase); DohEncoding("utitle", Swig_string_ucase); DohEncoding("mangle", string_mangle); DohEncoding("command", Swig_string_command); DohEncoding("schemify", Swig_string_schemify); DohEncoding("strip", Swig_string_strip); DohEncoding("rstrip", Swig_string_rstrip); DohEncoding("regex", Swig_string_regex); /* aliases for the case encoders */ DohEncoding("uppercase", Swig_string_upper); DohEncoding("lowercase", Swig_string_lower); DohEncoding("camelcase", Swig_string_ccase); DohEncoding("lowercamelcase", Swig_string_lccase); DohEncoding("undercase", Swig_string_ucase); DohEncoding("firstuppercase", Swig_string_first_upper); DohEncoding("firstlowercase", Swig_string_first_lower); /* Initialize typemaps */ Swig_typemap_init(); /* Initialize symbol table */ Swig_symbol_init(); /* Initialize type system */ SwigType_typesystem_init(); /* Initialize template system */ SwigType_template_init(); } swig-4.4.0/Source/Swig/swig.h0000664000175000017500000005371315075443613015664 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swig.h * * Header file for the SWIG core. * ----------------------------------------------------------------------------- */ #ifndef SWIG_SWIG_H #define SWIG_SWIG_H #include "swigconfig.h" #include #include #include #include #ifdef __cplusplus extern "C" { #endif #include "doh.h" /* Status codes */ #define SWIG_OK 1 #define SWIG_ERROR 0 #define SWIG_NOWRAP 0 /* Global macros */ #define NSPACE_SEPARATOR "." /* Namespace separator for the nspace feature - this should be changed to a target language configurable variable */ #define NSPACE_TODO 0 /* Languages that still need to implement and test the nspace feature use this */ /* Short names for common data types */ typedef DOH String; typedef DOH Hash; typedef DOH List; typedef DOH String_or_char; typedef DOH File; typedef DOH Parm; typedef DOH ParmList; typedef DOH Node; typedef DOH Symtab; typedef DOH Typetab; typedef DOH SwigType; /* --- Legacy DataType interface. These type codes are provided solely for backwards compatibility with older modules --- */ /* --- The ordering of type values is used to determine type-promotion in the parser. Do not change */ /* Numeric types */ #define T_BOOL 1 #define T_SCHAR 2 #define T_UCHAR 3 #define T_SHORT 4 #define T_USHORT 5 #define T_INT 7 #define T_UINT 8 #define T_LONG 9 #define T_ULONG 10 #define T_LONGLONG 11 #define T_ULONGLONG 12 #define T_FLOAT 20 #define T_DOUBLE 21 #define T_LONGDOUBLE 22 #define T_FLTCPLX 23 #define T_DBLCPLX 24 #define T_AUTO 26 #define T_COMPLEX T_DBLCPLX /* non-numeric */ #define T_CHAR 29 #define T_WCHAR 30 #define T_USER 31 #define T_VOID 32 #define T_STRING 33 #define T_POINTER 34 #define T_REFERENCE 35 #define T_ARRAY 36 #define T_FUNCTION 37 #define T_MPOINTER 38 #define T_VARARGS 39 #define T_RVALUE_REFERENCE 40 #define T_WSTRING 41 #define T_UNKNOWN 42 /* --- File interface --- */ #include "swigfile.h" /* --- Command line parsing --- */ #include "swigopt.h" /* --- Scanner Interface --- */ #include "swigscan.h" /* --- Functions for manipulating the string-based type encoding --- */ extern SwigType *NewSwigType(int typecode); extern SwigType *SwigType_del_element(SwigType *t); extern SwigType *SwigType_add_pointer(SwigType *t); extern SwigType *SwigType_add_memberpointer(SwigType *t, const_String_or_char_ptr qual); extern SwigType *SwigType_del_memberpointer(SwigType *t); extern SwigType *SwigType_del_pointer(SwigType *t); extern SwigType *SwigType_add_array(SwigType *t, const_String_or_char_ptr size); extern SwigType *SwigType_del_array(SwigType *t); extern SwigType *SwigType_pop_arrays(SwigType *t); extern SwigType *SwigType_add_reference(SwigType *t); extern SwigType *SwigType_del_reference(SwigType *t); extern SwigType *SwigType_add_rvalue_reference(SwigType *t); extern SwigType *SwigType_del_rvalue_reference(SwigType *t); extern SwigType *SwigType_add_variadic(SwigType *t); extern SwigType *SwigType_del_variadic(SwigType *t); extern SwigType *SwigType_add_qualifier(SwigType *t, const_String_or_char_ptr qual); extern SwigType *SwigType_del_qualifier(SwigType *t); extern SwigType *SwigType_add_function(SwigType *t, ParmList *parms); extern SwigType *SwigType_add_template(SwigType *t, ParmList *parms); extern SwigType *SwigType_pop_function(SwigType *t); extern SwigType *SwigType_pop_function_qualifiers(SwigType *t); extern SwigType *SwigType_function_parms_only(ParmList *parms); extern ParmList *SwigType_function_parms(const SwigType *t, Node *file_line_node); extern List *SwigType_split(const SwigType *t); extern String *SwigType_pop(SwigType *t); extern void SwigType_push(SwigType *t, String *s); extern SwigType *SwigType_last(SwigType *t); extern List *SwigType_parmlist(const SwigType *p); extern String *SwigType_parm(const SwigType *p); extern String *SwigType_str(const SwigType *s, const_String_or_char_ptr id); extern String *SwigType_lstr(const SwigType *s, const_String_or_char_ptr id); extern String *SwigType_rcaststr(const SwigType *s, const_String_or_char_ptr id); extern String *SwigType_lcaststr(const SwigType *s, const_String_or_char_ptr id); extern String *SwigType_manglestr(const SwigType *t); extern SwigType *SwigType_ltype(const SwigType *t); extern int SwigType_ispointer(const SwigType *t); extern int SwigType_ispointer_return(const SwigType *t); extern int SwigType_isfunctionpointer(const SwigType *t); extern int SwigType_ismemberpointer(const SwigType *t); extern int SwigType_isreference(const SwigType *t); extern int SwigType_isreference_return(const SwigType *t); extern int SwigType_isrvalue_reference(const SwigType *t); extern int SwigType_isvariadic(const SwigType *t); extern int SwigType_isarray(const SwigType *t); extern int SwigType_prefix_is_simple_1D_array(const SwigType *t); extern int SwigType_isfunction(const SwigType *t); extern int SwigType_isqualifier(const SwigType *t); extern int SwigType_isconst(const SwigType *t); extern int SwigType_issimple(const SwigType *t); extern int SwigType_ismutable(const SwigType *t); extern int SwigType_isvarargs(const SwigType *t); extern int SwigType_istemplate(const SwigType *t); extern int SwigType_isenum(const SwigType *t); extern int SwigType_check_decl(const SwigType *t, const_String_or_char_ptr decl); extern SwigType *SwigType_strip_qualifiers(const SwigType *t); extern SwigType *SwigType_strip_single_qualifier(const SwigType *t); extern SwigType *SwigType_functionpointer_decompose(SwigType *t); extern String *SwigType_base(const SwigType *t); extern String *SwigType_namestr(const SwigType *t); extern String *SwigType_templateprefix(const SwigType *t); extern String *SwigType_templatesuffix(const SwigType *t); extern String *SwigType_istemplate_templateprefix(const SwigType *t); extern String *SwigType_istemplate_only_templateprefix(const SwigType *t); extern String *SwigType_templateargs(const SwigType *t); extern String *SwigType_prefix(const SwigType *t); extern int SwigType_array_ndim(const SwigType *t); extern String *SwigType_array_getdim(const SwigType *t, int n); extern void SwigType_array_setdim(SwigType *t, int n, const_String_or_char_ptr rep); extern SwigType *SwigType_array_type(const SwigType *t); extern SwigType *SwigType_default_create(const SwigType *ty); extern SwigType *SwigType_default_deduce(const SwigType *t); extern void SwigType_typename_replace(SwigType *t, String *pat, String *rep); extern void SwigType_variadic_replace(SwigType *t, Parm *unexpanded_variadic_parm, ParmList *expanded_variadic_parms); extern SwigType *SwigType_remove_global_scope_prefix(const SwigType *t); extern SwigType *SwigType_alttype(const SwigType *t, int ltmap); /* --- Type-system management --- */ extern void SwigType_typesystem_init(void); extern int SwigType_typedef(const SwigType *type, const_String_or_char_ptr name); extern int SwigType_typedef_class(const_String_or_char_ptr name); extern int SwigType_typedef_using(const_String_or_char_ptr qname); extern void SwigType_inherit(String *subclass, String *baseclass, String *cast, String *conversioncode); extern int SwigType_issubtype(const SwigType *subtype, const SwigType *basetype); extern void SwigType_scope_alias(String *aliasname, Typetab *t); extern void SwigType_using_scope(Typetab *t); extern void SwigType_new_scope(const_String_or_char_ptr name); extern void SwigType_inherit_scope(Typetab *scope); extern Typetab *SwigType_pop_scope(void); extern Typetab *SwigType_set_scope(Typetab *h); extern void SwigType_print_scope(void); extern SwigType *SwigType_typedef_resolve(const SwigType *t); extern SwigType *SwigType_typedef_resolve_all(const SwigType *t); extern SwigType *SwigType_typedef_qualified(const SwigType *t); extern int SwigType_istypedef(const SwigType *t); extern int SwigType_isclass(const SwigType *t); extern void SwigType_attach_symtab(Symtab *syms); extern void SwigType_remember(const SwigType *t); extern void SwigType_remember_clientdata(const SwigType *t, const_String_or_char_ptr clientdata); extern void SwigType_remember_mangleddata(String *mangled, const_String_or_char_ptr clientdata); extern void (*SwigType_remember_trace(void (*tf) (const SwigType *, String *, String *))) (const SwigType *, String *, String *); extern void SwigType_emit_type_table(File *f_headers, File *f_table); extern int SwigType_type(const SwigType *t); /* --- Symbol table module --- */ extern void Swig_symbol_print_tables(Symtab *symtab); extern void Swig_symbol_print_tables_summary(void); extern void Swig_symbol_print_symbols(void); extern void Swig_symbol_print_csymbols(void); extern void Swig_symbol_init(void); extern void Swig_symbol_setscopename(const_String_or_char_ptr name); extern String *Swig_symbol_getscopename(void); extern String *Swig_symbol_qualifiedscopename(Symtab *symtab); extern String *Swig_symbol_qualified_language_scopename(Symtab *symtab); extern Symtab *Swig_symbol_newscope(void); extern Symtab *Swig_symbol_setscope(Symtab *); extern Symtab *Swig_symbol_getscope(const_String_or_char_ptr symname); extern Symtab *Swig_symbol_global_scope(void); extern Symtab *Swig_symbol_current(void); extern Symtab *Swig_symbol_popscope(void); extern Node *Swig_symbol_add(const_String_or_char_ptr symname, Node *n); extern void Swig_symbol_conflict_warn(Node *n, Node *c, const String *symname, int inclass); extern void Swig_symbol_cadd(const_String_or_char_ptr symname, Node *n); extern Node *Swig_symbol_clookup(const_String_or_char_ptr symname, Symtab *tab); extern Node *Swig_symbol_clookup_check(const_String_or_char_ptr symname, Symtab *tab, Node *(*checkfunc) (Node *)); extern Node *Swig_symbol_clookup_no_inherit(const_String_or_char_ptr name, Symtab *n); extern Symtab *Swig_symbol_cscope(const_String_or_char_ptr symname, Symtab *tab); extern Node *Swig_symbol_clookup_local(const_String_or_char_ptr symname, Symtab *tab); extern Node *Swig_symbol_clookup_local_check(const_String_or_char_ptr symname, Symtab *tab, Node *(*checkfunc) (Node *)); extern String *Swig_symbol_qualified(Node *n); extern Node *Swig_symbol_isoverloaded(Node *n); extern void Swig_symbol_remove(Node *n); extern void Swig_symbol_fix_overname(Node *n); extern void Swig_symbol_alias(const_String_or_char_ptr aliasname, Symtab *tab); extern void Swig_symbol_inherit(Symtab *tab); extern SwigType *Swig_symbol_type_qualify(const SwigType *ty, Symtab *tab); extern String *Swig_symbol_string_qualify(String *s, Symtab *tab); extern SwigType *Swig_symbol_typedef_reduce(const SwigType *ty, Symtab *tab); extern ParmList *Swig_symbol_template_defargs(Parm *parms, Parm *targs, Symtab *tscope, Symtab *tsdecl); extern SwigType *Swig_symbol_template_deftype(const SwigType *type, Symtab *tscope); extern SwigType *Swig_symbol_template_param_eval(const SwigType *p, Symtab *symtab); extern int Swig_symbol_isvalid(const String *s); /* --- Parameters and Parameter Lists --- */ #include "swigparm.h" extern String *ParmList_errorstr(ParmList *); extern int ParmList_is_compactdefargs(ParmList *p); /* --- Parse tree support --- */ #include "swigtree.h" /* -- Wrapper function Object */ #include "swigwrap.h" /* --- Naming functions --- */ extern void Swig_name_register(const_String_or_char_ptr method, const_String_or_char_ptr format); extern void Swig_name_unregister(const_String_or_char_ptr method); extern String *Swig_name_type(const_String_or_char_ptr tname); extern String *Swig_name_mangle_string(const String *s); extern String *Swig_name_mangle_type(const SwigType *s); extern String *Swig_name_wrapper(const_String_or_char_ptr fname); extern String *Swig_name_member(const_String_or_char_ptr nspace, const_String_or_char_ptr classname, const_String_or_char_ptr membername); extern String *Swig_name_get(const_String_or_char_ptr nspace, const_String_or_char_ptr vname); extern String *Swig_name_set(const_String_or_char_ptr nspace, const_String_or_char_ptr vname); extern String *Swig_name_construct(const_String_or_char_ptr nspace, const_String_or_char_ptr classname); extern String *Swig_name_copyconstructor(const_String_or_char_ptr nspace, const_String_or_char_ptr classname); extern String *Swig_name_destroy(const_String_or_char_ptr nspace, const_String_or_char_ptr classname); extern String *Swig_name_disown(const_String_or_char_ptr nspace, const_String_or_char_ptr classname); extern void Swig_naming_init(void); extern void Swig_name_namewarn_add(String *prefix, String *name, SwigType *decl, Hash *namewrn); extern void Swig_name_rename_add(String *prefix, String *name, SwigType *decl, Hash *namewrn, ParmList *declaratorparms); extern void Swig_name_inherit(String *base, String *derived); extern List *Swig_make_inherit_list(String *clsname, List *names, String *Namespaceprefix); extern void Swig_inherit_base_symbols(List *bases); extern int Swig_need_protected(Node *n); extern int Swig_need_redefined_warn(Node *a, Node *b, int InClass); extern String *Swig_name_make(Node *n, String *prefix, const_String_or_char_ptr cname, SwigType *decl, String *oldname); extern String *Swig_name_warning(Node *n, String *prefix, String *name, SwigType *decl); extern String *Swig_name_str(Node *n); extern String *Swig_name_decl(Node *n); extern String *Swig_name_fulldecl(Node *n); /* --- parameterized rename functions --- */ extern void Swig_name_object_set(Hash *namehash, String *name, SwigType *decl, DOH *object); extern DOH *Swig_name_object_get(Hash *namehash, String *prefix, String *name, SwigType *decl); extern void Swig_name_object_inherit(Hash *namehash, String *base, String *derived); extern void Swig_features_get(Hash *features, String *prefix, String *name, SwigType *decl, Node *n); extern void Swig_feature_set(Hash *features, const_String_or_char_ptr name, SwigType *decl, const_String_or_char_ptr featurename, const_String_or_char_ptr value, Hash *featureattribs); /* --- Misc --- */ extern char *Swig_copy_string(const char *c); extern void Swig_set_fakeversion(const char *version); extern const char *Swig_package_version(void); extern String *Swig_package_version_hex(void); extern void Swig_obligatory_macros(String *f_runtime, const char *language); extern void Swig_banner(File *f); extern void Swig_banner_target_lang(File *f, const_String_or_char_ptr commentchar); extern String *Swig_strip_c_comments(const String *s); extern String *Swig_new_subdirectory(String *basedirectory, String *subdirectory); extern void Swig_filename_correct(String *filename); extern String *Swig_filename_escape(String *filename); extern String *Swig_filename_escape_space(String *filename); extern void Swig_filename_unescape(String *filename); extern int Swig_storage_isextern(Node *n); extern int Swig_storage_isexternc(Node *n); extern int Swig_storage_isstatic_custom(Node *n, const_String_or_char_ptr storage); extern int Swig_storage_isstatic(Node *n); extern String *Swig_string_escape(String *s); extern void Swig_scopename_split(const String *s, String **prefix, String **last); extern String *Swig_scopename_prefix(const String *s); extern String *Swig_scopename_last(const String *s); extern String *Swig_scopename_first(const String *s); extern String *Swig_scopename_suffix(const String *s); extern List *Swig_scopename_tolist(const String *s); extern int Swig_scopename_check(const String *s); extern int Swig_scopename_isvalid(const String *s); extern String *Swig_string_lower(String *s); extern String *Swig_string_upper(String *s); extern String *Swig_string_title(String *s); extern void Swig_offset_string(String *s, int number); extern String *Swig_pcre_version(void); extern void Swig_init(void); extern int Swig_value_wrapper_mode(int mode); extern int Swig_is_generated_overload(Node *n); extern Node *Swig_item_in_list(List *list, const DOH *item); typedef enum { EMF_STANDARD, EMF_MICROSOFT } ErrorMessageFormat; extern void Swig_warning(int num, const_String_or_char_ptr filename, int line, const char *fmt, ...); extern void Swig_error(const_String_or_char_ptr filename, int line, const char *fmt, ...); extern int Swig_error_count(void); extern void Swig_error_silent(int s); extern void Swig_warnfilter(const_String_or_char_ptr wlist, int val); extern void Swig_warnall(void); extern int Swig_warn_count(void); extern void Swig_error_msg_format(ErrorMessageFormat format); extern void Swig_diagnostic(const_String_or_char_ptr filename, int line, const char *fmt, ...); extern String *Swig_stringify_with_location(DOH *object); /* --- C Wrappers --- */ extern void Swig_cresult_name_set(const char *new_name); extern const char *Swig_cresult_name(void); extern String *Swig_cparm_name(Parm *p, int i); extern String *Swig_wrapped_var_type(SwigType *t, int varcref); extern int Swig_cargs(Wrapper *w, ParmList *l); extern String *Swig_cresult(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr decl); extern String *Swig_cfunction_call(const_String_or_char_ptr name, ParmList *parms); extern String *Swig_cconstructor_call(const_String_or_char_ptr name); extern String *Swig_cppconstructor_call(const_String_or_char_ptr name, ParmList *parms); extern String *Swig_unref_call(Node *n); extern String *Swig_ref_call(Node *n, const String *lname); extern String *Swig_cdestructor_call(Node *n); extern String *Swig_cppdestructor_call(Node *n); extern String *Swig_cmemberset_call(const_String_or_char_ptr name, SwigType *type, String *self, int varcref); extern String *Swig_cmemberget_call(const_String_or_char_ptr name, SwigType *t, String *self, int varcref); extern int Swig_add_extension_code(Node *n, const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self); extern void Swig_replace_special_variables(Node *n, Node *parentnode, String *code); /* --- Transformations --- */ extern int Swig_MethodToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, int flags, SwigType *director_type, int is_director); extern int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, String *none_comparison, String *director_ctor, int cplus, int flags, String *directorname); extern int Swig_DestructorToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, int cplus, int flags); extern int Swig_MembersetToFunction(Node *n, String *classname, int flags); extern int Swig_MembergetToFunction(Node *n, String *classname, int flags); extern int Swig_VargetToFunction(Node *n, int flags); extern int Swig_VarsetToFunction(Node *n, int flags); #define CWRAP_EXTEND 0x01 #define CWRAP_SMART_POINTER 0x02 #define CWRAP_NATURAL_VAR 0x04 #define CWRAP_DIRECTOR_ONE_CALL 0x08 #define CWRAP_DIRECTOR_TWO_CALLS 0x10 #define CWRAP_ALL_PROTECTED_ACCESS 0x20 #define CWRAP_SMART_POINTER_OVERLOAD 0x40 /* --- Director Helpers --- */ extern Node *Swig_methodclass(Node *n); extern int Swig_directorclass(Node *n); extern Node *Swig_directormap(Node *n, String *type); /* --- Legacy Typemap API (somewhat simplified, ha!) --- */ extern void Swig_typemap_init(void); extern void Swig_typemap_register(const_String_or_char_ptr tmap_method, ParmList *pattern, const_String_or_char_ptr code, ParmList *locals, ParmList *kwargs); extern int Swig_typemap_copy(const_String_or_char_ptr tmap_method, ParmList *srcpattern, ParmList *pattern); extern void Swig_typemap_clear(const_String_or_char_ptr tmap_method, ParmList *pattern); extern int Swig_typemap_apply(ParmList *srcpat, ParmList *destpat); extern void Swig_typemap_clear_apply(ParmList *pattern); extern void Swig_typemap_replace_embedded_typemap(String *s, Node *file_line_node); extern void Swig_typemap_debug(void); extern void Swig_typemap_search_debug_set(void); extern void Swig_typemap_used_debug_set(void); extern void Swig_typemap_register_debug_set(void); extern String *Swig_typemap_lookup(const_String_or_char_ptr tmap_method, Node *n, const_String_or_char_ptr lname, Wrapper *f); extern String *Swig_typemap_lookup_out(const_String_or_char_ptr tmap_method, Node *n, const_String_or_char_ptr lname, Wrapper *f, String *actioncode); extern void Swig_typemap_attach_parms(const_String_or_char_ptr tmap_method, ParmList *parms, Wrapper *f); /* --- Code fragment support --- */ extern void Swig_fragment_register(Node *fragment); extern void Swig_fragment_emit(String *name); extern void Swig_fragment_clear(String *section); /* --- Extension support --- */ extern Hash *Swig_extend_hash(void); extern void Swig_extend_merge(Node *cls, Node *am); extern void Swig_extend_append_previous(Node *cls, Node *am); extern void Swig_extend_unused_check(void); /* hacks defined in C++ ! */ extern int Swig_director_mode(void); extern int Swig_director_protected_mode(void); extern int Swig_all_protected_mode(void); extern void Wrapper_director_mode_set(int); extern void Wrapper_director_protected_mode_set(int); extern void Wrapper_all_protected_mode_set(int); extern void Language_replace_special_variables(String *method, String *tm, Parm *parm); extern void Swig_print(DOH *object, int count); extern void Swig_print_with_location(DOH *object, int count); /* -- template init -- */ extern void SwigType_template_init(void); #ifdef __cplusplus } #endif #endif swig-4.4.0/Source/Swig/swigscan.h0000664000175000017500000001330515075443613016522 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigscan.h * * C/C++ scanner. * ----------------------------------------------------------------------------- */ typedef struct Scanner Scanner; extern Scanner *NewScanner(void); extern void DelScanner(Scanner *); extern void Scanner_clear(Scanner *); extern void Scanner_push(Scanner *, String *); extern void Scanner_pushtoken(Scanner *, int, const_String_or_char_ptr value); extern int Scanner_token(Scanner *); extern String *Scanner_text(Scanner *); extern void Scanner_skip_line(Scanner *); extern int Scanner_skip_balanced(Scanner *, int startchar, int endchar); extern String *Scanner_get_raw_text_balanced(Scanner *, int startchar, int endchar); extern void Scanner_set_location(Scanner *, String *file, int line); extern String *Scanner_file(Scanner *); extern int Scanner_line(Scanner *); extern int Scanner_start_line(Scanner *); extern void Scanner_idstart(Scanner *, const char *idchar); extern String *Scanner_errmsg(Scanner *); extern int Scanner_errline(Scanner *); extern int Scanner_isoperator(int tokval); extern void Scanner_locator(Scanner *, String *loc); /* Note: Tokens in range 100+ are for C/C++ operators */ #define SWIG_MAXTOKENS 200 #define SWIG_TOKEN_LPAREN 1 /* ( */ #define SWIG_TOKEN_RPAREN 2 /* ) */ #define SWIG_TOKEN_SEMI 3 /* ; */ #define SWIG_TOKEN_LBRACE 4 /* { */ #define SWIG_TOKEN_RBRACE 5 /* } */ #define SWIG_TOKEN_LBRACKET 6 /* [ */ #define SWIG_TOKEN_RBRACKET 7 /* ] */ #define SWIG_TOKEN_BACKSLASH 8 /* \ */ #define SWIG_TOKEN_ENDLINE 9 /* \n */ #define SWIG_TOKEN_STRING 10 /* "str" */ #define SWIG_TOKEN_POUND 11 /* # */ #define SWIG_TOKEN_COLON 12 /* : */ #define SWIG_TOKEN_DCOLON 13 /* :: */ #define SWIG_TOKEN_DCOLONSTAR 14 /* ::* */ #define SWIG_TOKEN_ID 15 /* identifier */ #define SWIG_TOKEN_FLOAT 16 /* 3.1415F */ #define SWIG_TOKEN_DOUBLE 17 /* 3.1415 */ #define SWIG_TOKEN_LONGDOUBLE 18 /* 3.1415L */ #define SWIG_TOKEN_INT 19 /* 314 */ #define SWIG_TOKEN_UINT 20 /* 314U */ #define SWIG_TOKEN_LONG 21 /* 314L */ #define SWIG_TOKEN_ULONG 22 /* 314UL */ #define SWIG_TOKEN_CHAR 23 /* 'charconst' */ #define SWIG_TOKEN_PERIOD 24 /* . */ #define SWIG_TOKEN_AT 25 /* @ */ #define SWIG_TOKEN_DOLLAR 26 /* $ */ #define SWIG_TOKEN_CODEBLOCK 27 /* %{ ... %} ... */ #define SWIG_TOKEN_LONGLONG 29 /* 314LL */ #define SWIG_TOKEN_ULONGLONG 30 /* 314ULL */ #define SWIG_TOKEN_QUESTION 31 /* ? */ #define SWIG_TOKEN_COMMENT 32 /* C or C++ comment */ #define SWIG_TOKEN_BOOL 33 /* true or false */ #define SWIG_TOKEN_WSTRING 34 /* L"str" */ #define SWIG_TOKEN_WCHAR 35 /* L'c' */ #define SWIG_TOKEN_ELLIPSIS 36 /* ... */ #define SWIG_TOKEN_LLBRACKET 37 /* [[ */ #define SWIG_TOKEN_RRBRACKET 38 /* ]] */ #define SWIG_TOKEN_ILLEGAL 99 #define SWIG_TOKEN_ERROR -1 #define SWIG_TOKEN_COMMA 101 /* , */ #define SWIG_TOKEN_STAR 102 /* * */ #define SWIG_TOKEN_EQUAL 103 /* = */ #define SWIG_TOKEN_EQUALTO 104 /* == */ #define SWIG_TOKEN_NOTEQUAL 105 /* != */ #define SWIG_TOKEN_PLUS 106 /* + */ #define SWIG_TOKEN_MINUS 107 /* - */ #define SWIG_TOKEN_AND 108 /* & */ #define SWIG_TOKEN_LAND 109 /* && */ #define SWIG_TOKEN_OR 110 /* | */ #define SWIG_TOKEN_LOR 111 /* || */ #define SWIG_TOKEN_XOR 112 /* ^ */ #define SWIG_TOKEN_LESSTHAN 113 /* < */ #define SWIG_TOKEN_GREATERTHAN 114 /* > */ #define SWIG_TOKEN_LTEQUAL 115 /* <= */ #define SWIG_TOKEN_GTEQUAL 116 /* >= */ #define SWIG_TOKEN_NOT 117 /* ~ */ #define SWIG_TOKEN_LNOT 118 /* ! */ #define SWIG_TOKEN_SLASH 119 /* / */ #define SWIG_TOKEN_PERCENT 120 /* % */ #define SWIG_TOKEN_LSHIFT 121 /* << */ #define SWIG_TOKEN_RSHIFT 122 /* >> */ #define SWIG_TOKEN_PLUSPLUS 123 /* ++ */ #define SWIG_TOKEN_MINUSMINUS 124 /* -- */ #define SWIG_TOKEN_PLUSEQUAL 125 /* += */ #define SWIG_TOKEN_MINUSEQUAL 126 /* -= */ #define SWIG_TOKEN_TIMESEQUAL 127 /* *= */ #define SWIG_TOKEN_DIVEQUAL 128 /* /= */ #define SWIG_TOKEN_ANDEQUAL 129 /* &= */ #define SWIG_TOKEN_OREQUAL 130 /* |= */ #define SWIG_TOKEN_XOREQUAL 131 /* ^= */ #define SWIG_TOKEN_LSEQUAL 132 /* <<= */ #define SWIG_TOKEN_RSEQUAL 133 /* >>= */ #define SWIG_TOKEN_MODEQUAL 134 /* %= */ #define SWIG_TOKEN_ARROW 135 /* -> */ #define SWIG_TOKEN_ARROWSTAR 136 /* ->* */ #define SWIG_TOKEN_LTEQUALGT 137 /* <=> */ swig-4.4.0/Source/Swig/swigwrap.h0000664000175000017500000000277515075443613016560 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigwrap.h * * Functions related to wrapper objects. * ----------------------------------------------------------------------------- */ typedef struct Wrapper { Hash *localh; String *def; String *locals; String *code; } Wrapper; extern Wrapper *NewWrapper(void); extern void DelWrapper(Wrapper *w); extern void Wrapper_compact_print_mode_set(int flag); extern void Wrapper_pretty_print(String *str, File *f); extern void Wrapper_compact_print(String *str, File *f); extern void Wrapper_print(Wrapper *w, File *f); extern int Wrapper_add_local(Wrapper *w, const_String_or_char_ptr name, const_String_or_char_ptr decl); extern int Wrapper_add_localv(Wrapper *w, const_String_or_char_ptr name, ...); extern int Wrapper_check_local(Wrapper *w, const_String_or_char_ptr name); extern char *Wrapper_new_local(Wrapper *w, const_String_or_char_ptr name, const_String_or_char_ptr decl); extern char *Wrapper_new_localv(Wrapper *w, const_String_or_char_ptr name, ...); swig-4.4.0/Source/Swig/parms.c0000664000175000017500000002335415075443613016026 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * parms.c * * Parameter list class. * ----------------------------------------------------------------------------- */ #include "swig.h" /* ------------------------------------------------------------------------ * NewParm() * * Create a new parameter from datatype 'type' and name 'name' copying * the file and line number from the Node from_node. * ------------------------------------------------------------------------ */ Parm *NewParm(SwigType *type, const_String_or_char_ptr name, Node *from_node) { Parm *p = NewParmWithoutFileLineInfo(type, name); Setfile(p, Getfile(from_node)); Setline(p, Getline(from_node)); return p; } /* ------------------------------------------------------------------------ * NewParmWithoutFileLineInfo() * * Create a new parameter from datatype 'type' and name 'name' without any * file / line numbering information. * ------------------------------------------------------------------------ */ Parm *NewParmWithoutFileLineInfo(SwigType *type, const_String_or_char_ptr name) { Parm *p = NewHash(); set_nodeType(p, "parm"); if (type) { SwigType *ntype = Copy(type); Setattr(p, "type", ntype); Delete(ntype); } Setattr(p, "name", name); return p; } /* ------------------------------------------------------------------------ * NewParmNode() * * Create a new parameter from datatype 'type' and name and symbol table as * well as file and line number from the 'from_node'. * The resulting Parm will be similar to a Node used for typemap lookups. * ------------------------------------------------------------------------ */ Parm *NewParmNode(SwigType *type, Node *from_node) { Parm *p = NewParm(type, Getattr(from_node, "name"), from_node); Setattr(p, "sym:symtab", Getattr(from_node, "sym:symtab")); return p; } /* ------------------------------------------------------------------------ * CopyParm() * ------------------------------------------------------------------------ */ Parm *CopyParm(Parm *p) { Parm *np = NewHash(); Iterator ki; for (ki = First(p); ki.key; ki = Next(ki)) { if (DohIsString(ki.item)) { DOH *c = Copy(ki.item); Setattr(np,ki.key,c); Delete(c); } } Setfile(np, Getfile(p)); Setline(np, Getline(p)); return np; } /* ------------------------------------------------------------------ * CopyParmListMax() * CopyParmList() * ------------------------------------------------------------------ */ ParmList *CopyParmListMax(ParmList *p, int count) { Parm *np; Parm *pp = 0; Parm *fp = 0; if (!p) return 0; while (p) { if (count == 0) break; np = CopyParm(p); if (pp) { set_nextSibling(pp, np); Delete(np); } else { fp = np; } pp = np; p = nextSibling(p); count--; } return fp; } ParmList *CopyParmList(ParmList *p) { return CopyParmListMax(p,-1); } /* ----------------------------------------------------------------------------- * ParmList_join() * * Join two parameter lists. Appends p2 to the end of p. * No copies are made. * Returns start of joined parameter list. * ----------------------------------------------------------------------------- */ ParmList *ParmList_join(ParmList *p, ParmList *p2) { Parm *firstparm = p ? p : p2; Parm *lastparm = 0; while (p) { lastparm = p; p = nextSibling(p); } if (lastparm) set_nextSibling(lastparm, p2); return firstparm; } /* ----------------------------------------------------------------------------- * ParmList_replace_last() * * Delete last parameter in p and replace it with parameter list p2. * p must have at least one element, that is, must not be NULL. * Return beginning of modified parameter list. * ----------------------------------------------------------------------------- */ ParmList *ParmList_replace_last(ParmList *p, ParmList *p2) { ParmList *start = p; int len = ParmList_len(p); assert(p); if (len == 1) { start = p2; } else if (len > 1) { Parm *secondlastparm = ParmList_nth_parm(p, len - 2); set_nextSibling(secondlastparm, p2); } return start; } /* ----------------------------------------------------------------------------- * ParmList_nth_parm() * * return the nth parameter (0 based) in the parameter list * return NULL if there are not enough parameters in the list * ----------------------------------------------------------------------------- */ Parm *ParmList_nth_parm(ParmList *p, unsigned int n) { while (p) { if (n == 0) { break; } n--; p = nextSibling(p); } return p; } /* ----------------------------------------------------------------------------- * ParmList_variadic_parm() * * Return the variadic parm (last in list if it is variadic), NULL otherwise * ----------------------------------------------------------------------------- */ Parm *ParmList_variadic_parm(ParmList *p) { Parm *lastparm = 0; while (p) { lastparm = p; p = nextSibling(p); } return lastparm && SwigType_isvariadic(Getattr(lastparm, "type")) ? lastparm : 0; } /* ----------------------------------------------------------------------------- * ParmList_numrequired() * * Return number of required arguments - the number of arguments excluding * default arguments * ----------------------------------------------------------------------------- */ int ParmList_numrequired(ParmList *p) { int i = 0; while (p) { SwigType *t = Getattr(p, "type"); String *value = Getattr(p, "value"); if (value) return i; if (!(SwigType_type(t) == T_VOID)) i++; else break; p = nextSibling(p); } return i; } /* ----------------------------------------------------------------------------- * int ParmList_len() * ----------------------------------------------------------------------------- */ int ParmList_len(ParmList *p) { int i = 0; while (p) { i++; p = nextSibling(p); } return i; } /* --------------------------------------------------------------------- * get_empty_type() * ---------------------------------------------------------------------- */ static SwigType *get_empty_type(void) { return NewStringEmpty(); } /* --------------------------------------------------------------------- * ParmList_str() * * Generates a string of parameters * ---------------------------------------------------------------------- */ String *ParmList_str(ParmList *p) { String *out = NewStringEmpty(); while (p) { String *type = Getattr(p, "type"); String *pstr = SwigType_str(type ? type : get_empty_type(), Getattr(p, "name")); Append(out, pstr); p = nextSibling(p); if (p) { Append(out, ","); } Delete(pstr); } return out; } /* --------------------------------------------------------------------- * ParmList_str_defaultargs() * * Generates a string of parameters including default arguments * ---------------------------------------------------------------------- */ String *ParmList_str_defaultargs(ParmList *p) { String *out = NewStringEmpty(); while (p) { String *value = Getattr(p, "value"); String *type = Getattr(p, "type"); String *pstr = SwigType_str(type ? type : get_empty_type(), Getattr(p, "name")); Append(out, pstr); if (value) { Printf(out, "=%s", value); } p = nextSibling(p); if (p) { Append(out, ","); } Delete(pstr); } return out; } /* ----------------------------------------------------------------------------- * ParmList_str_multibrackets() * * Generates a string of parameters including default arguments adding brackets * if more than one parameter * ----------------------------------------------------------------------------- */ String *ParmList_str_multibrackets(ParmList *p) { String *out; String *parm_str = ParmList_str_defaultargs(p); if (ParmList_len(p) > 1) out = NewStringf("(%s)", parm_str); else out = NewStringf("%s", parm_str); Delete(parm_str); return out; } /* --------------------------------------------------------------------- * ParmList_protostr() * * Generate a prototype string. * ---------------------------------------------------------------------- */ String *ParmList_protostr(ParmList *p) { String *out = NewStringEmpty(); while (p) { String *type = Getattr(p, "type"); String *pstr = SwigType_str(type ? type : get_empty_type(), 0); Append(out, pstr); p = nextSibling(p); if (p) { Append(out, ","); } Delete(pstr); } return out; } /* --------------------------------------------------------------------- * ParmList_has_defaultargs() * * Returns 1 if the parameter list passed in is has one or more default * arguments. Otherwise returns 0. * ---------------------------------------------------------------------- */ int ParmList_has_defaultargs(ParmList *p) { while (p) { if (Getattr(p, "value")) { return 1; } p = nextSibling(p); } return 0; } /* --------------------------------------------------------------------- * ParmList_has_varargs() * * Returns 1 if the parameter list passed in has varargs. * Otherwise returns 0. * ---------------------------------------------------------------------- */ int ParmList_has_varargs(ParmList *p) { Parm *lastparm = 0; while (p) { lastparm = p; p = nextSibling(p); } return lastparm ? SwigType_isvarargs(Getattr(lastparm, "type")) : 0; } swig-4.4.0/Source/Swig/typemap.c0000664000175000017500000021212515075443613016357 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * typemap.c * * A somewhat generalized implementation of SWIG1.1 typemaps. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" #include #if 0 #define SWIG_DEBUG #endif static int typemap_search_debug = 0; static int typemaps_used_debug = 0; static int typemap_register_debug = 0; static int in_typemap_search_multi = 0; static void replace_embedded_typemap(String *s, ParmList *parm_sublist, Wrapper *f, Node *file_line_node); /* ----------------------------------------------------------------------------- * Typemaps are stored in a collection of nested hash tables. Something like * this: * * [ type ] * +-------- [ name ] * +-------- [ name ] * * Each hash table [ type ] or [ name ] then contains references to the * different typemap methods. These are referenced by names such as * "tmap:in", "tmap:out", "tmap:argout", and so forth. * * The object corresponding to a specific typemap method has the following attributes: * * "type" - Typemap type * "pname" - Parameter name * "code" - Typemap code * "source" - Source directive (%apply or %typemap) for the typemap * "locals" - Local variables (if any) * "kwargs" - Typemap attributes * * Example for a typemap method named "in": * %typemap(in, warning="987:my warning", noblock=1) int &my_int (int tmp) "$1 = $input;" * * "type" - r.int * "pname" - my_int * "code" - $1 = $input; * "source" - typemap(in) int &my_int * "locals" - int tmp * "kwargs" - warning="987:my typemap warning", foo=123 * * ----------------------------------------------------------------------------- */ static Hash *typemaps; /* ----------------------------------------------------------------------------- * typemap_identifier_fix() * * Create a type that can be used as a hash key lookup independent of the various * ways a template parameter list can be defined. This is achieved by fully * resolving the template parameters. * * This is a copy and modification of feature_identifier_fix in parser.y. * ----------------------------------------------------------------------------- */ static SwigType *typemap_identifier_fix(const SwigType *s) { String *tp = SwigType_istemplate_templateprefix(s); if (tp) { String *ts, *ta, *tq, *tr; ts = SwigType_templatesuffix(s); ta = SwigType_templateargs(s); tq = Swig_symbol_type_qualify(ta, 0); tr = SwigType_typedef_resolve_all(ta); Append(tp,tr); Append(tp,ts); Delete(ts); Delete(ta); Delete(tq); Delete(tr); return tp; } else { return NewString(s); } } static Hash *get_typemap(const SwigType *type) { Hash *tm = 0; SwigType *dtype = 0; SwigType *hashtype; if (SwigType_istemplate(type)) { SwigType *rty = typemap_identifier_fix(type); String *ty = Swig_symbol_template_deftype(rty, 0); dtype = Swig_symbol_type_qualify(ty, 0); type = dtype; Delete(ty); Delete(rty); } /* remove unary scope operator (::) prefix indicating global scope for looking up in the hashmap */ hashtype = SwigType_remove_global_scope_prefix(type); tm = Getattr(typemaps, hashtype); Delete(dtype); Delete(hashtype); return tm; } static void set_typemap(const SwigType *type, Hash **tmhash) { SwigType *hashtype = 0; Hash *new_tm = 0; assert(*tmhash == 0); if (SwigType_istemplate(type)) { SwigType *rty = typemap_identifier_fix(type); String *ty = Swig_symbol_template_deftype(rty, 0); String *tyq = Swig_symbol_type_qualify(ty, 0); hashtype = SwigType_remove_global_scope_prefix(tyq); *tmhash = Getattr(typemaps, hashtype); Delete(rty); Delete(tyq); Delete(ty); } else { hashtype = SwigType_remove_global_scope_prefix(type); } if (!*tmhash) { /* this type has not been seen before even after resolving template parameter types */ new_tm = NewHash(); *tmhash = new_tm; } /* note that the unary scope operator (::) prefix indicating global scope has been removed from the type */ Setattr(typemaps, hashtype, *tmhash); Delete(hashtype); Delete(new_tm); } /* ----------------------------------------------------------------------------- * Swig_typemap_init() * * Initialize the typemap system * ----------------------------------------------------------------------------- */ void Swig_typemap_init(void) { typemaps = NewHash(); } static String *typemap_method_name(const_String_or_char_ptr tmap_method) { static Hash *names = 0; String *s; /* Due to "interesting" object-identity semantics of DOH, we have to make sure that we only intern strings without object identity into the hash table. (typemap_attach_kwargs calls typemap_method_name several times with the "same" String *tmap_method (i.e., same object identity) but differing string values.) Most other callers work around this by using char* rather than String *. -- mkoeppe, Jun 17, 2003 */ const char *method_without_object_identity = Char(tmap_method); if (!names) names = NewHash(); s = Getattr(names, method_without_object_identity); if (s) return s; s = NewStringf("tmap:%s", tmap_method); Setattr(names, method_without_object_identity, s); Delete(s); return s; } /* ----------------------------------------------------------------------------- * typemap_register() * * Internal implementation for Swig_typemap_register() * ----------------------------------------------------------------------------- */ static void typemap_register(const_String_or_char_ptr tmap_method, ParmList *parms, const_String_or_char_ptr code, ParmList *locals, ParmList *kwargs, String *source_directive) { Hash *tm; Hash *tm1; Hash *tm2; Parm *np; String *tm_method; SwigType *type; String *pname; if (!parms) return; if (typemap_register_debug) { Printf(stdout, "Registering - %s\n", tmap_method); Swig_print_node(parms); } tm_method = typemap_method_name(tmap_method); /* Register the first type in the parameter list */ type = Getattr(parms, "type"); pname = Getattr(parms, "name"); /* See if this type has been seen before */ tm = get_typemap(type); if (!tm) { set_typemap(type, &tm); } if (pname) { /* See if parameter has been seen before */ tm1 = Getattr(tm, pname); if (!tm1) { tm1 = NewHash(); Setattr(tm, pname, tm1); Delete(tm1); } tm = tm1; } /* Now see if this typemap method has been seen before */ tm2 = Getattr(tm, tm_method); if (!tm2) { tm2 = NewHash(); Setattr(tm, tm_method, tm2); Delete(tm2); } /* For a multi-argument typemap, the typemap code and information is really only stored in the last argument. However, to make this work, we perform a really neat trick using the typemap method name. For example, consider this typemap %typemap(in) (int foo, int *bar, char *blah[]) { ... } To store it, we look at typemaps for the following: typemap method type-name ---------------------------------------------- "in" int foo "in-int+foo:" int *bar "in-int+foo:-p.int+bar: char *blah[] Notice how the typemap method name expands to encode information about previous arguments. */ np = nextSibling(parms); if (np) { /* Make an entirely new typemap method key */ String *multi_tmap_method = NewStringf("%s-%s+%s:", tmap_method, type, pname); /* Now reregister on the remaining arguments */ typemap_register(multi_tmap_method, np, code, locals, kwargs, source_directive); Delete(multi_tmap_method); } else { ParmList *clocals = CopyParmList(locals); ParmList *ckwargs = CopyParmList(kwargs); Setfile(tm2, Getfile(code)); Setline(tm2, Getline(code)); Setattr(tm2, "code", code); Setattr(tm2, "type", type); Setattr(tm2, "source", source_directive); if (pname) { Setattr(tm2, "pname", pname); } Setattr(tm2, "locals", clocals); Setattr(tm2, "kwargs", ckwargs); Delete(clocals); Delete(ckwargs); } } /* ----------------------------------------------------------------------------- * Swig_typemap_register() * * Add a new, possibly multi-argument, typemap * ----------------------------------------------------------------------------- */ void Swig_typemap_register(const_String_or_char_ptr tmap_method, ParmList *parms, const_String_or_char_ptr code, ParmList *locals, ParmList *kwargs) { String *parms_str = ParmList_str_multibrackets(parms); String *source_directive = NewStringf("typemap(%s) %s", tmap_method, parms_str); typemap_register(tmap_method, parms, code, locals, kwargs, source_directive); Delete(source_directive); Delete(parms_str); } /* ----------------------------------------------------------------------------- * typemap_get() * * Retrieve typemap information. * ----------------------------------------------------------------------------- */ static Hash *typemap_get(SwigType *type, const_String_or_char_ptr name) { Hash *tm, *tm1; tm = get_typemap(type); if (!tm) { return 0; } if ((name) && Len(name)) { tm1 = Getattr(tm, name); return tm1; } return tm; } /* ----------------------------------------------------------------------------- * Swig_typemap_copy() * * Copy a typemap * ----------------------------------------------------------------------------- */ int Swig_typemap_copy(const_String_or_char_ptr tmap_method, ParmList *srcparms, ParmList *parms) { Hash *tm = 0; String *tm_method; Parm *p; String *pname; SwigType *ptype; String *tm_methods, *multi_tmap_method; if (ParmList_len(parms) != ParmList_len(srcparms)) return -1; tm_method = typemap_method_name(tmap_method); p = srcparms; tm_methods = NewString(tm_method); while (p) { ptype = Getattr(p, "type"); pname = Getattr(p, "name"); /* Lookup the type */ tm = typemap_get(ptype, pname); if (!tm) break; tm = Getattr(tm, tm_methods); if (!tm) break; /* Got a match. Look for next typemap */ multi_tmap_method = NewStringf("%s-%s+%s:", tm_methods, ptype, pname); Delete(tm_methods); tm_methods = multi_tmap_method; p = nextSibling(p); } Delete(tm_methods); if (!p && tm) { /* Got some kind of match */ String *parms_str = ParmList_str_multibrackets(parms); String *srcparms_str = ParmList_str_multibrackets(srcparms); String *source_directive = NewStringf("typemap(%s) %s = %s", tmap_method, parms_str, srcparms_str); typemap_register(tmap_method, parms, Getattr(tm, "code"), Getattr(tm, "locals"), Getattr(tm, "kwargs"), source_directive); Delete(source_directive); Delete(srcparms_str); Delete(parms_str); return 0; } /* Not found */ return -1; } /* ----------------------------------------------------------------------------- * Swig_typemap_clear() * * Delete a multi-argument typemap * ----------------------------------------------------------------------------- */ void Swig_typemap_clear(const_String_or_char_ptr tmap_method, ParmList *parms) { SwigType *type; String *name; Parm *p; String *multi_tmap_method; Hash *tm = 0; /* This might not work */ multi_tmap_method = NewString(tmap_method); p = parms; while (p) { type = Getattr(p, "type"); name = Getattr(p, "name"); tm = typemap_get(type, name); if (!tm) return; p = nextSibling(p); if (p) Printf(multi_tmap_method, "-%s+%s:", type, name); } if (tm) { tm = Getattr(tm, typemap_method_name(multi_tmap_method)); if (tm) { Delattr(tm, "code"); Delattr(tm, "locals"); Delattr(tm, "kwargs"); } } Delete(multi_tmap_method); } /* ----------------------------------------------------------------------------- * Swig_typemap_apply() * * Multi-argument %apply directive. This is pretty horrible so I sure hope * it works. * ----------------------------------------------------------------------------- */ static int count_args(String *s) { /* Count up number of arguments */ int na = 0; char *c = Char(s); while (*c) { if (*c == '+') na++; c++; } return na; } int Swig_typemap_apply(ParmList *src, ParmList *dest) { String *ssig, *dsig; Parm *p, *np, *lastp, *dp, *lastdp = 0; int narg = 0; SwigType *type = 0, *name; Hash *tm, *sm; int match = 0; /* Printf(stdout,"apply : %s --> %s\n", ParmList_str(src), ParmList_str(dest)); */ /* Create type signature of source */ ssig = NewStringEmpty(); dsig = NewStringEmpty(); p = src; dp = dest; lastp = 0; while (p) { lastp = p; lastdp = dp; np = nextSibling(p); if (np) { Printf(ssig, "-%s+%s:", Getattr(p, "type"), Getattr(p, "name")); Printf(dsig, "-%s+%s:", Getattr(dp, "type"), Getattr(dp, "name")); narg++; } p = np; dp = nextSibling(dp); } /* make sure a typemap node exists for the last destination node */ type = Getattr(lastdp, "type"); tm = get_typemap(type); if (!tm) { set_typemap(type, &tm); } name = Getattr(lastdp, "name"); if (name) { Hash *tm1 = Getattr(tm, name); if (!tm1) { tm1 = NewHash(); Setattr(tm, NewString(name), tm1); Delete(tm1); } tm = tm1; } /* This is a little nasty. We need to go searching for all possible typemaps in the source and apply them to the target */ type = Getattr(lastp, "type"); name = Getattr(lastp, "name"); /* See if there is a matching typemap in this scope */ sm = typemap_get(type, name); /* If there is no exact match on the type, look for a typemap by resolving the type until/if a suitable match on the type is found, such as typedef unsigned long size_t; ... %apply(size_t) {my_size}; ==> %apply(unsigned long) {my_size}; */ SwigType *ntype = Copy(type); while (!sm && ntype) { SwigType *nt = ntype; ntype = SwigType_typedef_resolve(ntype); if (ntype) { sm = typemap_get(ntype, name); } Delete(nt); } Delete(ntype); if (sm) { /* Got a typemap. Need to only merge attributes for methods that match our signature */ Iterator ki; Hash *deferred_add; match = 1; /* Since typemap_register can modify the `sm` hash, we *cannot* call typemap_register while iterating over sm. * Create a temporary hash of typemaps to add immediately after. */ deferred_add = NewHash(); for (ki = First(sm); ki.key; ki = Next(ki)) { /* Check for a signature match with the source signature */ if ((count_args(ki.key) == narg) && (Strstr(ki.key, ssig))) { String *oldm; /* A typemap we have to copy */ String *nkey = Copy(ki.key); Replace(nkey, ssig, dsig, DOH_REPLACE_ANY); /* Make sure the typemap doesn't already exist in the target map */ oldm = Getattr(tm, nkey); if (!oldm || (!Getattr(tm, "code"))) { String *code; Hash *sm1 = ki.item; code = Getattr(sm1, "code"); if (code) { Replace(nkey, dsig, "", DOH_REPLACE_ANY); Replace(nkey, "tmap:", "", DOH_REPLACE_ANY); Setattr(deferred_add, nkey, sm1); } } Delete(nkey); } } /* After assembling the key/item pairs, add the resulting typemaps */ for (ki = First(deferred_add); ki.key; ki = Next(ki)) { Hash *sm1 = ki.item; String *src_str = ParmList_str_multibrackets(src); String *dest_str = ParmList_str_multibrackets(dest); String *source_directive = NewStringf("apply %s { %s }", src_str, dest_str); typemap_register(ki.key, dest, Getattr(sm1, "code"), Getattr(sm1, "locals"), Getattr(sm1, "kwargs"), source_directive); Delete(source_directive); Delete(dest_str); Delete(src_str); } Delete(deferred_add); } Delete(ssig); Delete(dsig); return match; } /* ----------------------------------------------------------------------------- * Swig_typemap_clear_apply() * * %clear directive. Clears all typemaps for a type (in the current scope only). * ----------------------------------------------------------------------------- */ /* Multi-argument %clear directive */ void Swig_typemap_clear_apply(Parm *parms) { String *tsig; Parm *p, *np, *lastp; int narg = 0; Hash *tm; String *name; /* Create a type signature of the parameters */ tsig = NewStringEmpty(); p = parms; lastp = 0; while (p) { lastp = p; np = nextSibling(p); if (np) { Printf(tsig, "-%s+%s:", Getattr(p, "type"), Getattr(p, "name")); narg++; } p = np; } tm = get_typemap(Getattr(lastp, "type")); if (!tm) { Delete(tsig); return; } name = Getattr(lastp, "name"); if (name) { tm = Getattr(tm, name); } if (tm) { /* Clear typemaps that match our signature */ Iterator ki, ki2; char *ctsig = Char(tsig); for (ki = First(tm); ki.key; ki = Next(ki)) { char *ckey = Char(ki.key); if (strncmp(ckey, "tmap:", 5) == 0) { int na = count_args(ki.key); if ((na == narg) && strstr(ckey, ctsig)) { Hash *h = ki.item; for (ki2 = First(h); ki2.key; ki2 = Next(ki2)) { Delattr(h, ki2.key); } } } } } Delete(tsig); } /* Internal function to strip array dimensions. */ static SwigType *strip_arrays(SwigType *type) { SwigType *t; int ndim; int i; t = Copy(type); ndim = SwigType_array_ndim(t); for (i = 0; i < ndim; i++) { SwigType_array_setdim(t, i, "ANY"); } return t; } static void debug_search_result_display(Node *tm) { if (tm) Printf(stdout, " Using: %%%s\n", Getattr(tm, "source")); else Printf(stdout, " None found\n"); } /* ----------------------------------------------------------------------------- * typemap_search_helper() * * Helper function for typemap_search to see if there is a type match in the typemap * tm. A match is sought in this order: * %typemap(tm_method) ctype cqualifiedname * %typemap(tm_method) ctype cname * %typemap(tm_method) ctype * ----------------------------------------------------------------------------- */ static Hash *typemap_search_helper(int debug_display, Hash *tm, const String *tm_method, SwigType *ctype, const String *cqualifiedname, const String *cname, Hash **backup) { Hash *result = 0; Hash *tm1; if (debug_display && cqualifiedname) Printf(stdout, " Looking for: %s\n", SwigType_str(ctype, cqualifiedname)); if (tm && cqualifiedname) { tm1 = Getattr(tm, cqualifiedname); if (tm1) { result = Getattr(tm1, tm_method); /* See if there is a type - qualified name match */ if (result && Getattr(result, "code")) goto ret_result; if (result) *backup = result; } } if (debug_display && cname) Printf(stdout, " Looking for: %s\n", SwigType_str(ctype, cname)); if (tm && cname) { tm1 = Getattr(tm, cname); if (tm1) { result = Getattr(tm1, tm_method); /* See if there is a type - name match */ if (result && Getattr(result, "code")) goto ret_result; if (result) *backup = result; } } if (debug_display) Printf(stdout, " Looking for: %s\n", SwigType_str(ctype, 0)); if (tm) { result = Getattr(tm, tm_method); /* See if there is simply a type without name match */ if (result && Getattr(result, "code")) goto ret_result; if (result) *backup = result; } ret_result: return result; } /* ----------------------------------------------------------------------------- * typemap_search() * * Search for a typemap match. This is where the typemap pattern matching rules * are implemented... tries to find the most specific typemap that includes a * 'code' attribute. * ----------------------------------------------------------------------------- */ static Hash *typemap_search(const_String_or_char_ptr tmap_method, SwigType *type, const_String_or_char_ptr name, const_String_or_char_ptr qualifiedname, SwigType **matchtype, Node *node) { Hash *result = 0; Hash *tm; Hash *backup = 0; SwigType *primitive = 0; SwigType *ctype = 0; SwigType *ctype_unstripped = 0; int isarray; const String *cname = 0; const String *cqualifiedname = 0; String *tm_method = typemap_method_name(tmap_method); int debug_display = (in_typemap_search_multi == 0) && typemap_search_debug; if ((name) && Len(name)) cname = name; if ((qualifiedname) && Len(qualifiedname)) cqualifiedname = qualifiedname; if (debug_display) { String *typestr = SwigType_str(type, cqualifiedname ? cqualifiedname : cname); Swig_diagnostic(Getfile(node), Getline(node), "Searching for a suitable '%s' typemap for: %s\n", tmap_method, typestr); Delete(typestr); } ctype = Copy(type); ctype_unstripped = Copy(ctype); while (ctype) { /* Try to get an exact type-match */ tm = get_typemap(ctype); result = typemap_search_helper(debug_display, tm, tm_method, ctype, cqualifiedname, cname, &backup); if (result && Getattr(result, "code")) goto ret_result; { /* Look for the type reduced to just the template prefix - for templated types without the template parameter list being specified */ SwigType *template_prefix = SwigType_istemplate_only_templateprefix(ctype); if (template_prefix) { tm = get_typemap(template_prefix); result = typemap_search_helper(debug_display, tm, tm_method, template_prefix, cqualifiedname, cname, &backup); Delete(template_prefix); if (result && Getattr(result, "code")) goto ret_result; } } /* look for [ANY] arrays */ isarray = SwigType_isarray(ctype); if (isarray) { /* If working with arrays, strip away all of the dimensions and replace with "ANY". See if that generates a match */ SwigType *noarrays = strip_arrays(ctype); tm = get_typemap(noarrays); result = typemap_search_helper(debug_display, tm, tm_method, noarrays, cqualifiedname, cname, &backup); Delete(noarrays); if (result && Getattr(result, "code")) goto ret_result; } /* No match so far - try with a qualifier stripped (strip one qualifier at a time until none remain) * The order of stripping in SwigType_strip_single_qualifier is used to provide some sort of consistency * with the default (SWIGTYPE) typemap matching rules for the first qualifier to be stripped. */ { SwigType *oldctype = ctype; ctype = SwigType_strip_single_qualifier(oldctype); if (!Equal(ctype, oldctype)) { Delete(oldctype); continue; } Delete(oldctype); } /* Once all qualifiers are stripped try resolve a typedef */ { SwigType *oldctype = ctype; ctype = SwigType_typedef_resolve(ctype_unstripped); Delete(oldctype); Delete(ctype_unstripped); ctype_unstripped = Copy(ctype); } } /* Hmmm. Well, no match seems to be found at all. See if there is some kind of default (SWIGTYPE) mapping */ primitive = SwigType_default_create(type); while (primitive) { tm = get_typemap(primitive); result = typemap_search_helper(debug_display, tm, tm_method, primitive, cqualifiedname, cname, &backup); if (result && Getattr(result, "code")) goto ret_result; { SwigType *nprim = SwigType_default_deduce(primitive); Delete(primitive); primitive = nprim; } } if (ctype != type) { Delete(ctype); ctype = 0; } result = backup; ret_result: Delete(primitive); if (matchtype) *matchtype = Copy(ctype); Delete(ctype); Delete(ctype_unstripped); return result; } /* ----------------------------------------------------------------------------- * typemap_search_multi() * * Search for a multi-argument typemap. * ----------------------------------------------------------------------------- */ static Hash *typemap_search_multi(const_String_or_char_ptr tmap_method, ParmList *parms, int *nmatch) { SwigType *type; SwigType *mtype = 0; String *name; String *multi_tmap_method; Hash *tm; Hash *tm1 = 0; if (!parms) { *nmatch = 0; return 0; } type = Getattr(parms, "type"); name = Getattr(parms, "name"); /* Try to find a match on the first type */ tm = typemap_search(tmap_method, type, name, 0, &mtype, parms); if (tm) { if (mtype && SwigType_isarray(mtype)) { Setattr(parms, "tmap:match", mtype); } Delete(mtype); multi_tmap_method = NewStringf("%s-%s+%s:", tmap_method, type, name); in_typemap_search_multi++; tm1 = typemap_search_multi(multi_tmap_method, nextSibling(parms), nmatch); in_typemap_search_multi--; if (tm1) tm = tm1; if (Getattr(tm, "code")) { *(nmatch) = *nmatch + 1; if (typemap_search_debug && tm1 && (in_typemap_search_multi == 0)) { Printf(stdout, " Multi-argument typemap found...\n"); } } else { tm = 0; } Delete(multi_tmap_method); } if (typemap_search_debug && (in_typemap_search_multi == 0)) debug_search_result_display(tm); if (typemaps_used_debug && (in_typemap_search_multi == 0) && tm) { String *typestr = SwigType_str(type, name); Swig_diagnostic(Getfile(parms), Getline(parms), "Typemap for %s (%s) : %%%s\n", typestr, tmap_method, Getattr(tm, "source")); assert(Getfile(parms) && Len(Getfile(parms)) > 0); /* Missing file and line numbering information */ Delete(typestr); } return tm; } static int replace_with_override(String *src, const DOHString_or_char *token, const DOHString_or_char *replace, int flags, Hash *override_vars) { String *override_replace = override_vars ? Getattr(override_vars, token) : NULL; if (override_replace) return Replace(src, token, override_replace, flags); return Replace(src, token, replace, flags); } static void replace_local_types(ParmList *p, const String *name, const String *replace) { while (p) { SwigType *t = Getattr(p, "type"); Replace(t, name, replace, DOH_REPLACE_ANY); p = nextSibling(p); } } static void replace_local_names(ParmList *p, const String *name, const String *replace, Hash *override_vars) { while (p) { int replaced = 0; SwigType *n = Getattr(p, "name"); SwigType *origname = Copy(n); replaced = replace_with_override(n, name, replace, DOH_REPLACE_NUMBER_END, override_vars); if (replaced > 0 && !Getattr(p, "origname")) Setattr(p, "origname", origname); Delete(origname); p = nextSibling(p); } } static int check_locals_type(ParmList *p, const char *s) { while (p) { char *c = GetChar(p, "type"); if (strstr(c, s)) return 1; p = nextSibling(p); } return 0; } /* ----------------------------------------------------------------------------- * typemap_replace_vars() * * Replaces typemap variables in a string. index is the $n variable. * type and pname are the type and parameter name. * override_vars are only used to replace variable names (NOT types), so is only * used for $n, but not the various ways of representing types: $n_ltype, $n_type etc * ----------------------------------------------------------------------------- */ static int typemap_replace_vars(String *s, ParmList *locals, SwigType *type, SwigType *rtype, String *pname, String *lname, int index, Hash *override_vars) { char var[512]; char *varname; SwigType *ftype; int bare_substitution_count = 0; Replaceall(s, "$typemap", "$TYPEMAP"); /* workaround for $type substitution below */ ftype = SwigType_typedef_resolve_all(type); if (!pname) pname = lname; { int replace = 0; Parm *p = locals; while (p) { String *pname = Getattr(p, "name"); if (Strchr(Getattr(p, "type"), '$') || (pname && Strchr(pname, '$'))) replace = 1; p = nextSibling(p); } if (!replace) locals = 0; } sprintf(var, "$%d_", index); varname = &var[strlen(var)]; /* If the original datatype was an array. We're going to go through and substitute its array dimensions */ if (SwigType_isarray(type) || SwigType_isarray(ftype)) { String *size; int ndim; int i; if (SwigType_array_ndim(type) != SwigType_array_ndim(ftype)) type = ftype; ndim = SwigType_array_ndim(type); size = NewStringEmpty(); for (i = 0; i < ndim; i++) { String *dim = SwigType_array_getdim(type, i); if (index == 1) { char t[32]; sprintf(t, "$dim%d", i); Replace(s, t, dim, DOH_REPLACE_ANY); replace_local_types(locals, t, dim); } sprintf(varname, "dim%d", i); Replace(s, var, dim, DOH_REPLACE_ANY); replace_local_types(locals, var, dim); if (Len(size)) Putc('*', size); Append(size, dim); Delete(dim); } sprintf(varname, "size"); Replace(s, var, size, DOH_REPLACE_ANY); replace_local_types(locals, var, size); Delete(size); } /* Parameter name substitution */ if (index == 1) { Replace(s, "$parmname", pname, DOH_REPLACE_ANY); } strcpy(varname, "name"); Replace(s, var, pname, DOH_REPLACE_ANY); /* Type-related stuff */ { SwigType *star_type, *amp_type, *base_type, *lex_type; SwigType *ltype, *star_ltype, *amp_ltype; String *mangle, *star_mangle, *amp_mangle, *base_mangle, *base_name, *base_type_str; String *descriptor, *star_descriptor, *amp_descriptor; String *ts; char *sc; sc = Char(s); if (strstr(sc, "type") || check_locals_type(locals, "type")) { /* Given type : $type */ ts = SwigType_str(type, 0); if (index == 1) { Replace(s, "$type", ts, DOH_REPLACE_ANY); replace_local_types(locals, "$type", type); } strcpy(varname, "type"); Replace(s, var, ts, DOH_REPLACE_ANY); replace_local_types(locals, var, type); Delete(ts); sc = Char(s); } if (strstr(sc, "ltype") || check_locals_type(locals, "ltype")) { /* Local type: $ltype */ ltype = SwigType_ltype(type); ts = SwigType_str(ltype, 0); if (index == 1) { Replace(s, "$ltype", ts, DOH_REPLACE_ANY); replace_local_types(locals, "$ltype", ltype); } strcpy(varname, "ltype"); Replace(s, var, ts, DOH_REPLACE_ANY); replace_local_types(locals, var, ltype); Delete(ts); Delete(ltype); sc = Char(s); } if (strstr(sc, "mangle") || strstr(sc, "descriptor")) { /* Mangled type */ mangle = SwigType_manglestr(type); if (index == 1) Replace(s, "$mangle", mangle, DOH_REPLACE_ANY); strcpy(varname, "mangle"); Replace(s, var, mangle, DOH_REPLACE_ANY); descriptor = NewStringf("SWIGTYPE%s", mangle); if (index == 1) if (Replace(s, "$descriptor", descriptor, DOH_REPLACE_ANY)) SwigType_remember(type); strcpy(varname, "descriptor"); if (Replace(s, var, descriptor, DOH_REPLACE_ANY)) SwigType_remember(type); Delete(descriptor); Delete(mangle); } /* One pointer level removed */ /* This creates variables of the form $*n_type $*n_ltype */ if (SwigType_ispointer(ftype) || (SwigType_isarray(ftype)) || (SwigType_isreference(ftype)) || (SwigType_isrvalue_reference(ftype))) { if (!(SwigType_isarray(type) || SwigType_ispointer(type) || SwigType_isreference(type) || SwigType_isrvalue_reference(type))) { star_type = Copy(ftype); } else { star_type = Copy(type); } if (!(SwigType_isreference(star_type) || SwigType_isrvalue_reference(star_type))) { if (SwigType_isarray(star_type)) { SwigType_del_element(star_type); } else { SwigType_del_pointer(star_type); } ts = SwigType_str(star_type, 0); if (index == 1) { Replace(s, "$*type", ts, DOH_REPLACE_ANY); replace_local_types(locals, "$*type", star_type); } sprintf(varname, "$*%d_type", index); Replace(s, varname, ts, DOH_REPLACE_ANY); replace_local_types(locals, varname, star_type); Delete(ts); } else { SwigType_del_element(star_type); } star_ltype = SwigType_ltype(star_type); ts = SwigType_str(star_ltype, 0); if (index == 1) { Replace(s, "$*ltype", ts, DOH_REPLACE_ANY); replace_local_types(locals, "$*ltype", star_ltype); } sprintf(varname, "$*%d_ltype", index); Replace(s, varname, ts, DOH_REPLACE_ANY); replace_local_types(locals, varname, star_ltype); Delete(ts); Delete(star_ltype); star_mangle = SwigType_manglestr(star_type); if (index == 1) Replace(s, "$*mangle", star_mangle, DOH_REPLACE_ANY); sprintf(varname, "$*%d_mangle", index); Replace(s, varname, star_mangle, DOH_REPLACE_ANY); star_descriptor = NewStringf("SWIGTYPE%s", star_mangle); if (index == 1) if (Replace(s, "$*descriptor", star_descriptor, DOH_REPLACE_ANY)) SwigType_remember(star_type); sprintf(varname, "$*%d_descriptor", index); if (Replace(s, varname, star_descriptor, DOH_REPLACE_ANY)) SwigType_remember(star_type); Delete(star_descriptor); Delete(star_mangle); Delete(star_type); } else { /* TODO: Signal error if one of the $* substitutions is requested */ } /* One pointer level added */ amp_type = Copy(type); SwigType_add_pointer(amp_type); ts = SwigType_str(amp_type, 0); if (index == 1) { Replace(s, "$&type", ts, DOH_REPLACE_ANY); replace_local_types(locals, "$&type", amp_type); } sprintf(varname, "$&%d_type", index); Replace(s, varname, ts, DOH_REPLACE_ANY); replace_local_types(locals, varname, amp_type); Delete(ts); amp_ltype = SwigType_ltype(type); SwigType_add_pointer(amp_ltype); ts = SwigType_str(amp_ltype, 0); if (index == 1) { Replace(s, "$<ype", ts, DOH_REPLACE_ANY); replace_local_types(locals, "$<ype", amp_ltype); } sprintf(varname, "$&%d_ltype", index); Replace(s, varname, ts, DOH_REPLACE_ANY); replace_local_types(locals, varname, amp_ltype); Delete(ts); Delete(amp_ltype); amp_mangle = SwigType_manglestr(amp_type); if (index == 1) Replace(s, "$&mangle", amp_mangle, DOH_REPLACE_ANY); sprintf(varname, "$&%d_mangle", index); Replace(s, varname, amp_mangle, DOH_REPLACE_ANY); amp_descriptor = NewStringf("SWIGTYPE%s", amp_mangle); if (index == 1) if (Replace(s, "$&descriptor", amp_descriptor, DOH_REPLACE_ANY)) SwigType_remember(amp_type); sprintf(varname, "$&%d_descriptor", index); if (Replace(s, varname, amp_descriptor, DOH_REPLACE_ANY)) SwigType_remember(amp_type); Delete(amp_descriptor); Delete(amp_mangle); Delete(amp_type); /* Base type */ if (SwigType_isarray(type)) { base_type = Copy(type); Delete(SwigType_pop_arrays(base_type)); } else { base_type = SwigType_base(type); } base_type_str = SwigType_str(base_type, 0); base_name = SwigType_namestr(base_type_str); if (index == 1) { Replace(s, "$basetype", base_name, DOH_REPLACE_ANY); replace_local_types(locals, "$basetype", base_name); } strcpy(varname, "basetype"); Replace(s, var, base_type_str, DOH_REPLACE_ANY); replace_local_types(locals, var, base_name); base_mangle = SwigType_manglestr(base_type); if (index == 1) Replace(s, "$basemangle", base_mangle, DOH_REPLACE_ANY); strcpy(varname, "basemangle"); Replace(s, var, base_mangle, DOH_REPLACE_ANY); Delete(base_mangle); Delete(base_name); Delete(base_type_str); Delete(base_type); lex_type = SwigType_base(rtype); if (index == 1) Replace(s, "$lextype", lex_type, DOH_REPLACE_ANY); strcpy(varname, "lextype"); Replace(s, var, lex_type, DOH_REPLACE_ANY); Delete(lex_type); } /* Replace variable usage $n. with (&$n)-> */ { char temp[64]; sprintf(var, "$%d.", index); sprintf(temp, "(&$%d)->", index); Replace(s, var, temp, DOH_REPLACE_ANY); } /* Replace the bare $n variable */ sprintf(var, "$%d", index); bare_substitution_count = replace_with_override(s, var, lname, DOH_REPLACE_NUMBER_END, override_vars); replace_local_names(locals, var, lname, override_vars); Delete(ftype); return bare_substitution_count; } /* ------------------------------------------------------------------------ * static typemap_locals() * * Takes a string, a parameter list and a wrapper function argument and * creates the local variables. * ------------------------------------------------------------------------ */ static void typemap_locals(String *s, ParmList *l, Wrapper *f, int argnum) { Parm *p; char *new_name; p = l; while (p) { SwigType *pt = Getattr(p, "type"); SwigType *at = SwigType_alttype(pt, 1); String *pn = Getattr(p, "name"); String *value = Getattr(p, "value"); if (at) pt = at; if (pn) { if (Len(pn) > 0) { String *str; int isglobal = 0; str = NewStringEmpty(); if (strncmp(Char(pn), "_global_", 8) == 0) { isglobal = 1; } /* If the user gave us $type as the name of the local variable, we'll use the passed datatype instead */ if ((argnum >= 0) && !isglobal && !Getattr(p, "origname")) { Printf(str, "%s%d", pn, argnum); } else { Append(str, pn); } if (isglobal && Wrapper_check_local(f, str)) { p = nextSibling(p); Delete(str); if (at) Delete(at); continue; } if (value) { String *pstr = SwigType_str(pt, str); new_name = Wrapper_new_localv(f, str, pstr, "=", value, NIL); Delete(pstr); } else { String *pstr = SwigType_str(pt, str); new_name = Wrapper_new_localv(f, str, pstr, NIL); Delete(pstr); } if (!isglobal) { /* Substitute */ Replace(s, pn, new_name, DOH_REPLACE_ID | DOH_REPLACE_NOQUOTE); } Delete(str); } } p = nextSibling(p); if (at) Delete(at); } } /* ----------------------------------------------------------------------------- * typemap_warn() * * If any warning message is attached to this parameter's "tmap::warning" * attribute, return the warning message (special variables will need expanding * before displaying the warning). * ----------------------------------------------------------------------------- */ static String *typemap_warn(const_String_or_char_ptr tmap_method, Parm *p) { String *temp = NewStringf("%s:warning", tmap_method); String *w = Getattr(p, typemap_method_name(temp)); Delete(temp); return w ? Copy(w) : 0; } /* ----------------------------------------------------------------------------- * typemap_merge_fragment_kwargs() * * If multiple 'fragment' attributes are provided to a typemap, combine them by * concatenating with commas. * ----------------------------------------------------------------------------- */ static void typemap_merge_fragment_kwargs(Parm *kw) { Parm *reattach_kw = NULL; Parm *prev_kw = NULL; Parm *next_kw = NULL; String *fragment = NULL; while (kw) { next_kw = nextSibling(kw); if (Strcmp(Getattr(kw, "name"), "fragment") == 0) { String *thisfragment = Getattr(kw, "value"); String *kwtype = Getattr(kw, "type"); if (!fragment) { /* First fragment found; it should remain in the list */ fragment = thisfragment; prev_kw = kw; } else { /* Concatenate to previously found fragment */ Printv(fragment, ",", thisfragment, NULL); reattach_kw = prev_kw; } if (kwtype) { String *mangle = Swig_name_mangle_type(kwtype); Append(fragment, mangle); Delete(mangle); /* Remove 'type' from kwargs so it's not duplicated later */ Setattr(kw, "type", NULL); } } else { /* Not a fragment */ if (reattach_kw) { /* Update linked list to remove duplicate fragment */ DohIncref(kw); set_nextSibling(reattach_kw, kw); set_previousSibling(kw, reattach_kw); Delete(reattach_kw); reattach_kw = NULL; } prev_kw = kw; } kw = next_kw; } if (reattach_kw) { /* Update linked list to remove duplicate fragment */ set_nextSibling(reattach_kw, kw); } } /* ----------------------------------------------------------------------------- * Swig_typemap_lookup() * * Attach one or more typemaps to a node and optionally generate the typemap contents * into the wrapper. * * Looks for a typemap matching the given type and name and attaches the typemap code * and any typemap attributes to the provided node. * * The node should contain the "type" and "name" attributes for the typemap match on. * input. The typemap code and typemap attribute values are attached onto the node * prefixed with "tmap:". For example with tmap_method="in", the typemap code can be retrieved * with a call to Getattr(node, "tmap:in") (this is also the string returned) and the * "noblock" attribute can be retrieved with a call to Getattr(node, "tmap:in:noblock"). * * tmap_method - typemap method, eg "in", "out", "newfree" * node - the node to attach the typemap and typemap attributes to * lname - name of variable to substitute $1, $2 etc for * f - wrapper code to generate into if non null * actioncode - code to generate into f before the out typemap code, unless * the optimal attribute is set in the out typemap in which case * $1 in the out typemap will be replaced by the code in actioncode. * ----------------------------------------------------------------------------- */ static String *Swig_typemap_lookup_impl(const_String_or_char_ptr tmap_method, Node *node, const_String_or_char_ptr lname, Wrapper *f, String *actioncode) { SwigType *type; SwigType *mtype = 0; String *pname; String *qpname = 0; String *noscope_pname = 0; Hash *tm = 0; String *s = 0; String *sdef = 0; String *warning = 0; ParmList *locals; ParmList *kw; char temp[256]; String *symname; String *cname = 0; String *clname = 0; char *cmethod = Char(tmap_method); int optimal_attribute = 0; int optimal_substitution = 0; int delete_optimal_attribute = 0; int num_substitutions = 0; SwigType *matchtype = 0; type = Getattr(node, "type"); if (!type) return sdef; /* Special hook (hack!). Check for the 'ref' feature and add code it contains to any 'newfree' typemap code. * We could choose to put this hook into a number of different typemaps, not necessarily 'newfree'... * Rather confusingly 'newfree' is used to release memory and the 'ref' feature is used to add in memory references - yuck! */ if (Cmp(tmap_method, "newfree") == 0) { String *base = SwigType_base(type); Node *typenode = Swig_symbol_clookup(base, 0); if (typenode) sdef = Swig_ref_call(typenode, lname); Delete(base); } pname = Getattr(node, "name"); noscope_pname = Copy(pname); if (pname && Getattr(node, "sym:symtab")) { /* Add on a qualified name search for any symbol in the symbol table, for example: * struct Foo { * int *foo(int bar) -> Foo::foo * }; * Note that if node is a parameter (Parm *) then there will be no symbol table attached to the Parm *. */ String *qsn; if (Swig_scopename_check(pname)) { /* sometimes pname is qualified, so we remove all the scope for the lookup */ Delete(noscope_pname); noscope_pname = Swig_scopename_last(pname); /* Printf(stdout, "Removed scope: %s => %s\n", pname, noscope_pname); */ } qsn = Swig_symbol_qualified(node); if (qsn && Len(qsn)) { qpname = NewStringf("%s::%s", qsn, noscope_pname); Delete(qsn); } } tm = typemap_search(tmap_method, type, noscope_pname, qpname, &mtype, node); if (typemap_search_debug) debug_search_result_display(tm); if (typemaps_used_debug && tm) { String *typestr = SwigType_str(type, qpname ? qpname : pname); Swig_diagnostic(Getfile(node), Getline(node), "Typemap for %s (%s) : %%%s\n", typestr, tmap_method, Getattr(tm, "source")); assert(Getfile(node) && Len(Getfile(node)) > 0); /* Missing file and line numbering information */ Delete(typestr); } Delete(qpname); qpname = 0; Delete(noscope_pname); noscope_pname = 0; if (!tm) return sdef; s = Getattr(tm, "code"); if (!s) return sdef; /* Empty typemap. No match */ if (Cmp(s, "pass") == 0) return sdef; s = Copy(s); /* Make a local copy of the typemap code */ /* Look in the "out" typemap for the "optimal" attribute */ if (Cmp(cmethod, "out") == 0) { kw = Getattr(tm, "kwargs"); while (kw) { if (Cmp(Getattr(kw, "name"), "optimal") == 0) { optimal_attribute = GetFlag(kw, "value"); break; } kw = nextSibling(kw); } } if (optimal_attribute) { /* Note: "out" typemap is the only typemap that will have the "optimal" attribute set. * If f and actioncode are NULL, then the caller is just looking to attach the "out" attributes * ie, not use the typemap code, otherwise both f and actioncode must be non null. */ if (actioncode) { const String *result_equals = NewStringf("%s = ", Swig_cresult_name()); /* check that the code in the typemap can be used in this optimal way. * The code should be in the form "result = ...;\n". We need to extract * the "..." part. This may not be possible for various reasons, eg * code added by %exception. This optimal code generation is bit of a * hack and circumvents the normal requirement for a temporary variable * to hold the result returned from a wrapped function call. */ if (Strncmp(actioncode, result_equals, Len(result_equals)) == 0 && Strchr(actioncode, ';') == Char(actioncode) + Len(actioncode) - 2 && Char(actioncode)[Len(actioncode) - 1] == '\n') { clname = NewStringWithSize(Char(actioncode) + Len(result_equals), Len(actioncode) - Len(result_equals) - 2); lname = clname; actioncode = 0; optimal_substitution = 1; } else { Swig_warning(WARN_TYPEMAP_OUT_OPTIMAL_IGNORED, Getfile(node), Getline(node), "Method %s usage of the optimal attribute ignored\n", Swig_name_decl(node)); Swig_warning(WARN_TYPEMAP_OUT_OPTIMAL_IGNORED, Getfile(s), Getline(s), "in the out typemap as the following cannot be used to generate optimal code: %s\n", actioncode); delete_optimal_attribute = 1; } } else { assert(!f); } } if (actioncode) { assert(f); Append(f->code, actioncode); } /* emit local variables declared in typemap, eg emit declarations for aa and bb in: * %typemap(in) foo (int aa, int bb) "..." */ locals = Getattr(tm, "locals"); if (locals) locals = CopyParmList(locals); if (pname) { if (SwigType_istemplate(pname)) { cname = SwigType_namestr(pname); pname = cname; } } if (SwigType_istemplate((char *) lname)) { clname = SwigType_namestr((char *) lname); lname = clname; } matchtype = mtype && SwigType_isarray(mtype) ? mtype : type; num_substitutions = typemap_replace_vars(s, locals, matchtype, type, pname, (char *) lname, 1, NULL); if (optimal_substitution && num_substitutions > 1) { Swig_warning(WARN_TYPEMAP_OUT_OPTIMAL_MULTIPLE, Getfile(node), Getline(node), "Multiple calls to %s might be generated due to\n", Swig_name_decl(node)); Swig_warning(WARN_TYPEMAP_OUT_OPTIMAL_MULTIPLE, Getfile(s), Getline(s), "optimal attribute usage in the out typemap.\n"); } if (locals && f) { typemap_locals(s, locals, f, -1); } { ParmList *parm_sublist = NewParmWithoutFileLineInfo(type, pname); Setattr(parm_sublist, "lname", lname); replace_embedded_typemap(s, parm_sublist, f, tm); Delete(parm_sublist); } /* Attach kwargs - ie the typemap attributes */ kw = Getattr(tm, "kwargs"); typemap_merge_fragment_kwargs(kw); while (kw) { String *value = Copy(Getattr(kw, "value")); String *kwtype = Getattr(kw, "type"); char *ckwname = Char(Getattr(kw, "name")); { /* Expand special variables in typemap attributes. */ SwigType *ptype = Getattr(node, "type"); String *pname = Getattr(node, "name"); SwigType *mtype = Getattr(node, "tmap:match"); SwigType *matchtype = mtype ? mtype : ptype; ParmList *parm_sublist; typemap_replace_vars(value, NULL, matchtype, ptype, pname, (char *)lname, 1, NULL); /* Expand special variable macros (embedded typemaps) in typemap attributes. */ parm_sublist = NewParmWithoutFileLineInfo(ptype, pname); Setattr(parm_sublist, "lname", lname); replace_embedded_typemap(value, parm_sublist, NULL, tm); Delete(parm_sublist); } if (kwtype) { String *mangle = Swig_name_mangle_type(kwtype); Append(value, mangle); Delete(mangle); } sprintf(temp, "%s:%s", cmethod, ckwname); Setattr(node, typemap_method_name(temp), value); Delete(value); kw = nextSibling(kw); } if (delete_optimal_attribute) Delattr(node, "tmap:out:optimal"); Replace(s, "$name", pname, DOH_REPLACE_ANY); symname = Getattr(node, "sym:name"); if (symname) Replace(s, "$symname", symname, DOH_REPLACE_ANY); Setattr(node, typemap_method_name(tmap_method), s); if (locals) { sprintf(temp, "%s:locals", cmethod); Setattr(node, typemap_method_name(temp), locals); Delete(locals); } if (Checkattr(tm, "type", "SWIGTYPE")) { sprintf(temp, "%s:SWIGTYPE", cmethod); Setattr(node, typemap_method_name(temp), "1"); } /* Print warnings, if any */ warning = typemap_warn(cmethod, node); if (warning) { typemap_replace_vars(warning, 0, matchtype, type, pname, (char *) lname, 1, NULL); Replace(warning, "$name", pname, DOH_REPLACE_ANY); if (symname) Replace(warning, "$symname", symname, DOH_REPLACE_ANY); Swig_warning(0, Getfile(node), Getline(node), "%s\n", warning); Delete(warning); } /* Look for code fragments */ { String *fragment; sprintf(temp, "%s:fragment", cmethod); fragment = Getattr(node, typemap_method_name(temp)); if (fragment) { String *fname = Copy(fragment); Setfile(fname, Getfile(node)); Setline(fname, Getline(node)); Swig_fragment_emit(fname); Delete(fname); } } Delete(cname); Delete(clname); Delete(mtype); if (sdef) { /* put 'ref' and 'newfree' codes together */ String *p = NewStringf("%s\n%s", sdef, s); Delete(s); Delete(sdef); s = p; } Delete(actioncode); return s; } String *Swig_typemap_lookup_out(const_String_or_char_ptr tmap_method, Node *node, const_String_or_char_ptr lname, Wrapper *f, String *actioncode) { assert(actioncode); assert(Cmp(tmap_method, "out") == 0); return Swig_typemap_lookup_impl(tmap_method, node, lname, f, actioncode); } String *Swig_typemap_lookup(const_String_or_char_ptr tmap_method, Node *node, const_String_or_char_ptr lname, Wrapper *f) { return Swig_typemap_lookup_impl(tmap_method, node, lname, f, 0); } /* ----------------------------------------------------------------------------- * typemap_attach_kwargs() * * If this hash (tm) contains a linked list of parameters under its "kwargs" * attribute, add keys for each of those named keyword arguments to this * parameter for later use. * For example, attach the typemap attributes to firstp (first parameter in parameter list): * %typemap(in, foo="xyz") ... * A new attribute called "tmap:in:foo" with value "xyz" is attached to firstp. * Also expands special variables and special variable macros in the typemap attributes. * ----------------------------------------------------------------------------- */ static void typemap_attach_kwargs(Hash *tm, const_String_or_char_ptr tmap_method, Parm *firstp, int nmatch) { String *temp = NewStringEmpty(); Parm *kw = Getattr(tm, "kwargs"); typemap_merge_fragment_kwargs(kw); while (kw) { String *value = Copy(Getattr(kw, "value")); String *type = Getattr(kw, "type"); int i; Parm *p = firstp; /* Expand special variables */ for (i = 0; i < nmatch; i++) { SwigType *type = Getattr(p, "type"); String *pname = Getattr(p, "name"); String *lname = Getattr(p, "lname"); SwigType *mtype = Getattr(p, "tmap:match"); SwigType *matchtype = mtype ? mtype : type; typemap_replace_vars(value, NULL, matchtype, type, pname, lname, i + 1, NULL); p = nextSibling(p); } /* Expand special variable macros (embedded typemaps). * Special variable are expanded first above as they might be used in the special variable macros. * For example: $typemap(imtype, $2_type). */ p = firstp; for (i = 0; i < nmatch; i++) { SwigType *type = Getattr(p, "type"); String *pname = Getattr(p, "name"); String *lname = Getattr(p, "lname"); ParmList *parm_sublist = NewParmWithoutFileLineInfo(type, pname); Setattr(parm_sublist, "lname", lname); replace_embedded_typemap(value, parm_sublist, NULL, tm); p = nextSibling(p); } if (type) { Hash *v = NewHash(); Setattr(v, "type", type); Setattr(v, "value", value); Delete(value); value = v; } Clear(temp); Printf(temp, "%s:%s", tmap_method, Getattr(kw, "name")); Setattr(firstp, typemap_method_name(temp), value); Delete(value); kw = nextSibling(kw); } Clear(temp); Printf(temp, "%s:match_type", tmap_method); Setattr(firstp, typemap_method_name(temp), Getattr(tm, "type")); Delete(temp); } static void typemap_emit_code_fragments(const_String_or_char_ptr tmap_method, Parm *p) { String *temp = NewStringf("%s:fragment", tmap_method); String *f = Getattr(p, typemap_method_name(temp)); if (f) { String *fname = Copy(f); Setfile(fname, Getfile(p)); Setline(fname, Getline(p)); Swig_fragment_emit(fname); Delete(fname); } Delete(temp); } static String *typemap_get_option(Hash *tm, const_String_or_char_ptr name) { Parm *kw = Getattr(tm, "kwargs"); while (kw) { String *kname = Getattr(kw, "name"); if (Equal(kname, name)) { return Getattr(kw, "value"); } kw = nextSibling(kw); } return 0; } /* ----------------------------------------------------------------------------- * Swig_typemap_attach_parms() * * Given a parameter list, this function attaches all of the typemaps and typemap * attributes to the parameter for each type in the parameter list. * * This function basically provides the typemap code and typemap attribute values as * attributes on each parameter prefixed with "tmap:". For example with tmap_method="in", the typemap * code can be retrieved for the first parameter with a call to Getattr(parm, "tmap:in") * and the "numinputs" attribute can be retrieved with a call to Getattr(parm, "tmap:in:numinputs"). * * tmap_method - typemap method, eg "in", "out", "newfree" * parms - parameter list to attach each typemap and all typemap attributes * f - wrapper code to generate into if non null * override_vars - Hash of variables from $typemap(...) that will override special variable replacements * ----------------------------------------------------------------------------- */ static void typemap_attach_parms(const_String_or_char_ptr tmap_method, ParmList *parms, Wrapper *f, Hash *override_vars) { Parm *p, *firstp; Hash *tm; int nmatch = 0; int i; String *s; String *warning = 0; ParmList *locals; int argnum = 0; char temp[256]; char *cmethod = Char(tmap_method); String *kwmatch = 0; p = parms; #ifdef SWIG_DEBUG Printf(stdout, "Swig_typemap_attach_parms: %s\n", tmap_method); #endif while (p) { argnum++; nmatch = 0; #ifdef SWIG_DEBUG Printf(stdout, "parms: %s %s %s\n", tmap_method, Getattr(p, "name"), Getattr(p, "type")); #endif tm = typemap_search_multi(tmap_method, p, &nmatch); #ifdef SWIG_DEBUG if (tm) Printf(stdout, "found: %s\n", tm); #endif if (!tm) { p = nextSibling(p); continue; } /* Check if the typemap requires to match the type of another typemap, for example: %typemap(in) SWIGTYPE * (int var) {...} %typemap(freearg,match="in") SWIGTYPE * {if (var$argnum) ...} here, the freearg typemap requires the "in" typemap to match, or the 'var$argnum' variable will not exist. */ kwmatch = typemap_get_option(tm, "match"); if (kwmatch) { String *tmname = NewStringf("tmap:%s", kwmatch); String *tmin = Getattr(p, tmname); Delete(tmname); #ifdef SWIG_DEBUG if (tm) Printf(stdout, "matching: %s\n", kwmatch); #endif if (tmin) { String *tmninp = NewStringf("tmap:%s:numinputs", kwmatch); String *ninp = Getattr(p, tmninp); Delete(tmninp); if (ninp && Equal(ninp, "0")) { p = nextSibling(p); continue; } else { SwigType *typetm = Getattr(tm, "type"); String *temp = NewStringf("tmap:%s:match_type", kwmatch); SwigType *typein = Getattr(p, temp); Delete(temp); if (!Equal(typein, typetm)) { p = nextSibling(p); continue; } else { int nnmatch; Hash *tmapin = typemap_search_multi(kwmatch, p, &nnmatch); String *tmname = Getattr(tm, "pname"); String *tnname = Getattr(tmapin, "pname"); if (!(tmname && tnname && Equal(tmname, tnname)) && !(!tmname && !tnname)) { p = nextSibling(p); continue; } } } } else { p = nextSibling(p); continue; } } s = Getattr(tm, "code"); if (!s) { p = nextSibling(p); continue; } #ifdef SWIG_DEBUG if (s) Printf(stdout, "code: %s\n", s); #endif /* Empty typemap. No match */ if (Cmp(s, "pass") == 0) { p = nextSibling(p); continue; } s = Copy(s); locals = Getattr(tm, "locals"); if (locals) locals = CopyParmList(locals); firstp = p; #ifdef SWIG_DEBUG Printf(stdout, "nmatch: %d\n", nmatch); #endif for (i = 0; i < nmatch; i++) { SwigType *type = Getattr(p, "type"); String *pname = Getattr(p, "name"); String *lname = Getattr(p, "lname"); SwigType *mtype = Getattr(p, "tmap:match"); SwigType *matchtype = mtype ? mtype : type; typemap_replace_vars(s, locals, matchtype, type, pname, lname, i + 1, override_vars); if (mtype) Delattr(p, "tmap:match"); if (Checkattr(tm, "type", "SWIGTYPE")) { sprintf(temp, "%s:SWIGTYPE", cmethod); Setattr(p, typemap_method_name(temp), "1"); } p = nextSibling(p); } if (locals && f) { typemap_locals(s, locals, f, argnum); } replace_embedded_typemap(s, firstp, f, tm); /* Attach attributes to object */ #ifdef SWIG_DEBUG Printf(stdout, "attach: %s %s %s\n", Getattr(firstp, "name"), typemap_method_name(tmap_method), s); #endif Setattr(firstp, typemap_method_name(tmap_method), s); /* Code object */ if (locals) { sprintf(temp, "%s:locals", cmethod); Setattr(firstp, typemap_method_name(temp), locals); Delete(locals); } /* Attach a link to the next parameter. Needed for multimaps */ sprintf(temp, "%s:next", cmethod); Setattr(firstp, typemap_method_name(temp), p); /* Attach kwargs */ typemap_attach_kwargs(tm, tmap_method, firstp, nmatch); /* Replace the argument number */ sprintf(temp, "%d", argnum); Replace(s, "$argnum", temp, DOH_REPLACE_ANY); /* Print warnings, if any */ warning = typemap_warn(tmap_method, firstp); if (warning) { SwigType *type = Getattr(firstp, "type"); String *pname = Getattr(firstp, "name"); String *lname = Getattr(firstp, "lname"); SwigType *mtype = Getattr(firstp, "tmap:match"); SwigType *matchtype = mtype ? mtype : type; typemap_replace_vars(warning, 0, matchtype, type, pname, lname, 1, override_vars); Replace(warning, "$argnum", temp, DOH_REPLACE_ANY); Swig_warning(0, Getfile(firstp), Getline(firstp), "%s\n", warning); Delete(warning); } /* Look for code fragments */ typemap_emit_code_fragments(tmap_method, firstp); /* increase argnum to consider numinputs */ argnum += nmatch - 1; Delete(s); #ifdef SWIG_DEBUG Printf(stdout, "res: %s %s %s\n", Getattr(firstp, "name"), typemap_method_name(tmap_method), Getattr(firstp, typemap_method_name(tmap_method))); #endif } #ifdef SWIG_DEBUG Printf(stdout, "Swig_typemap_attach_parms: end\n"); #endif } void Swig_typemap_attach_parms(const_String_or_char_ptr tmap_method, ParmList *parms, Wrapper *f) { typemap_attach_parms(tmap_method, parms, f, NULL); } /* Splits the arguments of an embedded typemap */ static List *split_embedded_typemap(String *s) { List *args = 0; char *c, *start; int level = 0; int angle_level = 0; int leading = 1; args = NewList(); c = strchr(Char(s), '('); assert(c); c++; start = c; while (*c) { if (*c == '\"') { c++; while (*c) { if (*c == '\\') { c++; } else { if (*c == '\"') break; } c++; } } if ((level == 0) && angle_level == 0 && ((*c == ',') || (*c == ')'))) { String *tmp = NewStringWithSize(start, (int)(c - start)); Append(args, tmp); Delete(tmp); start = c + 1; leading = 1; if (*c == ')') break; c++; continue; } if (*c == '(') level++; if (*c == ')') level--; if (*c == '<') angle_level++; if (*c == '>' && *(c - 1) != '-') angle_level--; if (isspace((int) *c) && leading) start++; if (!isspace((int) *c)) leading = 0; c++; } return args; } /* ----------------------------------------------------------------------------- * Swig_typemap_replace_embedded_typemap() * * For special variable macro $typemap(...) expansion outside of typemaps. * Only limited usage works as most typemap special variables ($1, $input etc) * are not expanded correctly outside of typemaps. * ----------------------------------------------------------------------------- */ void Swig_typemap_replace_embedded_typemap(String *s, Node *file_line_node) { Setfile(s, Getfile(file_line_node)); Setline(s, Getline(file_line_node)); Replaceall(s, "$typemap", "$TYPEMAP"); replace_embedded_typemap(s, 0, 0, file_line_node); } /* ----------------------------------------------------------------------------- * replace_embedded_typemap() * * This function replaces the special variable macro $typemap(...) with typemap * code. The general form of $typemap is as follows: * * $typemap(method, typelist, var1=value, var2=value, ...) * * where varx parameters are optional and undocumented; they were initially an experiment in the first implementation of $typemap. * They allow one to override known special variable replacements or even add in new special variables for replacement * from the calling typemap to the called typemap. * A search is made using the typemap matching rules of form: * * %typemap(method) typelist {...} * * and if found will substitute in the typemap contents, making appropriate variable replacements. * * For example: * $typemap(in, int) # simple usage matching %typemap(in) int { ... } * $typemap(in, int b) # simple usage matching %typemap(in) int b { ... } or above %typemap * $typemap(in, (Foo a, int b)) # multi-argument typemap matching %typemap(in) (Foo a, int b) {...} * ----------------------------------------------------------------------------- */ static void replace_embedded_typemap(String *s, ParmList *parm_sublist, Wrapper *f, Node *file_line_node) { char *start = 0; while ((start = strstr(Char(s), "$TYPEMAP("))) { /* note $typemap capitalisation to $TYPEMAP hack */ /* Gather the parameters */ char *end = 0, *c; int level = 0; String *dollar_typemap; int syntax_error = 1; c = start; while (*c) { if (*c == '(') level++; if (*c == ')') { level--; if (level == 0) { end = c + 1; break; } } c++; } if (end) { dollar_typemap = NewStringWithSize(start, (int)((end - start))); syntax_error = 0; } else { dollar_typemap = NewStringWithSize(start, (int)((c - start))); } if (!syntax_error) { List *l; String *tmap_method; Hash *override_vars; syntax_error = 1; /* Split apart each parameter in $typemap(...) */ l = split_embedded_typemap(dollar_typemap); if (Len(l) >= 2) { ParmList *to_match_parms; tmap_method = Getitem(l, 0); /* the second parameter might contain multiple sub-parameters for multi-argument * typemap matching, so split these parameters apart */ to_match_parms = Swig_cparse_parms(Getitem(l, 1), file_line_node); if (to_match_parms) { Parm *p = to_match_parms; Parm *sub_p = parm_sublist; String *empty_string = NewStringEmpty(); String *lname = empty_string; while (p) { if (sub_p) { lname = Getattr(sub_p, "lname"); sub_p = nextSibling(sub_p); } Setattr(p, "lname", lname); p = nextSibling(p); } Delete(empty_string); } /* process optional extra parameters - the override variable replacements (undocumented) */ override_vars = NewHash(); { int i, ilen; ilen = Len(l); for (i = 2; i < ilen; i++) { String *parm = Getitem(l, i); char *eq = strchr(Char(parm), '='); char *c = Char(parm); if (eq && (eq - c > 0)) { String *name = NewStringWithSize(c, (int)(eq - c)); String *value = NewString(eq + 1); Insert(name, 0, "$"); Setattr(override_vars, name, value); } else { to_match_parms = 0; /* error - variable replacement parameters must be of form varname=value */ } } } /* Perform a typemap search */ if (to_match_parms) { static int already_substituting = 0; String *tm; String *attr; int match = 0; #ifdef SWIG_DEBUG Printf(stdout, "Swig_typemap_attach_parms: embedded\n"); #endif if (already_substituting < 10) { char* found_colon; already_substituting++; if ((in_typemap_search_multi == 0) && typemap_search_debug) { String *dtypemap = NewString(dollar_typemap); Replaceall(dtypemap, "$TYPEMAP", "$typemap"); Printf(stdout, " Containing: %s\n", dtypemap); Delete(dtypemap); } found_colon = Strchr(tmap_method, ':'); if (found_colon) { /* Substitute from a keyword argument to a typemap. Avoid emitting local variables from the attached typemap by passing NULL for the file. */ String *temp_tmap_method = NewStringWithSize(Char(tmap_method), (int)(found_colon - Char(tmap_method))); typemap_attach_parms(temp_tmap_method, to_match_parms, NULL, override_vars); Delete(temp_tmap_method); } else { typemap_attach_parms(tmap_method, to_match_parms, f, override_vars); } already_substituting--; /* Look for the typemap code */ attr = NewStringf("tmap:%s", tmap_method); tm = Getattr(to_match_parms, attr); if (tm) { Printf(attr, "%s", ":next"); /* fail if multi-argument lookup requested in $typemap(...) and the lookup failed */ if (!Getattr(to_match_parms, attr)) { /* Replace parameter variables */ Iterator ki; for (ki = First(override_vars); ki.key; ki = Next(ki)) { Replace(tm, ki.key, ki.item, DOH_REPLACE_ANY); } /* offer the target language module the chance to make special variable substitutions */ Language_replace_special_variables(tmap_method, tm, to_match_parms); /* finish up - do the substitution */ Replace(s, dollar_typemap, tm, DOH_REPLACE_ANY); Delete(tm); match = 1; } } if (!match) { String *dtypemap = NewString(dollar_typemap); Replaceall(dtypemap, "$TYPEMAP", "$typemap"); Swig_error(Getfile(s), Getline(s), "No typemap found for %s\n", dtypemap); Delete(dtypemap); } Delete(attr); } else { /* Simple recursive call check to prevent infinite recursion - this strategy only allows a limited * number of calls by a embedded typemaps to other embedded typemaps though */ String *dtypemap = NewString(dollar_typemap); Replaceall(dtypemap, "$TYPEMAP", "$typemap"); Swig_error(Getfile(s), Getline(s), "Likely recursive $typemap calls containing %s. Use -debug-tmsearch to debug.\n", dtypemap); Delete(dtypemap); } syntax_error = 0; } Delete(override_vars); } Delete(l); } if (syntax_error) { String *dtypemap = NewString(dollar_typemap); Replaceall(dtypemap, "$TYPEMAP", "$typemap"); Swig_error(Getfile(s), Getline(s), "Syntax error in: %s\n", dtypemap); Delete(dtypemap); } Replace(s, dollar_typemap, "", DOH_REPLACE_ANY); Delete(dollar_typemap); } } /* ----------------------------------------------------------------------------- * Swig_typemap_debug() * * Display all typemaps * ----------------------------------------------------------------------------- */ void Swig_typemap_debug(void) { int nesting_level = 2; Printf(stdout, "---[ typemaps ]--------------------------------------------------------------\n"); Swig_print(typemaps, nesting_level); Printf(stdout, "-----------------------------------------------------------------------------\n"); } /* ----------------------------------------------------------------------------- * Swig_typemap_search_debug_set() * * Turn on typemap searching debug display * ----------------------------------------------------------------------------- */ void Swig_typemap_search_debug_set(void) { typemap_search_debug = 1; } /* ----------------------------------------------------------------------------- * Swig_typemap_used_debug_set() * * Turn on typemaps used debug display * ----------------------------------------------------------------------------- */ void Swig_typemap_used_debug_set(void) { typemaps_used_debug = 1; } /* ----------------------------------------------------------------------------- * Swig_typemap_register_debug_set() * * Turn on typemaps used debug display * ----------------------------------------------------------------------------- */ void Swig_typemap_register_debug_set(void) { typemap_register_debug = 1; } swig-4.4.0/Source/Swig/include.c0000664000175000017500000002621015075443613016321 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * include.c * * The functions in this file are used to manage files in the SWIG library. * General purpose functions for opening, including, and retrieving pathnames * are provided. * ----------------------------------------------------------------------------- */ #include "swig.h" static List *directories = 0; /* List of include directories */ static String *lastpath = 0; /* Last file that was included */ static List *pdirectories = 0; /* List of pushed directories */ static int dopush = 1; /* Whether to push directories */ static int file_debug = 0; /* This functions determine whether to push/pop dirs in the preprocessor */ void Swig_set_push_dir(int push) { dopush = push; } int Swig_get_push_dir(void) { return dopush; } /* ----------------------------------------------------------------------------- * Swig_add_directory() * * Adds a directory to the SWIG search path. * ----------------------------------------------------------------------------- */ List *Swig_add_directory(const_String_or_char_ptr dirname) { String *adirname; if (!directories) directories = NewList(); assert(directories); if (dirname) { adirname = NewString(dirname); Append(directories,adirname); Delete(adirname); } return directories; } /* ----------------------------------------------------------------------------- * Swig_push_directory() * * Inserts a directory at the front of the SWIG search path. This is used by * the preprocessor to grab files in the same directory as other included files. * ----------------------------------------------------------------------------- */ void Swig_push_directory(const_String_or_char_ptr dirname) { String *pdirname; if (!Swig_get_push_dir()) return; if (!pdirectories) pdirectories = NewList(); assert(pdirectories); pdirname = NewString(dirname); assert(pdirname); Insert(pdirectories,0,pdirname); Delete(pdirname); } /* ----------------------------------------------------------------------------- * Swig_pop_directory() * * Pops a directory off the front of the SWIG search path. This is used by * the preprocessor. * ----------------------------------------------------------------------------- */ void Swig_pop_directory(void) { if (!Swig_get_push_dir()) return; if (!pdirectories) return; Delitem(pdirectories, 0); } /* ----------------------------------------------------------------------------- * Swig_last_file() * * Returns the full pathname of the last file opened. * ----------------------------------------------------------------------------- */ String *Swig_last_file(void) { assert(lastpath); return lastpath; } /* ----------------------------------------------------------------------------- * Swig_search_path_any() * * Returns a list of the current search paths. * ----------------------------------------------------------------------------- */ static List *Swig_search_path_any(int syspath) { String *filename; List *slist; int i, ilen; slist = NewList(); assert(slist); filename = NewStringEmpty(); assert(filename); Printf(filename, ".%s", SWIG_FILE_DELIMITER); Append(slist, filename); Delete(filename); /* If there are any pushed directories. Add them first */ if (pdirectories) { ilen = Len(pdirectories); for (i = 0; i < ilen; i++) { filename = NewString(Getitem(pdirectories,i)); Append(filename,SWIG_FILE_DELIMITER); Append(slist,filename); Delete(filename); } } /* Add system directories next */ ilen = Len(directories); for (i = 0; i < ilen; i++) { filename = NewString(Getitem(directories,i)); Append(filename,SWIG_FILE_DELIMITER); if (syspath) { /* If doing a system include, put the system directories first */ Insert(slist,i,filename); } else { /* Otherwise, just put the system directories after the pushed directories (if any) */ Append(slist,filename); } Delete(filename); } return slist; } List *Swig_search_path(void) { return Swig_search_path_any(0); } /* ----------------------------------------------------------------------------- * Swig_open() * * open a file, optionally looking for it in the include path. Returns an open * FILE * on success. * ----------------------------------------------------------------------------- */ static FILE *Swig_open_file(const_String_or_char_ptr name, int sysfile, int use_include_path) { FILE *f; String *filename; List *spath = 0; char *cname; int i, ilen, nbytes; char bom[3]; if (!directories) directories = NewList(); assert(directories); cname = Char(name); filename = NewString(cname); assert(filename); if (file_debug) { Printf(stdout, " Open: %s\n", filename); } f = fopen(Char(filename), "r"); if (!f && use_include_path) { spath = Swig_search_path_any(sysfile); ilen = Len(spath); for (i = 0; i < ilen; i++) { Clear(filename); Printf(filename, "%s%s", Getitem(spath, i), cname); f = fopen(Char(filename), "r"); if (f) break; } Delete(spath); } if (f) { Delete(lastpath); lastpath = filename; /* Skip the UTF-8 BOM if it's present */ nbytes = (int)fread(bom, 1, 3, f); if (nbytes == 3 && bom[0] == (char)0xEF && bom[1] == (char)0xBB && bom[2] == (char)0xBF) { /* skip */ } else { fseek(f, 0, SEEK_SET); } } return f; } /* Open a file - searching the include paths to find it */ FILE *Swig_include_open(const_String_or_char_ptr name) { return Swig_open_file(name, 0, 1); } /* Open a file - does not use include paths to find it */ FILE *Swig_open(const_String_or_char_ptr name) { return Swig_open_file(name, 0, 0); } /* ----------------------------------------------------------------------------- * Swig_read_file() * * Reads data from an open FILE * and returns it as a string. * ----------------------------------------------------------------------------- */ String *Swig_read_file(FILE *f) { int len; char buffer[4096]; String *str = NewStringEmpty(); assert(str); while (1) { size_t c = fread(buffer, 1, sizeof(buffer), f); if (c > 0) Write(str, buffer, (int)c); if (c < sizeof(buffer)) break; } len = Len(str); /* Add a newline if not present on last line -- the preprocessor seems to * rely on \n and not EOF terminating lines */ if (len) { char *cstr = Char(str); if (cstr[len - 1] != '\n') { Append(str, "\n"); } } return str; } /* ----------------------------------------------------------------------------- * Swig_include() * * Opens a file and returns it as a string. * ----------------------------------------------------------------------------- */ static String *Swig_include_any(const_String_or_char_ptr name, int sysfile) { FILE *f; String *str; String *file; f = Swig_open_file(name, sysfile, 1); if (!f) return 0; str = Swig_read_file(f); fclose(f); Seek(str, 0, SEEK_SET); file = Copy(Swig_last_file()); Setfile(str, file); Delete(file); Setline(str, 1); return str; } String *Swig_include(const_String_or_char_ptr name) { return Swig_include_any(name, 0); } String *Swig_include_sys(const_String_or_char_ptr name) { return Swig_include_any(name, 1); } /* ----------------------------------------------------------------------------- * Swig_insert_file() * * Copies the contents of a file into another file * ----------------------------------------------------------------------------- */ int Swig_insert_file(const_String_or_char_ptr filename, File *outfile) { char buffer[4096]; int nbytes; FILE *f = Swig_include_open(filename); if (!f) return -1; while ((nbytes = Read(f, buffer, 4096)) > 0) { Write(outfile, buffer, nbytes); } fclose(f); return 0; } /* ----------------------------------------------------------------------------- * Swig_register_filebyname() * * Register a "named" file with the core. Named files can become targets * for %insert directives and other SWIG operations. This function takes * the place of the f_header, f_wrapper, f_init, and other global variables * in SWIG1.1 * ----------------------------------------------------------------------------- */ static Hash *named_files = 0; void Swig_register_filebyname(const_String_or_char_ptr filename, File *outfile) { if (!named_files) named_files = NewHash(); Setattr(named_files, filename, outfile); } /* ----------------------------------------------------------------------------- * Swig_filebyname() * * Get a named file * ----------------------------------------------------------------------------- */ File *Swig_filebyname(const_String_or_char_ptr filename) { if (!named_files) return 0; return Getattr(named_files, filename); } /* ----------------------------------------------------------------------------- * Swig_file_extension() * * Returns the extension of a file * ----------------------------------------------------------------------------- */ String *Swig_file_extension(const_String_or_char_ptr filename) { String *name = Swig_file_filename(filename); const char *c = strrchr(Char(name), '.'); String *extension = c ? NewString(c) : NewString(""); Delete(name); return extension; } /* ----------------------------------------------------------------------------- * Swig_file_basename() * * Returns the filename with the extension removed. * ----------------------------------------------------------------------------- */ String *Swig_file_basename(const_String_or_char_ptr filename) { String *extension = Swig_file_extension(filename); String *basename = NewStringWithSize(filename, Len(filename) - Len(extension)); Delete(extension); return basename; } /* ----------------------------------------------------------------------------- * Swig_file_filename() * * Return the file name with any leading path stripped off * ----------------------------------------------------------------------------- */ String *Swig_file_filename(const_String_or_char_ptr filename) { const char *delim = SWIG_FILE_DELIMITER; const char *c = strrchr(Char(filename), *delim); return c ? NewString(c + 1) : NewString(filename); } /* ----------------------------------------------------------------------------- * Swig_file_dirname() * * Return the name of the directory associated with a file * ----------------------------------------------------------------------------- */ String *Swig_file_dirname(const_String_or_char_ptr filename) { const char *delim = SWIG_FILE_DELIMITER; const char *c = strrchr(Char(filename), *delim); return c ? NewStringWithSize(filename, (int)(c - Char(filename) + 1)) : NewString(""); } /* * Swig_file_debug() */ void Swig_file_debug_set(void) { file_debug = 1; } swig-4.4.0/Source/Swig/swigtree.h0000664000175000017500000000455315075443613016542 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigtree.h * * These functions are used to access and manipulate the SWIG parse tree. * The structure of this tree is modeled directly after XML-DOM. The attribute * and function names are meant to be similar. * ----------------------------------------------------------------------------- */ /* Macros to traverse the DOM tree */ #define nodeType(x) Getattr(x,"nodeType") #define parentNode(x) Getattr(x,"parentNode") #define previousSibling(x) Getattr(x,"previousSibling") #define nextSibling(x) Getattr(x,"nextSibling") #define firstChild(x) Getattr(x,"firstChild") #define lastChild(x) Getattr(x,"lastChild") /* Macros to set up the DOM tree (mostly used by the parser) */ #define set_nodeType(x,v) Setattr(x,"nodeType",v) #define set_parentNode(x,v) Setattr(x,"parentNode",v) #define set_previousSibling(x,v) Setattr(x,"previousSibling",v) #define set_nextSibling(x,v) Setattr(x,"nextSibling",v) #define set_firstChild(x,v) Setattr(x,"firstChild",v) #define set_lastChild(x,v) Setattr(x,"lastChild",v) /* Utility functions */ extern int checkAttribute(Node *obj, const_String_or_char_ptr name, const_String_or_char_ptr value); extern void appendChild(Node *node, Node *child); extern void prependChild(Node *node, Node *child); extern void removeNode(Node *node); extern Node *copyNode(Node *node); extern void appendSibling(Node *node, Node *child); /* Node restoration/restore functions */ extern void Swig_require(const char *ns, Node *node, ...); extern void Swig_save(const char *ns, Node *node, ...); extern void Swig_restore(Node *node); /* Debugging of parse trees */ extern void Swig_print_tags(File *obj, Node *root); extern void Swig_print_tree(Node *obj); extern void Swig_print_node(Node *obj); extern int Swig_print_quiet(int quiet); swig-4.4.0/Source/Swig/error.c0000664000175000017500000002406015075443613016030 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * error.c * * Error handling functions. These are used to issue warnings and * error messages. * ----------------------------------------------------------------------------- */ #include "swig.h" #include #include /* ----------------------------------------------------------------------------- * Commentary on the warning filter. * * The warning filter is a string of numbers prefaced by (-) or (+) to * indicate whether or not a warning message is displayed. For example: * * "-304-201-140+210+201" * * The filter string is scanned left to right and the first occurrence * of a warning number is used to determine printing behavior. * * The same number may appear more than once in the string. For example, in the * above string, "201" appears twice. This simply means that warning 201 * was disabled after it was previously enabled. This may only be temporary * setting--the first number may be removed later in which case the warning * is reenabled. * ----------------------------------------------------------------------------- */ #if defined(_WIN32) # define DEFAULT_ERROR_MSG_FORMAT EMF_MICROSOFT #else # define DEFAULT_ERROR_MSG_FORMAT EMF_STANDARD #endif static ErrorMessageFormat msg_format = DEFAULT_ERROR_MSG_FORMAT; static int silence = 0; /* Silent operation */ static String *filter = 0; /* Warning filter */ static int warnall = 0; static int nwarning = 0; static int nerrors = 0; static int init_fmt = 0; static char wrn_wnum_fmt[64]; static char wrn_nnum_fmt[64]; static char err_line_fmt[64]; static char err_eof_fmt[64]; static char diag_line_fmt[64]; static char diag_eof_fmt[64]; static String *format_filename(const_String_or_char_ptr filename); /* ----------------------------------------------------------------------------- * Swig_warning() * * Issue a warning message on stderr. * ----------------------------------------------------------------------------- */ void Swig_warning(int wnum, const_String_or_char_ptr filename, int line, const char *fmt, ...) { String *out; char *msg; int wrn = 1; va_list ap; if (silence) return; if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT); va_start(ap, fmt); out = NewStringEmpty(); vPrintf(out, fmt, ap); msg = Char(out); if (isdigit((unsigned char) *msg)) { unsigned long result = strtoul(msg, &msg, 10); if (msg != Char(out)) { msg++; wnum = result; } } /* Check in the warning filter */ if (filter) { char temp[32]; char *c; char *f = Char(filter); sprintf(temp, "%d", wnum); while (*f != '\0' && (c = strstr(f, temp))) { if (*(c - 1) == '-') { wrn = 0; /* Warning disabled */ break; } if (*(c - 1) == '+') { wrn = 1; /* Warning enabled */ break; } f += strlen(temp); } } if (warnall || wrn) { String *formatted_filename = format_filename(filename); String *full_message = NewString(""); if (wnum) { Printf(full_message, wrn_wnum_fmt, formatted_filename, line, wnum); } else { Printf(full_message, wrn_nnum_fmt, formatted_filename, line); } Printf(full_message, "%s", msg); Printv(stderr, full_message, NIL); nwarning++; Delete(full_message); Delete(formatted_filename); } Delete(out); va_end(ap); } /* ----------------------------------------------------------------------------- * Swig_error() * * Issue an error message on stderr. * ----------------------------------------------------------------------------- */ void Swig_error(const_String_or_char_ptr filename, int line, const char *fmt, ...) { va_list ap; String *formatted_filename = NULL; String *full_message = NULL; if (silence) return; if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT); va_start(ap, fmt); formatted_filename = format_filename(filename); full_message = NewString(""); if (line > 0) { Printf(full_message, err_line_fmt, formatted_filename, line); } else { Printf(full_message, err_eof_fmt, formatted_filename); } vPrintf(full_message, fmt, ap); Printv(stderr, full_message, NIL); va_end(ap); nerrors++; Delete(full_message); Delete(formatted_filename); } /* ----------------------------------------------------------------------------- * Swig_error_count() * * Returns number of errors received. * ----------------------------------------------------------------------------- */ int Swig_error_count(void) { return nerrors; } /* ----------------------------------------------------------------------------- * Swig_error_silent() * * Set silent flag * ----------------------------------------------------------------------------- */ void Swig_error_silent(int s) { silence = s; } /* ----------------------------------------------------------------------------- * Swig_warnfilter() * * Takes a comma separate list of warning numbers and puts in the filter. * ----------------------------------------------------------------------------- */ void Swig_warnfilter(const_String_or_char_ptr wlist, int add) { char *c; char *cw; String *s; if (!filter) filter = NewStringEmpty(); s = NewString(""); cw = Char(wlist); while (*cw != '\0') { if (*cw != ' ') { Putc(*cw, s); } ++cw; } c = Char(s); c = strtok(c, ", "); while (c) { if (isdigit((int) *c) || (*c == '+') || (*c == '-')) { /* Even if c is a digit, the rest of the string might not be, eg in the case of typemap * warnings (a bit odd really), eg: %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) */ if (add) { Insert(filter, 0, c); if (isdigit((int) *c)) { Insert(filter, 0, "-"); } } else { char *temp = (char *)Malloc(sizeof(char)*strlen(c) + 2); if (isdigit((int) *c)) { sprintf(temp, "-%s", c); } else { strcpy(temp, c); } Replace(filter, temp, "", DOH_REPLACE_FIRST); Free(temp); } } c = strtok(NULL, ", "); } Delete(s); } void Swig_warnall(void) { warnall = 1; } /* ----------------------------------------------------------------------------- * Swig_warn_count() * * Return the number of warnings * ----------------------------------------------------------------------------- */ int Swig_warn_count(void) { return nwarning; } /* ----------------------------------------------------------------------------- * Swig_error_msg_format() * * Set the type of error/warning message display * ----------------------------------------------------------------------------- */ void Swig_error_msg_format(ErrorMessageFormat format) { const char *error = "Error"; const char *warning = "Warning"; const char *fmt_eof = 0; const char *fmt_line = 0; /* here 'format' could be directly a string instead of an enum, but by now a switch is used to translated into one. */ switch (format) { case EMF_MICROSOFT: fmt_line = "%s(%d) "; fmt_eof = "%s(999999) "; /* Is there a special character for EOF? Just use a large number. */ break; case EMF_STANDARD: default: fmt_line = "%s:%d"; fmt_eof = "%s:EOF"; } sprintf(wrn_wnum_fmt, "%s: %s %%d: ", fmt_line, warning); sprintf(wrn_nnum_fmt, "%s: %s: ", fmt_line, warning); sprintf(err_line_fmt, "%s: %s: ", fmt_line, error); sprintf(err_eof_fmt, "%s: %s: ", fmt_eof, error); sprintf(diag_line_fmt, "%s: ", fmt_line); sprintf(diag_eof_fmt, "%s: ", fmt_eof); msg_format = format; init_fmt = 1; } /* ----------------------------------------------------------------------------- * format_filename() * * Remove double backslashes in Windows filename paths for display * ----------------------------------------------------------------------------- */ static String *format_filename(const_String_or_char_ptr filename) { String *formatted_filename = NewString(filename); #if defined(_WIN32) Replaceall(formatted_filename, "\\\\", "\\"); #endif return formatted_filename; } /* ----------------------------------------------------------------------------- * Swig_stringify_with_location() * * Return a string representation of any DOH object with line and file location * information in the appropriate error message format. The string representation * is enclosed within [] brackets after the line and file information. * ----------------------------------------------------------------------------- */ String *Swig_stringify_with_location(DOH *object) { String *str = NewStringEmpty(); if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT); if (object) { int line = Getline(object); String *formatted_filename = format_filename(Getfile(object)); if (line > 0) { Printf(str, diag_line_fmt, formatted_filename, line); } else { Printf(str, diag_eof_fmt, formatted_filename); } if (Len(object) == 0) { Printf(str, "[EMPTY]"); } else { Printf(str, "[%s]", object); } Delete(formatted_filename); } else { Printf(str, "[NULL]"); } return str; } /* ----------------------------------------------------------------------------- * Swig_diagnostic() * * Issue a diagnostic message on stdout. * ----------------------------------------------------------------------------- */ void Swig_diagnostic(const_String_or_char_ptr filename, int line, const char *fmt, ...) { va_list ap; String *formatted_filename = NULL; if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT); va_start(ap, fmt); formatted_filename = format_filename(filename); if (line > 0) { Printf(stdout, diag_line_fmt, formatted_filename, line); } else { Printf(stdout, diag_eof_fmt, formatted_filename); } vPrintf(stdout, fmt, ap); va_end(ap); Delete(formatted_filename); } swig-4.4.0/Source/Swig/swigopt.h0000664000175000017500000000157015075443613016401 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigopt.h * * Header file for the SWIG command line processing functions * ----------------------------------------------------------------------------- */ extern void Swig_init_args(int argc, char **argv); extern void Swig_mark_arg(int n); extern int Swig_check_marked(int n); extern void Swig_check_options(int check_input); extern void Swig_arg_error(void); swig-4.4.0/Source/Swig/fragment.c0000664000175000017500000001274215075443613016506 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * fragment.c * * This file manages named code fragments. Code fragments are typically * used to hold helper-code that may or may not be included in the wrapper * file (depending on what features are actually used in the interface). * * By using fragments, it's possible to greatly reduce the amount of * wrapper code and to generate cleaner wrapper files. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "swigwarn.h" #include "cparse.h" static Hash *fragments = 0; static Hash *looking_fragments = 0; static int debug = 0; /* ----------------------------------------------------------------------------- * Swig_fragment_register() * * Add a fragment. Use the original Node*, so, if something needs to be * changed, lang.cxx doesn't need to be touched again. * ----------------------------------------------------------------------------- */ void Swig_fragment_register(Node *fragment) { if (Getattr(fragment, "emitonly")) { Swig_fragment_emit(fragment); return; } else { String *name = Copy(Getattr(fragment, "value")); String *type = Getattr(fragment, "type"); if (type) { SwigType *rtype = SwigType_typedef_resolve_all(type); String *mangle = Swig_name_mangle_type(type); Append(name, mangle); Delete(mangle); Delete(rtype); if (debug) Printf(stdout, "register fragment %s %s\n", name, type); } if (!fragments) { fragments = NewHash(); } if (!Getattr(fragments, name)) { String *section = Copy(Getattr(fragment, "section")); String *ccode = Copy(Getattr(fragment, "code")); Hash *kwargs = Getattr(fragment, "kwargs"); Setmeta(ccode, "section", section); if (kwargs) { Setmeta(ccode, "kwargs", kwargs); } Setfile(ccode, Getfile(fragment)); Setline(ccode, Getline(fragment)); /* Replace $descriptor() macros */ Swig_cparse_replace_descriptor(ccode); Setattr(fragments, name, ccode); if (debug) Printf(stdout, "registering fragment %s %s\n", name, section); Delete(section); Delete(ccode); } Delete(name); } } /* ----------------------------------------------------------------------------- * Swig_fragment_emit() * * Emit a fragment * ----------------------------------------------------------------------------- */ static char *char_index(char *str, char c) { while (*str && (c != *str)) ++str; return (c == *str) ? str : 0; } void Swig_fragment_emit(Node *n) { String *code; char *pc, *tok; String *t; String *mangle = 0; String *name = 0; String *type = 0; name = Getattr(n, "value"); if (!name) { name = n; } if (!fragments) { Swig_warning(WARN_FRAGMENT_NOT_FOUND, Getfile(n), Getline(n), "Fragment '%s' not found.\n", name); return; } type = Getattr(n, "type"); if (type) { mangle = Swig_name_mangle_type(type); } if (debug) Printf(stdout, "looking fragment %s %s\n", name, type); t = Copy(name); tok = Char(t); pc = char_index(tok, ','); if (pc) *pc = 0; while (tok) { String *name = NewString(tok); if (mangle) Append(name, mangle); if (looking_fragments && Getattr(looking_fragments, name)) { return; } code = Getattr(fragments, name); if (debug) Printf(stdout, "looking subfragment %s\n", name); if (code && (Strcmp(code, "ignore") != 0)) { String *section = Getmeta(code, "section"); Hash *nn = Getmeta(code, "kwargs"); if (!looking_fragments) looking_fragments = NewHash(); Setattr(looking_fragments, name, "1"); while (nn) { if (Equal(Getattr(nn, "name"), "fragment")) { if (debug) Printf(stdout, "emitting fragment %s %s\n", nn, type); Setfile(nn, Getfile(n)); Setline(nn, Getline(n)); Swig_fragment_emit(nn); } nn = nextSibling(nn); } if (section) { File *f = Swig_filebyname(section); if (!f) { Swig_error(Getfile(code), Getline(code), "Bad section '%s' in %%fragment declaration for code fragment '%s'\n", section, name); } else { if (debug) Printf(stdout, "emitting subfragment %s %s\n", name, section); if (debug) Printf(f, "/* begin fragment %s */\n", name); Printf(f, "%s\n", code); if (debug) Printf(f, "/* end fragment %s */\n\n", name); Setattr(fragments, name, "ignore"); Delattr(looking_fragments, name); } } } else if (!code && type) { SwigType *rtype = SwigType_typedef_resolve_all(type); if (!Equal(type, rtype)) { String *name = Copy(Getattr(n, "value")); String *mangle = Swig_name_mangle_type(type); Append(name, mangle); Setfile(name, Getfile(n)); Setline(name, Getline(n)); Swig_fragment_emit(name); Delete(mangle); Delete(name); } Delete(rtype); } if (!code) { Swig_warning(WARN_FRAGMENT_NOT_FOUND, Getfile(n), Getline(n), "Fragment '%s' not found.\n", name); } tok = pc ? pc + 1 : 0; if (tok) { pc = char_index(tok, ','); if (pc) *pc = 0; } Delete(name); } Delete(t); } swig-4.4.0/Source/Swig/scanner.c0000664000175000017500000013227415075443613016337 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * scanner.c * * This file implements a general purpose C/C++ compatible lexical scanner. * This scanner isn't intended to be plugged directly into a parser built * with yacc. Rather, it contains a lot of generic code that could be used * to easily construct yacc-compatible scanners. * ----------------------------------------------------------------------------- */ #include "swig.h" #include extern String *cparse_file; extern int cparse_line; extern int cparse_cplusplus; extern int cparse_start_line; struct Scanner { String *text; /* Current token value */ List *scanobjs; /* Objects being scanned */ String *str; /* Current object being scanned */ char *idstart; /* Optional identifier start characters */ int nexttoken; /* Next token to be returned */ int start_line; /* Starting line of certain declarations */ int line; int yylen; /* Length of text pushed into text */ String *error; /* Last error message (if any) */ int error_line; /* Error line number */ int freeze_line; /* Suspend line number updates */ List *brackets; /* Current level of < > brackets on each level */ }; typedef struct Locator { String *filename; int line_number; struct Locator *next; } Locator; static int follow_locators = 0; static void brackets_push(Scanner *); static void brackets_clear(Scanner *); /* ----------------------------------------------------------------------------- * NewScanner() * * Create a new scanner object * ----------------------------------------------------------------------------- */ Scanner *NewScanner(void) { Scanner *s; s = (Scanner *) Malloc(sizeof(Scanner)); s->line = 1; s->nexttoken = -1; s->start_line = 1; s->yylen = 0; s->idstart = NULL; s->scanobjs = NewList(); s->text = NewStringEmpty(); s->str = 0; s->error = 0; s->error_line = 0; s->freeze_line = 0; s->brackets = NewList(); brackets_push(s); return s; } /* ----------------------------------------------------------------------------- * DelScanner() * * Delete a scanner object. * ----------------------------------------------------------------------------- */ void DelScanner(Scanner *s) { assert(s); Delete(s->scanobjs); Delete(s->brackets); Delete(s->text); Delete(s->error); Delete(s->str); Free(s->idstart); Free(s); } /* ----------------------------------------------------------------------------- * Scanner_clear() * * Clear the contents of a scanner object. * ----------------------------------------------------------------------------- */ void Scanner_clear(Scanner *s) { assert(s); Delete(s->str); Clear(s->text); Clear(s->scanobjs); brackets_clear(s); Delete(s->error); s->str = 0; s->error = 0; s->line = 1; s->nexttoken = -1; s->start_line = 0; s->yylen = 0; /* Should these be cleared too? s->idstart; s->error_line; s->freeze_line; */ } /* ----------------------------------------------------------------------------- * Scanner_push() * * Push some new text into the scanner. The scanner will start parsing this text * immediately before returning to the old text. * ----------------------------------------------------------------------------- */ void Scanner_push(Scanner *s, String *txt) { assert(s && txt); Push(s->scanobjs, txt); if (s->str) { Setline(s->str,s->line); Delete(s->str); } s->str = txt; DohIncref(s->str); s->line = Getline(txt); } /* ----------------------------------------------------------------------------- * Scanner_pushtoken() * * Push a token into the scanner. This token will be returned on the next * call to Scanner_token(). * ----------------------------------------------------------------------------- */ void Scanner_pushtoken(Scanner *s, int nt, const_String_or_char_ptr val) { assert(s); assert((nt >= 0) && (nt < SWIG_MAXTOKENS)); s->nexttoken = nt; if ( Char(val) != Char(s->text) ) { Clear(s->text); Append(s->text,val); } } /* ----------------------------------------------------------------------------- * Scanner_set_location() * * Set the file and line number location of the scanner. * ----------------------------------------------------------------------------- */ void Scanner_set_location(Scanner *s, String *file, int line) { Setline(s->str, line); Setfile(s->str, file); s->line = line; } /* ----------------------------------------------------------------------------- * Scanner_file() * * Get the current file. * ----------------------------------------------------------------------------- */ String *Scanner_file(Scanner *s) { return Getfile(s->str); } /* ----------------------------------------------------------------------------- * Scanner_line() * * Get the current line number * ----------------------------------------------------------------------------- */ int Scanner_line(Scanner *s) { return s->line; } /* ----------------------------------------------------------------------------- * Scanner_start_line() * * Get the line number on which the current token starts * ----------------------------------------------------------------------------- */ int Scanner_start_line(Scanner *s) { return s->start_line; } /* ----------------------------------------------------------------------------- * Scanner_idstart() * * Change the set of additional characters that can be used to start an identifier. * ----------------------------------------------------------------------------- */ void Scanner_idstart(Scanner *s, const char *id) { Free(s->idstart); s->idstart = Swig_copy_string(id); } /* ----------------------------------------------------------------------------- * nextchar() * * Returns the next character from the scanner or EOF if end of the string. * ----------------------------------------------------------------------------- */ static int nextchar(Scanner *s) { int nc; if (!s->str) return EOF; while ((nc = Getc(s->str)) == EOF) { Delete(s->str); s->str = 0; Delitem(s->scanobjs, 0); if (Len(s->scanobjs) == 0) return EOF; s->str = Getitem(s->scanobjs, 0); s->line = Getline(s->str); DohIncref(s->str); } if ((nc == '\n') && (!s->freeze_line)) s->line++; Putc(nc, s->text); return nc; } /* ----------------------------------------------------------------------------- * set_error() * * Sets error information on the scanner. * ----------------------------------------------------------------------------- */ static void set_error(Scanner *s, int line, const_String_or_char_ptr msg) { s->error_line = line; s->error = NewString(msg); } /* ----------------------------------------------------------------------------- * Scanner_errmsg() * Scanner_errline() * * Returns error information (if any) * ----------------------------------------------------------------------------- */ String *Scanner_errmsg(Scanner *s) { return s->error; } int Scanner_errline(Scanner *s) { return s->error_line; } /* ----------------------------------------------------------------------------- * freeze_line() * * Freezes the current line number. * ----------------------------------------------------------------------------- */ static void freeze_line(Scanner *s, int val) { s->freeze_line = val; } /* ----------------------------------------------------------------------------- * brackets_count() * * Returns the number of brackets at the current depth. * A syntax error with unbalanced ) brackets will result in a NULL pointer return. * ----------------------------------------------------------------------------- */ static int *brackets_count(Scanner *s) { int *count; if (Len(s->brackets) > 0) count = (int *)Data(Getitem(s->brackets, 0)); else count = 0; return count; } /* ----------------------------------------------------------------------------- * brackets_clear() * * Resets the current depth and clears all brackets. * Usually called at the end of statements; * ----------------------------------------------------------------------------- */ static void brackets_clear(Scanner *s) { Clear(s->brackets); brackets_push(s); /* base bracket count should always be created */ } /* ----------------------------------------------------------------------------- * brackets_increment() * * Increases the number of brackets at the current depth. * Usually called when a single '<' is found. * ----------------------------------------------------------------------------- */ static void brackets_increment(Scanner *s) { int *count = brackets_count(s); if (count) (*count)++; } /* ----------------------------------------------------------------------------- * brackets_decrement() * * Decreases the number of brackets at the current depth. * Usually called when a single '>' is found. * ----------------------------------------------------------------------------- */ static void brackets_decrement(Scanner *s) { int *count = brackets_count(s); if (count) (*count)--; } /* ----------------------------------------------------------------------------- * brackets_reset() * * Sets the number of '<' brackets back to zero. Called at the point where * it is no longer possible to have a matching closing >> pair for a template. * ----------------------------------------------------------------------------- */ static void brackets_reset(Scanner *s) { int *count = brackets_count(s); if (count) *count = 0; } /* ----------------------------------------------------------------------------- * brackets_push() * * Increases the depth of brackets. * Usually called when '(' is found. * ----------------------------------------------------------------------------- */ static void brackets_push(Scanner *s) { int *newInt = (int *)Malloc(sizeof(int)); *newInt = 0; Push(s->brackets, NewVoid(newInt, Free)); } /* ----------------------------------------------------------------------------- * brackets_pop() * * Decreases the depth of brackets. * Usually called when ')' is found. * ----------------------------------------------------------------------------- */ static void brackets_pop(Scanner *s) { if (Len(s->brackets) > 0) /* protect against unbalanced ')' brackets */ Delitem(s->brackets, 0); } /* ----------------------------------------------------------------------------- * brackets_allow_shift() * * Return 1 to allow shift (>>), or 0 if (>>) should be split into (> >). * This is for C++11 template syntax for closing templates. * ----------------------------------------------------------------------------- */ static int brackets_allow_shift(Scanner *s) { int *count = brackets_count(s); return !count || (*count <= 0); } /* ----------------------------------------------------------------------------- * retract() * * Retract n characters * ----------------------------------------------------------------------------- */ static void retract(Scanner *s, int n) { int i, l; char *str; str = Char(s->text); l = Len(s->text); assert(n <= l); for (i = 0; i < n; i++) { if (str[l - 1] == '\n') { if (!s->freeze_line) s->line--; } (void)Seek(s->str, -1, SEEK_CUR); Delitem(s->text, DOH_END); } } /* ----------------------------------------------------------------------------- * get_escape() * * Get escape sequence. Called when a backslash is found in a string * ----------------------------------------------------------------------------- */ static void get_escape(Scanner *s) { int result = 0; int state = 0; int c; while (1) { c = nextchar(s); if (c == EOF) break; switch (state) { case 0: if (c == 'n') { Delitem(s->text, DOH_END); Append(s->text,"\n"); return; } if (c == 'r') { Delitem(s->text, DOH_END); Append(s->text,"\r"); return; } if (c == 't') { Delitem(s->text, DOH_END); Append(s->text,"\t"); return; } if (c == 'a') { Delitem(s->text, DOH_END); Append(s->text,"\a"); return; } if (c == 'b') { Delitem(s->text, DOH_END); Append(s->text,"\b"); return; } if (c == 'f') { Delitem(s->text, DOH_END); Append(s->text,"\f"); return; } if (c == '\\') { Delitem(s->text, DOH_END); Append(s->text,"\\"); return; } if (c == 'v') { Delitem(s->text, DOH_END); Append(s->text,"\v"); return; } if (c == 'e') { // '\e' is a non-standard alternative to '\033' (the escape character) // in both C and C++, but is supported by at least GCC and clang. MSVC // issues a warning and treats it as an 'e'. Delitem(s->text, DOH_END); Append(s->text,"\033"); return; } if (c == '\'') { Delitem(s->text, DOH_END); Append(s->text,"\'"); return; } if (c == '\"') { Delitem(s->text, DOH_END); Append(s->text,"\""); return; } if (c == '\n') { Delitem(s->text, DOH_END); return; } if (isdigit(c)) { state = 10; result = (c - '0'); Delitem(s->text, DOH_END); } else if (c == 'x') { state = 20; Delitem(s->text, DOH_END); } else { Delitem(s->text, DOH_END); Putc('\\',s->text); Putc((char)c,s->text); return; } break; case 10: // Second digit of octal escape sequence case 11: // Third digit of octal escape sequence if (c < '0' || c > '7') { retract(s,1); Putc((char)result,s->text); return; } result = (result << 3) + (c - '0'); Delitem(s->text, DOH_END); if (state == 11) { if (result > 255) Swig_error(Scanner_file(s), Scanner_line(s), "octal escape sequence out of range\n"); Putc((char)result,s->text); return; } state = 11; break; case 20: if (!isxdigit(c)) { retract(s,1); Putc((char)result, s->text); return; } if (isdigit(c)) result = (result << 4) + (c - '0'); else result = (result << 4) + (10 + tolower(c) - 'a'); Delitem(s->text, DOH_END); break; } } return; } /* ----------------------------------------------------------------------------- * look() * * Return the raw value of the next token. * ----------------------------------------------------------------------------- */ static int look(Scanner *s) { int state = 0; int c = 0; String *str_delimiter = 0; Clear(s->text); s->start_line = s->line; Setfile(s->text, Getfile(s->str)); while (1) { switch (state) { case 0: if ((c = nextchar(s)) == EOF) return (0); /* Process delimiters */ if (c == '\n') { return SWIG_TOKEN_ENDLINE; } else if (!isspace(c)) { retract(s, 1); state = 1000; Clear(s->text); Setline(s->text, s->line); Setfile(s->text, Getfile(s->str)); } break; case 1000: if ((c = nextchar(s)) == EOF) return (0); if (c == '%') state = 4; /* Possibly a SWIG directive */ /* Look for possible identifiers or unicode/delimiter strings */ else if ((isalpha(c)) || (c == '_') || (s->idstart && strchr(s->idstart, c))) { state = 7; } /* Look for single character symbols */ else if (c == '(') { brackets_push(s); return SWIG_TOKEN_LPAREN; } else if (c == ')') { brackets_pop(s); return SWIG_TOKEN_RPAREN; } else if (c == ';') { brackets_clear(s); return SWIG_TOKEN_SEMI; } else if (c == ',') return SWIG_TOKEN_COMMA; else if (c == '*') state = 220; else if (c == '}') return SWIG_TOKEN_RBRACE; else if (c == '{') { brackets_reset(s); return SWIG_TOKEN_LBRACE; } else if (c == '=') state = 33; else if (c == '+') state = 200; else if (c == '-') state = 210; else if (c == '&') state = 31; else if (c == '|') state = 32; else if (c == '^') state = 230; else if (c == '<') state = 60; else if (c == '>') state = 61; else if (c == '~') return SWIG_TOKEN_NOT; else if (c == '!') state = 3; else if (c == '\\') return SWIG_TOKEN_BACKSLASH; else if (c == '@') return SWIG_TOKEN_AT; else if (c == '$') state = 75; else if (c == '#') return SWIG_TOKEN_POUND; else if (c == '?') return SWIG_TOKEN_QUESTION; /* Look for multi-character sequences */ else if (c == '/') { state = 1; /* Comment (maybe) */ s->start_line = s->line; } else if (c == ':') state = 5; /* maybe double colon */ else if (c == '0') state = 83; /* Maybe a hex, octal or binary number */ else if (c == '\"') { state = 2; /* A string constant */ s->start_line = s->line; Clear(s->text); } else if (c == '\'') { s->start_line = s->line; Clear(s->text); state = 9; /* A character constant */ } else if (c == '.') state = 100; /* Maybe a number, maybe ellipsis, just a period */ else if (c == '[') state = 102; /* Maybe a bracket or a double bracket */ else if (c == ']') state = 103; /* Maybe a bracket or a double bracket */ else if (isdigit(c)) state = 8; /* A numerical value */ else state = 99; /* An error */ break; case 1: /* Comment block */ if ((c = nextchar(s)) == EOF) return (0); if (c == '/') { state = 10; /* C++ style comment */ Clear(s->text); Setline(s->text, Getline(s->str)); Setfile(s->text, Getfile(s->str)); Append(s->text, "//"); } else if (c == '*') { state = 11; /* C style comment */ Clear(s->text); Setline(s->text, Getline(s->str)); Setfile(s->text, Getfile(s->str)); Append(s->text, "/*"); } else if (c == '=') { return SWIG_TOKEN_DIVEQUAL; } else { retract(s, 1); return SWIG_TOKEN_SLASH; } break; case 10: /* C++ style comment */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated comment\n"); return SWIG_TOKEN_ERROR; } if (c == '\n') { retract(s,1); return SWIG_TOKEN_COMMENT; } else { state = 10; } break; case 11: /* C style comment block */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated comment\n"); return SWIG_TOKEN_ERROR; } if (c == '*') { state = 12; } else { state = 11; } break; case 12: /* Still in C style comment */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated comment\n"); return SWIG_TOKEN_ERROR; } if (c == '*') { state = 12; } else if (c == '/') { return SWIG_TOKEN_COMMENT; } else { state = 11; } break; case 2: /* Processing a string */ if (!str_delimiter) { state=20; break; } if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated string\n"); return SWIG_TOKEN_ERROR; } else if (c == '(') { state = 20; } else { Putc( (char)c, str_delimiter ); } break; case 20: /* Inside the string */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated string\n"); return SWIG_TOKEN_ERROR; } if (!str_delimiter) { /* Ordinary string: "value" */ if (c == '\"') { Delitem(s->text, DOH_END); return SWIG_TOKEN_STRING; } else if (c == '\\') { Delitem(s->text, DOH_END); get_escape(s); } } else { /* Custom delimiter string: R"XXXX(value)XXXX" */ if (c==')') { int i=0; String *end_delimiter = NewStringEmpty(); while ((c = nextchar(s)) != EOF && c != '\"') { Putc( (char)c, end_delimiter ); i++; } if (Strcmp( str_delimiter, end_delimiter )==0) { int len = Len(s->text); Delslice(s->text, len - 2 - Len(str_delimiter), len); /* Delete ending )XXXX" */ Delslice(s->text, 0, Len(str_delimiter) + 1); /* Delete starting XXXX( */ Delete( end_delimiter ); /* Correct end delimiter )XXXX" occurred */ Delete( str_delimiter ); str_delimiter = 0; return SWIG_TOKEN_STRING; } else { /* Incorrect end delimiter occurred */ if (c == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated raw string, started with R\"%s( is not terminated by )%s\"\n", str_delimiter, str_delimiter); return SWIG_TOKEN_ERROR; } retract( s, i ); Delete( end_delimiter ); } } } break; case 3: /* Maybe a not equals */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_LNOT; else if (c == '=') return SWIG_TOKEN_NOTEQUAL; else { retract(s, 1); return SWIG_TOKEN_LNOT; } break; case 31: /* AND or Logical AND or ANDEQUAL */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_AND; else if (c == '&') return SWIG_TOKEN_LAND; else if (c == '=') return SWIG_TOKEN_ANDEQUAL; else { retract(s, 1); return SWIG_TOKEN_AND; } break; case 32: /* OR or Logical OR */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_OR; else if (c == '|') return SWIG_TOKEN_LOR; else if (c == '=') return SWIG_TOKEN_OREQUAL; else { retract(s, 1); return SWIG_TOKEN_OR; } break; case 33: /* EQUAL or EQUALTO */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_EQUAL; else if (c == '=') return SWIG_TOKEN_EQUALTO; else { retract(s, 1); return SWIG_TOKEN_EQUAL; } break; case 4: /* A wrapper generator directive (maybe) */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_PERCENT; if (c == '{') { state = 40; /* Include block */ Clear(s->text); Setline(s->text, Getline(s->str)); Setfile(s->text, Getfile(s->str)); s->start_line = s->line; } else if (s->idstart && strchr(s->idstart, '%') && ((isalpha(c)) || (c == '_'))) { state = 7; } else if (c == '=') { return SWIG_TOKEN_MODEQUAL; } else if (c == '}') { Swig_error(cparse_file, cparse_line, "Syntax error. Extraneous '%%}'\n"); Exit(EXIT_FAILURE); } else { retract(s, 1); return SWIG_TOKEN_PERCENT; } break; case 40: /* Process an include block */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated block\n"); return SWIG_TOKEN_ERROR; } if (c == '%') state = 41; break; case 41: /* Still processing include block */ if ((c = nextchar(s)) == EOF) { set_error(s,s->start_line,"Unterminated code block"); return 0; } if (c == '}') { Delitem(s->text, DOH_END); Delitem(s->text, DOH_END); Seek(s->text,0,SEEK_SET); return SWIG_TOKEN_CODEBLOCK; } else { state = 40; } break; case 5: /* Maybe a double colon */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_COLON; if (c == ':') state = 50; else { retract(s, 1); return SWIG_TOKEN_COLON; } break; case 50: /* DCOLON, DCOLONSTAR */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_DCOLON; else if (c == '*') return SWIG_TOKEN_DCOLONSTAR; else { retract(s, 1); return SWIG_TOKEN_DCOLON; } break; case 60: /* shift operators */ if ((c = nextchar(s)) == EOF) { brackets_increment(s); return SWIG_TOKEN_LESSTHAN; } if (c == '<') state = 240; else if (c == '=') { if ((c = nextchar(s)) == EOF) { return SWIG_TOKEN_LTEQUAL; } else if (c == '>' && cparse_cplusplus) { /* Spaceship operator */ return SWIG_TOKEN_LTEQUALGT; } else { retract(s, 1); return SWIG_TOKEN_LTEQUAL; } } else { retract(s, 1); brackets_increment(s); return SWIG_TOKEN_LESSTHAN; } break; case 61: if ((c = nextchar(s)) == EOF) { brackets_decrement(s); return SWIG_TOKEN_GREATERTHAN; } if (c == '>' && brackets_allow_shift(s)) state = 250; else if (c == '=') return SWIG_TOKEN_GTEQUAL; else { retract(s, 1); brackets_decrement(s); return SWIG_TOKEN_GREATERTHAN; } break; case 7: /* Identifier or true/false or unicode/custom delimiter string */ if (c == 'R') { /* Possibly CUSTOM DELIMITER string */ state = 72; break; } else if (c == 'L') { /* Probably identifier but may be a wide string literal */ state = 77; break; } else if (c != 'u' && c != 'U') { /* Definitely an identifier */ state = 70; break; } if ((c = nextchar(s)) == EOF) { state = 76; } else if (c == '\"') { /* Definitely u, U or L string */ retract(s, 1); state = 1000; } else if (c == '\'') { /* Definitely u, U or L char */ retract(s, 1); state = 77; } else if (c == 'R') { /* Possibly CUSTOM DELIMITER u, U, L string */ state = 73; } else if (c == '8') { /* Possibly u8 string/char */ state = 71; } else { retract(s, 1); /* Definitely an identifier */ state = 70; } break; case 70: /* Identifier */ if ((c = nextchar(s)) == EOF) state = 76; else if (isalnum(c) || (c == '_') || (c == '$')) { state = 70; } else { retract(s, 1); state = 76; } break; case 71: /* Possibly u8 string/char */ if ((c = nextchar(s)) == EOF) { state = 76; } else if (c=='\"') { retract(s, 1); /* Definitely u8 string */ state = 1000; } else if (c=='\'') { retract(s, 1); /* Definitely u8 char */ state = 77; } else if (c=='R') { state = 74; /* Possibly CUSTOM DELIMITER u8 string */ } else { retract(s, 2); /* Definitely an identifier. Retract 8" */ state = 70; } break; case 72: /* Possibly CUSTOM DELIMITER string */ case 73: case 74: if ((c = nextchar(s)) == EOF) { state = 76; } else if (c=='\"') { retract(s, 1); /* Definitely custom delimiter u, U or L string */ str_delimiter = NewStringEmpty(); state = 1000; } else { if (state==72) { retract(s, 1); /* Definitely an identifier. Retract ? */ } else if (state==73) { retract(s, 2); /* Definitely an identifier. Retract R? */ } else if (state==74) { retract(s, 3); /* Definitely an identifier. Retract 8R? */ } state = 70; } break; case 75: /* Special identifier $ */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_DOLLAR; if (isalnum(c) || (c == '_') || (c == '*') || (c == '&')) { state = 70; } else { retract(s,1); if (Len(s->text) == 1) return SWIG_TOKEN_DOLLAR; state = 76; } break; case 76: /* Identifier, true/false or alternative token */ if (cparse_cplusplus) { if (Strcmp(s->text, "true") == 0) return SWIG_TOKEN_BOOL; if (Strcmp(s->text, "false") == 0) return SWIG_TOKEN_BOOL; if (Strcmp(s->text, "and") == 0) return SWIG_TOKEN_LAND; if (Strcmp(s->text, "and_eq") == 0) return SWIG_TOKEN_ANDEQUAL; if (Strcmp(s->text, "bitand") == 0) return SWIG_TOKEN_AND; if (Strcmp(s->text, "bitor") == 0) return SWIG_TOKEN_OR; if (Strcmp(s->text, "compl") == 0) return SWIG_TOKEN_NOT; if (Strcmp(s->text, "not") == 0) return SWIG_TOKEN_LNOT; if (Strcmp(s->text, "not_eq") == 0) return SWIG_TOKEN_NOTEQUAL; if (Strcmp(s->text, "or") == 0) return SWIG_TOKEN_LOR; if (Strcmp(s->text, "or_eq") == 0) return SWIG_TOKEN_OREQUAL; if (Strcmp(s->text, "xor") == 0) return SWIG_TOKEN_XOR; if (Strcmp(s->text, "xor_eq") == 0) return SWIG_TOKEN_XOREQUAL; } return SWIG_TOKEN_ID; case 77: /*identifier or wide string literal*/ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_ID; else if (c == '\"') { s->start_line = s->line; Clear(s->text); state = 78; } else if (c == '\'') { s->start_line = s->line; Clear(s->text); state = 79; } else if (isalnum(c) || (c == '_') || (c == '$')) state = 7; else { retract(s, 1); return SWIG_TOKEN_ID; } break; case 78: /* Processing a wide string literal*/ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated wide string\n"); return SWIG_TOKEN_ERROR; } if (c == '\"') { Delitem(s->text, DOH_END); return SWIG_TOKEN_WSTRING; } else if (c == '\\') { Delitem(s->text, DOH_END); get_escape(s); } break; case 79: /* Processing a wide char literal */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated wide character constant\n"); return SWIG_TOKEN_ERROR; } if (c == '\'') { Delitem(s->text, DOH_END); return (SWIG_TOKEN_WCHAR); } else if (c == '\\') { Delitem(s->text, DOH_END); get_escape(s); } break; case 8: /* A numerical digit */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_INT; if (c == '.') { state = 81; } else if ((c == 'e') || (c == 'E')) { state = 82; } else if ((c == 'f') || (c == 'F')) { return SWIG_TOKEN_FLOAT; } else if (isdigit(c)) { state = 8; } else if ((c == 'l') || (c == 'L')) { state = 87; } else if ((c == 'u') || (c == 'U')) { state = 88; } else { retract(s, 1); return SWIG_TOKEN_INT; } break; case 81: /* A floating pointer number of some sort */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_DOUBLE; if (isdigit(c)) state = 81; else if ((c == 'e') || (c == 'E')) state = 820; else if ((c == 'f') || (c == 'F')) { return SWIG_TOKEN_FLOAT; } else if ((c == 'l') || (c == 'L')) { Delitem(s->text, DOH_END); return SWIG_TOKEN_LONGDOUBLE; } else { retract(s, 1); return (SWIG_TOKEN_DOUBLE); } break; case 82: if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Exponent does not have any digits\n"); return SWIG_TOKEN_ERROR; } if ((isdigit(c)) || (c == '-') || (c == '+')) state = 86; else { retract(s, 2); Swig_error(cparse_file, cparse_start_line, "Exponent does not have any digits\n"); return SWIG_TOKEN_ERROR; } break; case 820: /* Like case 82, but we've seen a decimal point. */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Exponent does not have any digits\n"); return SWIG_TOKEN_ERROR; } if ((isdigit(c)) || (c == '-') || (c == '+')) state = 86; else { retract(s, 2); Swig_error(cparse_file, cparse_start_line, "Exponent does not have any digits\n"); return SWIG_TOKEN_ERROR; } break; case 83: /* Might be a hexadecimal, octal or binary number */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_INT; if (isdigit(c)) state = 84; else if ((c == 'e') || (c == 'E')) state = 82; else if ((c == 'x') || (c == 'X')) state = 85; else if ((c == 'b') || (c == 'B')) state = 850; else if (c == '.') state = 81; else if ((c == 'l') || (c == 'L')) { state = 87; } else if ((c == 'u') || (c == 'U')) { state = 88; } else { retract(s, 1); return SWIG_TOKEN_INT; } break; case 84: /* This is an octal number */ if (c == '8' || c == '9') { Swig_error(Scanner_file(s), Scanner_line(s), "Invalid digit '%c' in octal constant\n", c); } if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_INT; if (isdigit(c)) state = 84; else if (c == '.') state = 81; else if ((c == 'e') || (c == 'E')) state = 82; else if ((c == 'l') || (c == 'L')) { state = 87; } else if ((c == 'u') || (c == 'U')) { state = 88; } else { retract(s, 1); return SWIG_TOKEN_INT; } break; case 85: /* This is an hex number */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_INT; if (isxdigit(c)) state = 85; else if (c == '.') /* hexadecimal float */ state = 860; else if ((c == 'p') || (c == 'P')) /* hexadecimal float */ state = 820; else if ((c == 'l') || (c == 'L')) { state = 87; } else if ((c == 'u') || (c == 'U')) { state = 88; } else { retract(s, 1); return SWIG_TOKEN_INT; } break; case 850: /* This is a binary number */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_INT; if ((c == '0') || (c == '1')) state = 850; else if (isdigit(c)) { Swig_error(Scanner_file(s), Scanner_line(s), "Invalid digit '%c' in binary constant\n", c); } else if ((c == 'l') || (c == 'L')) { state = 87; } else if ((c == 'u') || (c == 'U')) { state = 88; } else { retract(s, 1); return SWIG_TOKEN_INT; } break; case 860: /* hexadecimal float */ if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Hexadecimal floating literals require an exponent\n"); return SWIG_TOKEN_ERROR; } if (isxdigit(c)) state = 860; else if ((c == 'p') || (c == 'P')) state = 820; else { retract(s, 2); Swig_error(cparse_file, cparse_start_line, "Hexadecimal floating literals require an exponent\n"); return SWIG_TOKEN_ERROR; } break; case 86: /* Rest of floating point number */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_DOUBLE; if (isdigit(c)) state = 86; else if ((c == 'f') || (c == 'F')) { return SWIG_TOKEN_FLOAT; } else if ((c == 'l') || (c == 'L')) { Delitem(s->text, DOH_END); return SWIG_TOKEN_LONGDOUBLE; } else { retract(s, 1); return SWIG_TOKEN_DOUBLE; } break; case 87: /* A long integer of some sort */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_LONG; if ((c == 'u') || (c == 'U')) { return SWIG_TOKEN_ULONG; } else if ((c == 'l') || (c == 'L')) { state = 870; } else { retract(s, 1); return SWIG_TOKEN_LONG; } break; /* A long long integer */ case 870: if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_LONGLONG; if ((c == 'u') || (c == 'U')) { return SWIG_TOKEN_ULONGLONG; } else { retract(s, 1); return SWIG_TOKEN_LONGLONG; } /* An unsigned number */ case 88: if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_UINT; if ((c == 'l') || (c == 'L')) { state = 880; } else { retract(s, 1); return SWIG_TOKEN_UINT; } break; /* Possibly an unsigned long long or unsigned long */ case 880: if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_ULONG; if ((c == 'l') || (c == 'L')) return SWIG_TOKEN_ULONGLONG; else { retract(s, 1); return SWIG_TOKEN_ULONG; } /* A character constant */ case 9: if ((c = nextchar(s)) == EOF) { Swig_error(cparse_file, cparse_start_line, "Unterminated character constant\n"); return SWIG_TOKEN_ERROR; } if (c == '\'') { Delitem(s->text, DOH_END); return (SWIG_TOKEN_CHAR); } else if (c == '\\') { Delitem(s->text, DOH_END); get_escape(s); } break; /* A period or an ellipsis or maybe a floating point number */ case 100: if ((c = nextchar(s)) == EOF) return (0); if (isdigit(c)) state = 81; else if (c == '.') state = 101; else { retract(s, 1); return SWIG_TOKEN_PERIOD; } break; /* An ellipsis */ case 101: if ((c = nextchar(s)) == EOF) return (0); if (c == '.') { return SWIG_TOKEN_ELLIPSIS; } else { retract(s, 2); return SWIG_TOKEN_PERIOD; } break; /* A left bracket or a double left bracket */ case 102: if ((c = nextchar(s)) == EOF) { return SWIG_TOKEN_LBRACKET; } else if (c == '[') { return SWIG_TOKEN_LLBRACKET; } else { retract(s, 1); return SWIG_TOKEN_LBRACKET; } break; /* a right bracket or a double right bracket */ case 103: if ((c = nextchar(s)) == EOF) { return SWIG_TOKEN_RBRACKET; } else if (c == ']') { return SWIG_TOKEN_RRBRACKET; } else { retract(s, 1); return SWIG_TOKEN_RBRACKET; } break; case 200: /* PLUS, PLUSPLUS, PLUSEQUAL */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_PLUS; else if (c == '+') return SWIG_TOKEN_PLUSPLUS; else if (c == '=') return SWIG_TOKEN_PLUSEQUAL; else { retract(s, 1); return SWIG_TOKEN_PLUS; } break; case 210: /* MINUS, MINUSMINUS, MINUSEQUAL, ARROW */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_MINUS; else if (c == '-') return SWIG_TOKEN_MINUSMINUS; else if (c == '=') return SWIG_TOKEN_MINUSEQUAL; else if (c == '>') state = 211; else { retract(s, 1); return SWIG_TOKEN_MINUS; } break; case 211: /* ARROW, ARROWSTAR */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_ARROW; else if (c == '*') return SWIG_TOKEN_ARROWSTAR; else { retract(s, 1); return SWIG_TOKEN_ARROW; } break; case 220: /* STAR, TIMESEQUAL */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_STAR; else if (c == '=') return SWIG_TOKEN_TIMESEQUAL; else { retract(s, 1); return SWIG_TOKEN_STAR; } break; case 230: /* XOR, XOREQUAL */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_XOR; else if (c == '=') return SWIG_TOKEN_XOREQUAL; else { retract(s, 1); return SWIG_TOKEN_XOR; } break; case 240: /* LSHIFT, LSEQUAL */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_LSHIFT; else if (c == '=') return SWIG_TOKEN_LSEQUAL; else { retract(s, 1); return SWIG_TOKEN_LSHIFT; } break; case 250: /* RSHIFT, RSEQUAL */ if ((c = nextchar(s)) == EOF) return SWIG_TOKEN_RSHIFT; else if (c == '=') return SWIG_TOKEN_RSEQUAL; else { retract(s, 1); return SWIG_TOKEN_RSHIFT; } break; /* An illegal character */ default: return SWIG_TOKEN_ILLEGAL; } } } /* ----------------------------------------------------------------------------- * Scanner_token() * * Real entry point to return the next token. Returns 0 if at end of input. * ----------------------------------------------------------------------------- */ int Scanner_token(Scanner *s) { int t; Delete(s->error); if (s->nexttoken >= 0) { t = s->nexttoken; s->nexttoken = -1; return t; } s->start_line = 0; t = look(s); if (!s->start_line) { Setline(s->text,s->line); } else { Setline(s->text,s->start_line); } return t; } /* ----------------------------------------------------------------------------- * Scanner_text() * * Return the lexene associated with the last returned token. * ----------------------------------------------------------------------------- */ String *Scanner_text(Scanner *s) { return s->text; } /* ----------------------------------------------------------------------------- * Scanner_skip_line() * * Skips to the end of a line * ----------------------------------------------------------------------------- */ void Scanner_skip_line(Scanner *s) { Clear(s->text); Setfile(s->text, Getfile(s->str)); Setline(s->text, s->line); while (1) { int c; if ((c = nextchar(s)) == EOF) return; if (c == '\\') { nextchar(s); } else if (c == '\n') { return; } } } /* ----------------------------------------------------------------------------- * Scanner_skip_balanced() * * Skips a piece of code enclosed in begin/end symbols such as '{...}' or * (...). Ignores symbols inside comments or strings. * * Returns 0 on success, -1 if no matching endchar could be found. * ----------------------------------------------------------------------------- */ int Scanner_skip_balanced(Scanner *s, int startchar, int endchar) { int old_line = s->line; long position = Tell(s->str); int num_levels = 1; int starttok = 0; int endtok = 0; switch (endchar) { case '}': starttok = SWIG_TOKEN_LBRACE; endtok = SWIG_TOKEN_RBRACE; break; case ')': starttok = SWIG_TOKEN_LPAREN; endtok = SWIG_TOKEN_RPAREN; break; case ']': starttok = SWIG_TOKEN_LBRACKET; endtok = SWIG_TOKEN_RBRACKET; break; case '>': starttok = SWIG_TOKEN_LESSTHAN; endtok = SWIG_TOKEN_GREATERTHAN; break; default: assert(0); } while (1) { int tok = Scanner_token(s); if (tok == starttok) { num_levels++; } else if (tok == endtok) { if (--num_levels == 0) break; } else if (tok == SWIG_TOKEN_RRBRACKET && endtok == SWIG_TOKEN_RBRACKET) { num_levels -= 2; if (num_levels <= 0) { if (num_levels < 0) Scanner_pushtoken(s, SWIG_TOKEN_RBRACKET, "]"); break; } } else if (tok == SWIG_TOKEN_COMMENT) { char *loc = Char(s->text); if (strncmp(loc, "/*@SWIG", 7) == 0 && loc[Len(s->text)-3] == '@') { Scanner_locator(s, s->text); } } else if (tok == 0) { return -1; } } Delete(s->text); s->text = NewStringWithSize(Char(s->str) + position - 1, Tell(s->str) - position + 1); Char(s->text)[0] = startchar; Setfile(s->text, Getfile(s->str)); Setline(s->text, old_line); return 0; } /* ----------------------------------------------------------------------------- * Scanner_get_raw_text_balanced() * * Returns raw text between 2 braces, does not change scanner state in any way * ----------------------------------------------------------------------------- */ String *Scanner_get_raw_text_balanced(Scanner *s, int startchar, int endchar) { String *result = NULL; int old_line = s->line; String *old_text = Copy(s->text); long position = Tell(s->str); int num_levels = 1; int starttok = 0; int endtok = 0; switch (endchar) { case '}': starttok = SWIG_TOKEN_LBRACE; endtok = SWIG_TOKEN_RBRACE; break; case ')': starttok = SWIG_TOKEN_LPAREN; endtok = SWIG_TOKEN_RPAREN; break; case ']': starttok = SWIG_TOKEN_LBRACKET; endtok = SWIG_TOKEN_RBRACKET; break; case '>': starttok = SWIG_TOKEN_LESSTHAN; endtok = SWIG_TOKEN_GREATERTHAN; break; default: assert(0); } while (1) { int tok = Scanner_token(s); if (tok == starttok) { num_levels++; } else if (tok == endtok) { if (--num_levels == 0) { result = NewStringWithSize(Char(s->str) + position - 1, Tell(s->str) - position + 1); Char(result)[0] = startchar; Setfile(result, Getfile(s->str)); Setline(result, old_line); break; } } else if (tok == SWIG_TOKEN_COMMENT) { char *loc = Char(s->text); if (strncmp(loc, "/*@SWIG", 7) == 0 && loc[Len(s->text)-3] == '@') { Scanner_locator(s, s->text); } } else if (tok == 0) { break; } } /* Reset the scanner state. */ Seek(s->str, position, SEEK_SET); Delete(s->text); s->text = old_text; s->line = old_line; return result; } /* ----------------------------------------------------------------------------- * Scanner_isoperator() * * Returns 0 or 1 depending on whether or not a token corresponds to a C/C++ * operator. * ----------------------------------------------------------------------------- */ int Scanner_isoperator(int tokval) { if (tokval >= 100) return 1; return 0; } /* ---------------------------------------------------------------------- * Scanner_locator() * * Support for locator strings. These are strings of the form * @SWIG:filename,line,id@ emitted by the SWIG preprocessor. They * are primarily used for macro line number reporting. * We just use the locator to mark when to activate/deactivate linecounting. * ---------------------------------------------------------------------- */ void Scanner_locator(Scanner *s, String *loc) { static Locator *locs = 0; static int expanding_macro = 0; if (!follow_locators) { if (Equal(loc, "/*@SWIG@*/")) { /* End locator. */ if (expanding_macro) --expanding_macro; } else { /* Begin locator. */ ++expanding_macro; } /* Freeze line number processing in Scanner */ freeze_line(s,expanding_macro); } else { int c; Locator *l; (void)Seek(loc, 7, SEEK_SET); c = Getc(loc); if (c == '@') { /* Empty locator. We pop the last location off */ if (locs) { Scanner_set_location(s, locs->filename, locs->line_number); cparse_file = locs->filename; cparse_line = locs->line_number; l = locs->next; Free(locs); locs = l; } return; } /* We're going to push a new location */ l = (Locator *) Malloc(sizeof(Locator)); l->filename = cparse_file; l->line_number = cparse_line; l->next = locs; locs = l; /* Now, parse the new location out of the locator string */ { String *fn = NewStringEmpty(); /* Putc(c, fn); */ while ((c = Getc(loc)) != EOF) { if ((c == '@') || (c == ',')) break; Putc(c, fn); } cparse_file = Swig_copy_string(Char(fn)); Clear(fn); cparse_line = 1; /* Get the line number */ while ((c = Getc(loc)) != EOF) { if ((c == '@') || (c == ',')) break; Putc(c, fn); } cparse_line = atoi(Char(fn)); Clear(fn); /* Get the rest of it */ while ((c = Getc(loc)) != EOF) { if (c == '@') break; Putc(c, fn); } /* Swig_diagnostic(cparse_file, cparse_line, "Scanner_set_location\n"); */ Scanner_set_location(s, cparse_file, cparse_line); Delete(fn); } } } void Swig_cparse_follow_locators(int v) { follow_locators = v; } swig-4.4.0/Source/Swig/tree.c0000664000175000017500000002651015075443613015640 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * tree.c * * This file provides some general purpose functions for manipulating * parse trees. * ----------------------------------------------------------------------------- */ #include "swig.h" #include #include static int debug_quiet = 0; /* ----------------------------------------------------------------------------- * Swig_print_quiet() * * Set quiet mode when printing a parse tree node * ----------------------------------------------------------------------------- */ int Swig_print_quiet(int quiet) { int previous_quiet = debug_quiet; debug_quiet = quiet; return previous_quiet; } /* ----------------------------------------------------------------------------- * Swig_print_tags() * * Dump the tag structure of a parse tree to standard output * ----------------------------------------------------------------------------- */ void Swig_print_tags(DOH *obj, DOH *root) { DOH *croot, *newroot; DOH *cobj; if (!root) croot = NewStringEmpty(); else croot = root; while (obj) { Swig_diagnostic(Getfile(obj), Getline(obj), "%s . %s\n", croot, nodeType(obj)); cobj = firstChild(obj); if (cobj) { newroot = NewStringf("%s . %s", croot, nodeType(obj)); Swig_print_tags(cobj, newroot); Delete(newroot); } obj = nextSibling(obj); } if (!root) Delete(croot); } static int indent_level = 0; static void print_indent(int l) { int i; for (i = 0; i < indent_level; i++) { fputc(' ', stdout); } if (l) { fputc('|', stdout); fputc(' ', stdout); } } /* ----------------------------------------------------------------------------- * Swig_print_node(Node *n) * ----------------------------------------------------------------------------- */ void Swig_print_node(Node *obj) { Iterator ki; Node *cobj; List *keys = Keys(obj); print_indent(0); if (debug_quiet) Printf(stdout, "+++ %s ----------------------------------------\n", nodeType(obj)); else Printf(stdout, "+++ %s - %p ----------------------------------------\n", nodeType(obj), obj); SortList(keys, 0); ki = First(keys); while (ki.item) { String *k = ki.item; DOH *value = Getattr(obj, k); if (Equal(k, "nodeType") || (*(Char(k)) == '$')) { /* Do nothing */ } else if (debug_quiet && (Equal(k, "firstChild") || Equal(k, "lastChild") || Equal(k, "parentNode") || Equal(k, "nextSibling") || Equal(k, "previousSibling") || Equal(k, "symtab") || Equal(k, "csymtab") || Equal(k, "sym:symtab") || Equal(k, "sym:nextSibling") || Equal(k, "sym:previousSibling") || Equal(k, "csym:nextSibling") || Equal(k, "csym:previousSibling"))) { /* Do nothing */ } else if (Equal(k, "kwargs") || Equal(k, "parms") || Equal(k, "wrap:parms") || Equal(k, "pattern") || Equal(k, "templateparms") || Equal(k, "templateparmsraw") || Equal(k, "template_parameters") || Equal(k, "throws")) { print_indent(2); /* Differentiate parameter lists by displaying within single quotes */ Printf(stdout, "%-12s - \'%s\'\n", k, ParmList_str_defaultargs(value)); } else { DOH *o; const char *trunc = ""; print_indent(2); if (DohIsString(value)) { o = Str(value); if (Len(o) > 80) { trunc = "..."; } Printf(stdout, "%-12s - \"%(escape)-0.80s%s\"\n", k, o, trunc); Delete(o); /* } else if (DohIsSequence(value)) { Printf(stdout, "%-12s - %s\n", k, value); */ } else { Printf(stdout, "%-12s - %p\n", k, value); } } ki = Next(ki); } cobj = firstChild(obj); if (cobj) { indent_level += 6; Printf(stdout, "\n"); Swig_print_tree(cobj); indent_level -= 6; } else { print_indent(1); Printf(stdout, "\n"); } Delete(keys); } /* ----------------------------------------------------------------------------- * Swig_print_tree() * * Dump the tree structure of a parse tree to standard output * ----------------------------------------------------------------------------- */ void Swig_print_tree(DOH *obj) { while (obj) { Swig_print_node(obj); obj = nextSibling(obj); } } /* ----------------------------------------------------------------------------- * appendChild() * * Appends a new child to a node * ----------------------------------------------------------------------------- */ void appendChild(Node *node, Node *chd) { Node *lc; if (!chd) return; lc = lastChild(node); if (!lc) { set_firstChild(node, chd); } else { set_nextSibling(lc, chd); set_previousSibling(chd, lc); } while (chd) { lc = chd; set_parentNode(chd, node); chd = nextSibling(chd); } set_lastChild(node, lc); } /* ----------------------------------------------------------------------------- * prependChild() * * Prepends a new child to a node * ----------------------------------------------------------------------------- */ void prependChild(Node *node, Node *chd) { Node *fc; if (!chd) return; fc = firstChild(node); if (fc) { set_nextSibling(chd, fc); set_previousSibling(fc, chd); } set_firstChild(node, chd); while (chd) { set_parentNode(chd, node); chd = nextSibling(chd); } } void appendSibling(Node *node, Node *chd) { Node *parent; Node *lc = node; while (nextSibling(lc)) lc = nextSibling(lc); set_nextSibling(lc, chd); set_previousSibling(chd, lc); parent = parentNode(node); if (parent) { while (chd) { lc = chd; set_parentNode(chd, parent); chd = nextSibling(chd); } set_lastChild(parent, lc); } } /* ----------------------------------------------------------------------------- * removeNode() * * Removes a node from the parse tree. Detaches it from its parent's child list. * ----------------------------------------------------------------------------- */ void removeNode(Node *n) { Node *parent; Node *prev; Node *next; parent = parentNode(n); if (!parent) return; prev = previousSibling(n); next = nextSibling(n); if (prev) { set_nextSibling(prev, next); } else { if (parent) { set_firstChild(parent, next); } } if (next) { set_previousSibling(next, prev); } else { if (parent) { set_lastChild(parent, prev); } } /* Delete attributes */ Delattr(n,"parentNode"); Delattr(n,"nextSibling"); Delattr(n,"previousSibling"); } /* ----------------------------------------------------------------------------- * copyNode() * * Copies a node, but only copies simple attributes (no lists, hashes). * ----------------------------------------------------------------------------- */ Node *copyNode(Node *n) { Iterator ki; Node *c = NewHash(); for (ki = First(n); ki.key; ki = Next(ki)) { if (DohIsString(ki.item)) { Setattr(c, ki.key, Copy(ki.item)); } } Setfile(c, Getfile(n)); Setline(c, Getline(n)); return c; } /* ----------------------------------------------------------------------------- * checkAttribute() * ----------------------------------------------------------------------------- */ int checkAttribute(Node *n, const_String_or_char_ptr name, const_String_or_char_ptr value) { String *v = Getattr(n, name); return v ? Equal(v, value) : 0; } /* ----------------------------------------------------------------------------- * Swig_require() * ns - namespace for the view name for saving any attributes under * n - node * ... - list of attribute names of type char* * * An attribute is optional if it is prefixed by ?, eg "?value". All * non-optional attributes are checked for on node n and if any do not exist * SWIG exits with a fatal error. * * If the attribute name is prefixed by * or ?, eg "*value" then a copy of the * attribute is saved. The saved attributes will be restored on a subsequent * call to Swig_restore(). All the saved attributes are saved in the view * namespace (prefixed by ns). * * This function can be called more than once with different namespaces. * ----------------------------------------------------------------------------- */ void Swig_require(const char *ns, Node *n, ...) { va_list ap; char *name; DOH *obj; va_start(ap, n); name = va_arg(ap, char *); while (name) { int newref = 0; int opt = 0; if (*name == '*') { newref = 1; name++; } else if (*name == '?') { newref = 1; opt = 1; name++; } obj = Getattr(n, name); if (!opt && !obj) { Swig_error(Getfile(n), Getline(n), "Internal error (Swig_require). Missing attribute '%s' in node '%s'.\n", name, nodeType(n)); Exit(EXIT_FAILURE); } if (!obj) obj = None; if (newref) { /* Save a copy of the attribute */ Setattr(n, NewStringf("%s:%s", ns, name), obj); } name = va_arg(ap, char *); } va_end(ap); /* Save the view */ { String *view = Getattr(n, "view"); if (view) { if (Strcmp(view, ns) != 0) { Setattr(n, NewStringf("%s:view", ns), view); Setattr(n, "view", NewString(ns)); } } else { Setattr(n, "view", NewString(ns)); } } } /* ----------------------------------------------------------------------------- * Swig_save() * Same as Swig_require(), but all attribute names are optional and all attributes * are saved, ie behaves as if all the attribute names were prefixed by ?. * ----------------------------------------------------------------------------- */ void Swig_save(const char *ns, Node *n, ...) { va_list ap; char *name; DOH *obj; va_start(ap, n); name = va_arg(ap, char *); while (name) { if (*name == '*') { name++; } else if (*name == '?') { name++; } obj = Getattr(n, name); if (!obj) obj = None; /* Save a copy of the attribute */ if (Setattr(n, NewStringf("%s:%s", ns, name), obj)) { Printf(stderr, "Swig_save('%s','%s'): Warning, attribute '%s' was already saved.\n", ns, nodeType(n), name); } name = va_arg(ap, char *); } va_end(ap); /* Save the view */ { String *view = Getattr(n, "view"); if (view) { if (Strcmp(view, ns) != 0) { Setattr(n, NewStringf("%s:view", ns), view); Setattr(n, "view", NewString(ns)); } } else { Setattr(n, "view", NewString(ns)); } } } /* ----------------------------------------------------------------------------- * Swig_restore() * Restores attributes saved by a previous call to Swig_require() or Swig_save(). * ----------------------------------------------------------------------------- */ void Swig_restore(Node *n) { String *temp; int len; List *l; String *ns; Iterator ki; ns = Getattr(n, "view"); assert(ns); l = NewList(); temp = NewStringf("%s:", ns); len = Len(temp); for (ki = First(n); ki.key; ki = Next(ki)) { if (Strncmp(temp, ki.key, len) == 0) { Append(l, ki.key); } } for (ki = First(l); ki.item; ki = Next(ki)) { DOH *obj = Getattr(n, ki.item); Setattr(n, Char(ki.item) + len, obj); Delattr(n, ki.item); } Delete(l); Delete(temp); } swig-4.4.0/Source/Swig/cwrap.c0000664000175000017500000014314415075443613016020 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * cwrap.c * * This file defines a variety of wrapping rules for C/C++ handling including * the naming of local variables, calling conventions, and so forth. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" extern int UseWrapperSuffix; static const char *cresult_variable_name = "result"; static Parm *nonvoid_parms(Parm *p) { if (p) { SwigType *t = Getattr(p, "type"); if (SwigType_type(t) == T_VOID) return 0; } return p; } /* ----------------------------------------------------------------------------- * Swig_cresult_name_set() * * Change the name of the variable used to hold the return value from C/C++ wrapper functions * from the default "result". * ----------------------------------------------------------------------------- */ void Swig_cresult_name_set(const char *new_name) { cresult_variable_name = new_name; } /* ----------------------------------------------------------------------------- * Swig_cresult_name() * * Get the name of the variable used to hold the return value from C/C++ wrapper functions * ----------------------------------------------------------------------------- */ const char *Swig_cresult_name(void) { return cresult_variable_name; } /* ----------------------------------------------------------------------------- * Swig_cparm_name() * * Generates a name for the ith argument in an argument list * ----------------------------------------------------------------------------- */ String *Swig_cparm_name(Parm *p, int i) { String *name = NewStringf("arg%d", i + 1); if (p) { Setattr(p, "lname", name); } return name; } /* ----------------------------------------------------------------------------- * Swig_clocal() * * Creates a string that declares a C local variable type. Converts references * and user defined types to pointers. * ----------------------------------------------------------------------------- */ static String *Swig_clocal(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr value) { String *decl; decl = NewStringEmpty(); switch (SwigType_type(t)) { case T_REFERENCE: if (value) { String *lstrname = SwigType_lstr(t, name); String *lstr = SwigType_lstr(t, 0); Printf(decl, "%s = (%s) &%s_defvalue", lstrname, lstr, name); Delete(lstrname); Delete(lstr); } else { String *lstrname = SwigType_lstr(t, name); Printf(decl, "%s = 0", lstrname); Delete(lstrname); } break; case T_RVALUE_REFERENCE: if (value) { String *lstrname = SwigType_lstr(t, name); String *lstr = SwigType_lstr(t, 0); Printf(decl, "%s = (%s) &%s_defrvalue", lstrname, lstr, name); Delete(lstrname); Delete(lstr); } else { String *lstrname = SwigType_lstr(t, name); Printf(decl, "%s = 0", lstrname); Delete(lstrname); } break; case T_VOID: break; case T_VARARGS: Printf(decl, "void *%s = 0", name); break; default: if (value) { String *lcaststr = SwigType_lcaststr(t, value); String *lstrn = SwigType_lstr(t, name); Printf(decl, "%s = %s", lstrn, lcaststr); Delete(lcaststr); Delete(lstrn); } else { String *lstrname = SwigType_lstr(t, name); Append(decl, lstrname); Delete(lstrname); } } return decl; } /* ----------------------------------------------------------------------------- * Swig_wrapped_var_convert() * * Converts a member variable for use in the get and set wrapper methods. * This function only converts user defined types to pointers. * ----------------------------------------------------------------------------- */ String *Swig_wrapped_var_type(SwigType *t, int varcref) { SwigType *ty; if (!Strstr(t, "enum $unnamed")) { ty = Copy(t); } else { /* Change the type for unnamed enum instance variables */ ty = NewString("int"); } if (SwigType_isclass(t)) { if (varcref) { if (cparse_cplusplus) { if (!SwigType_isconst(ty)) SwigType_add_qualifier(ty, "const"); SwigType_add_reference(ty); } else { return Copy(ty); } } else { SwigType_add_pointer(ty); } } return ty; } static String *Swig_wrapped_member_var_type(SwigType *t, int varcref) { SwigType *ty; if (!Strstr(t, "enum $unnamed")) { ty = Copy(t); } else { /* Change the type for unnamed enum instance variables */ ty = NewString("int"); } if (SwigType_isclass(t)) { if (varcref) { if (cparse_cplusplus) { if (!SwigType_isconst(ty)) SwigType_add_qualifier(ty, "const"); SwigType_add_reference(ty); } else { return Copy(ty); } } else { SwigType_add_pointer(ty); } } return ty; } static String *Swig_wrapped_var_deref(SwigType *t, const_String_or_char_ptr name, int varcref) { if (SwigType_isclass(t)) { if (varcref) { if (cparse_cplusplus) { return NewStringf("*%s", name); } else { return NewStringf("%s", name); } } else { return NewStringf("*%s", name); } } else { return SwigType_rcaststr(t, name); } } static String *Swig_wrapped_var_assign(SwigType *t, const_String_or_char_ptr name, int varcref) { if (SwigType_isclass(t)) { if (varcref) { return NewStringf("%s", name); } else { return NewStringf("&%s", name); } } else { return SwigType_lcaststr(t, name); } } /* ----------------------------------------------------------------------------- * Swig_cargs() * * Emit all of the local variables for a list of parameters. Returns the * number of parameters. * Default values for the local variables are only emitted if the compact default * argument behaviour is required. * ----------------------------------------------------------------------------- */ int Swig_cargs(Wrapper *w, ParmList *p) { int i = 0; int compactdefargs = ParmList_is_compactdefargs(p); while (p != 0) { String *lname = Swig_cparm_name(p, i); SwigType *pt = Getattr(p, "type"); if ((SwigType_type(pt) != T_VOID)) { String *local = 0; SwigType *type = SwigType_typedef_resolve_all(pt); /* default values only emitted if in compact default args mode */ String *pvalue = (compactdefargs) ? Getattr(p, "value") : 0; /* When using compactdefaultargs, the code generated initialises a variable via a constructor call that accepts the * default value as a parameter. The default constructor is not called and therefore SwigValueWrapper is not needed. */ SwigType *altty = pvalue ? 0 : SwigType_alttype(type, 0); int tycode = SwigType_type(type); if (tycode == T_REFERENCE) { if (pvalue) { String *rvalue = SwigType_typedef_resolve_all(pvalue); String *qvalue = SwigType_typedef_qualified(rvalue); String *defname = NewStringf("%s_defvalue", lname); String *str = SwigType_str(pt, defname); String *defvalue = NewStringf("%s = %s", str, qvalue); Wrapper_add_localv(w, defname, defvalue, NIL); Delete(str); Delete(rvalue); Delete(qvalue); Delete(defname); Delete(defvalue); } } else if (tycode == T_RVALUE_REFERENCE) { if (pvalue) { String *rvalue = SwigType_typedef_resolve_all(pvalue); String *qvalue = SwigType_typedef_qualified(rvalue); String *defname = NewStringf("%s_defrvalue", lname); String *str = SwigType_str(pt, defname); String *defvalue = NewStringf("%s = %s", str, qvalue); Wrapper_add_localv(w, defname, defvalue, NIL); Delete(str); Delete(rvalue); Delete(qvalue); Delete(defname); Delete(defvalue); } } else if (!pvalue && ((tycode == T_POINTER) || (tycode == T_STRING) || (tycode == T_WSTRING) || (tycode == T_ARRAY))) { pvalue = (String *) "0"; } if (!altty) { local = Swig_clocal(pt, lname, pvalue); } else { local = Swig_clocal(altty, lname, pvalue); Delete(altty); } Wrapper_add_localv(w, lname, local, NIL); Delete(local); i++; } Delete(lname); p = nextSibling(p); } return (i); } /* ----------------------------------------------------------------------------- * Swig_cresult() * * This function generates the C code needed to set the result of a C * function call. * ----------------------------------------------------------------------------- */ String *Swig_cresult(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr decl) { String *fcall; fcall = NewStringEmpty(); switch (SwigType_type(t)) { case T_VOID: break; case T_REFERENCE: { String *lstr = SwigType_lstr(t, 0); Printf(fcall, "%s = (%s) &", name, lstr); Delete(lstr); } break; case T_RVALUE_REFERENCE: { String *const_lvalue_str; String *lstr = SwigType_lstr(t, 0); SwigType *tt = Copy(t); SwigType_del_rvalue_reference(tt); SwigType_add_qualifier(tt, "const"); SwigType_add_reference(tt); const_lvalue_str = SwigType_rcaststr(tt, 0); Printf(fcall, "%s = (%s) &%s", name, lstr, const_lvalue_str); Delete(const_lvalue_str); Delete(tt); Delete(lstr); } break; case T_USER: Printf(fcall, "%s = ", name); break; default: /* Normal return value */ { String *lstr = SwigType_lstr(t, 0); Printf(fcall, "%s = (%s)", name, lstr); Delete(lstr); } break; } /* Now print out function call */ Append(fcall, decl); /* A sick hack */ { char *c = Char(decl) + Len(decl) - 1; if (!((*c == ';') || (*c == '}'))) Append(fcall, ";"); } return fcall; } /* ----------------------------------------------------------------------------- * Swig_cfunction_call() * * Creates a string that calls a C function using the local variable rules * defined above. * * name(arg0, arg1, arg2, ... argn) * * ----------------------------------------------------------------------------- */ String *Swig_cfunction_call(const_String_or_char_ptr name, ParmList *parms) { String *func; int i = 0; int comma = 0; Parm *p = parms; String *nname; func = NewStringEmpty(); nname = SwigType_namestr(name); /* SWIGTEMPLATEDISAMBIGUATOR is compiler dependent (swiglabels.swg), - SUN Studio 9 requires 'template', - gcc-3.4 forbids the use of 'template'. the rest seems not caring very much, */ if (SwigType_istemplate(name)) { String *prefix = Swig_scopename_prefix(nname); if (!prefix || Len(prefix) == 0) { Printf(func, "%s(", nname); } else { String *last = Swig_scopename_last(nname); Printf(func, "%s::SWIGTEMPLATEDISAMBIGUATOR %s(", prefix, last); Delete(last); } Delete(prefix); } else { Printf(func, "%s(", nname); } Delete(nname); while (p) { SwigType *pt = Getattr(p, "type"); if ((SwigType_type(pt) != T_VOID)) { SwigType *rpt = SwigType_typedef_resolve_all(pt); String *pname = Swig_cparm_name(p, i); String *rcaststr = SwigType_rcaststr(rpt, pname); if (comma) { Append(func, ","); } if (cparse_cplusplus && SwigType_type(rpt) == T_USER) Printv(func, "SWIG_STD_MOVE(", rcaststr, ")", NIL); else Printv(func, rcaststr, NIL); Delete(rpt); Delete(pname); Delete(rcaststr); comma = 1; i++; } p = nextSibling(p); } Append(func, ")"); return func; } /* ----------------------------------------------------------------------------- * Swig_cmethod_call() * * Generates a string that calls a C++ method from a list of parameters. * * arg0->name(arg1, arg2, arg3, ..., argn) * * self is an argument that defines how to handle the first argument. Normally, * it should be set to "this->". With C++ proxy classes enabled, it could be * set to "(*this)->" or some similar sequence. * ----------------------------------------------------------------------------- */ static String *Swig_cmethod_call(const_String_or_char_ptr name, ParmList *parms, const_String_or_char_ptr self, String *explicit_qualifier, SwigType *director_type) { String *func, *nname; int i = 0; Parm *p = parms; SwigType *pt; int comma = 0; func = NewStringEmpty(); if (!p) return func; if (!self) self = "(this)->"; Append(func, self); if (SwigType_istemplate(name) && (strncmp(Char(name), "operator ", 9) == 0)) { /* fix for template + operators and compilers like gcc 3.3.5 */ String *tprefix = SwigType_templateprefix(name); nname = tprefix; } else { nname = SwigType_namestr(name); } if (director_type) { const char *pname = "darg"; String *rcaststr = SwigType_rcaststr(director_type, pname); Replaceall(func, "this", rcaststr); Delete(rcaststr); } else { pt = Getattr(p, "type"); /* If the method is invoked through a dereferenced pointer, we don't add any casts (needed for smart pointers). Otherwise, we cast to the appropriate type */ if (Strstr(func, "*this")) { String *pname = Swig_cparm_name(p, 0); Replaceall(func, "this", pname); Delete(pname); } else { String *pname = Swig_cparm_name(p, 0); String *rcaststr = SwigType_rcaststr(pt, pname); Replaceall(func, "this", rcaststr); Delete(rcaststr); Delete(pname); } /* SWIGTEMPLATEDESIMBUAGATOR is compiler dependent (swiglabels.swg), - SUN Studio 9 requires 'template', - gcc-3.4 forbids the use of 'template' (correctly implementing the ISO C++ standard) the others don't seem to care, */ if (SwigType_istemplate(name)) Printf(func, "SWIGTEMPLATEDISAMBIGUATOR "); if (explicit_qualifier) { Printv(func, explicit_qualifier, "::", NIL); } } Printf(func, "%s(", nname); i++; p = nextSibling(p); while (p) { pt = Getattr(p, "type"); if ((SwigType_type(pt) != T_VOID)) { String *pname = Swig_cparm_name(p, i); String *rcaststr = SwigType_rcaststr(pt, pname); if (comma) Append(func, ","); if (cparse_cplusplus && SwigType_type(pt) == T_USER) Printv(func, "SWIG_STD_MOVE(", rcaststr, ")", NIL); else Printv(func, rcaststr, NIL); Delete(rcaststr); Delete(pname); comma = 1; i++; } p = nextSibling(p); } Append(func, ")"); Delete(nname); return func; } /* ----------------------------------------------------------------------------- * Swig_cconstructor_call() * * Creates a string that calls a C constructor function. * * calloc(1,sizeof(name)); * ----------------------------------------------------------------------------- */ String *Swig_cconstructor_call(const_String_or_char_ptr name) { DOH *func; func = NewStringEmpty(); Printf(func, "calloc(1, sizeof(%s))", name); return func; } /* ----------------------------------------------------------------------------- * Swig_cppconstructor_call() * * Creates a string that calls a C function using the local variable rules * defined above. * * name(arg0, arg1, arg2, ... argn) * * ----------------------------------------------------------------------------- */ static String *Swig_cppconstructor_base_call(const_String_or_char_ptr name, ParmList *parms, int skip_self) { String *func; String *nname; int i = 0; int comma = 0; Parm *p = parms; SwigType *pt; if (skip_self) { if (p) p = nextSibling(p); i++; } nname = SwigType_namestr(name); func = NewStringEmpty(); Printf(func, "new %s(", nname); while (p) { pt = Getattr(p, "type"); if ((SwigType_type(pt) != T_VOID)) { String *rcaststr = 0; String *pname = 0; if (comma) Append(func, ","); if (!Getattr(p, "arg:byname")) { pname = Swig_cparm_name(p, i); i++; } else { pname = Getattr(p, "value"); if (pname) pname = Copy(pname); else pname = Copy(Getattr(p, "name")); } rcaststr = SwigType_rcaststr(pt, pname); if (cparse_cplusplus && SwigType_type(pt) == T_USER) Printv(func, "SWIG_STD_MOVE(", rcaststr, ")", NIL); else Printv(func, rcaststr, NIL); Delete(rcaststr); comma = 1; Delete(pname); } p = nextSibling(p); } Append(func, ")"); Delete(nname); return func; } String *Swig_cppconstructor_call(const_String_or_char_ptr name, ParmList *parms) { return Swig_cppconstructor_base_call(name, parms, 0); } static String *Swig_cppconstructor_nodirector_call(const_String_or_char_ptr name, ParmList *parms) { return Swig_cppconstructor_base_call(name, parms, 1); } static String *Swig_cppconstructor_director_call(const_String_or_char_ptr name, ParmList *parms) { return Swig_cppconstructor_base_call(name, parms, 0); } /* ----------------------------------------------------------------------------- * recursive_flag_search() * * This function searches for the class attribute 'attr' in the class * 'n' or recursively in its bases. * * If you define SWIG_FAST_REC_SEARCH, the method will set the found * 'attr' in the target class 'n'. If not, the method will set the * 'noattr' one. This prevents of having to navigate the entire * hierarchy tree every time, so, it is an O(1) method... or something * like that. However, it populates all the parsed classes with the * 'attr' and/or 'noattr' attributes. * * If you undefine the SWIG_FAST_REC_SEARCH no attribute will be set * while searching. This could be slower for large projects with very * large hierarchy trees... or maybe not. But it will be cleaner. * * Maybe later a swig option can be added to switch at runtime. * * ----------------------------------------------------------------------------- */ /* #define SWIG_FAST_REC_SEARCH 1 */ static String *recursive_flag_search(Node *n, const String *attr, const String *noattr) { String *f = 0; n = Swig_methodclass(n); if (GetFlag(n, noattr)) { return 0; } f = GetFlagAttr(n, attr); if (f) { return f; } else { List *bl = Getattr(n, "bases"); if (bl) { Iterator bi; for (bi = First(bl); bi.item; bi = Next(bi)) { f = recursive_flag_search(bi.item, attr, noattr); if (f) { #ifdef SWIG_FAST_REC_SEARCH SetFlagAttr(n, attr, f); #endif return f; } } } } #ifdef SWIG_FAST_REC_SEARCH SetFlag(n, noattr); #endif return 0; } /* ----------------------------------------------------------------------------- * Swig_unref_call() * * Find the "feature:unref" call, if any. * ----------------------------------------------------------------------------- */ String *Swig_unref_call(Node *n) { String *unref = recursive_flag_search(n, "feature:unref", "feature:nounref"); if (unref) { String *pname = Swig_cparm_name(0, 0); unref = NewString(unref); Replaceall(unref, "$this", pname); Replaceall(unref, "$self", pname); Delete(pname); } return unref; } /* ----------------------------------------------------------------------------- * Swig_ref_call() * * Find the "feature:ref" call, if any. * ----------------------------------------------------------------------------- */ String *Swig_ref_call(Node *n, const String *lname) { String *ref = recursive_flag_search(n, "feature:ref", "feature:noref"); if (ref) { ref = NewString(ref); Replaceall(ref, "$this", lname); Replaceall(ref, "$self", lname); } return ref; } /* ----------------------------------------------------------------------------- * Swig_cdestructor_call() * * Creates a string that calls a C destructor function. * * free((char *) arg0); * ----------------------------------------------------------------------------- */ String *Swig_cdestructor_call(Node *n) { Node *cn = Swig_methodclass(n); String *unref = Swig_unref_call(cn); if (unref) { return unref; } else { String *pname = Swig_cparm_name(0, 0); String *call = NewStringf("free((char *) %s);", pname); Delete(pname); return call; } } /* ----------------------------------------------------------------------------- * Swig_cppdestructor_call() * * Creates a string that calls a C destructor function. * * delete arg1; * ----------------------------------------------------------------------------- */ String *Swig_cppdestructor_call(Node *n) { Node *cn = Swig_methodclass(n); String *unref = Swig_unref_call(cn); if (unref) { return unref; } else { String *pname = Swig_cparm_name(0, 0); String *call = NewStringf("delete %s;", pname); Delete(pname); return call; } } /* ----------------------------------------------------------------------------- * Swig_cmemberset_call() * * Generates a string that sets the name of a member in a C++ class or C struct. * * arg0->name = arg1 * * ----------------------------------------------------------------------------- */ String *Swig_cmemberset_call(const_String_or_char_ptr name, SwigType *type, String *self, int varcref) { String *func; String *pname0 = Swig_cparm_name(0, 0); String *pname1 = Swig_cparm_name(0, 1); func = NewStringEmpty(); if (!self) self = NewString("(this)->"); else self = NewString(self); Replaceall(self, "this", pname0); if (SwigType_type(type) != T_ARRAY) { if (!Strstr(type, "enum $unnamed")) { String *dref = Swig_wrapped_var_deref(type, pname1, varcref); int extra_cast = 0; if (cparse_cplusplusout) { /* Required for C nested structs compiled as C++ as a duplicate of the nested struct is put into the global namespace. * We could improve this by adding the extra casts just for nested structs rather than all structs. */ String *base = SwigType_base(type); extra_cast = SwigType_isclass(base); Delete(base); } if (extra_cast) { String *lstr; SwigType *ptype = Copy(type); SwigType_add_pointer(ptype); lstr = SwigType_lstr(ptype, 0); Printf(func, "if (%s) *(%s)&%s%s = %s", pname0, lstr, self, name, dref); Delete(lstr); Delete(ptype); } else { Printf(func, "if (%s) %s%s = %s", pname0, self, name, dref); } Delete(dref); } else { Printf(func, "if (%s && sizeof(int) == sizeof(%s%s)) *(int*)(void*)&(%s%s) = %s", pname0, self, name, self, name, pname1); } } Delete(self); Delete(pname0); Delete(pname1); return (func); } /* ----------------------------------------------------------------------------- * Swig_cmemberget_call() * * Generates a string that sets the name of a member in a C++ class or C struct. * * arg0->name * * ----------------------------------------------------------------------------- */ String *Swig_cmemberget_call(const_String_or_char_ptr name, SwigType *t, String *self, int varcref) { String *func; String *call; String *pname0 = Swig_cparm_name(0, 0); if (!self) self = NewString("(this)->"); else self = NewString(self); Replaceall(self, "this", pname0); func = NewStringEmpty(); call = Swig_wrapped_var_assign(t, "", varcref); Printf(func, "%s (%s%s)", call, self, name); Delete(self); Delete(call); Delete(pname0); return func; } /* ----------------------------------------------------------------------------- * Swig_replace_special_variables() * * Replaces special variables with a value from the supplied node * ----------------------------------------------------------------------------- */ void Swig_replace_special_variables(Node *n, Node *parentnode, String *code) { Node *parentclass = parentnode; String *overloaded = Getattr(n, "sym:overloaded"); Replaceall(code, "$name", Getattr(n, "name")); Replaceall(code, "$symname", Getattr(n, "sym:name")); Replaceall(code, "$wrapname", Getattr(n, "wrap:name")); Replaceall(code, "$overname", overloaded ? Char(Getattr(n, "sym:overname")) : ""); if (Strstr(code, "$decl")) { String *decl = Swig_name_decl(n); Replaceall(code, "$decl", decl); Delete(decl); } if (Strstr(code, "$fulldecl")) { String *fulldecl = Swig_name_fulldecl(n); Replaceall(code, "$fulldecl", fulldecl); Delete(fulldecl); } if (parentclass && !Equal(nodeType(parentclass), "class")) parentclass = 0; if (Strstr(code, "$parentclasssymname")) { String *parentclasssymname = 0; if (parentclass) parentclasssymname = Getattr(parentclass, "sym:name"); Replaceall(code, "$parentclasssymname", parentclasssymname ? parentclasssymname : ""); } if (Strstr(code, "$parentclassname")) { String *parentclassname = 0; if (parentclass) parentclassname = Getattr(parentclass, "name"); Replaceall(code, "$parentclassname", parentclassname ? SwigType_str(parentclassname, NULL) : ""); } } /* ----------------------------------------------------------------------------- * extension_code() * * Generates an extension function (a function defined in %extend) * * return_type function_name(parms) code * * ----------------------------------------------------------------------------- */ static String *extension_code(Node *n, const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self) { String *parms_str = cplusplus ? ParmList_str_defaultargs(parms) : ParmList_str(parms); String *sig = NewStringf("%s(%s)", function_name, (cplusplus || Len(parms_str)) ? parms_str : "void"); String *rt_sig = SwigType_str(return_type, sig); String *body = NewStringf("SWIGINTERN %s", rt_sig); Printv(body, code, "\n", NIL); if (Strchr(body, '$')) { Swig_replace_special_variables(n, parentNode(parentNode(n)), body); if (self) Replaceall(body, "$self", self); } Delete(parms_str); Delete(sig); Delete(rt_sig); return body; } /* ----------------------------------------------------------------------------- * Swig_add_extension_code() * * Generates an extension function (a function defined in %extend) and * adds it to the "wrap:code" attribute of a node * * See also extension_code() * * ----------------------------------------------------------------------------- */ int Swig_add_extension_code(Node *n, const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self) { String *body = extension_code(n, function_name, parms, return_type, code, cplusplus, self); Setattr(n, "wrap:code", body); Delete(body); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_MethodToFunction(Node *n) * * Converts a C++ method node to a function accessor function. * ----------------------------------------------------------------------------- */ int Swig_MethodToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, int flags, SwigType *director_type, int is_director) { String *name; ParmList *parms; SwigType *type; Parm *p; String *self = 0; int is_smart_pointer_overload = 0; String *qualifier = Getattr(n, "qualifier"); String *directorScope = NewString(nspace); Replace(directorScope, NSPACE_SEPARATOR, "_", DOH_REPLACE_ANY); /* If smart pointer without const overload or mutable method, change self dereferencing */ if (flags & CWRAP_SMART_POINTER) { if (flags & CWRAP_SMART_POINTER_OVERLOAD) { if (qualifier && strncmp(Char(qualifier), "q(const)", 8) == 0) { self = NewString("(*(this))->"); is_smart_pointer_overload = 1; } else if (Swig_storage_isstatic(n)) { String *cname = Getattr(n, "extendsmartclassname") ? Getattr(n, "extendsmartclassname") : classname; String *ctname = SwigType_namestr(cname); self = NewStringf("(*(%s const *)this)->", ctname); is_smart_pointer_overload = 1; Delete(ctname); } else { self = NewString("(*this)->"); } } else { self = NewString("(*this)->"); } } /* If node is a member template expansion, we don't allow added code */ if (Getattr(n, "templatetype")) flags &= ~(CWRAP_EXTEND); name = Getattr(n, "name"); parms = CopyParmList(nonvoid_parms(Getattr(n, "parms"))); type = NewString(classname); if (qualifier) { SwigType_push(type, qualifier); } SwigType_add_pointer(type); p = NewParm(type, "self", n); Setattr(p, "self", "1"); Setattr(p, "hidden","1"); /* Disable the 'this' ownership in 'self' to manage inplace operations like: A& A::operator+=(int i) { ...; return *this;} Here the 'self' parameter ownership needs to be disabled since there could be two objects sharing the same 'this' pointer: the input and the result one. And worse, the pointer could be deleted in one of the objects (input), leaving the other (output) with just a seg. fault to happen. To avoid the previous problem, use %feature("self:disown") *::operator+=; %feature("new") *::operator+=; These two lines just transfer the ownership of the 'this' pointer from the input to the output wrapping object. This happens in python, but may also happen in other target languages. */ if (GetFlag(n, "feature:self:disown")) { Setattr(p, "wrap:disown", "1"); } set_nextSibling(p, parms); Delete(type); /* Generate action code for the access */ if (!(flags & CWRAP_EXTEND)) { String *explicit_qualifier = 0; String *call = 0; String *cres = 0; String *explicitcall_name = 0; int pure_virtual = !(Cmp(Getattr(n, "storage"), "virtual")) && !(Cmp(Getattr(n, "value"), "0")); /* Call the explicit method rather than allow for a polymorphic call */ if ((flags & CWRAP_DIRECTOR_TWO_CALLS) || (flags & CWRAP_DIRECTOR_ONE_CALL)) { String *access = Getattr(n, "access"); if (access && (Cmp(access, "protected") == 0)) { /* If protected access (can only be if a director method) then call the extra public accessor method (language module must provide this) */ String *explicit_qualifier_tmp = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname")); explicitcall_name = NewStringf("%sSwigPublic", name); if (Len(directorScope) > 0) explicit_qualifier = NewStringf("SwigDirector_%s_%s", directorScope, explicit_qualifier_tmp); else explicit_qualifier = NewStringf("SwigDirector_%s", explicit_qualifier_tmp); Delete(explicit_qualifier_tmp); } else { explicit_qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname")); } } if (!self && SwigType_isrvalue_reference(Getattr(n, "refqualifier"))) { String *memory_header = NewString(""); Setfile(memory_header, Getfile(n)); Setline(memory_header, Getline(n)); Swig_fragment_emit(memory_header); self = NewString("std::move(*this)."); Delete(memory_header); } call = Swig_cmethod_call(explicitcall_name ? explicitcall_name : name, p, self, explicit_qualifier, director_type); cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call); if (pure_virtual && is_director && (flags & CWRAP_DIRECTOR_TWO_CALLS)) { String *qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname")); Delete(cres); cres = NewStringf("Swig::DirectorPureVirtualException::raise(\"%s::%s\");", qualifier, name); Delete(qualifier); } if (flags & CWRAP_DIRECTOR_TWO_CALLS) { /* Create two method calls, one to call the explicit method, the other a normal polymorphic function call */ String *cres_both_calls = NewStringf(""); String *call_extra = Swig_cmethod_call(name, p, self, 0, director_type); String *cres_extra = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call_extra); Printv(cres_both_calls, "if (upcall) {\n", cres, "\n", "} else {", cres_extra, "\n}", NIL); Setattr(n, "wrap:action", cres_both_calls); Delete(cres_extra); Delete(call_extra); Delete(cres_both_calls); } else { Setattr(n, "wrap:action", cres); } Delete(explicitcall_name); Delete(call); Delete(cres); Delete(explicit_qualifier); } else { /* Methods with default arguments are wrapped with additional methods for each default argument, * however, only one extra %extend method is generated. */ String *defaultargs = Getattr(n, "defaultargs"); String *code = Getattr(n, "code"); String *cname = Getattr(n, "extendsmartclassname") ? Getattr(n, "extendsmartclassname") : classname; String *membername = Swig_name_member(nspace, cname, name); String *mangled = Swig_name_mangle_string(membername); int is_smart_pointer = flags & CWRAP_SMART_POINTER; type = Getattr(n, "type"); /* Check if the method is overloaded. If so, and it has code attached, we append an extra suffix to avoid a name-clash in the generated wrappers. This allows overloaded methods to be defined in C. But when not using the suffix used for overloaded functions, we still need to ensure that the wrapper name doesn't conflict with any wrapper functions for some languages, so optionally make it sufficiently unique by appending a suffix similar to the one used for overloaded functions to it. */ if (code) { if (Getattr(n, "sym:overloaded")) { Append(mangled, Getattr(defaultargs ? defaultargs : n, "sym:overname")); } else if (UseWrapperSuffix) { Append(mangled, "__SWIG"); } } /* See if there is any code that we need to emit */ if (!defaultargs && code && !is_smart_pointer) { Swig_add_extension_code(n, mangled, p, type, code, cparse_cplusplus, "self"); } if (is_smart_pointer) { int i = 0; Parm *pp = p; String *func = NewStringf("%s(", mangled); String *cres; if (!Swig_storage_isstatic(n)) { String *pname = Swig_cparm_name(pp, i); String *ctname = SwigType_namestr(cname); String *fadd = 0; if (is_smart_pointer_overload) { String *nclassname = SwigType_namestr(classname); fadd = NewStringf("(%s const *)((%s const *)%s)->operator ->()", ctname, nclassname, pname); Delete(nclassname); } else { fadd = NewStringf("(%s*)(%s)->operator ->()", ctname, pname); } Append(func, fadd); Delete(ctname); Delete(fadd); Delete(pname); pp = nextSibling(pp); if (pp) Append(func, ","); } else { pp = nextSibling(pp); } ++i; while (pp) { SwigType *pt = Getattr(pp, "type"); if ((SwigType_type(pt) != T_VOID)) { String *pname = Swig_cparm_name(pp, i++); String *rcaststr = SwigType_rcaststr(pt, pname); Append(func, rcaststr); Delete(rcaststr); Delete(pname); pp = nextSibling(pp); if (pp) Append(func, ","); } } Append(func, ")"); cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), func); Setattr(n, "wrap:action", cres); Delete(cres); } else { String *call = Swig_cfunction_call(mangled, p); String *cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(call); Delete(cres); } Delete(membername); Delete(mangled); } Setattr(n, "parms", p); Delete(p); Delete(self); Delete(parms); Delete(directorScope); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_methodclass() * * This function returns the class node for a given method or class. * ----------------------------------------------------------------------------- */ Node *Swig_methodclass(Node *n) { if (Equal(nodeType(n), "class")) return n; return GetFlag(n, "feature:extend") ? parentNode(parentNode(n)) : parentNode(n); } int Swig_directorclass(Node *n) { Node *classNode = Swig_methodclass(n); assert(classNode != 0); return (Getattr(classNode, "vtable") != 0); } Node *Swig_directormap(Node *module, String *type) { int is_void = !Cmp(type, "void"); if (!is_void && module) { /* ?? follow the inheritance hierarchy? */ String *base = SwigType_base(type); Node *directormap = Getattr(module, "wrap:directormap"); if (directormap) return Getattr(directormap, base); } return 0; } /* ----------------------------------------------------------------------------- * Swig_ConstructorToFunction() * * This function creates a C wrapper for a C constructor function. * ----------------------------------------------------------------------------- */ int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, String *none_comparison, String *director_ctor, int cplus, int flags, String *directorname) { Parm *p; ParmList *directorparms; SwigType *type; int use_director = Swig_directorclass(n); ParmList *parms = CopyParmList(nonvoid_parms(Getattr(n, "parms"))); /* Prepend the list of prefix_args (if any) */ Parm *prefix_args = Getattr(n, "director:prefix_args"); if (prefix_args != NIL) { Parm *p2, *p3; directorparms = CopyParmList(prefix_args); for (p = directorparms; nextSibling(p); p = nextSibling(p)); for (p2 = parms; p2; p2 = nextSibling(p2)) { p3 = CopyParm(p2); set_nextSibling(p, p3); Delete(p3); p = p3; } } else directorparms = parms; type = NewString(classname); SwigType_add_pointer(type); if (flags & CWRAP_EXTEND) { /* Constructors with default arguments are wrapped with additional constructor methods for each default argument, * however, only one extra %extend method is generated. */ String *call; String *cres; String *defaultargs = Getattr(n, "defaultargs"); String *code = Getattr(n, "code"); String *membername = Swig_name_construct(nspace, classname); String *mangled = Swig_name_mangle_string(membername); /* Check if the constructor is overloaded. If so, and it has code attached, we append an extra suffix to avoid a name-clash in the generated wrappers. This allows overloaded constructors to be defined in C. */ if (Getattr(n, "sym:overloaded") && code) { Append(mangled, Getattr(defaultargs ? defaultargs : n, "sym:overname")); } /* See if there is any code that we need to emit */ if (!defaultargs && code) { Swig_add_extension_code(n, mangled, parms, type, code, cparse_cplusplus, "self"); } call = Swig_cfunction_call(mangled, parms); cres = Swig_cresult(type, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(cres); Delete(call); Delete(membername); Delete(mangled); } else { if (cplus) { /* if a C++ director class exists, create it rather than the original class */ if (use_director) { Node *parent = Swig_methodclass(n); int abstract = Getattr(parent, "abstracts") != 0; String *action = NewStringEmpty(); String *tmp_none_comparison = Copy(none_comparison); String *director_call; String *nodirector_call; Replaceall(tmp_none_comparison, "$arg", "arg1"); director_call = Swig_cppconstructor_director_call(directorname, directorparms); nodirector_call = Swig_cppconstructor_nodirector_call(classname, parms); if (abstract) { /* whether or not the abstract class has been subclassed in python, * create a director instance (there's no way to create a normal * instance). if any of the pure virtual methods haven't been * implemented in the target language, calls to those methods will * generate Swig::DirectorPureVirtualException exceptions. */ String *cres = Swig_cresult(type, Swig_cresult_name(), director_call); Append(action, cres); Delete(cres); } else { /* (scottm): The code for creating a new director is now a string template that gets passed in via the director_ctor argument. $comparison : an 'if' comparison from none_comparison $director_new: Call new for director class $nondirector_new: Call new for non-director class */ String *cres; Append(action, director_ctor); Replaceall(action, "$comparison", tmp_none_comparison); cres = Swig_cresult(type, Swig_cresult_name(), director_call); Replaceall(action, "$director_new", cres); Delete(cres); cres = Swig_cresult(type, Swig_cresult_name(), nodirector_call); Replaceall(action, "$nondirector_new", cres); Delete(cres); } Setattr(n, "wrap:action", action); Delete(tmp_none_comparison); Delete(action); } else { String *call = Swig_cppconstructor_call(classname, parms); String *cres = Swig_cresult(type, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(cres); Delete(call); } } else { String *call = Swig_cconstructor_call(classname); String *cres = Swig_cresult(type, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(cres); Delete(call); } } Setattr(n, "type", type); Setattr(n, "parms", parms); Delete(type); if (directorparms != parms) Delete(directorparms); Delete(parms); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_DestructorToFunction() * * This function creates a C wrapper for a destructor function. * ----------------------------------------------------------------------------- */ int Swig_DestructorToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, int cplus, int flags) { SwigType *type; Parm *p; type = NewString(classname); SwigType_add_pointer(type); p = NewParm(type, "self", n); Setattr(p, "self", "1"); Setattr(p, "hidden", "1"); Setattr(p, "wrap:disown", "1"); Delete(type); type = NewString("void"); if (flags & CWRAP_EXTEND) { String *cres; String *call; String *membername, *mangled, *code; membername = Swig_name_destroy(nspace, classname); mangled = Swig_name_mangle_string(membername); code = Getattr(n, "code"); if (code) { Swig_add_extension_code(n, mangled, p, type, code, cparse_cplusplus, "self"); } call = Swig_cfunction_call(mangled, p); cres = NewStringf("%s;", call); Setattr(n, "wrap:action", cres); Delete(membername); Delete(mangled); Delete(call); Delete(cres); } else { if (cplus) { String *call = Swig_cppdestructor_call(n); String *cres = NewStringf("%s", call); Setattr(n, "wrap:action", cres); Delete(call); Delete(cres); } else { String *call = Swig_cdestructor_call(n); String *cres = NewStringf("%s", call); Setattr(n, "wrap:action", cres); Delete(call); Delete(cres); } } Setattr(n, "type", type); Setattr(n, "parms", p); Delete(type); Delete(p); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_MembersetToFunction() * * This function creates a C wrapper for setting a structure member. * ----------------------------------------------------------------------------- */ int Swig_MembersetToFunction(Node *n, String *classname, int flags) { String *name; ParmList *parms; Parm *p; SwigType *t; SwigType *ty; SwigType *type; SwigType *void_type = NewString("void"); String *self = 0; int varcref = flags & CWRAP_NATURAL_VAR; if (flags & CWRAP_SMART_POINTER) { self = NewString("(*this)->"); } if (flags & CWRAP_ALL_PROTECTED_ACCESS) { self = NewStringf("darg->"); } name = Getattr(n, "name"); type = Getattr(n, "type"); t = NewString(classname); SwigType_add_pointer(t); parms = NewParm(t, "self", n); Setattr(parms, "self", "1"); Setattr(parms, "hidden","1"); Delete(t); ty = Swig_wrapped_member_var_type(type, varcref); p = NewParm(ty, name, n); Setattr(parms, "hidden", "1"); set_nextSibling(parms, p); /* If the type is a pointer or reference. We mark it with a special wrap:disown attribute */ if (SwigType_check_decl(type, "p.")) { Setattr(p, "wrap:disown", "1"); } Delete(p); if (flags & CWRAP_EXTEND) { String *call; String *cres; String *code = Getattr(n, "code"); String *sname = Swig_name_set(0, name); String *membername = Swig_name_member(0, classname, sname); String *mangled = Swig_name_mangle_string(membername); if (code) { /* I don't think this ever gets run - WSF */ Swig_add_extension_code(n, mangled, parms, void_type, code, cparse_cplusplus, "self"); } call = Swig_cfunction_call(mangled, parms); cres = NewStringf("%s;", call); Setattr(n, "wrap:action", cres); Delete(cres); Delete(call); Delete(mangled); Delete(membername); Delete(sname); } else { String *call = Swig_cmemberset_call(name, type, self, varcref); String *cres = NewStringf("%s;", call); Setattr(n, "wrap:action", cres); Delete(call); Delete(cres); } Setattr(n, "type", void_type); Setattr(n, "parms", parms); Delete(parms); Delete(ty); Delete(void_type); Delete(self); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_MembergetToFunction() * * This function creates a C wrapper for getting a structure member. * ----------------------------------------------------------------------------- */ int Swig_MembergetToFunction(Node *n, String *classname, int flags) { String *name; ParmList *parms; SwigType *t; SwigType *ty; SwigType *type; String *self = 0; int varcref = flags & CWRAP_NATURAL_VAR; if (flags & CWRAP_SMART_POINTER) { if (Swig_storage_isstatic(n)) { Node *sn = Getattr(n, "cplus:staticbase"); String *base = Getattr(sn, "name"); self = NewStringf("%s::", base); } else if (flags & CWRAP_SMART_POINTER_OVERLOAD) { String *nclassname = SwigType_namestr(classname); self = NewStringf("(*(%s const *)this)->", nclassname); Delete(nclassname); } else { self = NewString("(*this)->"); } } if (flags & CWRAP_ALL_PROTECTED_ACCESS) { self = NewStringf("darg->"); } name = Getattr(n, "name"); type = Getattr(n, "type"); t = NewString(classname); SwigType_add_pointer(t); parms = NewParm(t, "self", n); Setattr(parms, "self", "1"); Setattr(parms, "hidden","1"); Delete(t); ty = Swig_wrapped_member_var_type(type, varcref); if (flags & CWRAP_EXTEND) { String *call; String *cres; String *code = Getattr(n, "code"); String *gname = Swig_name_get(0, name); String *membername = Swig_name_member(0, classname, gname); String *mangled = Swig_name_mangle_string(membername); if (code) { /* I don't think this ever gets run - WSF */ Swig_add_extension_code(n, mangled, parms, ty, code, cparse_cplusplus, "self"); } call = Swig_cfunction_call(mangled, parms); cres = Swig_cresult(ty, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(cres); Delete(call); Delete(mangled); Delete(membername); Delete(gname); } else { String *call = Swig_cmemberget_call(name, type, self, varcref); String *cres = Swig_cresult(ty, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(call); Delete(cres); } Setattr(n, "type", ty); Setattr(n, "parms", parms); Delete(parms); Delete(ty); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_VarsetToFunction() * * This function creates a C wrapper for setting a global variable or static member * variable. * ----------------------------------------------------------------------------- */ int Swig_VarsetToFunction(Node *n, int flags) { String *name, *nname; ParmList *parms; SwigType *type, *ty; int varcref = flags & CWRAP_NATURAL_VAR; name = Getattr(n, "name"); type = Getattr(n, "type"); nname = SwigType_namestr(name); ty = Swig_wrapped_var_type(type, varcref); parms = NewParm(ty, name, n); if (flags & CWRAP_EXTEND) { String *sname = Swig_name_set(0, name); String *mangled = Swig_name_mangle_string(sname); String *call = Swig_cfunction_call(mangled, parms); String *cres = NewStringf("%s;", call); Setattr(n, "wrap:action", cres); Delete(cres); Delete(call); Delete(mangled); Delete(sname); } else { if (!Strstr(type, "enum $unnamed")) { String *pname = Swig_cparm_name(0, 0); String *dref = Swig_wrapped_var_deref(type, pname, varcref); String *call = NewStringf("%s = %s;", nname, dref); Setattr(n, "wrap:action", call); Delete(call); Delete(dref); Delete(pname); } else { String *pname = Swig_cparm_name(0, 0); String *call = NewStringf("if (sizeof(int) == sizeof(%s)) *(int*)(void*)&(%s) = %s;", nname, nname, pname); Setattr(n, "wrap:action", call); Delete(pname); Delete(call); } } Setattr(n, "type", "void"); Setattr(n, "parms", parms); Delete(parms); Delete(ty); Delete(nname); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Swig_VargetToFunction() * * This function creates a C wrapper for getting a global variable or static member * variable. * ----------------------------------------------------------------------------- */ int Swig_VargetToFunction(Node *n, int flags) { String *cres, *call; String *name; SwigType *type; SwigType *ty = 0; int varcref = flags & CWRAP_NATURAL_VAR; name = Getattr(n, "name"); type = Getattr(n, "type"); ty = Swig_wrapped_var_type(type, varcref); if (flags & CWRAP_EXTEND) { String *sname = Swig_name_get(0, name); String *mangled = Swig_name_mangle_string(sname); call = Swig_cfunction_call(mangled, 0); cres = Swig_cresult(ty, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(mangled); Delete(sname); } else { String *nname = 0; if (Equal(nodeType(n), "constant")) { nname = NewStringf("(%s)", Getattr(n, "value")); } else { nname = SwigType_namestr(name); } call = Swig_wrapped_var_assign(type, nname, varcref); cres = Swig_cresult(ty, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(nname); } Setattr(n, "type", ty); Delattr(n, "parms"); Delete(cres); Delete(call); Delete(ty); return SWIG_OK; } swig-4.4.0/Source/Swig/deprecate.c0000664000175000017500000000753315075443613016641 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * deprecate.c * * The functions in this file are SWIG core functions that are deprecated * or which do not fit in nicely with everything else. Generally this means * that the function and/or API needs to be changed in some future release. * ----------------------------------------------------------------------------- */ #include "swig.h" /* --------------------------------------------------------------------- * ParmList_is_compactdefargs() * * Returns 1 if the parameter list passed in is marked for compact argument * handling (by the "compactdefargs" attribute). Otherwise returns 0. * ---------------------------------------------------------------------- */ /* Discussion: "compactdefargs" is a property set by the Parser to indicate special handling of default arguments. This property seems to be something that is associated with functions and methods rather than low-level ParmList objects. Therefore, I don't like the fact that this special purpose feature is bolted onto the side of ParmList objects. Proposed solution: 1. "compactdefargs" should be a feature set on function/method nodes instead of ParmList objects. For example, if you have a function, you would check the function node to see if the parameters are to be handled in this way. Difficulties: 1. This is used by functions in cwrap.c and emit.cxx, none of which are passed information about the function/method node. We might have to change the API of those functions to make this work correctly. For example: int emit_num_required(ParmList *parms) might become int emit_num_required(ParmList *parms, int compactargs) */ int ParmList_is_compactdefargs(ParmList *p) { int compactdefargs = 0; if (p) { compactdefargs = Getattr(p, "compactdefargs") ? 1 : 0; /* The "compactdefargs" attribute should only be set on the first parameter in the list. * However, sometimes an extra parameter is inserted at the beginning of the parameter list, * so we check the 2nd parameter too. */ if (!compactdefargs) { Parm *nextparm = nextSibling(p); compactdefargs = (nextparm && Getattr(nextparm, "compactdefargs")) ? 1 : 0; } } return compactdefargs; } /* --------------------------------------------------------------------- * ParmList_errorstr() * * Generate a prototype string suitable for use in error/warning messages. * Similar to ParmList_protostr() but is also aware of hidden parameters. * ---------------------------------------------------------------------- */ /* Discussion. This function is used to generate error messages, but take into account that there might be a hidden parameter. Although this involves parameter lists, it really isn't a core feature of swigparm.h or parms.c. This is because the "hidden" attribute of parameters is added elsewhere (cwrap.c). For now, this function is placed here because it doesn't really seem to fit in with the parms.c interface. */ String *ParmList_errorstr(ParmList *p) { String *out = NewStringEmpty(); while (p) { if (Getattr(p,"hidden")) { p = nextSibling(p); } else { String *pstr = SwigType_str(Getattr(p, "type"), 0); Append(out, pstr); p = nextSibling(p); if (p) { Append(out, ","); } Delete(pstr); } } return out; } swig-4.4.0/Source/Swig/wrapfunc.c0000664000175000017500000003100315075443613016517 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * wrapfunc.c * * This file defines a object for creating wrapper functions. Primarily * this is used for convenience since it allows pieces of a wrapper function * to be created in a piecemeal manner. * ----------------------------------------------------------------------------- */ #include "swig.h" #include static int Compact_mode = 0; /* set to 0 on default */ static int Max_line_size = 128; /* ----------------------------------------------------------------------------- * NewWrapper() * * Create a new wrapper function object. * ----------------------------------------------------------------------------- */ Wrapper *NewWrapper(void) { Wrapper *w; w = (Wrapper *) Malloc(sizeof(Wrapper)); w->localh = NewHash(); w->locals = NewStringEmpty(); w->code = NewStringEmpty(); w->def = NewStringEmpty(); return w; } /* ----------------------------------------------------------------------------- * DelWrapper() * * Delete a wrapper function object. * ----------------------------------------------------------------------------- */ void DelWrapper(Wrapper *w) { Delete(w->localh); Delete(w->locals); Delete(w->code); Delete(w->def); Free(w); } /* ----------------------------------------------------------------------------- * Wrapper_compact_print_mode_set() * * Set compact_mode. * ----------------------------------------------------------------------------- */ void Wrapper_compact_print_mode_set(int flag) { Compact_mode = flag; } /* ----------------------------------------------------------------------------- * Wrapper_pretty_print() * * Formats a wrapper function and fixes up the indentation. * ----------------------------------------------------------------------------- */ void Wrapper_pretty_print(String *str, File *f) { String *ts; int level = 0; int c, i; int empty = 1; int indent = 2; int plevel = 0; int label = 0; ts = NewStringEmpty(); Seek(str, 0, SEEK_SET); while ((c = Getc(str)) != EOF) { if (c == '\"') { Putc(c, ts); while ((c = Getc(str)) != EOF) { if (c == '\\') { Putc(c, ts); c = Getc(str); } Putc(c, ts); if (c == '\"') break; } empty = 0; } else if (c == '\'') { Putc(c, ts); while ((c = Getc(str)) != EOF) { if (c == '\\') { Putc(c, ts); c = Getc(str); } Putc(c, ts); if (c == '\'') break; } empty = 0; } else if (c == ':') { Putc(c, ts); if ((c = Getc(str)) == '\n') { if (!empty && !strchr(Char(ts), '?')) label = 1; } Ungetc(c, str); } else if (c == '(') { Putc(c, ts); plevel += indent; empty = 0; } else if (c == ')') { Putc(c, ts); plevel -= indent; empty = 0; } else if (c == '{') { Putc(c, ts); Putc('\n', ts); for (i = 0; i < level; i++) Putc(' ', f); Printf(f, "%s", ts); Clear(ts); level += indent; while ((c = Getc(str)) != EOF) { if (!isspace(c)) { Ungetc(c, str); break; } } empty = 0; } else if (c == '}') { if (!empty) { Putc('\n', ts); for (i = 0; i < level; i++) Putc(' ', f); Printf(f, "%s", ts); Clear(ts); } level -= indent; Putc(c, ts); empty = 0; } else if (c == '\n') { Putc(c, ts); empty = 0; if (!empty) { int slevel = level; if (label && (slevel >= indent)) slevel -= indent; if ((Char(ts))[0] != '#') { for (i = 0; i < slevel; i++) Putc(' ', f); } Printf(f, "%s", ts); for (i = 0; i < plevel; i++) Putc(' ', f); } Clear(ts); label = 0; empty = 1; } else if (c == '/') { empty = 0; Putc(c, ts); c = Getc(str); if (c != EOF) { Putc(c, ts); if (c == '/') { /* C++ comment */ while ((c = Getc(str)) != EOF) { if (c == '\n') { Ungetc(c, str); break; } Putc(c, ts); } } else if (c == '*') { /* C comment */ int endstar = 0; while ((c = Getc(str)) != EOF) { if (endstar && c == '/') { /* end of C comment */ Putc(c, ts); break; } endstar = (c == '*'); Putc(c, ts); if (c == '\n') { /* multi-line C comment. Could be improved slightly. */ for (i = 0; i < level; i++) Putc(' ', ts); } } } } } else { if (!empty || !isspace(c)) { Putc(c, ts); empty = 0; } } } if (!empty) Printf(f, "%s", ts); Delete(ts); Printf(f, "\n"); } /* ----------------------------------------------------------------------------- * Wrapper_compact_print() * * Formats a wrapper function and fixes up the indentation. * Print out in compact format, with Compact enabled. * ----------------------------------------------------------------------------- */ void Wrapper_compact_print(String *str, File *f) { String *ts, *tf; /*temp string & temp file */ int level = 0; int c, i; int empty = 1; int indent = 2; ts = NewStringEmpty(); tf = NewStringEmpty(); Seek(str, 0, SEEK_SET); while ((c = Getc(str)) != EOF) { if (c == '\"') { /* string 1 */ empty = 0; Putc(c, ts); while ((c = Getc(str)) != EOF) { if (c == '\\') { Putc(c, ts); c = Getc(str); } Putc(c, ts); if (c == '\"') break; } } else if (c == '\'') { /* string 2 */ empty = 0; Putc(c, ts); while ((c = Getc(str)) != EOF) { if (c == '\\') { Putc(c, ts); c = Getc(str); } Putc(c, ts); if (c == '\'') break; } } else if (c == '{') { /* start of {...} */ empty = 0; Putc(c, ts); if (Len(tf) == 0) { for (i = 0; i < level; i++) Putc(' ', tf); } else if ((Len(tf) + Len(ts)) < Max_line_size) { Putc(' ', tf); } else { Putc('\n', tf); Printf(f, "%s", tf); Clear(tf); for (i = 0; i < level; i++) Putc(' ', tf); } Append(tf, ts); Clear(ts); level += indent; while ((c = Getc(str)) != EOF) { if (!isspace(c)) { Ungetc(c, str); break; } } } else if (c == '}') { /* end of {...} */ empty = 0; if (Len(tf) == 0) { for (i = 0; i < level; i++) Putc(' ', tf); } else if ((Len(tf) + Len(ts)) < Max_line_size) { Putc(' ', tf); } else { Putc('\n', tf); Printf(f, "%s", tf); Clear(tf); for (i = 0; i < level; i++) Putc(' ', tf); } Append(tf, ts); Putc(c, tf); Clear(ts); level -= indent; } else if (c == '\n') { /* line end */ while ((c = Getc(str)) != EOF) { if (!isspace(c)) break; } if (c == '#') { Putc('\n', ts); } else if (c == '}') { Putc(' ', ts); } else if ((c != EOF) || (Len(ts) != 0)) { if (Len(tf) == 0) { for (i = 0; i < level; i++) Putc(' ', tf); } else if ((Len(tf) + Len(ts)) < Max_line_size) { Putc(' ', tf); } else { Putc('\n', tf); Printf(f, "%s", tf); Clear(tf); for (i = 0; i < level; i++) Putc(' ', tf); } Append(tf, ts); Clear(ts); } Ungetc(c, str); empty = 1; } else if (c == '/') { /* comment */ empty = 0; c = Getc(str); if (c != EOF) { if (c == '/') { /* C++ comment */ while ((c = Getc(str)) != EOF) { if (c == '\n') { Ungetc(c, str); break; } } } else if (c == '*') { /* C comment */ int endstar = 0; while ((c = Getc(str)) != EOF) { if (endstar && c == '/') { /* end of C comment */ break; } endstar = (c == '*'); } } else { Putc('/', ts); Putc(c, ts); } } } else if (c == '#') { /* Preprocessor line */ Putc('#', ts); while ((c = Getc(str)) != EOF) { Putc(c, ts); if (c == '\\') { /* Continued line of the same PP */ c = Getc(str); if (c == '\n') Putc(c, ts); else Ungetc(c, str); } else if (c == '\n') break; } if (!empty) { Append(tf, "\n"); } Append(tf, ts); Printf(f, "%s", tf); Clear(tf); Clear(ts); for (i = 0; i < level; i++) Putc(' ', tf); empty = 1; } else { if (!empty || !isspace(c)) { Putc(c, ts); empty = 0; } } } if (!empty) { Append(tf, ts); } if (Len(tf) != 0) Printf(f, "%s", tf); Delete(ts); Delete(tf); Printf(f, "\n"); } /* ----------------------------------------------------------------------------- * Wrapper_print() * * Print out a wrapper function. Does pretty or compact printing as well. * ----------------------------------------------------------------------------- */ void Wrapper_print(Wrapper *w, File *f) { String *str; str = NewStringEmpty(); Printf(str, "%s\n", w->def); Printf(str, "%s\n", w->locals); Printf(str, "%s\n", w->code); if (Compact_mode == 1) Wrapper_compact_print(str, f); else Wrapper_pretty_print(str, f); Delete(str); } /* ----------------------------------------------------------------------------- * Wrapper_add_local() * * Adds a new local variable declaration to a function. Returns -1 if already * present (which may or may not be okay to the caller). * ----------------------------------------------------------------------------- */ int Wrapper_add_local(Wrapper *w, const_String_or_char_ptr name, const_String_or_char_ptr decl) { /* See if the local has already been declared */ if (Getattr(w->localh, name)) { return -1; } Setattr(w->localh, name, decl); Printf(w->locals, "%s;\n", decl); return 0; } /* ----------------------------------------------------------------------------- * Wrapper_add_localv() * * Same as add_local(), but allows a NULL terminated list of strings to be * used as a replacement for decl. This saves the caller the trouble of having * to manually construct the 'decl' string before calling. * ----------------------------------------------------------------------------- */ int Wrapper_add_localv(Wrapper *w, const_String_or_char_ptr name, ...) { va_list ap; int ret; String *decl; DOH *obj; decl = NewStringEmpty(); va_start(ap, name); obj = va_arg(ap, void *); while (obj) { Append(decl, obj); Putc(' ', decl); obj = va_arg(ap, void *); } va_end(ap); ret = Wrapper_add_local(w, name, decl); Delete(decl); return ret; } /* ----------------------------------------------------------------------------- * Wrapper_check_local() * * Check to see if a local name has already been declared * ----------------------------------------------------------------------------- */ int Wrapper_check_local(Wrapper *w, const_String_or_char_ptr name) { if (Getattr(w->localh, name)) { return 1; } return 0; } /* ----------------------------------------------------------------------------- * Wrapper_new_local() * * Adds a new local variable with a guarantee that a unique local name will be * used. Returns the name that was actually selected. * ----------------------------------------------------------------------------- */ char *Wrapper_new_local(Wrapper *w, const_String_or_char_ptr name, const_String_or_char_ptr decl) { int i; String *nname = NewString(name); String *ndecl = NewString(decl); char *ret; i = 0; while (Wrapper_check_local(w, nname)) { Clear(nname); Printf(nname, "%s%d", name, i); i++; } Replace(ndecl, name, nname, DOH_REPLACE_ID); Setattr(w->localh, nname, ndecl); Printf(w->locals, "%s;\n", ndecl); ret = Char(nname); Delete(nname); Delete(ndecl); return ret; /* Note: nname should still exists in the w->localh hash */ } /* ----------------------------------------------------------------------------- * Wrapper_new_localv() * * Same as add_local(), but allows a NULL terminated list of strings to be * used as a replacement for decl. This saves the caller the trouble of having * to manually construct the 'decl' string before calling. * ----------------------------------------------------------------------------- */ char *Wrapper_new_localv(Wrapper *w, const_String_or_char_ptr name, ...) { va_list ap; char *ret; String *decl; DOH *obj; decl = NewStringEmpty(); va_start(ap, name); obj = va_arg(ap, void *); while (obj) { Append(decl, obj); Putc(' ', decl); obj = va_arg(ap, void *); } va_end(ap); ret = Wrapper_new_local(w, name, decl); Delete(decl); return ret; } swig-4.4.0/Source/Swig/swigfile.h0000664000175000017500000000372215075443613016517 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigfile.h * * File handling functions in the SWIG core * ----------------------------------------------------------------------------- */ extern List *Swig_add_directory(const_String_or_char_ptr dirname); extern void Swig_push_directory(const_String_or_char_ptr dirname); extern void Swig_pop_directory(void); extern String *Swig_last_file(void); extern List *Swig_search_path(void); extern FILE *Swig_include_open(const_String_or_char_ptr name); extern FILE *Swig_open(const_String_or_char_ptr name); extern String *Swig_read_file(FILE *f); extern String *Swig_include(const_String_or_char_ptr name); extern String *Swig_include_sys(const_String_or_char_ptr name); extern int Swig_insert_file(const_String_or_char_ptr name, File *outfile); extern void Swig_set_push_dir(int dopush); extern int Swig_get_push_dir(void); extern void Swig_register_filebyname(const_String_or_char_ptr filename, File *outfile); extern File *Swig_filebyname(const_String_or_char_ptr filename); extern String *Swig_file_extension(const_String_or_char_ptr filename); extern String *Swig_file_basename(const_String_or_char_ptr filename); extern String *Swig_file_filename(const_String_or_char_ptr filename); extern String *Swig_file_dirname(const_String_or_char_ptr filename); extern void Swig_file_debug_set(void); /* Delimiter used in accessing files and directories */ #if defined(_WIN32) # define SWIG_FILE_DELIMITER "\\" #else # define SWIG_FILE_DELIMITER "/" #endif swig-4.4.0/Source/Swig/extend.c0000664000175000017500000001126615075443613016172 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * extend.c * * Extensions support (%extend) * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" static Hash *extendhash = 0; /* Hash table of added methods */ /* ----------------------------------------------------------------------------- * Swig_extend_hash() * * Access the extend hash * ----------------------------------------------------------------------------- */ Hash *Swig_extend_hash(void) { if (!extendhash) extendhash = NewHash(); return extendhash; } /* ----------------------------------------------------------------------------- * Swig_extend_merge() * * Extension merge. This function is used to handle the %extend directive * when it appears before a class definition. To handle this, the %extend * actually needs to take precedence. Therefore, we will selectively nuke symbols * from the current symbol table, replacing them with the added methods. * ----------------------------------------------------------------------------- */ void Swig_extend_merge(Node *cls, Node *am) { Node *n; n = firstChild(am); while (n) { String *symname; if (Strcmp(nodeType(n),"constructor") == 0) { symname = Getattr(n,"sym:name"); if (symname) { if (Strcmp(symname,Getattr(n,"name")) == 0) { /* If the name and the sym:name of a constructor are the same, then it hasn't been renamed. However---the name of the class itself might have been renamed so we need to do a consistency check here */ if (Getattr(cls,"sym:name")) { Setattr(n,"sym:name", Getattr(cls,"sym:name")); } } } } symname = Getattr(n,"sym:name"); DohIncref(symname); if ((symname) && (!Getattr(n,"error"))) { Node *c; /* Remove node from its symbol table */ Swig_symbol_remove(n); c = Swig_symbol_add(symname,n); if (c != n) { /* Conflict with previous definition. Nuke previous definition */ String *e = NewStringEmpty(); String *en = NewStringEmpty(); String *ec = NewStringEmpty(); Printf(ec, "Redefinition of identifier '%s' by %%extend ignored,", symname); Printf(en, "%%extend definition of '%s'.", symname); SWIG_WARN_NODE_BEGIN(n); Swig_warning(WARN_PARSE_REDEFINED, Getfile(c), Getline(c), "%s\n", ec); Swig_warning(WARN_PARSE_REDEFINED, Getfile(n), Getline(n), "%s\n", en); SWIG_WARN_NODE_END(n); Printf(e, "%s:%d:%s\n%s:%d:%s\n", Getfile(c), Getline(c), ec, Getfile(n),Getline(n),en); Setattr(c, "error", e); Delete(e); Delete(en); Delete(ec); Swig_symbol_remove(c); /* Remove class definition */ Swig_symbol_add(symname, n); /* Insert extend definition */ } } n = nextSibling(n); } } /* ----------------------------------------------------------------------------- * Swig_extend_append_previous() * ----------------------------------------------------------------------------- */ void Swig_extend_append_previous(Node *cls, Node *am) { Node *n, *ne; Node *pe = 0; Node *ae = 0; if (!am) return; n = firstChild(am); while (n) { ne = nextSibling(n); set_nextSibling(n,0); /* typemaps and fragments need to be prepended */ if (((Cmp(nodeType(n),"typemap") == 0) || (Cmp(nodeType(n),"fragment") == 0))) { if (!pe) pe = Swig_cparse_new_node("extend"); appendChild(pe, n); } else { if (!ae) ae = Swig_cparse_new_node("extend"); appendChild(ae, n); } n = ne; } if (pe) prependChild(cls,pe); if (ae) appendChild(cls,ae); } /* ----------------------------------------------------------------------------- * Swig_extend_unused_check() * * Check for unused %extend. Special case, don't report unused * extensions for templates * ----------------------------------------------------------------------------- */ void Swig_extend_unused_check(void) { Iterator ki; if (!extendhash) return; for (ki = First(extendhash); ki.key; ki = Next(ki)) { if (!Strchr(ki.key,'<')) { SWIG_WARN_NODE_BEGIN(ki.item); Swig_warning(WARN_PARSE_EXTEND_UNDEF,Getfile(ki.item), Getline(ki.item), "%%extend defined for an undeclared class %s.\n", SwigType_namestr(ki.key)); SWIG_WARN_NODE_END(ki.item); } } } swig-4.4.0/Source/Swig/.deps/0000775000175000017500000000000015075470612015541 5ustar williamwilliamswig-4.4.0/Source/Swig/swigparm.h0000664000175000017500000000355115075443613016537 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigparm.h * * Functions related to the handling of function/method parameters and * parameter lists. * ----------------------------------------------------------------------------- */ /* Individual parameters */ extern Parm *NewParm(SwigType *type, const_String_or_char_ptr name, Node *from_node); extern Parm *NewParmWithoutFileLineInfo(SwigType *type, const_String_or_char_ptr name); extern Parm *NewParmNode(SwigType *type, Node *from_node); extern Parm *CopyParm(Parm *p); /* Parameter lists */ extern ParmList *CopyParmList(ParmList *p); extern ParmList *CopyParmListMax(ParmList *p, int count); extern ParmList *ParmList_join(ParmList *p, ParmList *p2); extern ParmList *ParmList_replace_last(ParmList *p, ParmList *p2); extern Parm *ParmList_nth_parm(ParmList *p, unsigned int n); extern Parm *ParmList_variadic_parm(ParmList *p); extern Parm *ParmList_add_parm(ParmList *p, Parm *newparm); extern int ParmList_numrequired(ParmList *); extern int ParmList_len(ParmList *); extern int ParmList_has_defaultargs(ParmList *p); extern int ParmList_has_varargs(ParmList *p); /* Output functions */ extern String *ParmList_str(ParmList *); extern String *ParmList_str_defaultargs(ParmList *); extern String *ParmList_str_multibrackets(ParmList *); extern String *ParmList_protostr(ParmList *); swig-4.4.0/Source/Swig/stype.c0000664000175000017500000012357415075443613016055 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * stype.c * * This file provides general support for datatypes that are encoded in * the form of simple strings. * ----------------------------------------------------------------------------- */ #include "swig.h" #include "cparse.h" #include /* ----------------------------------------------------------------------------- * Synopsis * * The purpose of this module is to provide a general purpose type representation * based on simple text strings. * * General idea: * * Types are represented by a base type (e.g., "int") and a collection of * type operators applied to the base (e.g., pointers, arrays, etc...). * * Encoding: * * Types are encoded as strings of type constructors such as follows: * * String Encoding C Example * --------------- --------- * p.p.int int ** * a(300).a(400).int int [300][400] * p.q(const).char char const * * * All type constructors are denoted by a trailing '.': * * 'p.' = Pointer (*) * 'r.' = Reference (&) * 'z.' = Rvalue reference (&&) * 'v.' = Variadic (...) * 'a(n).' = Array of size n [n] * 'f(..,..).' = Function with arguments (args) * 'q(str).' = Qualifier, such as const or volatile (cv-qualifier) * 'm(cls).' = Pointer to member (cls::*) * * The encoding follows the order that you might describe a type in words. * For example "p.a(200).int" is "A pointer to array of int's" and * "p.q(const).char" is "a pointer to a const char". * * This representation of types is fairly convenient because ordinary string * operations can be used for type manipulation. For example, a type could be * formed by combining two strings such as the following: * * "p.p." + "a(400).int" = "p.p.a(400).int" * * Similarly, one could strip a 'const' declaration from a type doing something * like this: * * Replace(t,"q(const).","",DOH_REPLACE_ANY) * * More examples: * * String Encoding C++ Example * --------------- ----------- * p.f(bool).r.q(const).long const long & (*)(bool) * m(Funcs).q(const).f(bool).long long (Funcs::*)(bool) const * r.q(const).m(Funcs).f(int).long long (Funcs::*const &)(int) * m(Funcs).z.q(const).f(bool).long long (Funcs::*)(bool) const && * * Function decl examples: * * f(bool). long a(bool); * r.f(bool). long b(bool) &; * z.f(bool). long c(bool) &&; * z.q(const).f(bool). long d(bool) const &&; * * For the most part, this module tries to minimize the use of special * characters (*, [, <, etc...) in its type encoding. One reason for this * is that SWIG might be extended to encode data in formats such as XML * where you might want to do this: * * * p.p.int * ... * * * Or alternatively, * * blah * * In either case, it's probably best to avoid characters such as '&', '*', or '<'. * * Why not use C syntax? Well, C syntax is fairly complicated to parse * and not particularly easy to manipulate---especially for adding, deleting and * composing type constructors. The string representation presented here makes * this pretty easy. * * Why not use a bunch of nested data structures? Are you kidding? How * would that be easier to use than a few simple string operations? * ----------------------------------------------------------------------------- */ SwigType *NewSwigType(int t) { switch (t) { case T_BOOL: return NewString("bool"); case T_UNKNOWN: /* Handle like T_INT since we used to just use T_INT where we now use * T_UNKNOWN. */ case T_INT: return NewString("int"); case T_UINT: return NewString("unsigned int"); case T_SHORT: return NewString("short"); case T_USHORT: return NewString("unsigned short"); case T_LONG: return NewString("long"); case T_ULONG: return NewString("unsigned long"); case T_FLOAT: return NewString("float"); case T_DOUBLE: return NewString("double"); case T_LONGDOUBLE: return NewString("long double"); case T_FLTCPLX: return NewString("float _Complex"); case T_COMPLEX: return NewString("_Complex"); case T_CHAR: return NewString("char"); case T_SCHAR: return NewString("signed char"); case T_UCHAR: return NewString("unsigned char"); case T_STRING: { SwigType *t = NewString("char"); SwigType_add_qualifier(t, "const"); SwigType_add_pointer(t); return t; } case T_WCHAR: return NewString("wchar_t"); case T_WSTRING: { SwigType *t = NewString("wchar_t"); SwigType_add_pointer(t); return t; } case T_LONGLONG: return NewString("long long"); case T_ULONGLONG: return NewString("unsigned long long"); case T_VOID: return NewString("void"); case T_AUTO: return NewString("auto"); default: break; } return NewStringEmpty(); } /* ----------------------------------------------------------------------------- * SwigType_push() * * Push a type constructor onto the type * ----------------------------------------------------------------------------- */ void SwigType_push(SwigType *t, String *cons) { if (!cons) return; if (!Len(cons)) return; if (Len(t)) { char *c = Char(cons); if (c[strlen(c) - 1] != '.') Insert(t, 0, "."); } Insert(t, 0, cons); } /* ----------------------------------------------------------------------------- * SwigType_ispointer_return() * * Testing functions for querying a raw datatype * ----------------------------------------------------------------------------- */ int SwigType_ispointer_return(const SwigType *t) { char *c; int idx; if (!t) return 0; c = Char(t); idx = (int)strlen(c) - 4; if (idx >= 0) { return (strcmp(c + idx, ").p.") == 0); } return 0; } int SwigType_isreference_return(const SwigType *t) { char *c; int idx; if (!t) return 0; c = Char(t); idx = (int)strlen(c) - 4; if (idx >= 0) { return (strcmp(c + idx, ").r.") == 0); } return 0; } int SwigType_isconst(const SwigType *t) { char *c; if (!t) return 0; c = Char(t); if (strncmp(c, "q(", 2) == 0) { String *q = SwigType_parm(t); if (strstr(Char(q), "const")) { Delete(q); return 1; } Delete(q); } /* Hmmm. Might be const through a typedef */ if (SwigType_issimple(t)) { int ret; SwigType *td = SwigType_typedef_resolve(t); if (td) { ret = SwigType_isconst(td); Delete(td); return ret; } } return 0; } int SwigType_ismutable(const SwigType *t) { int r; SwigType *qt = SwigType_typedef_resolve_all(t); if (SwigType_isreference(qt) || SwigType_isrvalue_reference(qt)) { Delete(SwigType_pop(qt)); } else { while (SwigType_isarray(qt)) { Delete(SwigType_pop(qt)); } } r = SwigType_isconst(qt); Delete(qt); return r ? 0 : 1; } int SwigType_isenum(const SwigType *t) { char *c = Char(t); if (!t) return 0; if (strncmp(c, "enum ", 5) == 0) { return 1; } return 0; } int SwigType_issimple(const SwigType *t) { char *c = Char(t); if (!t) return 0; while (*c) { if (*c == '<') { int nest = 1; c++; while (*c && nest) { if (*c == '<') nest++; if (*c == '>') nest--; c++; } c--; } if (*c == '.') return 0; c++; } return 1; } /* ----------------------------------------------------------------------------- * SwigType_default_create() * * Create the default type for this datatype. This takes a type and strips it * down to a generic form first by resolving all typedefs. * * Rules: * Pointers: p.SWIGTYPE * References: r.SWIGTYPE * Arrays no dimension: a().SWIGTYPE * Arrays with dimension: a(ANY).SWIGTYPE * Member pointer: m(CLASS).SWIGTYPE * Function pointer: f(ANY).SWIGTYPE * Enums: enum SWIGTYPE * Types: SWIGTYPE * * Examples (also see SwigType_default_deduce): * * int [2][4] * a(2).a(4).int * a(ANY).a(ANY).SWIGTYPE * * struct A {}; * typedef A *Aptr; * Aptr const & * r.q(const).Aptr * r.q(const).p.SWIGTYPE * * enum E {e1, e2}; * enum E const & * r.q(const).enum E * r.q(const).enum SWIGTYPE * ----------------------------------------------------------------------------- */ SwigType *SwigType_default_create(const SwigType *ty) { SwigType *r = 0; List *l; Iterator it; int numitems; if (!SwigType_isvarargs(ty)) { SwigType *t = SwigType_typedef_resolve_all(ty); r = NewStringEmpty(); l = SwigType_split(t); numitems = Len(l); if (numitems >= 1) { String *last_subtype = Getitem(l, numitems-1); if (SwigType_isenum(last_subtype)) Setitem(l, numitems-1, NewString("enum SWIGTYPE")); else Setitem(l, numitems-1, NewString("SWIGTYPE")); } for (it = First(l); it.item; it = Next(it)) { String *subtype = it.item; if (SwigType_isarray(subtype)) { if (Equal(subtype, "a().")) Append(r, NewString("a().")); else Append(r, NewString("a(ANY).")); } else if (SwigType_isfunction(subtype)) { Append(r, NewString("f(ANY).SWIGTYPE")); break; } else if (SwigType_ismemberpointer(subtype)) { Append(r, NewString("m(CLASS).SWIGTYPE")); break; } else { Append(r, subtype); } } Delete(l); Delete(t); } return r; } /* ----------------------------------------------------------------------------- * SwigType_default_deduce() * * This function implements type deduction used in the typemap matching rules * and is very close to the type deduction used in partial template class * specialization matching in that the most specialized type is always chosen. * SWIGTYPE is used as the generic type. The basic idea is to repeatedly call * this function to find a deduced type until nothing matches. * * The type t must have already been converted to the default type via a call to * SwigType_default_create() before calling this function. * * Example deductions (matching the examples described in SwigType_default_create), * where the most specialized matches are highest in the list: * * a(ANY).a(ANY).SWIGTYPE * a(ANY).a().SWIGTYPE * a(ANY).p.SWIGTYPE * a(ANY).SWIGTYPE * a().SWIGTYPE * p.SWIGTYPE * SWIGTYPE * * r.q(const).p.SWIGTYPE * r.q(const).SWIGTYPE * r.SWIGTYPE * SWIGTYPE * * r.q(const).enum SWIGTYPE * r.enum SWIGTYPE * r.SWIGTYPE * SWIGTYPE * ----------------------------------------------------------------------------- */ SwigType *SwigType_default_deduce(const SwigType *t) { SwigType *r = NewStringEmpty(); List *l; Iterator it; int numitems; l = SwigType_split(t); numitems = Len(l); if (numitems >= 1) { String *last_subtype = Getitem(l, numitems-1); int is_enum = SwigType_isenum(last_subtype); if (numitems >=2 ) { String *subtype = Getitem(l, numitems-2); /* last but one */ if (SwigType_isarray(subtype)) { if (is_enum) { /* enum deduction, enum SWIGTYPE => SWIGTYPE */ Setitem(l, numitems-1, NewString("SWIGTYPE")); } else { /* array deduction, a(ANY). => a(). => p. */ String *deduced_subtype = 0; if (Strcmp(subtype, "a().") == 0) { deduced_subtype = NewString("p."); } else if (Strcmp(subtype, "a(ANY).") == 0) { deduced_subtype = NewString("a()."); } else { assert(0); } Setitem(l, numitems-2, deduced_subtype); } } else if (SwigType_ismemberpointer(subtype)) { /* member pointer deduction, m(CLASS). => p. */ Setitem(l, numitems-2, NewString("p.")); } else if (is_enum && !SwigType_isqualifier(subtype)) { /* enum deduction, enum SWIGTYPE => SWIGTYPE */ Setitem(l, numitems-1, NewString("SWIGTYPE")); } else { /* simple type deduction, eg, r.p.p. => r.p. */ /* also function pointers eg, p.f(ANY). => p. */ Delitem(l, numitems-2); } } else { if (is_enum) { /* enum deduction, enum SWIGTYPE => SWIGTYPE */ Setitem(l, numitems-1, NewString("SWIGTYPE")); } else { /* delete the only item, we are done with deduction */ Delitem(l, 0); } } } else { assert(0); } for (it = First(l); it.item; it = Next(it)) { Append(r, it.item); } if (Len(r) == 0) { Delete(r); r = 0; } Delete(l); return r; } /* ----------------------------------------------------------------------------- * SwigType_namestr() * * Returns a string of the base type. Takes care of template expansions * ----------------------------------------------------------------------------- */ String *SwigType_namestr(const SwigType *t) { String *r; String *suffix; List *p; int i, sz; char *d = Char(t); char *c = strstr(d, "<("); if (!c || !strstr(c + 2, ")>")) return NewString(t); r = NewStringWithSize(d, (int)(c - d)); if (*(c - 1) == '<') Putc(' ', r); Putc('<', r); p = SwigType_parmlist(c + 1); sz = Len(p); for (i = 0; i < sz; i++) { String *str = SwigType_str(Getitem(p, i), 0); /* Avoid creating a <: token, which is the same as [ in C++ - put a space after '<'. */ if (i == 0 && Len(str)) Putc(' ', r); Append(r, str); if ((i + 1) < sz) Putc(',', r); Delete(str); } Putc(' ', r); Putc('>', r); suffix = SwigType_templatesuffix(t); if (Len(suffix) > 0) { String *suffix_namestr = SwigType_namestr(suffix); Append(r, suffix_namestr); Delete(suffix_namestr); } else { Append(r, suffix); } Delete(suffix); Delete(p); return r; } /* ----------------------------------------------------------------------------- * SwigType_str() * * Create a C string representation of a datatype. * ----------------------------------------------------------------------------- */ String *SwigType_str(const SwigType *s, const_String_or_char_ptr id) { String *result; String *element = 0; String *nextelement; String *forwardelement; SwigType *member_function_qualifiers = 0; List *elements; int nelements, i; if (id) { /* Stringify the id expanding templates, for example when the id is a fully qualified class template name */ String *id_str = NewString(id); /* unfortunate copy due to current const limitations */ result = SwigType_str(id_str, 0); Delete(id_str); } else { result = NewStringEmpty(); } elements = SwigType_split(s); nelements = Len(elements); if (nelements > 0) { element = Getitem(elements, 0); } /* Now, walk the type list and start emitting */ for (i = 0; i < nelements; i++) { if (i < (nelements - 1)) { nextelement = Getitem(elements, i + 1); forwardelement = nextelement; if (SwigType_isqualifier(nextelement)) { if (i < (nelements - 2)) forwardelement = Getitem(elements, i + 2); } } else { nextelement = 0; forwardelement = 0; } if (SwigType_isqualifier(element)) { if (!member_function_qualifiers) { DOH *q = 0; q = SwigType_parm(element); Insert(result, 0, " "); Insert(result, 0, q); Delete(q); } } else if (SwigType_ispointer(element)) { Insert(result, 0, "*"); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } } else if (SwigType_ismemberpointer(element)) { String *q; q = SwigType_parm(element); Insert(result, 0, "::*"); Insert(result, 0, q); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } { String *next3elements = NewStringEmpty(); int j; for (j = i + 1; j < i + 4 && j < nelements; j++) { Append(next3elements, Getitem(elements, j)); } if (SwigType_isfunction(next3elements)) member_function_qualifiers = SwigType_pop_function_qualifiers(next3elements); Delete(next3elements); } Delete(q); } else if (SwigType_isreference(element)) { if (!member_function_qualifiers) Insert(result, 0, "&"); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } } else if (SwigType_isrvalue_reference(element)) { if (!member_function_qualifiers) Insert(result, 0, "&&"); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } } else if (SwigType_isvariadic(element)) { Insert(result, 0, "..."); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } } else if (SwigType_isarray(element)) { DOH *size; Append(result, "["); size = SwigType_parm(element); Append(result, size); Append(result, "]"); Delete(size); } else if (SwigType_isfunction(element)) { DOH *parms, *p; int j, plen; Append(result, "("); parms = SwigType_parmlist(element); plen = Len(parms); for (j = 0; j < plen; j++) { p = SwigType_str(Getitem(parms, j), 0); Append(result, p); if (j < (plen - 1)) Append(result, ","); } Append(result, ")"); if (member_function_qualifiers) { String *p = SwigType_str(member_function_qualifiers, 0); Append(result, " "); Append(result, p); Delete(p); Delete(member_function_qualifiers); member_function_qualifiers = 0; } Delete(parms); } else { if (strcmp(Char(element), "v(...)") == 0) { Insert(result, 0, "..."); } else { String *bs = SwigType_namestr(element); Insert(result, 0, " "); Insert(result, 0, bs); Delete(bs); } } element = nextelement; } Delete(elements); Chop(result); return result; } /* ----------------------------------------------------------------------------- * SwigType_ltype(const SwigType *ty) * * Create a locally assignable type * ----------------------------------------------------------------------------- */ SwigType *SwigType_ltype(const SwigType *s) { String *result; String *element; SwigType *td, *tc = 0; List *elements; int nelements, i; int firstarray = 1; int notypeconv = 0; int ignore_member_function_qualifiers = 0; result = NewStringEmpty(); tc = Copy(s); /* Nuke all leading qualifiers */ while (SwigType_isqualifier(tc)) { Delete(SwigType_pop(tc)); } if (SwigType_issimple(tc)) { /* Resolve any typedef definitions */ SwigType *tt = Copy(tc); td = 0; while ((td = SwigType_typedef_resolve(tt))) { if (td && (SwigType_isconst(td) || SwigType_isarray(td) || SwigType_isreference(td) || SwigType_isrvalue_reference(td))) { /* We need to use the typedef type */ Delete(tt); break; } else if (td) { Delete(tt); tt = td; } } if (td) { Delete(tc); tc = td; } } elements = SwigType_split(tc); nelements = Len(elements); /* Now, walk the type list and start emitting */ for (i = 0; i < nelements; i++) { element = Getitem(elements, i); /* when we see a function, we need to preserve the following types */ if (SwigType_isfunction(element)) { notypeconv = 1; ignore_member_function_qualifiers = 0; } if (ignore_member_function_qualifiers) { /* cv-qualifiers and ref-qualifiers up until the f() element have already been added */ } else if (SwigType_isqualifier(element)) { /* swallow cv-qualifiers */ } else if (SwigType_ispointer(element)) { Append(result, element); firstarray = 0; } else if (SwigType_ismemberpointer(element)) { Append(result, element); { String *next3elements = NewStringEmpty(); int j; for (j = i + 1; j < i + 4 && j < nelements; j++) { Append(next3elements, Getitem(elements, j)); } if (SwigType_isfunction(next3elements)) { SwigType *member_function_qualifiers = SwigType_pop_function_qualifiers(next3elements); /* compilers won't let us cast from a member function without qualifiers to one with qualifiers, so the qualifiers are kept in the ltype */ if (member_function_qualifiers) Append(result, member_function_qualifiers); Delete(member_function_qualifiers); ignore_member_function_qualifiers = 1; } Delete(next3elements); } firstarray = 0; } else if (SwigType_isreference(element)) { if (notypeconv) { Append(result, element); } else { Append(result, "p."); } firstarray = 0; } else if (SwigType_isrvalue_reference(element)) { if (notypeconv) { Append(result, element); } else { Append(result, "p."); } firstarray = 0; } else if (SwigType_isarray(element) && firstarray) { if (notypeconv) { Append(result, element); } else { Append(result, "p."); } firstarray = 0; } else if (SwigType_isenum(element)) { int anonymous_enum = (Cmp(element, "enum ") == 0); if (notypeconv || !anonymous_enum) { Append(result, element); } else { Append(result, "int"); } } else { Append(result, element); } } Delete(elements); Delete(tc); return result; } /* ----------------------------------------------------------------------------- * SwigType_lstr() * * Produces a type-string that is suitable as a lvalue in an expression. * That is, a type that can be freely assigned a value without violating * any C assignment rules. * * - Qualifiers such as 'const' and 'volatile' are stripped. * Except for member function cv-qualifiers and ref-qualifiers. * - Arrays are converted into a *single* pointer (i.e., * double [][] becomes double *). * - References are converted into a pointer. * - Typedef names that refer to read-only types will be replaced * with an equivalent assignable version. * -------------------------------------------------------------------- */ String *SwigType_lstr(const SwigType *s, const_String_or_char_ptr id) { String *result; SwigType *tc; tc = SwigType_ltype(s); result = SwigType_str(tc, id); Delete(tc); return result; } /* ----------------------------------------------------------------------------- * SwigType_rcaststr() * * Produces a casting string that maps the type returned by lstr() to the real * datatype printed by str(). * ----------------------------------------------------------------------------- */ String *SwigType_rcaststr(const SwigType *s, const_String_or_char_ptr name) { String *result, *cast; String *element = 0; String *nextelement; String *forwardelement; String *member_function_qualifiers = 0; SwigType *td, *tc = 0; const SwigType *rs; List *elements; int nelements, i; int clear = 1; int firstarray = 1; int isreference = 0; int isfunction = 0; result = NewStringEmpty(); if (SwigType_isconst(s)) { tc = Copy(s); Delete(SwigType_pop(tc)); if (SwigType_ismemberpointer(tc)) rs = s; else rs = tc; } else { rs = s; } if ((SwigType_isconst(rs) || SwigType_isarray(rs) || SwigType_isreference(rs) || SwigType_isrvalue_reference(rs))) { td = 0; } else { td = SwigType_typedef_resolve(rs); } if (td) { if ((SwigType_isconst(td) || SwigType_isarray(td) || SwigType_isreference(td) || SwigType_isrvalue_reference(td))) { elements = SwigType_split(td); } else { elements = SwigType_split(rs); } Delete(td); } else { elements = SwigType_split(rs); } nelements = Len(elements); if (nelements > 0) { element = Getitem(elements, 0); } /* Now, walk the type list and start emitting */ for (i = 0; i < nelements; i++) { if (i < (nelements - 1)) { nextelement = Getitem(elements, i + 1); forwardelement = nextelement; if (SwigType_isqualifier(nextelement)) { if (i < (nelements - 2)) forwardelement = Getitem(elements, i + 2); } } else { nextelement = 0; forwardelement = 0; } if (SwigType_isqualifier(element)) { if (!member_function_qualifiers) { DOH *q = 0; q = SwigType_parm(element); Insert(result, 0, " "); Insert(result, 0, q); Delete(q); clear = 0; } } else if (SwigType_ispointer(element)) { Insert(result, 0, "*"); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } firstarray = 0; } else if (SwigType_ismemberpointer(element)) { String *q; Insert(result, 0, "::*"); q = SwigType_parm(element); Insert(result, 0, q); if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } { String *next3elements = NewStringEmpty(); int j; for (j = i + 1; j < i + 4 && j < nelements; j++) { Append(next3elements, Getitem(elements, j)); } if (SwigType_isfunction(next3elements)) member_function_qualifiers = SwigType_pop_function_qualifiers(next3elements); Delete(next3elements); } firstarray = 0; Delete(q); } else if (SwigType_isreference(element)) { if (!member_function_qualifiers) { Insert(result, 0, "&"); if (!isfunction) isreference = 1; } if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } } else if (SwigType_isrvalue_reference(element)) { if (!member_function_qualifiers) { Insert(result, 0, "&&"); if (!isfunction) isreference = 1; } if ((forwardelement) && ((SwigType_isfunction(forwardelement) || (SwigType_isarray(forwardelement))))) { Insert(result, 0, "("); Append(result, ")"); } clear = 0; } else if (SwigType_isarray(element)) { DOH *size; if (firstarray && !isreference) { Append(result, "(*)"); firstarray = 0; } else { Append(result, "["); size = SwigType_parm(element); Append(result, size); Append(result, "]"); Delete(size); clear = 0; } } else if (SwigType_isfunction(element)) { DOH *parms, *p; int j, plen; Append(result, "("); parms = SwigType_parmlist(element); plen = Len(parms); for (j = 0; j < plen; j++) { p = SwigType_str(Getitem(parms, j), 0); Append(result, p); Delete(p); if (j < (plen - 1)) Append(result, ","); } Append(result, ")"); Delete(parms); if (member_function_qualifiers) { String *p = SwigType_str(member_function_qualifiers, 0); Append(result, " "); Append(result, p); Delete(p); Delete(member_function_qualifiers); member_function_qualifiers = 0; clear = 0; } isfunction = 1; } else { String *bs = SwigType_namestr(element); Insert(result, 0, " "); Insert(result, 0, bs); Delete(bs); } element = nextelement; } Delete(elements); if (clear) { cast = NewStringEmpty(); } else { cast = NewStringf("(%s)", result); } if (name) { if (isreference) { Append(cast, "*"); } Append(cast, name); } Delete(result); Delete(tc); return cast; } /* ----------------------------------------------------------------------------- * SwigType_lcaststr() * * Casts a variable from the real type to the local datatype. * ----------------------------------------------------------------------------- */ String *SwigType_lcaststr(const SwigType *s, const_String_or_char_ptr name) { String *result; result = NewStringEmpty(); if (name && Len(name) == 0) name = 0; if (SwigType_isarray(s)) { String *lstr = SwigType_lstr(s, 0); Printf(result, "(%s)%s", lstr, name); Delete(lstr); } else if (SwigType_isreference(s)) { String *str = SwigType_str(s, 0); Printf(result, "(%s)", str); Delete(str); if (name) Append(result, name); } else if (SwigType_isrvalue_reference(s)) { String *str = SwigType_str(s, 0); Printf(result, "(%s)", str); Delete(str); if (name) Append(result, name); } else if (SwigType_isqualifier(s)) { String *lstr = SwigType_lstr(s, 0); Printf(result, "(%s)%s", lstr, name); Delete(lstr); } else if (SwigType_ispointer(s)) { SwigType *sc = Copy(s); Delete(SwigType_pop(sc)); if (SwigType_isqualifier(sc) && name && !Equal(name, "0")) { /* Only cast const pointer values (remove const in cast - they are assigned to a non-const pointer type) */ SwigType_add_pointer(sc); String *lstr = SwigType_lstr(sc, 0); Printf(result, "(%s)%s", lstr, name); Delete(lstr); } else { if (name) Append(result, name); } Delete(sc); } else { if (name) Append(result, name); } return result; } #if 0 /* Alternative implementation for manglestr_default. Mangling is similar to the original except for a few subtle differences for example in templates: namespace foo { template class bar {}; typedef int Integer; void test2(bar *x); } Mangling is more consistent and changes from _p_foo__barT_int_p_t to _p_foo__barT_p_int_t. */ static void mangle_stringcopy(String *destination, const char *source, int count) { while (count-- > 0) { char newc = '_'; if (!(*source == '.' || *source == ':' || *source == ' ')) newc = *source; /* TODO: occasionally '*' or numerics need converting to '_', eg in array dimensions and template expressions */ Putc(newc, destination); source++; } } static void mangle_subtype(String *mangled, SwigType *s); /* ----------------------------------------------------------------------------- * mangle_namestr() * * Mangles a type taking care of template expansions. Similar to SwigType_namestr(). * The type may include a trailing '.', for example "p." * ----------------------------------------------------------------------------- */ static void mangle_namestr(String *mangled, SwigType *t) { int length = Len(t); if (SwigType_isqualifier(t)) { Append(mangled, "q_"); mangle_stringcopy(mangled, Char(t)+2, length-4); Append(mangled, "__"); } else if (SwigType_ismemberpointer(t)) { Append(mangled, "m_"); mangle_stringcopy(mangled, Char(t)+2, length-4); Append(mangled, "__"); } else if (SwigType_isarray(t)) { Append(mangled, "a_"); mangle_stringcopy(mangled, Char(t)+2, length-4); Append(mangled, "__"); } else if (SwigType_isfunction(t)) { List *p = SwigType_parmlist(t); int sz = Len(p); int i; Append(mangled, "f_"); for (i = 0; i < sz; i++) { mangle_subtype(mangled, Getitem(p, i)); Putc('_', mangled); } Append(mangled, (sz > 0) ? "_" : "__"); } else if (SwigType_isvarargs(t)) { Append(mangled, "___"); } else { char *d = Char(t); char *c = strstr(d, "<("); if (!c || !strstr(c + 2, ")>")) { /* not a template type */ mangle_stringcopy(mangled, Char(t), Len(t)); } else { /* a template type */ String *suffix; List *p; int i, sz; mangle_stringcopy(mangled, d, c-d); Putc('T', mangled); Putc('_', mangled); p = SwigType_parmlist(c + 1); sz = Len(p); for (i = 0; i < sz; i++) { mangle_subtype(mangled, Getitem(p, i)); Putc('_', mangled); } Putc('t', mangled); suffix = SwigType_templatesuffix(t); if (Len(suffix) > 0) { mangle_namestr(mangled, suffix); } else { Append(mangled, suffix); } Delete(suffix); Delete(p); } } } static void mangle_subtype(String *mangled, SwigType *s) { List *elements; int nelements, i; assert(s); elements = SwigType_split(s); nelements = Len(elements); for (i = 0; i < nelements; i++) { SwigType *element = Getitem(elements, i); mangle_namestr(mangled, element); } Delete(elements); } static String *manglestr_default(const SwigType *s) { String *mangled = NewString("_"); SwigType *sr = SwigType_typedef_resolve_all(s); SwigType *sq = SwigType_typedef_qualified(sr); SwigType *ss = SwigType_remove_global_scope_prefix(sq); SwigType *type = ss; SwigType *lt; if (SwigType_istemplate(ss)) { SwigType *ty = Swig_symbol_template_deftype(ss, 0); Delete(ss); ss = ty; type = ss; } lt = SwigType_ltype(type); Replace(lt, "struct ", "", DOH_REPLACE_ANY); Replace(lt, "class ", "", DOH_REPLACE_ANY); Replace(lt, "union ", "", DOH_REPLACE_ANY); Replace(lt, "enum ", "", DOH_REPLACE_ANY); mangle_subtype(mangled, lt); Delete(ss); Delete(sq); Delete(sr); return mangled; } #else static String *manglestr_default(const SwigType *s) { char *c; String *result = 0; String *base = 0; SwigType *lt; SwigType *sr = SwigType_typedef_resolve_all(s); SwigType *sq = SwigType_typedef_qualified(sr); SwigType *ss = SwigType_remove_global_scope_prefix(sq); SwigType *type = ss; if (SwigType_istemplate(ss)) { SwigType *dt = Swig_symbol_template_deftype(ss, 0); String *ty = Swig_symbol_type_qualify(dt, 0); Delete(dt); Delete(ss); ss = ty; type = ss; } lt = SwigType_ltype(type); result = SwigType_prefix(lt); base = SwigType_base(lt); c = Char(result); while (*c) { if (!isalnum((int) *c)) *c = '_'; c++; } if (SwigType_istemplate(base)) { String *b = SwigType_namestr(base); Delete(base); base = b; } Replace(base, "struct ", "", DOH_REPLACE_ANY); /* This might be problematic */ Replace(base, "class ", "", DOH_REPLACE_ANY); Replace(base, "union ", "", DOH_REPLACE_ANY); Replace(base, "enum ", "", DOH_REPLACE_ANY); c = Char(base); while (*c) { if (*c == '<') *c = 'T'; else if (*c == '>') *c = 't'; else if (*c == '*') *c = 'p'; else if (*c == '[') *c = 'a'; else if (*c == ']') *c = 'A'; else if (*c == '&') *c = 'R'; else if (*c == '(') *c = 'f'; else if (*c == ')') *c = 'F'; else if (!isalnum((int) *c)) *c = '_'; c++; } Append(result, base); Insert(result, 0, "_"); Delete(lt); Delete(base); Delete(ss); Delete(sq); Delete(sr); return result; } #endif String *SwigType_manglestr(const SwigType *s) { #if 0 /* Debugging checks to ensure a proper SwigType is passed in and not a stringified type */ String *angle = Strchr(s, '<'); if (angle && Strncmp(angle, "<(", 2) != 0) Printf(stderr, "SwigType_manglestr error: %s\n", s); else if (Strchr(s, '*') || Strchr(s, '&') || Strchr(s, '[')) Printf(stderr, "SwigType_manglestr error: %s\n", s); #endif return manglestr_default(s); } /* ----------------------------------------------------------------------------- * SwigType_typename_replace() * * Replaces a typename in a type with something else. Needed for templates. * Collapses duplicate const into a single const. * Reference collapsing probably should be implemented here. * Example: * t=r.q(const).T pat=T rep=int => r.q(const).int * t=r.q(const).T pat=T rep=q(const).int => r.q(const).int (duplicate const removed) * ----------------------------------------------------------------------------- */ void SwigType_typename_replace(SwigType *t, String *pat, String *rep) { String *nt; int i, ilen; List *elem; if (!Strstr(t, pat)) return; if (Equal(t, pat)) { Replace(t, pat, rep, DOH_REPLACE_ANY); return; } nt = NewStringEmpty(); elem = SwigType_split(t); ilen = Len(elem); for (i = 0; i < ilen; i++) { String *e = Getitem(elem, i); if (SwigType_issimple(e)) { if (Equal(e, pat)) { /* Replaces a type of the form 'pat' with 'rep' */ if (SwigType_isconst(rep) && i > 0 && SwigType_isconst(Getitem(elem, i - 1))) { /* Collapse duplicate const into a single const */ SwigType *rep_without_const = Copy(rep); Delete(SwigType_pop(rep_without_const)); Replace(e, pat, rep_without_const, DOH_REPLACE_ANY); Delete(rep_without_const); } else { Replace(e, pat, rep, DOH_REPLACE_ANY); } } else if (SwigType_istemplate(e)) { /* Replaces a type of the form 'pat' with 'rep' */ { /* To match "e=TemplateTemplateT<(float)>" * with "pat=TemplateTemplateT" * we need to compare only the first part of the string e. */ int len = Len(pat); /* Len(e) > len, not >= (because we expect at least a * character '<' following the template typename) */ if (Len(e) > len) { String *firstPartOfType = NewStringWithSize(e, len); const char* e_as_char = Char(e); if (Equal(firstPartOfType, pat) && e_as_char[len] == '<') { String *repbase = SwigType_templateprefix(rep); Replace(e, pat, repbase, DOH_REPLACE_ID | DOH_REPLACE_FIRST); Delete(repbase); } Delete(firstPartOfType); } } { String *tsuffix; List *tparms = SwigType_parmlist(e); int j, jlen; String *nt = SwigType_templateprefix(e); Append(nt, "<("); jlen = Len(tparms); for (j = 0; j < jlen; j++) { SwigType_typename_replace(Getitem(tparms, j), pat, rep); Append(nt, Getitem(tparms, j)); if (j < (jlen - 1)) Putc(',', nt); } tsuffix = SwigType_templatesuffix(e); SwigType_typename_replace(tsuffix, pat, rep); Printf(nt, ")>%s", tsuffix); Delete(tsuffix); Clear(e); Append(e, nt); Delete(nt); Delete(tparms); } } else if (Swig_scopename_check(e)) { String *first = 0; String *rest = 0; Swig_scopename_split(e, &first, &rest); /* Swig_scopename_split doesn't handle :: prefix very well ... could do with a rework */ if (Strncmp(rest, "::", 2) == 0) { String *tmp = NewString(Char(rest) + 2); Clear(rest); Printv(rest, tmp, NIL); Delete(tmp); assert(!first); } Clear(e); if (first) SwigType_typename_replace(first, pat, rep); SwigType_typename_replace(rest, pat, rep); Printv(e, first ? first : "", "::", rest, NIL); Delete(first); Delete(rest); } } else if (SwigType_isfunction(e)) { int j, jlen; List *fparms = SwigType_parmlist(e); Clear(e); Append(e, "f("); jlen = Len(fparms); for (j = 0; j < jlen; j++) { SwigType_typename_replace(Getitem(fparms, j), pat, rep); Append(e, Getitem(fparms, j)); if (j < (jlen - 1)) Putc(',', e); } Append(e, ")."); Delete(fparms); } else if (SwigType_isarray(e)) { Replace(e, pat, rep, DOH_REPLACE_ID); } Append(nt, e); } Clear(t); Append(t, nt); Delete(nt); Delete(elem); } /* ----------------------------------------------------------------------------- * SwigType_variadic_replace() * * Replaces variadic parameter with a list of (zero or more) parameters. * Needed for variadic templates. * ----------------------------------------------------------------------------- */ void SwigType_variadic_replace(SwigType *t, Parm *unexpanded_variadic_parm, ParmList *expanded_variadic_parms) { String *nt; int i, ilen; List *elem; if (!unexpanded_variadic_parm) return; if (SwigType_isvariadic(t)) { /* Based on expand_variadic_parms() but input is single SwigType (t) instead of ParmList */ String *unexpanded_name = Getattr(unexpanded_variadic_parm, "name"); ParmList *expanded = CopyParmList(expanded_variadic_parms); Parm *ep = expanded; SwigType *fparms; while (ep) { SwigType *newtype = Copy(t); SwigType_del_variadic(newtype); Replaceid(newtype, unexpanded_name, Getattr(ep, "type")); Setattr(ep, "type", newtype); ep = nextSibling(ep); } Clear(t); fparms = SwigType_function_parms_only(expanded); Append(t, fparms); Delete(expanded); return; } nt = NewStringEmpty(); elem = SwigType_split(t); ilen = Len(elem); for (i = 0; i < ilen; i++) { String *e = Getitem(elem, i); if (SwigType_isfunction(e)) { int j, jlen; List *fparms = SwigType_parmlist(e); Clear(e); Append(e, "f("); jlen = Len(fparms); for (j = 0; j < jlen; j++) { SwigType *type = Getitem(fparms, j); SwigType_variadic_replace(type, unexpanded_variadic_parm, expanded_variadic_parms); if (Len(type) > 0) { if (j != 0) Putc(',', e); Append(e, type); } else { assert(j == jlen - 1); /* A variadic parm was replaced with zero parms, variadic parms are only changed at the end of the list */ } } Append(e, ")."); Delete(fparms); } Append(nt, e); } Clear(t); Append(t, nt); Delete(nt); Delete(elem); } /* ----------------------------------------------------------------------------- * SwigType_remove_global_scope_prefix() * * Removes the unary scope operator (::) prefix indicating global scope in all * components of the type * ----------------------------------------------------------------------------- */ SwigType *SwigType_remove_global_scope_prefix(const SwigType *t) { SwigType *result; const char *type = Char(t); if (strncmp(type, "::", 2) == 0) type += 2; result = NewString(type); Replaceall(result, ".::", "."); Replaceall(result, "(::", "("); Replaceall(result, "enum ::", "enum "); return result; } /* ----------------------------------------------------------------------------- * SwigType_check_decl() * * Checks type declarators for a match * ----------------------------------------------------------------------------- */ int SwigType_check_decl(const SwigType *ty, const SwigType *decl) { SwigType *t, *t1, *t2; int r; t = SwigType_typedef_resolve_all(ty); t1 = SwigType_strip_qualifiers(t); t2 = SwigType_prefix(t1); r = Equal(t2, decl); Delete(t); Delete(t1); Delete(t2); return r == 1; } swig-4.4.0/Source/Modules/0000775000175000017500000000000015075470607015233 5ustar williamwilliamswig-4.4.0/Source/Modules/octave.cxx0000664000175000017500000014314215075443613017242 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * octave.cxx * * Octave language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" static String *global_name = 0; static String *op_prefix = 0; static const char *usage = "\ Octave Options (available with -octave)\n\ -globals - Set used to access C global variables [default: 'cvar']\n\ Use '.' to load C global variables into module namespace\n\ -opprefix - Prefix for global operator functions [default: 'op_']\n\ \n"; class OCTAVE:public Language { private: File *f_begin; File *f_runtime; File *f_header; File *f_doc; File *f_wrappers; File *f_init; File *f_initbeforefunc; File *f_directors; File *f_directors_h; String *s_global_tab; String *s_members_tab; String *class_name; int have_constructor; int have_destructor; String *constructor_name; Hash *docs; void Octave_begin_function(Node *n, File *f, const_String_or_char_ptr cname, const_String_or_char_ptr wname, bool dld) { if (dld) { String *tname = texinfo_name(n, "std::string()"); Printf(f, "SWIG_DEFUN( %s, %s, %s ) {", cname, wname, tname); } else { Printf(f, "static octave_value_list %s (const octave_value_list& args, int nargout) {", wname); } } public: OCTAVE(): f_begin(0), f_runtime(0), f_header(0), f_doc(0), f_wrappers(0), f_init(0), f_initbeforefunc(0), f_directors(0), f_directors_h(0), s_global_tab(0), s_members_tab(0), class_name(0), have_constructor(0), have_destructor(0), constructor_name(0), docs(0) { /* Add code to manage protected constructors and directors */ director_prot_ctor_code = NewString(""); Printv(director_prot_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " error(\"accessing abstract class or protected constructor\"); \n", " SWIG_fail;\n", "}\n", NIL); enable_cplus_runtime_mode(); allow_overloading(); director_multiple_inheritance = 1; directorLanguage(); docs = NewHash(); } virtual void main(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); } else if (strcmp(argv[i], "-globals") == 0) { if (argv[i + 1]) { global_name = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-opprefix") == 0) { if (argv[i + 1]) { op_prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-cppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is now always on.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is no longer supported.\n", argv[i]); Swig_mark_arg(i); Exit(EXIT_FAILURE); } } } if (!global_name) global_name = NewString("cvar"); if (!op_prefix) op_prefix = NewString("op_"); SWIG_library_directory("octave"); Preprocessor_define("SWIGOCTAVE 1", 0); SWIG_config_file("octave.swg"); allow_overloading(); // Octave API is C++, so output must be C++ compatible even when wrapping C code if (!cparse_cplusplus) Swig_cparse_cplusplusout(1); } virtual int top(Node *n) { { Node *mod = Getattr(n, "module"); if (mod) { Node *options = Getattr(mod, "options"); if (options) { int dirprot = 0; if (Getattr(options, "dirprot")) { dirprot = 1; } if (Getattr(options, "nodirprot")) { dirprot = 0; } if (Getattr(options, "directors")) { allow_directors(); if (dirprot) allow_dirprot(); } } } } String *module = Getattr(n, "name"); String *outfile = Getattr(n, "outfile"); f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_header = NewString(""); f_doc = NewString(""); f_wrappers = NewString(""); f_init = NewString(""); f_initbeforefunc = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); s_global_tab = NewString(""); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("header", f_header); Swig_register_filebyname("doc", f_doc); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("init", f_init); Swig_register_filebyname("initbeforefunc", f_initbeforefunc); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "OCTAVE"); Printf(f_runtime, "#define SWIG_name_d \"%s\"\n", module); Printf(f_runtime, "#define SWIG_name %s\n", module); Printf(f_runtime, "\n"); Printf(f_runtime, "#define SWIG_global_name \"%s\"\n", global_name); Printf(f_runtime, "#define SWIG_op_prefix \"%s\"\n", op_prefix); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Swig_banner(f_directors_h); if (dirprot_mode()) { // Printf(f_directors_h, "#include \n"); // Printf(f_directors_h, "#include \n\n"); } } Printf(f_runtime, "\n"); Printf(s_global_tab, "\nstatic const struct swig_octave_member swig_globals[] = {\n"); Printf(f_init, "static bool SWIG_init_user(octave_swig_type* module_ns)\n{\n"); if (!CPlusPlus) Printf(f_header,"extern \"C\" {\n"); Language::top(n); if (!CPlusPlus) Printf(f_header,"}\n"); if (Len(docs)) emit_doc_texinfo(); if (Swig_directors_enabled()) { Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } Printf(f_init, "return true;\n}\n"); Printf(s_global_tab, "{0,0,0,0,0,0}\n};\n"); Printv(f_wrappers, s_global_tab, NIL); SwigType_emit_type_table(f_runtime, f_wrappers); Dump(f_runtime, f_begin); Dump(f_header, f_begin); Dump(f_doc, f_begin); if (Swig_directors_enabled()) { Dump(f_directors_h, f_begin); Dump(f_directors, f_begin); } Dump(f_wrappers, f_begin); Dump(f_initbeforefunc, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(s_global_tab); Delete(f_initbeforefunc); Delete(f_init); Delete(f_wrappers); Delete(f_doc); Delete(f_header); Delete(f_directors); Delete(f_directors_h); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } String *texinfo_escape(String *_s) { const char* s=(const char*)Data(_s); while (*s&&(*s=='\t'||*s=='\r'||*s=='\n'||*s==' ')) ++s; String *r = NewString(""); for (int j=0;s[j];++j) { if (s[j] == '\n') { Append(r, "\\n\\\n"); } else if (s[j] == '\r') { Append(r, "\\r"); } else if (s[j] == '\t') { Append(r, "\\t"); } else if (s[j] == '\\') { Append(r, "\\\\"); } else if (s[j] == '\'') { Append(r, "\\\'"); } else if (s[j] == '\"') { Append(r, "\\\""); } else Putc(s[j], r); } return r; } void emit_doc_texinfo() { for (Iterator it = First(docs); it.key; it = Next(it)) { String *wrap_name = it.key; String *synopsis = Getattr(it.item, "synopsis"); String *decl_info = Getattr(it.item, "decl_info"); String *cdecl_info = Getattr(it.item, "cdecl_info"); String *args_info = Getattr(it.item, "args_info"); String *doc_str = NewString(""); Printv(doc_str, synopsis, decl_info, cdecl_info, args_info, NIL); String *escaped_doc_str = texinfo_escape(doc_str); if (Len(doc_str)>0) { Printf(f_doc,"static const char* %s_texinfo = ",wrap_name); Printf(f_doc,"\"-*- texinfo -*-\\n\\\n%s", escaped_doc_str); if (Len(decl_info)) Printf(f_doc,"\\n\\\n@end deftypefn"); Printf(f_doc,"\";\n"); } Delete(escaped_doc_str); Delete(doc_str); Delete(wrap_name); } Printf(f_doc,"\n"); } bool is_empty_doc_node(Node* n) { if (!n) return true; String *synopsis = Getattr(n, "synopsis"); String *decl_info = Getattr(n, "decl_info"); String *cdecl_info = Getattr(n, "cdecl_info"); String *args_info = Getattr(n, "args_info"); return !Len(synopsis) && !Len(decl_info) && !Len(cdecl_info) && !Len(args_info); } String *texinfo_name(Node* n, const char* defval = "0") { String *tname = NewString(""); String *iname = Getattr(n, "sym:name"); String *wname = Swig_name_wrapper(iname); Node* d = Getattr(docs, wname); if (is_empty_doc_node(d)) Printf(tname, defval); else Printf(tname, "%s_texinfo", wname); return tname; } void process_autodoc(Node *n) { String *iname = Getattr(n, "sym:name"); String *name = Getattr(n, "name"); String *wname = Swig_name_wrapper(iname); String *str = Getattr(n, "feature:docstring"); bool autodoc_enabled = !Cmp(Getattr(n, "feature:autodoc"), "1"); Node* d = Getattr(docs, wname); if (!d) { d = NewHash(); Setattr(d, "synopsis", NewString("")); Setattr(d, "decl_info", NewString("")); Setattr(d, "cdecl_info", NewString("")); Setattr(d, "args_info", NewString("")); Setattr(docs, wname, d); } String *synopsis = Getattr(d, "synopsis"); String *decl_info = Getattr(d, "decl_info"); // String *cdecl_info = Getattr(d, "cdecl_info"); String *args_info = Getattr(d, "args_info"); // * couldn't we just emit the docs here? if (autodoc_enabled) { String *decl_str = NewString(""); String *args_str = NewString(""); make_autodocParmList(n, decl_str, args_str); Append(decl_info, "@deftypefn {Loadable Function} "); SwigType *type = Getattr(n, "type"); if (type && Strcmp(type, "void")) { Node *nn = classLookup(Getattr(n, "type")); String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0); Append(decl_info, "@var{retval} = "); Printf(args_str, "%s@var{retval} is of type %s. ", args_str, type_str); Delete(type_str); } Append(decl_info, name); Append(decl_info, " ("); Append(decl_info, decl_str); Append(decl_info, ")\n"); Append(args_info, args_str); Delete(decl_str); Delete(args_str); } if (str && Len(str) > 0) { // strip off {} if necessary char *t = Char(str); if (*t == '{') { Delitem(str, 0); Delitem(str, DOH_END); } // emit into synopsis section Append(synopsis, str); } } virtual int importDirective(Node *n) { String *modname = Getattr(n, "module"); if (modname) Printf(f_init, "if (!SWIG_Octave_LoadModule(\"%s\")) return false;\n", modname); return Language::importDirective(n); } const char *get_implicitconv_flag(Node *n) { int conv = 0; if (n && GetFlag(n, "feature:implicitconv")) { conv = 1; } return conv ? "SWIG_POINTER_IMPLICIT_CONV" : "0"; } /* ----------------------------------------------------------------------------- * addMissingParameterNames() * For functions that have not had nameless parameters set in the Language class. * * Inputs: * plist - entire parameter list * arg_offset - argument number for first parameter * Side effects: * The "lname" attribute in each parameter in plist will be contain a parameter name * ----------------------------------------------------------------------------- */ void addMissingParameterNames(Node* n, ParmList *plist, int arg_offset) { Parm *p = plist; int i = arg_offset; while (p) { if (!Getattr(p, "lname")) { String *name = makeParameterName(n, p, i); Setattr(p, "lname", name); Delete(name); } i++; p = nextSibling(p); } } void make_autodocParmList(Node *n, String *decl_str, String *args_str) { String *pdocs = 0; ParmList *plist = CopyParmList(Getattr(n, "parms")); Parm *p; Parm *pnext; int arg_num = is_wrapping_class() ? 1 : 0; addMissingParameterNames(n, plist, arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms Swig_typemap_attach_parms("in", plist, 0); Swig_typemap_attach_parms("doc", plist, 0); for (p = plist; p; p = pnext, arg_num++) { String *tm = Getattr(p, "tmap:in"); if (tm) { pnext = Getattr(p, "tmap:in:next"); if (checkAttribute(p, "tmap:in:numinputs", "0")) { continue; } } else { pnext = nextSibling(p); } String *name = 0; String *type = 0; String *value = 0; String *pdoc = Getattr(p, "tmap:doc"); if (pdoc) { name = Getattr(p, "tmap:doc:name"); type = Getattr(p, "tmap:doc:type"); value = Getattr(p, "tmap:doc:value"); } String *made_name = 0; if (!name) { name = made_name = makeParameterName(n, p, arg_num); } type = type ? type : Getattr(p, "type"); value = value ? value : Getattr(p, "value"); if (SwigType_isvarargs(type)) break; String *tex_name = NewString(""); if (name) Printf(tex_name, "@var{%s}", name); else Printf(tex_name, "@var{?}"); if (Len(decl_str)) Append(decl_str, ", "); Append(decl_str, tex_name); if (value) { String *new_value = convertValue(value, Getattr(p, "numval"), Getattr(p, "stringval"), Getattr(p, "type")); if (new_value) { value = new_value; } else { Node *lookup = Swig_symbol_clookup(value, 0); if (lookup) value = Getattr(lookup, "sym:name"); } Printf(decl_str, " = %s", value); } Node *nn = classLookup(Getattr(p, "type")); String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0); Printf(args_str, "%s is of type %s. ", tex_name, type_str); Delete(type_str); Delete(tex_name); Delete(made_name); } if (pdocs) Setattr(n, "feature:pdocs", pdocs); Delete(plist); } /* ------------------------------------------------------------ * convertValue() * Check if string v can be an Octave value literal, * (eg. number or string), or translate it to an Octave literal. * ------------------------------------------------------------ */ String *convertValue(String *v, String *numval, String *stringval, SwigType *t) { if (stringval) { return stringval; } if (numval) { if (SwigType_type(t) == T_BOOL) { return NewString(*Char(numval) == '0' ? "false" : "true"); } return numval; } if (Equal(v, "0") || Equal(v, "NULL") || Equal(v, "nullptr")) return SwigType_ispointer(t) ? NewString("None") : NewString("0"); // FIXME: TRUE and FALSE are not standard and could be defined in other ways if (Equal(v, "TRUE")) return NewString("true"); if (Equal(v, "FALSE")) return NewString("false"); return 0; } virtual int functionWrapper(Node *n) { Parm *p; String *tm; int j; String *nodeType = Getattr(n, "nodeType"); int constructor = (!Cmp(nodeType, "constructor")); int destructor = (!Cmp(nodeType, "destructor")); String *storage = Getattr(n, "storage"); bool overloaded = !!Getattr(n, "sym:overloaded"); bool last_overload = overloaded && !Getattr(n, "sym:nextSibling"); String *iname = Getattr(n, "sym:name"); String *wname = Swig_name_wrapper(iname); String *overname = Copy(wname); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); if (!overloaded && !addSymbol(iname, n)) return SWIG_ERROR; if (overloaded) Append(overname, Getattr(n, "sym:overname")); if (!overloaded || last_overload) process_autodoc(n); Wrapper *f = NewWrapper(); Octave_begin_function(n, f->def, iname, overname, !overloaded); // Start default try block to execute // cleanup code if exception is thrown Printf(f->code, "try {\n"); emit_parameter_variables(l, f); emit_attach_parmmaps(l, f); Setattr(n, "wrap:parms", l); int num_arguments = emit_num_arguments(l); int num_required = emit_num_required(l); int varargs = emit_isvarargs(l); char source[64]; Printf(f->code, "if (!SWIG_check_num_args(\"%s\",args.length(),%i,%i,%i)) " "{\n SWIG_fail;\n }\n", iname, num_arguments, num_required, varargs); if (constructor && num_arguments == 1 && num_required == 1) { if (Cmp(storage, "explicit") == 0) { Node *parent = Swig_methodclass(n); if (GetFlag(parent, "feature:implicitconv")) { String *desc = NewStringf("SWIGTYPE%s", SwigType_manglestr(Getattr(n, "type"))); Printf(f->code, "if (SWIG_CheckImplicit(%s)) SWIG_fail;\n", desc); Delete(desc); } } } for (j = 0, p = l; j < num_arguments; ++j) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *tm = Getattr(p, "tmap:in"); if (tm) { if (!tm || checkAttribute(p, "tmap:in:numinputs", "0")) { p = nextSibling(p); continue; } sprintf(source, "args(%d)", j); Setattr(p, "emit:input", source); Replaceall(tm, "$input", Getattr(p, "emit:input")); if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } if (Getattr(p, "tmap:in:implicitconv")) { const char *convflag = "0"; if (!Getattr(p, "hidden")) { SwigType *ptype = Getattr(p, "type"); convflag = get_implicitconv_flag(classLookup(ptype)); } Replaceall(tm, "$implicitconv", convflag); Setattr(p, "implicitconv", convflag); } String *getargs = NewString(""); if (j >= num_required) Printf(getargs, "if (%dcode, getargs, "\n", NIL); Delete(getargs); p = Getattr(p, "tmap:in:next"); continue; } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); break; } } // Check for trailing varargs if (varargs) { if (p && (tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", "varargs"); Printv(f->code, tm, "\n", NIL); } } // Insert constraint checking code for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } // Insert cleanup code String *cleanup = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { if (Getattr(p, "tmap:freearg:implicitconv")) { const char *convflag = "0"; if (!Getattr(p, "hidden")) { SwigType *ptype = Getattr(p, "type"); convflag = get_implicitconv_flag(classLookup(ptype)); } if (strcmp(convflag, "0") == 0) { tm = 0; } } if (tm && (Len(tm) != 0)) { Printv(cleanup, tm, "\n", NIL); } p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } // Insert argument output code String *outarg = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { Replaceall(tm, "$result", "_outp"); Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } int director_method = is_member_director(n) && !is_smart_pointer() && !destructor; if (director_method) { Wrapper_add_local(f, "upcall", "bool upcall = false"); Append(f->code, "upcall = !!dynamic_cast(arg1);\n"); } Setattr(n, "wrap:name", overname); Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); Wrapper_add_local(f, "_out", "octave_value_list _out"); Wrapper_add_local(f, "_outp", "octave_value_list *_outp=&_out"); Wrapper_add_local(f, "_outv", "octave_value _outv"); // Return the function value if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { Replaceall(tm, "$result", "_outv"); if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "1"); else Replaceall(tm, "$owner", "0"); Printf(f->code, "%s\n", tm); Printf(f->code, "if (_outv.is_defined()) _outp = " "SWIG_Octave_AppendOutput(_outp, _outv);\n"); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), iname); } emit_return_variable(n, returntype, f); Printv(f->code, outarg, NIL); Printv(f->code, cleanup, NIL); if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } } if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Replaceall(tm, "$result", "_outv"); Printf(f->code, "%s\n", tm); Delete(tm); } Printf(f->code, "return _out;\n"); // Execute cleanup code if branched to fail: label Printf(f->code, "fail:\n"); Printv(f->code, cleanup, NIL); Printf(f->code, "return octave_value_list();\n"); // Execute cleanup code if exception was thrown Printf(f->code, "}\n"); Printf(f->code, "catch(...) {\n"); Printv(f->code, cleanup, NIL); Printf(f->code, "throw;\n"); Printf(f->code, "}\n"); // End wrapper function Printf(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(f->code, "$symname", iname); Wrapper_print(f, f_wrappers); DelWrapper(f); if (last_overload) dispatchFunction(n); if (!overloaded || last_overload) { String *tname = texinfo_name(n); Printf(s_global_tab, "{\"%s\",%s,0,0,2,%s},\n", iname, wname, tname); Delete(tname); } Delete(overname); Delete(wname); Delete(cleanup); Delete(outarg); return SWIG_OK; } void dispatchFunction(Node *n) { Wrapper *f = NewWrapper(); String *iname = Getattr(n, "sym:name"); String *wname = Swig_name_wrapper(iname); int maxargs; bool check_emitted = false; String *dispatch = Swig_overload_dispatch(n, "return %s(args, nargout);", &maxargs, &check_emitted); String *tmp = NewString(""); Octave_begin_function(n, f->def, iname, wname, true); Wrapper_add_local(f, "argc", "int argc = args.length()"); if (maxargs > 0 && check_emitted) { Printf(tmp, "octave_value_ref argv[%d]={", maxargs); for (int j = 0; j < maxargs; ++j) Printf(tmp, "%soctave_value_ref(args,%d)", j ? "," : " ", j); Printf(tmp, "}"); Wrapper_add_local(f, "argv", tmp); } Printv(f->code, dispatch, "\n", NIL); Printf(f->code, "error(\"No matching function for overload\");\n"); Printf(f->code, "return octave_value_list();\n"); Printv(f->code, "}\n", NIL); Wrapper_print(f, f_wrappers); Delete(tmp); DelWrapper(f); Delete(dispatch); Delete(wname); } virtual int variableWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); if (!addSymbol(iname, n)) return SWIG_ERROR; String *tm; Wrapper *getf = NewWrapper(); Wrapper *setf = NewWrapper(); String *getname = Swig_name_get(NSPACE_TODO, iname); String *setname = Swig_name_set(NSPACE_TODO, iname); String *getwname = Swig_name_wrapper(getname); String *setwname = Swig_name_wrapper(setname); Octave_begin_function(n, setf->def, setname, setwname, true); Printf(setf->code, "if (!SWIG_check_num_args(\"%s_set\",args.length(),1,1,0)) return octave_value_list();\n", iname); if (!is_immutable(n)) { Setattr(n, "wrap:name", setname); if ((tm = Swig_typemap_lookup("varin", n, name, 0))) { Replaceall(tm, "$input", "args(0)"); if (Getattr(n, "tmap:varin:implicitconv")) { Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); } emit_action_code(n, setf->code, tm); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0)); } Append(setf->code, "return octave_value_list();\n"); Append(setf->code, "fail:\n"); Append(setf->code, "return octave_value_list();\n"); } else { Printf(setf->code, "return octave_set_immutable(args,nargout);"); } Append(setf->code, "}\n"); Wrapper_print(setf, f_wrappers); Setattr(n, "wrap:name", getname); int addfail = 0; Octave_begin_function(n, getf->def, getname, getwname, true); Wrapper_add_local(getf, "obj", "octave_value obj"); if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { Replaceall(tm, "$result", "obj"); addfail = emit_action_code(n, getf->code, tm); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); } Append(getf->code, "return obj;\n"); if (addfail) { Append(getf->code, "fail:\n"); Append(getf->code, "return octave_value_list();\n"); } Append(getf->code, "}\n"); Wrapper_print(getf, f_wrappers); Printf(s_global_tab, "{\"%s\",0,%s,%s,2,0},\n", iname, getwname, setwname); Delete(getwname); Delete(setwname); DelWrapper(setf); DelWrapper(getf); return SWIG_OK; } virtual int constantWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *cppvalue = Getattr(n, "cppvalue"); String *tm; if (!addSymbol(iname, n)) return SWIG_ERROR; if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(iname); String *str = SwigType_str(type, wname); Printf(f_header, "static %s = %s;\n", str, value); Delete(str); value = wname; } if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) { Replaceall(tm, "$value", cppvalue ? cppvalue : value); Replaceall(tm, "$nsname", iname); Printf(f_init, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); return SWIG_NOWRAP; } return SWIG_OK; } virtual int nativeWrapper(Node *n) { return Language::nativeWrapper(n); } virtual int enumDeclaration(Node *n) { return Language::enumDeclaration(n); } virtual int enumvalueDeclaration(Node *n) { return Language::enumvalueDeclaration(n); } virtual int classDeclaration(Node *n) { return Language::classDeclaration(n); } virtual int classHandler(Node *n) { have_constructor = 0; have_destructor = 0; constructor_name = 0; class_name = Getattr(n, "sym:name"); if (!addSymbol(class_name, n)) return SWIG_ERROR; // This is a bug, due to the fact that swig_type -> octave_class mapping // is 1-to-n. static Hash *emitted = NewHash(); String *mangled_classname = Swig_name_mangle_type(Getattr(n, "name")); if (Getattr(emitted, mangled_classname)) { Delete(mangled_classname); return SWIG_NOWRAP; } Setattr(emitted, mangled_classname, "1"); Delete(mangled_classname); assert(!s_members_tab); s_members_tab = NewString(""); Printv(s_members_tab, "static swig_octave_member swig_", class_name, "_members[] = {\n", NIL); Language::classHandler(n); SwigType *t = Copy(Getattr(n, "name")); SwigType_add_pointer(t); // Replace storing a pointer to underlying class with a smart pointer (intended for use with non-intrusive smart pointers) SwigType *smart = Getattr(n, "smart"); String *wrap_class = NewStringf("&_wrap_class_%s", class_name); if (smart) { SwigType *psmart = Copy(smart); SwigType_add_pointer(psmart); SwigType_remember_clientdata(psmart, wrap_class); Delete(psmart); } SwigType_remember_clientdata(t, wrap_class); int use_director = Swig_directorclass(n); if (use_director) { String *nspace = Getattr(n, "sym:nspace"); String *cname = Swig_name_disown(nspace, class_name); String *wcname = Swig_name_wrapper(cname); String *cnameshdw = NewStringf("%s_shadow", cname); String *wcnameshdw = Swig_name_wrapper(cnameshdw); Octave_begin_function(n, f_wrappers, cnameshdw, wcnameshdw, true); Printf(f_wrappers, " if (args.length()!=1) {\n"); Printf(f_wrappers, " error(\"disown takes no arguments\");\n"); Printf(f_wrappers, " return octave_value_list();\n"); Printf(f_wrappers, " }\n"); Printf(f_wrappers, " %s (args, nargout);\n", wcname); Printf(f_wrappers, " return args;\n"); Printf(f_wrappers, "}\n"); Printf(s_members_tab, "{\"__disown\",%s,0,0,0,0},\n", wcnameshdw); Delete(wcname); Delete(cname); Delete(wcnameshdw); Delete(cnameshdw); } Printf(s_members_tab, "{0,0,0,0,0,0}\n};\n"); Printv(f_wrappers, s_members_tab, NIL); String *base_class_names = NewString(""); String *base_class = NewString(""); List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator b; b = First(baselist); while (b.item) { String *bname = Getattr(b.item, "name"); if ((!bname) || GetFlag(b.item, "feature:ignore") || (!Getattr(b.item, "module"))) { b = Next(b); continue; } String *bname_mangled = SwigType_manglestr(SwigType_add_pointer(Copy(bname))); Printf(base_class_names, "\"%s\",", bname_mangled); Printf(base_class, "0,"); b = Next(b); Delete(bname_mangled); } } Printv(f_wrappers, "static const char *swig_", class_name, "_base_names[] = {", base_class_names, "0};\n", NIL); Printv(f_wrappers, "static const swig_type_info *swig_", class_name, "_base[] = {", base_class, "0};\n", NIL); Printv(f_wrappers, "static swig_octave_class _wrap_class_", class_name, " = {\"", class_name, "\", &SWIGTYPE", SwigType_manglestr(t), ",", NIL); Printv(f_wrappers, Swig_directorclass(n) ? "1," : "0,", NIL); if (have_constructor) { String *nspace = Getattr(n, "sym:nspace"); String *cname = Swig_name_construct(nspace, constructor_name); String *wcname = Swig_name_wrapper(cname); String *tname = texinfo_name(n); Printf(f_wrappers, "%s,%s,", wcname, tname); Delete(tname); Delete(wcname); Delete(cname); } else Printv(f_wrappers, "0,0,", NIL); if (have_destructor) { String *nspace = Getattr(n, "sym:nspace"); String *cname = Swig_name_destroy(nspace, class_name); String *wcname = Swig_name_wrapper(cname); Printf(f_wrappers, "%s,", wcname); Delete(wcname); Delete(cname); } else Printv(f_wrappers, "0", ",", NIL); Printf(f_wrappers, "swig_%s_members,swig_%s_base_names,swig_%s_base };\n\n", class_name, class_name, class_name); Delete(base_class); Delete(base_class_names); Delete(t); Delete(s_members_tab); s_members_tab = 0; class_name = 0; return SWIG_OK; } virtual int memberfunctionHandler(Node *n) { Language::memberfunctionHandler(n); assert(s_members_tab); assert(class_name); String *name = Getattr(n, "name"); String *iname = GetChar(n, "sym:name"); String *realname = iname ? iname : name; String *wname = Getattr(n, "wrap:name"); assert(wname); if (!Getattr(n, "sym:nextSibling")) { String *tname = texinfo_name(n); String *rname = Copy(wname); bool overloaded = !!Getattr(n, "sym:overloaded"); if (overloaded) Delslice(rname, Len(rname) - Len(Getattr(n, "sym:overname")), DOH_END); Printf(s_members_tab, "{\"%s\",%s,0,0,0,%s},\n", realname, rname, tname); Delete(rname); Delete(tname); } return SWIG_OK; } virtual int membervariableHandler(Node *n) { Setattr(n, "feature:autodoc", "0"); Language::membervariableHandler(n); assert(s_members_tab); assert(class_name); String *symname = Getattr(n, "sym:name"); String *getname = Swig_name_get(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)); String *setname = Swig_name_set(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)); String *getwname = Swig_name_wrapper(getname); String *setwname = GetFlag(n, "feature:immutable") ? NewString("octave_set_immutable") : Swig_name_wrapper(setname); assert(s_members_tab); Printf(s_members_tab, "{\"%s\",0,%s,%s,0,0},\n", symname, getwname, setwname); Delete(getname); Delete(setname); Delete(getwname); Delete(setwname); return SWIG_OK; } virtual int constructorHandler(Node *n) { have_constructor = 1; if (!constructor_name) constructor_name = NewString(Getattr(n, "sym:name")); int use_director = Swig_directorclass(n); if (use_director) { Parm *parms = Getattr(n, "parms"); Parm *self; String *name = NewString("self"); String *type = NewString("void"); SwigType_add_pointer(type); self = NewParm(type, name, n); Delete(type); Delete(name); Setattr(self, "lname", "self_obj"); if (parms) set_nextSibling(self, parms); Setattr(n, "parms", self); Setattr(n, "wrap:self", "1"); Setattr(n, "hidden", "1"); Delete(self); } return Language::constructorHandler(n); } virtual int destructorHandler(Node *n) { have_destructor = 1; return Language::destructorHandler(n); } virtual int staticmemberfunctionHandler(Node *n) { Language::staticmemberfunctionHandler(n); assert(s_members_tab); assert(class_name); String *name = Getattr(n, "name"); String *iname = GetChar(n, "sym:name"); String *realname = iname ? iname : name; String *wname = Getattr(n, "wrap:name"); assert(wname); if (!Getattr(n, "sym:nextSibling")) { String *tname = texinfo_name(n); String *rname = Copy(wname); bool overloaded = !!Getattr(n, "sym:overloaded"); if (overloaded) Delslice(rname, Len(rname) - Len(Getattr(n, "sym:overname")), DOH_END); Printf(s_members_tab, "{\"%s\",%s,0,0,1,%s},\n", realname, rname, tname); Delete(rname); Delete(tname); } return SWIG_OK; } virtual int memberconstantHandler(Node *n) { return Language::memberconstantHandler(n); } virtual int staticmembervariableHandler(Node *n) { Setattr(n, "feature:autodoc", "0"); Language::staticmembervariableHandler(n); if (!GetFlag(n, "wrappedasconstant")) { assert(s_members_tab); assert(class_name); String *symname = Getattr(n, "sym:name"); String *getname = Swig_name_get(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)); String *setname = Swig_name_set(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)); String *getwname = Swig_name_wrapper(getname); String *setwname = GetFlag(n, "feature:immutable") ? NewString("octave_set_immutable") : Swig_name_wrapper(setname); assert(s_members_tab); Printf(s_members_tab, "{\"%s\",0,%s,%s,1,0},\n", symname, getwname, setwname); Delete(getname); Delete(setname); Delete(getwname); Delete(setwname); } return SWIG_OK; } int classDirectorInit(Node *n) { String *declaration = Swig_director_declaration(n); Printf(f_directors_h, "\n"); Printf(f_directors_h, "%s\n", declaration); Printf(f_directors_h, "public:\n"); Delete(declaration); return Language::classDirectorInit(n); } int classDirectorEnd(Node *n) { Printf(f_directors_h, "};\n\n"); return Language::classDirectorEnd(n); } int classDirectorConstructor(Node *n) { Node *parent = Getattr(n, "parentNode"); String *sub = NewString(""); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *classname = NewString(""); Printf(classname, "SwigDirector_%s", supername); // insert self parameter Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("void"); SwigType_add_pointer(type); p = NewParm(type, NewString("self"), n); set_nextSibling(p, parms); parms = p; if (!Getattr(n, "defaultargs")) { // constructor { Wrapper *w = NewWrapper(); String *call; String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, classname, parms, 0); call = Swig_csuperclass_call(0, basetype, superparms); Printf(w->def, "%s::%s: %s," "\nSwig::Director(static_cast<%s*>(this)) { \n", classname, target, call, basetype); Append(w->def, "}\n"); Delete(target); Wrapper_print(w, f_directors); Delete(call); DelWrapper(w); } // constructor header { String *target = Swig_method_decl(0, decl, classname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(classname); Delete(supername); Delete(parms); return Language::classDirectorConstructor(n); } int classDirectorDefaultConstructor(Node *n) { String *classname = Swig_class_name(n); { Wrapper *w = NewWrapper(); Printf(w->def, "SwigDirector_%s::SwigDirector_%s(void* self) :" "\nSwig::Director((octave_swig_type*)self,static_cast<%s*>(this)) { \n", classname, classname, classname); Append(w->def, "}\n"); Wrapper_print(w, f_directors); DelWrapper(w); } Printf(f_directors_h, " SwigDirector_%s(octave_swig_type* self);\n", classname); Delete(classname); return Language::classDirectorDefaultConstructor(n); } int classDirectorMethod(Node *n, Node *parent, String *super) { int is_void = 0; int is_pointer = 0; String *decl = Getattr(n, "decl"); SwigType *returntype = Getattr(n, "type"); String *name = Getattr(n, "name"); String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *symname = Getattr(n, "sym:name"); String *declaration = NewString(""); ParmList *l = Getattr(n, "parms"); Wrapper *w = NewWrapper(); String *tm; String *wrap_args = NewString(""); String *value = Getattr(n, "value"); String *storage = Getattr(n, "storage"); bool pure_virtual = false; int status = SWIG_OK; int idx; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { pure_virtual = true; } } // determine if the method returns a pointer is_pointer = SwigType_ispointer_return(decl); is_void = (!Cmp(returntype, "void") && !is_pointer); // virtual method definition String *target; String *pclassname = NewStringf("SwigDirector_%s", classname); String *qualified_name = NewStringf("%s::%s", pclassname, name); SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); // header declaration target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Get any exception classes in the throws typemap if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = 0; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { Parm *p; int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } String *str = SwigType_str(Getattr(p, "type"), 0); Append(w->def, str); Append(declaration, str); Delete(str); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); // declare method return value // if the return value is a reference or const reference, a specialized typemap must // handle it, including declaration of c_result ($result). if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (ignored_method) { if (!pure_virtual) { if (!is_void) Printf(w->code, "return "); String *super_call = Swig_method_call(super, l); Printf(w->code, "%s;\n", super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); } } else { // attach typemaps to arguments (C/C++ -> Octave) String *parse_args = NewString(""); Swig_director_parms_fixup(l); Swig_typemap_attach_parms("in", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("directorargout", l, w); Parm *p; int outputs = 0; if (!is_void) outputs++; // build argument list and type conversion string p = l; while (p) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } if (Getattr(p, "tmap:directorargout") != 0) outputs++; String *pname = Getattr(p, "name"); String *ptype = Getattr(p, "type"); Wrapper_add_local(w, "tmpv", "octave_value tmpv"); if ((tm = Getattr(p, "tmap:directorin")) != 0) { String *parse = Getattr(p, "tmap:directorin:parse"); if (!parse) { Setattr(p, "emit:directorinput", "tmpv"); Replaceall(tm, "$input", "tmpv"); Replaceall(tm, "$owner", "0"); Printv(wrap_args, tm, "\n", NIL); Printf(wrap_args, "args.append(tmpv);\n"); Putc('O', parse_args); } else { Append(parse_args, parse); Setattr(p, "emit:directorinput", pname); Replaceall(tm, "$input", pname); Replaceall(tm, "$owner", "0"); if (Len(tm) == 0) Append(tm, pname); } p = Getattr(p, "tmap:directorin:next"); continue; } else if (Cmp(ptype, "void")) { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_NOWRAP; break; } p = nextSibling(p); } String *method_name = Getattr(n, "sym:name"); Printv(w->code, wrap_args, NIL); // emit method invocation Wrapper_add_local(w, "args", "octave_value_list args"); Wrapper_add_local(w, "out", "octave_value_list out"); Wrapper_add_local(w, "idx", "std::list idx"); Printf(w->code, "idx.push_back(octave_value_list(\"%s\"));\n", method_name); Printf(w->code, "idx.push_back(args);\n"); Printf(w->code, "out=swig_get_self()->subsref(\".(\",idx,%d);\n", outputs); String *cleanup = NewString(""); String *outarg = NewString(""); idx = 0; // marshal return value if (!is_void) { Printf(w->code, "if (out.length()<%d) {\n", outputs); Printf(w->code, "Swig::DirectorTypeMismatchException::raise(\"Octave " "method %s.%s failed to return the required number " "of arguments.\");\n", classname, method_name); Printf(w->code, "}\n"); tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w); if (tm != 0) { char temp[24]; sprintf(temp, "out(%d)", idx); Replaceall(tm, "$input", temp); // Replaceall(tm, "$argnum", temp); Replaceall(tm, "$disown", Getattr(n, "wrap:disown") ? "SWIG_POINTER_DISOWN" : "0"); if (Getattr(n, "tmap:directorout:implicitconv")) { Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); } Replaceall(tm, "$result", "c_result"); Printv(w->code, tm, "\n", NIL); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_ERROR; } } idx++; // marshal outputs for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { char temp[24]; sprintf(temp, "out(%d)", idx); Replaceall(tm, "$result", temp); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } Delete(parse_args); Delete(cleanup); Delete(outarg); } if (!is_void) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { Printf(w->code, "return (%s) c_result;\n", rettype); } else { Printf(w->code, "return (%s) *c_result;\n", rettype); } Delete(rettype); } } Append(w->code, "}\n"); // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } // emit the director method if (status == SWIG_OK) { Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } // clean up Delete(wrap_args); Delete(pclassname); DelWrapper(w); return status; } String *runtimeCode() { String *s = NewString(""); String *srun = Swig_include_sys("octrun.swg"); if (!srun) { Printf(stderr, "*** Unable to open 'octrun.swg'\n"); } else { Append(s, srun); Delete(srun); } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigoctaverun.h"); } }; extern "C" Language *swig_octave(void) { return new OCTAVE(); } swig-4.4.0/Source/Modules/swigmod.h0000664000175000017500000003731615075443613017064 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigmod.h * * Main header file for SWIG modules. * ----------------------------------------------------------------------------- */ #ifndef SWIG_SWIGMOD_H #define SWIG_SWIGMOD_H #include "swig.h" #include "preprocessor.h" #include "swigwarn.h" #define NOT_VIRTUAL 0 #define PLAIN_VIRTUAL 1 #define PURE_VIRTUAL 2 extern String *input_file; extern int line_number; extern int start_line; extern int CPlusPlus; // C++ mode extern int Extend; // Extend mode extern int Verbose; extern int IsVirtual; extern int ImportMode; extern int NoExcept; // -no_except option extern int Abstract; // abstract base class extern int SmartPointer; // smart pointer methods being emitted /* Overload "argc" and "argv" */ extern String *argv_template_string; extern String *argc_template_string; /* Miscellaneous stuff */ #define tab0 "" #define tab2 " " #define tab4 " " #define tab8 " " class Dispatcher { public: Dispatcher ():cplus_mode(PUBLIC) { } virtual ~ Dispatcher () { } virtual int emit_one(Node *n); virtual int emit_children(Node *n); virtual int defaultHandler(Node *n); /* Top of the parse tree */ virtual int top(Node *n) = 0; /* SWIG directives */ virtual int applyDirective(Node *n); virtual int clearDirective(Node *n); virtual int constantDirective(Node *n); virtual int extendDirective(Node *n); virtual int fragmentDirective(Node *n); virtual int importDirective(Node *n); virtual int includeDirective(Node *n); virtual int insertDirective(Node *n); virtual int moduleDirective(Node *n); virtual int nativeDirective(Node *n); virtual int pragmaDirective(Node *n); virtual int typemapDirective(Node *n); virtual int typemapitemDirective(Node *n); virtual int typemapcopyDirective(Node *n); virtual int typesDirective(Node *n); /* C/C++ parsing */ virtual int cDeclaration(Node *n); virtual int externDeclaration(Node *n); virtual int enumDeclaration(Node *n); virtual int enumvalueDeclaration(Node *n); virtual int enumforwardDeclaration(Node *n); virtual int classDeclaration(Node *n); virtual int classforwardDeclaration(Node *n); virtual int constructorDeclaration(Node *n); virtual int destructorDeclaration(Node *n); virtual int accessDeclaration(Node *n); virtual int usingDeclaration(Node *n); virtual int namespaceDeclaration(Node *n); virtual int templateDeclaration(Node *n); virtual int lambdaDeclaration(Node *n); enum AccessMode { PUBLIC, PRIVATE, PROTECTED }; protected: AccessMode cplus_mode; AccessMode accessModeFromString(String *access); int abstractClassTest(Node *n); /* Is class really abstract? */ }; /* ---------------------------------------------------------------------------- * class language: * * This class defines the functions that need to be supported by the * scripting language being used. The translator calls these virtual * functions to output different types of code for different languages. * ------------------------------------------------------------------------- */ class Language:public Dispatcher { public: Language(); virtual ~Language(); virtual int emit_one(Node *n); String *directorClassName(Node *n); /* Parse command line options */ virtual void main(int argc, char *argv[]); /* Top of the parse tree */ virtual int top(Node *n); /* SWIG directives */ virtual int applyDirective(Node *n); virtual int clearDirective(Node *n); virtual int constantDirective(Node *n); virtual int extendDirective(Node *n); virtual int fragmentDirective(Node *n); virtual int importDirective(Node *n); virtual int includeDirective(Node *n); virtual int insertDirective(Node *n); virtual int moduleDirective(Node *n); virtual int nativeDirective(Node *n); virtual int pragmaDirective(Node *n); virtual int typemapDirective(Node *n); virtual int typemapcopyDirective(Node *n); virtual int typesDirective(Node *n); /* C/C++ parsing */ virtual int cDeclaration(Node *n); virtual int externDeclaration(Node *n); virtual int enumDeclaration(Node *n); virtual int enumvalueDeclaration(Node *n); virtual int enumforwardDeclaration(Node *n); virtual int classDeclaration(Node *n); virtual int classforwardDeclaration(Node *n); virtual int constructorDeclaration(Node *n); virtual int destructorDeclaration(Node *n); virtual int accessDeclaration(Node *n); virtual int namespaceDeclaration(Node *n); virtual int usingDeclaration(Node *n); /* Function handlers */ virtual int functionHandler(Node *n); virtual int globalfunctionHandler(Node *n); virtual int memberfunctionHandler(Node *n); virtual int staticmemberfunctionHandler(Node *n); virtual int callbackfunctionHandler(Node *n); /* Variable handlers */ virtual int variableHandler(Node *n); virtual int globalvariableHandler(Node *n); virtual int membervariableHandler(Node *n); virtual int staticmembervariableHandler(Node *n); /* C++ handlers */ virtual int memberconstantHandler(Node *n); virtual int constructorHandler(Node *n); virtual int copyconstructorHandler(Node *n); virtual int destructorHandler(Node *n); virtual int classHandler(Node *n); /* Miscellaneous */ virtual int typedefHandler(Node *n); /* Low-level code generation */ virtual int constantWrapper(Node *n); virtual int variableWrapper(Node *n); virtual int functionWrapper(Node *n); virtual int nativeWrapper(Node *n); /* C++ director class generation */ virtual int classDirector(Node *n); virtual int classDirectorInit(Node *n); virtual int classDirectorEnd(Node *n); virtual int unrollVirtualMethods(Node *n, Node *parent, List *vm, int &virtual_destructor, int protectedbase = 0); virtual int classDirectorConstructor(Node *n); virtual int classDirectorDefaultConstructor(Node *n); virtual int classDirectorMethod(Node *n, Node *parent, String *super); virtual int classDirectorConstructors(Node *n); virtual int classDirectorDestructor(Node *n); virtual int classDirectorMethods(Node *n); virtual int classDirectorDisown(Node *n); /* Miscellaneous */ virtual int validIdentifier(String *s); /* valid identifier? */ virtual int addSymbol(const String *s, const Node *n, const_String_or_char_ptr scope = ""); /* Add symbol */ virtual int addInterfaceSymbol(const String *interface_name, Node *n, const_String_or_char_ptr scope = ""); virtual void dumpSymbols(); virtual Node *symbolLookup(const String *s, const_String_or_char_ptr scope = ""); /* Symbol lookup */ virtual Hash* symbolAddScope(const_String_or_char_ptr scope/*, Node *n = 0*/); virtual Hash* symbolScopeLookup(const_String_or_char_ptr scope); virtual Hash* symbolScopePseudoSymbolLookup(const_String_or_char_ptr scope); static Node *classLookup(const SwigType *s); /* Class lookup */ static Node *enumLookup(SwigType *s); /* Enum lookup */ virtual int is_immutable(Node *n); /* Is variable assignable? */ virtual String *runtimeCode(); /* returns the language specific runtime code */ virtual String *defaultExternalRuntimeFilename(); /* the default filename for the external runtime */ virtual void replaceSpecialVariables(String *method, String *tm, Parm *parm); /* Language specific special variable substitutions for $typemap() */ /* Runtime is C++ based, so extern "C" header section */ void enable_cplus_runtime_mode(); /* Returns the cplus_runtime mode */ int cplus_runtime_mode(); /* Flag for language to support directors */ void directorLanguage(int val = 1); /* Allow director related code generation */ void allow_directors(int val = 1); /* Allow director protected members related code generation */ void allow_dirprot(int val = 1); /* Allow all protected members code generation (for directors) */ void allow_allprotected(int val = 0); /* Returns the dirprot mode */ int dirprot_mode() const; /* Check if the non public constructor is needed (for directors) */ int need_nonpublic_ctor(Node *n); /* Check if the non public member is needed (for directors) */ int need_nonpublic_member(Node *n); /* Set none comparison string */ void setSubclassInstanceCheck(String *s); /* Set overload variable templates argc and argv */ void setOverloadResolutionTemplates(String *argc, String *argv); /* Language instance is a singleton - get instance */ static Language* instance(); protected: /* Allow multiple-input typemaps */ void allow_multiple_input(int val = 1); /* Allow overloaded functions */ void allow_overloading(int val = 1); /* Wrapping class query */ int is_wrapping_class() const; /* Return the node for the current class */ Node *getCurrentClass() const; /* Return C++ mode */ int getCPlusMode() const; /* Return the namespace for the class/enum - the nspace feature */ String *getNSpace() const; /* Return the real name of the current class */ String *getClassName() const; /* Return the classes hash */ Hash *getClassHash() const; /* Return the current class prefix */ String *getClassPrefix() const; /* Return the current enum class prefix */ String *getEnumClassPrefix() const; /* Fully qualified type name to use */ String *getClassType() const; /* Return true if the current method is part of a smart-pointer */ int is_smart_pointer() const; /* Return the name to use for the given parameter. */ virtual String *makeParameterName(Node *n, Parm *p, int arg_num, bool setter = false) const; /* Some language modules require additional wrappers for virtual methods not declared in sub-classes */ virtual bool extraDirectorProtectedCPPMethodsRequired() const; public: enum NestedClassSupport { NCS_None, // Target language does not have an equivalent to nested classes NCS_Full, // Target language does have an equivalent to nested classes and is fully implemented NCS_Unknown // Target language may or may not have an equivalent to nested classes. If it does, it has not been implemented yet. }; /* Does target language support nested classes? Default is NCS_Unknown. If NCS_Unknown is returned, then the nested classes will be ignored unless %feature "flatnested" is applied to them, in which case they will appear in global space. If the target language does not support the notion of class nesting, the language module should return NCS_None from this function, and the nested classes will be moved to the global scope (like implicit global %feature "flatnested"). */ virtual NestedClassSupport nestedClassesSupport() const; /* Returns true if the target language supports key word arguments (kwargs) */ virtual bool kwargsSupport() const; protected: /* Identifies if a protected members that are generated when the allprotected option is used. This does not include protected virtual methods as they are turned on with the dirprot option. */ bool isNonVirtualProtectedAccess(Node *n) const; /* Identify if a wrapped global or member variable n should use the naturalvar feature */ int use_naturalvar_mode(Node *n) const; /* Director subclass comparison test */ String *none_comparison; /* Director constructor "template" code */ String *director_ctor_code; /* Director 'protected' constructor "template" code, disabled by default. Each language that needs it, has to define it. */ String *director_prot_ctor_code; /* Director allows multiple inheritance */ int director_multiple_inheritance; /* Used to translate Doxygen comments to target documentation format */ class DoxygenTranslator *doxygenTranslator; private: void unrollOneVirtualMethod(String *classname, Node *n, Node *parent, List *vm, int &virtual_destructor, int protectedbase); Hash *symtabs; /* symbol tables */ int overloading; int multiinput; int cplus_runtime; static Language *this_; }; extern "C" { typedef Language *(*ModuleFactory) (void); } enum Status {Disabled, Deprecated, Experimental, Supported}; struct TargetLanguageModule { const char *name; ModuleFactory fac; const char *help; Status status; }; int SWIG_main(int argc, char *argv[], const TargetLanguageModule *tlm); void emit_parameter_variables(ParmList *l, Wrapper *f); void emit_return_variable(Node *n, SwigType *rt, Wrapper *f); void SWIG_config_file(const_String_or_char_ptr ); const String *SWIG_output_directory(); void SWIG_config_cppext(const char *ext); void Swig_print_xml(Node *obj, String *filename); /* get the list of generated files */ List *SWIG_output_files(); void SWIG_library_directory(const char *); int emit_num_arguments(ParmList *); int emit_num_required(ParmList *); int emit_isvarargs(ParmList *p); bool emit_isvarargs_function(Node *n); void emit_attach_parmmaps(ParmList *, Wrapper *f); void emit_mark_varargs(ParmList *l); String *emit_action(Node *n); int emit_action_code(Node *n, String *wrappercode, String *action); void Swig_overload_check(Node *n); String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted, const_String_or_char_ptr fmt_fastdispatch = 0); String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted); List *Swig_overload_rank(Node *n, bool script_lang_wrapping); SwigType *cplus_value_type(SwigType *t); int Swig_directors_enabled(); /* directors.cxx start */ String *Swig_csuperclass_call(String *base, String *method, ParmList *l); String *Swig_class_declaration(Node *n, String *name); String *Swig_class_name(Node *n); String *Swig_method_call(const_String_or_char_ptr name, ParmList *parms); String *Swig_method_decl(SwigType *return_base_type, SwigType *decl, const_String_or_char_ptr id, List *args, int default_args); String *Swig_director_declaration(Node *n); void Swig_director_emit_dynamic_cast(Node *n, Wrapper *f); void Swig_director_parms_fixup(ParmList *parms); bool Swig_director_can_unwrap(Node *n); /* directors.cxx end */ /* Utilities */ int is_public(Node *n); int is_private(Node *n); int is_protected(Node *n); int is_member_director(Node *parentnode, Node *member); int is_member_director(Node *member); int is_non_virtual_protected_access(Node *n); /* Check if the non-virtual protected members are required (for directors) */ void Wrapper_virtual_elimination_mode_set(int); void Wrapper_fast_dispatch_mode_set(int); void Wrapper_cast_dispatch_mode_set(int); void Wrapper_naturalvar_mode_set(int); void clean_overloaded(Node *n); extern "C" { const char *Swig_to_string(DOH *object, int count = -1); const char *Swig_to_string_with_location(DOH *object, int count = -1); void Swig_print(DOH *object, int count = -1); void Swig_print_with_location(DOH *object, int count = -1); } void Swig_default_allocators(Node *n); void Swig_process_types(Node *n); /* Contracts */ void Swig_contracts(Node *n); void Swig_contract_mode_set(int flag); int Swig_contract_mode_get(); /* Nested classes */ void Swig_nested_process_classes(Node *n); void Swig_nested_name_unnamed_c_structs(Node *n); /* Interface feature */ void Swig_interface_feature_enable(); void Swig_interface_propagate_methods(Node *n); /* Miscellaneous */ template class save_value { T _value; T& _value_ptr; save_value(const save_value&); save_value& operator=(const save_value&); public: save_value(T& value) : _value(value), _value_ptr(value) {} save_value(T& value, T new_val) : _value(value), _value_ptr(value) { value = new_val; } ~save_value() { _value_ptr = _value; } }; #endif swig-4.4.0/Source/Modules/contract.cxx0000664000175000017500000002360315075443613017575 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * contract.cxx * * Support for Wrap by Contract in SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" /* Contract structure. This holds rules about the different kinds of contract sections and their combination rules */ struct contract { const char *section; const char *combiner; }; /* Contract rules. This table defines what contract sections are recognized as well as how contracts are to combined via inheritance */ static contract Rules[] = { {"require:", "&&"}, {"ensure:", "||"}, {NULL, NULL} }; /* ---------------------------------------------------------------------------- * class Contracts: * * This class defines the functions that need to be used in * "wrap by contract" module. * ------------------------------------------------------------------------- */ class Contracts:public Dispatcher { String *make_expression(String *s, Node *n); void substitute_parms(String *s, ParmList *p, int method); public: Hash *ContractSplit(Node *n); int emit_contract(Node *n, int method); int cDeclaration(Node *n); int constructorDeclaration(Node *n); int externDeclaration(Node *n); int extendDirective(Node *n); int importDirective(Node *n); int includeDirective(Node *n); int namespaceDeclaration(Node *n); int classDeclaration(Node *n); virtual int top(Node *n); }; static int Contract_Mode = 0; /* contract option */ static int InClass = 0; /* Parsing C++ or not */ static int InConstructor = 0; static Node *CurrentClass = 0; /* Set the contract mode, default is 0 (not open) */ /* Normally set in main.cxx, when get the "-contracts" option */ void Swig_contract_mode_set(int flag) { Contract_Mode = flag; } /* Get the contract mode */ int Swig_contract_mode_get() { return Contract_Mode; } /* Apply contracts */ void Swig_contracts(Node *n) { Contracts *a = new Contracts; a->top(n); delete a; } /* Split the whole contract into preassertion, postassertion and others */ Hash *Contracts::ContractSplit(Node *n) { String *contract = Getattr(n, "feature:contract"); Hash *result; if (!contract) return NULL; result = NewHash(); String *current_section = NewString(""); const char *current_section_name = Rules[0].section; List *l = SplitLines(contract); Iterator i; for (i = First(l); i.item; i = Next(i)) { int found = 0; if (Strchr(i.item, '{')) continue; if (Strchr(i.item, '}')) continue; for (int j = 0; Rules[j].section; j++) { if (Strstr(i.item, Rules[j].section)) { if (Len(current_section)) { Setattr(result, current_section_name, current_section); current_section = Getattr(result, Rules[j].section); if (!current_section) current_section = NewString(""); } current_section_name = Rules[j].section; found = 1; break; } } if (!found) Append(current_section, i.item); } if (Len(current_section)) Setattr(result, current_section_name, current_section); return result; } /* This function looks in base classes and collects contracts found */ static void inherit_contracts(Node *c, Node *n, Hash *contracts, Hash *messages) { Node *b, *temp; String *name, *type, *local_decl, *base_decl; List *bases; int found = 0; bases = Getattr(c, "bases"); if (!bases) return; name = Getattr(n, "name"); type = Getattr(n, "type"); local_decl = Getattr(n, "decl"); if (local_decl) { local_decl = SwigType_typedef_resolve_all(local_decl); } else { return; } /* Width first search */ for (int i = 0; i < Len(bases); i++) { b = Getitem(bases, i); temp = firstChild(b); while (temp) { base_decl = Getattr(temp, "decl"); if (base_decl) { base_decl = SwigType_typedef_resolve_all(base_decl); if ((checkAttribute(temp, "storage", "virtual")) && (checkAttribute(temp, "name", name)) && (checkAttribute(temp, "type", type)) && (!Strcmp(local_decl, base_decl))) { /* Yes, match found. */ Hash *icontracts = Getattr(temp, "contract:rules"); Hash *imessages = Getattr(temp, "contract:messages"); found = 1; if (icontracts && imessages) { /* Add inherited contracts and messages to the contract rules above */ int j = 0; for (j = 0; Rules[j].section; j++) { String *t = Getattr(contracts, Rules[j].section); String *s = Getattr(icontracts, Rules[j].section); if (s) { if (t) { Insert(t, 0, "("); Printf(t, ") %s (%s)", Rules[j].combiner, s); String *m = Getattr(messages, Rules[j].section); Printf(m, " %s [%s from %s]", Rules[j].combiner, Getattr(imessages, Rules[j].section), Getattr(b, "name")); } else { Setattr(contracts, Rules[j].section, NewString(s)); Setattr(messages, Rules[j].section, NewStringf("[%s from %s]", Getattr(imessages, Rules[j].section), Getattr(b, "name"))); } } } } } Delete(base_decl); } temp = nextSibling(temp); } } Delete(local_decl); if (!found) { for (int j = 0; j < Len(bases); j++) { b = Getitem(bases, j); inherit_contracts(b, n, contracts, messages); } } } /* This function cleans up the assertion string by removing some extraneous characters. Splitting the assertion into pieces */ String *Contracts::make_expression(String *s, Node *n) { String *str_assert, *expr = 0; List *list_assert; str_assert = NewString(s); /* Omit all useless characters and split by ; */ Replaceall(str_assert, "\n", ""); Replaceall(str_assert, "{", ""); Replaceall(str_assert, "}", ""); Replace(str_assert, " ", "", DOH_REPLACE_ANY | DOH_REPLACE_NOQUOTE); Replace(str_assert, "\t", "", DOH_REPLACE_ANY | DOH_REPLACE_NOQUOTE); list_assert = Split(str_assert, ';', -1); Delete(str_assert); /* build up new assertion */ str_assert = NewString(""); Iterator ei; for (ei = First(list_assert); ei.item; ei = Next(ei)) { expr = ei.item; if (Len(expr)) { Replaceid(expr, Getattr(n, "name"), Swig_cresult_name()); if (Len(str_assert)) Append(str_assert, "&&"); Printf(str_assert, "(%s)", expr); } } Delete(list_assert); return str_assert; } /* This function substitutes parameter names for argument names in the contract specification. Note: it is assumed that the wrapper code uses arg1 for self and arg2..argn for arguments. */ void Contracts::substitute_parms(String *s, ParmList *p, int method) { int argnum = 1; char argname[32]; if (method) { Replaceid(s, "$self", "arg1"); argnum++; } while (p) { sprintf(argname, "arg%d", argnum); String *name = Getattr(p, "name"); if (name) { Replaceid(s, name, argname); } argnum++; p = nextSibling(p); } } int Contracts::emit_contract(Node *n, int method) { Hash *contracts; Hash *messages; String *c; ParmList *cparms; if (!Getattr(n, "feature:contract")) return SWIG_ERROR; /* Get contract parameters */ cparms = Getmeta(Getattr(n, "feature:contract"), "parms"); /* Split contract into preassert & postassert */ contracts = ContractSplit(n); if (!contracts) return SWIG_ERROR; /* This messages hash is used to hold the error messages that will be displayed on failed contract. */ messages = NewHash(); /* Take the different contract expressions and clean them up a bit */ Iterator i; for (i = First(contracts); i.item; i = Next(i)) { String *e = make_expression(i.item, n); substitute_parms(e, cparms, method); Setattr(contracts, i.key, e); /* Make a string containing error messages */ Setattr(messages, i.key, NewString(e)); } /* If we're in a class. We need to inherit other assertions. */ if (InClass) { inherit_contracts(CurrentClass, n, contracts, messages); } /* Save information */ Setattr(n, "contract:rules", contracts); Setattr(n, "contract:messages", messages); /* Okay. Generate the contract runtime code. */ if ((c = Getattr(contracts, "require:"))) { Setattr(n, "contract:preassert", NewStringf("SWIG_contract_assert(%s, \"Contract violation: require: %s\");\n", c, Getattr(messages, "require:"))); } if ((c = Getattr(contracts, "ensure:"))) { Setattr(n, "contract:postassert", NewStringf("SWIG_contract_assert(%s, \"Contract violation: ensure: %s\");\n", c, Getattr(messages, "ensure:"))); } return SWIG_OK; } int Contracts::cDeclaration(Node *n) { int ret = SWIG_OK; String *decl = Getattr(n, "decl"); /* Not a function. Don't even bother with it (for now) */ if (!SwigType_isfunction(decl)) return SWIG_OK; if (Getattr(n, "feature:contract")) ret = emit_contract(n, InClass && !Swig_storage_isstatic(n)); return ret; } int Contracts::constructorDeclaration(Node *n) { int ret = SWIG_OK; InConstructor = 1; if (Getattr(n, "feature:contract")) ret = emit_contract(n, 0); InConstructor = 0; return ret; } int Contracts::externDeclaration(Node *n) { return emit_children(n); } int Contracts::extendDirective(Node *n) { return emit_children(n); } int Contracts::importDirective(Node *n) { return emit_children(n); } int Contracts::includeDirective(Node *n) { return emit_children(n); } int Contracts::namespaceDeclaration(Node *n) { return emit_children(n); } int Contracts::classDeclaration(Node *n) { int ret = SWIG_OK; int oldInClass = InClass; Node *oldClass = CurrentClass; InClass = 1; CurrentClass = n; emit_children(n); InClass = oldInClass; CurrentClass = oldClass; return ret; } int Contracts::top(Node *n) { emit_children(n); return SWIG_OK; } swig-4.4.0/Source/Modules/overload.cxx0000664000175000017500000007214115075443613017574 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * overload.cxx * * This file is used to analyze overloaded functions and methods. * It looks at signatures and tries to gather information for * building a dispatch function. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #define MAX_OVERLOAD 4096 /* Overload "argc" and "argv" */ String *argv_template_string; String *argc_template_string; namespace { struct Overloaded { Node *n; /* Node */ int argc; /* Argument count */ ParmList *parms; /* Parameters used for overload check */ int error; /* Ambiguity error */ bool implicitconv_function; /* For ordering implicitconv functions*/ }; } static int fast_dispatch_mode = 0; static int cast_dispatch_mode = 0; /* Set fast_dispatch_mode */ void Wrapper_fast_dispatch_mode_set(int flag) { fast_dispatch_mode = flag; } void Wrapper_cast_dispatch_mode_set(int flag) { cast_dispatch_mode = flag; } /* ----------------------------------------------------------------------------- * mark_implicitconv_function() * * Mark function if it contains an implicitconv type in the parameter list * ----------------------------------------------------------------------------- */ static void mark_implicitconv_function(Overloaded& onode) { Parm *parms = onode.parms; if (parms) { bool is_implicitconv_function = false; Parm *p = parms; while (p) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } if (GetFlag(p, "implicitconv")) { is_implicitconv_function = true; break; } p = nextSibling(p); } if (is_implicitconv_function) onode.implicitconv_function = true; } } /* ----------------------------------------------------------------------------- * Swig_overload_rank() * * This function takes an overloaded declaration and creates a list that ranks * all overloaded methods in an order that can be used to generate a dispatch * function. * Slight difference in the way this function is used by scripting languages and * statically typed languages. The script languages call this method via * Swig_overload_dispatch() - where wrappers for all overloaded methods are generated, * however sometimes the code can never be executed. The non-scripting languages * call this method via Swig_overload_check() for each overloaded method in order * to determine whether or not the method should be wrapped. Note the slight * difference when overloading methods that differ by const only. The * scripting languages will ignore the const method, whereas the non-scripting * languages ignore the first method parsed. * ----------------------------------------------------------------------------- */ List *Swig_overload_rank(Node *n, bool script_lang_wrapping) { Overloaded nodes[MAX_OVERLOAD]; int nnodes = 0; Node *o = Getattr(n, "sym:overloaded"); Node *c; if (!o) return 0; c = o; while (c) { if (Getattr(c, "error")) { c = Getattr(c, "sym:nextSibling"); continue; } /* if (SmartPointer && Getattr(c,"cplus:staticbase")) { c = Getattr(c,"sym:nextSibling"); continue; } */ /* Make a list of all the declarations (methods) that are overloaded with * this one particular method name */ if (Getattr(c, "wrap:name")) { assert(nnodes < MAX_OVERLOAD); nodes[nnodes].n = c; nodes[nnodes].parms = Getattr(c, "wrap:parms"); nodes[nnodes].argc = emit_num_required(nodes[nnodes].parms); nodes[nnodes].error = 0; nodes[nnodes].implicitconv_function = false; mark_implicitconv_function(nodes[nnodes]); nnodes++; } c = Getattr(c, "sym:nextSibling"); } /* Sort the declarations by required argument count */ { int i, j; for (i = 0; i < nnodes; i++) { for (j = i + 1; j < nnodes; j++) { if (nodes[i].argc > nodes[j].argc) { Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; } } } } /* Sort the declarations by argument types */ { int i, j; for (i = 0; i < nnodes - 1; i++) { if (nodes[i].argc == nodes[i + 1].argc) { for (j = i + 1; (j < nnodes) && (nodes[j].argc == nodes[i].argc); j++) { Parm *p1 = nodes[i].parms; Parm *p2 = nodes[j].parms; int differ = 0; int num_checked = 0; while (p1 && p2 && (num_checked < nodes[i].argc)) { // Printf(stdout,"p1 = '%s', p2 = '%s'\n", Getattr(p1,"type"), Getattr(p2,"type")); if (checkAttribute(p1, "tmap:in:numinputs", "0")) { p1 = Getattr(p1, "tmap:in:next"); continue; } if (checkAttribute(p2, "tmap:in:numinputs", "0")) { p2 = Getattr(p2, "tmap:in:next"); continue; } String *t1 = Getattr(p1, "tmap:typecheck:precedence"); String *t2 = Getattr(p2, "tmap:typecheck:precedence"); if ((!t1) && (!nodes[i].error)) { Swig_warning(WARN_TYPEMAP_TYPECHECK, Getfile(nodes[i].n), Getline(nodes[i].n), "Overloaded method %s not supported (incomplete type checking rule - no precedence level in typecheck typemap for '%s').\n", Swig_name_decl(nodes[i].n), SwigType_str(Getattr(p1, "type"), 0)); nodes[i].error = 1; } else if ((!t2) && (!nodes[j].error)) { Swig_warning(WARN_TYPEMAP_TYPECHECK, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s not supported (incomplete type checking rule - no precedence level in typecheck typemap for '%s').\n", Swig_name_decl(nodes[j].n), SwigType_str(Getattr(p2, "type"), 0)); nodes[j].error = 1; } if (t1 && t2) { int t1v, t2v; t1v = atoi(Char(t1)); t2v = atoi(Char(t2)); differ = t1v - t2v; } else if (!t1 && t2) differ = 1; else if (t1 && !t2) differ = -1; else if (!t1 && !t2) differ = -1; num_checked++; if (differ > 0) { Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; break; } else if ((differ == 0) && (Strcmp(t1, "0") == 0)) { t1 = Getattr(p1, "equivtype"); t1 = t1 ? t1 : Getattr(p1, "ltype"); if (!t1) { t1 = SwigType_ltype(Getattr(p1, "type")); if (Getattr(p1, "tmap:typecheck:SWIGTYPE")) { SwigType_add_pointer(t1); } Setattr(p1, "ltype", t1); } t2 = Getattr(p2, "equivtype"); t2 = t2 ? t2 : Getattr(p2, "ltype"); if (!t2) { t2 = SwigType_ltype(Getattr(p2, "type")); if (Getattr(p2, "tmap:typecheck:SWIGTYPE")) { SwigType_add_pointer(t2); } Setattr(p2, "ltype", t2); } /* Need subtype check here. If t2 is a subtype of t1, then we need to change the order */ if (SwigType_issubtype(t2, t1)) { Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; } if (Strcmp(t1, t2) != 0) { differ = 1; break; } } else if (differ) { break; } if (Getattr(p1, "tmap:in:next")) { p1 = Getattr(p1, "tmap:in:next"); } else { p1 = nextSibling(p1); } if (Getattr(p2, "tmap:in:next")) { p2 = Getattr(p2, "tmap:in:next"); } else { p2 = nextSibling(p2); } } if (!differ) { /* See if declarations differ by const only */ String *decl1 = Getattr(nodes[i].n, "decl"); String *decl2 = Getattr(nodes[j].n, "decl"); if (decl1 && decl2) { /* Remove ref-qualifiers. Note that rvalue ref-qualifiers are already ignored and * it is illegal to overload a function with and without ref-qualifiers. So with * all the combinations of ref-qualifiers and cv-qualifiers, we just detect * the cv-qualifier (const) overloading. */ String *d1 = Copy(decl1); String *d2 = Copy(decl2); if (SwigType_isreference(d1) || SwigType_isrvalue_reference(d1)) { Delete(SwigType_pop(d1)); } if (SwigType_isreference(d2) || SwigType_isrvalue_reference(d2)) { Delete(SwigType_pop(d2)); } String *dq1 = Copy(d1); String *dq2 = Copy(d2); if (SwigType_isconst(d1)) { Delete(SwigType_pop(dq1)); } if (SwigType_isconst(d2)) { Delete(SwigType_pop(dq2)); } if (Strcmp(dq1, dq2) == 0) { if (SwigType_isconst(d1) && !SwigType_isconst(d2)) { if (script_lang_wrapping) { // Swap nodes so that the const method gets ignored (shadowed by the non-const method) Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; } differ = 1; if (!nodes[j].error) { if (script_lang_wrapping) { Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[i].n), Getline(nodes[i].n), "using non-const method %s instead.\n", Swig_name_decl(nodes[i].n)); } else { if (!Getattr(nodes[j].n, "overload:ignore")) { Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[i].n), Getline(nodes[i].n), "using %s instead.\n", Swig_name_decl(nodes[i].n)); } } } nodes[j].error = 1; } else if (!SwigType_isconst(d1) && SwigType_isconst(d2)) { differ = 1; if (!nodes[j].error) { if (script_lang_wrapping) { Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[i].n), Getline(nodes[i].n), "using non-const method %s instead.\n", Swig_name_decl(nodes[i].n)); } else { if (!Getattr(nodes[j].n, "overload:ignore")) { Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[i].n), Getline(nodes[i].n), "using %s instead.\n", Swig_name_decl(nodes[i].n)); } } } nodes[j].error = 1; } } Delete(dq1); Delete(dq2); } } if (!differ) { if (!nodes[j].error) { if (script_lang_wrapping) { Swig_warning(WARN_LANG_OVERLOAD_SHADOW, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s effectively ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_SHADOW, Getfile(nodes[i].n), Getline(nodes[i].n), "as it is shadowed by %s.\n", Swig_name_decl(nodes[i].n)); } else { if (!Getattr(nodes[j].n, "overload:ignore")) { Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[i].n), Getline(nodes[i].n), "using %s instead.\n", Swig_name_decl(nodes[i].n)); } } nodes[j].error = 1; } } } } } } List *result = NewList(); { int i; int argc_changed_index = -1; for (i = 0; i < nnodes; i++) { if (nodes[i].error) Setattr(nodes[i].n, "overload:ignore", "1"); Append(result, nodes[i].n); // Printf(stdout,"[ %d ] %d %s\n", i, nodes[i].implicitconv_function, ParmList_errorstr(nodes[i].parms)); if (i == nnodes-1 || nodes[i].argc != nodes[i+1].argc) { if (argc_changed_index+2 < nnodes && (nodes[argc_changed_index+1].argc == nodes[argc_changed_index+2].argc)) { // Add additional implicitconv functions in same order as already ranked. // Consider overloaded functions by argument count... only add additional implicitconv functions if // the number of functions with the same arg count > 1, ie, only if overloaded by same argument count. int j; for (j = argc_changed_index + 1; j <= i; j++) { if (nodes[j].implicitconv_function) { SetFlag(nodes[j].n, "implicitconvtypecheckoff"); Append(result, nodes[j].n); // Printf(stdout,"[ %d ] %d + %s\n", j, nodes[j].implicitconv_function, ParmList_errorstr(nodes[j].parms)); } } } argc_changed_index = i; } } } return result; } /* ----------------------------------------------------------------------------- * print_typecheck() * ----------------------------------------------------------------------------- */ static bool print_typecheck(String *f, int j, Parm *pj, bool implicitconvtypecheckoff) { char tmp[256]; sprintf(tmp, Char(argv_template_string), j); String *tm = Getattr(pj, "tmap:typecheck"); if (tm) { tm = Copy(tm); Replaceid(tm, Getattr(pj, "lname"), "_v"); String *conv = Getattr(pj, "implicitconv"); if (conv && !implicitconvtypecheckoff) { Replaceall(tm, "$implicitconv", conv); } else { Replaceall(tm, "$implicitconv", "0"); } Replaceall(tm, "$input", tmp); Printv(f, tm, "\n", NIL); Delete(tm); return true; } else return false; } /* ----------------------------------------------------------------------------- * ReplaceFormat() * ----------------------------------------------------------------------------- */ static String *ReplaceFormat(const_String_or_char_ptr fmt, int j) { String *lfmt = NewString(fmt); char buf[50]; sprintf(buf, "%d", j); Replaceall(lfmt, "$numargs", buf); int i; String *commaargs = NewString(""); for (i = 0; i < j; i++) { Printv(commaargs, ", ", NIL); Printf(commaargs, Char(argv_template_string), i); } Replaceall(lfmt, "$commaargs", commaargs); return lfmt; } /* ----------------------------------------------------------------------------- * Swig_overload_dispatch() * * Generate a dispatch function. argc is assumed to hold the argument count. * argv is the argument vector. * * Note that for C++ class member functions, Swig_overload_dispatch() assumes * that argc includes the "self" argument and that the first element of argv[] * is the "self" argument. So for a member function: * * Foo::bar(int x, int y, int z); * * the argc should be 4 (not 3!) and the first element of argv[] would be * the appropriate scripting language reference to "self". For regular * functions (and static class functions) the argc and argv only include * the regular function arguments. * ----------------------------------------------------------------------------- */ /* Cast dispatch mechanism. */ String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted) { int i, j; *maxargs = 0; *check_emitted = false; String *f = NewString(""); String *sw = NewString(""); Printf(f, "{\n"); Printf(f, "unsigned long _index = 0;\n"); Printf(f, "SWIG_TypeRank _rank = 0; \n"); /* Get a list of methods ranked by precedence values and argument count */ List *dispatch = Swig_overload_rank(n, true); int nfunc = Len(dispatch); /* Loop over the functions */ bool emitcheck = true; for (i = 0; i < nfunc; i++) { int fn = 0; Node *ni = Getitem(dispatch, i); Parm *pi = Getattr(ni, "wrap:parms"); bool implicitconvtypecheckoff = GetFlag(ni, "implicitconvtypecheckoff") != 0; int num_required = emit_num_required(pi); int num_arguments = emit_num_arguments(pi); if (num_arguments > *maxargs) *maxargs = num_arguments; if (num_required == num_arguments) { Printf(f, "if (%s == %d) {\n", argc_template_string, num_required); } else { Printf(f, "if ((%s >= %d) && (%s <= %d)) {\n", argc_template_string, num_required, argc_template_string, num_arguments); } Printf(f, "SWIG_TypeRank _ranki = 0;\n"); Printf(f, "SWIG_TypeRank _rankm = 0;\n"); if (num_arguments) Printf(f, "SWIG_TypeRank _pi = 1;\n"); /* create a list with the wrappers that collide with the current one based on argument number */ List *coll = NewList(); for (int k = i + 1; k < nfunc; k++) { Node *nk = Getitem(dispatch, k); Parm *pk = Getattr(nk, "wrap:parms"); int nrk = emit_num_required(pk); int nak = emit_num_arguments(pk); if ((nrk >= num_required && nrk <= num_arguments) || (nak >= num_required && nak <= num_arguments) || (nrk <= num_required && nak >= num_arguments)) Append(coll, nk); } // printf("overload: %s coll=%d\n", Char(Getattr(n, "sym:name")), Len(coll)); int num_braces = 0; bool test = (num_arguments > 0); if (test) { int need_v = 1; j = 0; Parm *pj = pi; while (pj) { if (checkAttribute(pj, "tmap:in:numinputs", "0")) { pj = Getattr(pj, "tmap:in:next"); continue; } String *tm = Getattr(pj, "tmap:typecheck"); if (tm) { tm = Copy(tm); /* normalise for comparison later */ Replaceid(tm, Getattr(pj, "lname"), "_v"); /* if all the wrappers have the same type check on this argument we can optimize it out */ for (int k = 0; k < Len(coll) && !emitcheck; k++) { Node *nk = Getitem(coll, k); Parm *pk = Getattr(nk, "wrap:parms"); int nak = emit_num_arguments(pk); if (nak <= j) continue; int l = 0; Parm *pl = pk; /* finds arg j on the collider wrapper */ while (pl && l <= j) { if (checkAttribute(pl, "tmap:in:numinputs", "0")) { pl = Getattr(pl, "tmap:in:next"); continue; } if (l == j) { /* we are at arg j, so we compare the tmaps now */ String *tml = Getattr(pl, "tmap:typecheck"); /* normalise it before comparing */ if (tml) Replaceid(tml, Getattr(pl, "lname"), "_v"); if (!tml || Cmp(tm, tml)) emitcheck = true; //printf("tmap: %s[%d] (%d) => %s\n\n", // Char(Getattr(nk, "sym:name")), // l, emitcheck, tml?Char(tml):0); } Parm *pl1 = Getattr(pl, "tmap:in:next"); if (pl1) pl = pl1; else pl = nextSibling(pl); l++; } } if (emitcheck) { *check_emitted = true; if (need_v) { Printf(f, "int _v = 0;\n"); need_v = 0; } if (j >= num_required) { Printf(f, "if (%s > %d) {\n", argc_template_string, j); num_braces++; } String *tmp = NewStringf(argv_template_string, j); String *conv = Getattr(pj, "implicitconv"); if (conv && !implicitconvtypecheckoff) { Replaceall(tm, "$implicitconv", conv); } else { Replaceall(tm, "$implicitconv", "0"); } Replaceall(tm, "$input", tmp); Printv(f, "{\n", tm, "}\n", NIL); Delete(tm); fn = i + 1; Printf(f, "if (!_v) goto check_%d;\n", fn); Printf(f, "_ranki += _v*_pi;\n"); Printf(f, "_rankm += _pi;\n"); Printf(f, "_pi *= SWIG_MAXCASTRANK;\n"); } } if (!Getattr(pj, "tmap:in:SWIGTYPE") && Getattr(pj, "tmap:typecheck:SWIGTYPE")) { /* we emit a warning if the argument defines the 'in' typemap, but not the 'typecheck' one */ Swig_warning(WARN_TYPEMAP_TYPECHECK_UNDEF, Getfile(ni), Getline(ni), "Overloaded method %s with no explicit typecheck typemap for arg %d of type '%s'.\n", Swig_name_decl(n), j, SwigType_str(Getattr(pj, "type"), 0)); Swig_warning(WARN_TYPEMAP_TYPECHECK_UNDEF, Getfile(ni), Getline(ni), "Dispatching calls to this method may not work correctly, see the 'Typemaps and Overloading' section in the Typemaps chapter of the SWIG documentation.\n"); } Parm *pj1 = Getattr(pj, "tmap:in:next"); if (pj1) pj = pj1; else pj = nextSibling(pj); j++; } } /* close braces */ for ( /* empty */ ; num_braces > 0; num_braces--) Printf(f, "}\n"); Printf(f, "if (!_index || (_ranki < _rank)) {\n"); Printf(f, " _rank = _ranki; _index = %d;\n", i + 1); Printf(f, " if (_rank == _rankm) goto dispatch;\n"); Printf(f, "}\n"); String *lfmt = ReplaceFormat(fmt, num_arguments); Printf(sw, "case %d:\n", i + 1); Printf(sw, Char(lfmt), Getattr(ni, "wrap:name")); Printf(sw, "\n"); Printf(f, "}\n"); /* braces closes "if" for this method */ if (fn) Printf(f, "check_%d:\n\n", fn); if (implicitconvtypecheckoff) Delattr(ni, "implicitconvtypecheckoff"); Delete(lfmt); Delete(coll); } Delete(dispatch); Printf(f, "dispatch:\n"); Printf(f, "switch(_index) {\n"); Printf(f, "%s", sw); Printf(f, "}\n"); Printf(f, "}\n"); return f; } /* Fast dispatch mechanism, provided by Salvador Fandi~no Garc'ia (#930586). */ static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted, const_String_or_char_ptr fmt_fastdispatch) { int i, j; *maxargs = 0; *check_emitted = false; String *f = NewString(""); /* Get a list of methods ranked by precedence values and argument count */ List *dispatch = Swig_overload_rank(n, true); int nfunc = Len(dispatch); /* Loop over the functions */ for (i = 0; i < nfunc; i++) { int fn = 0; Node *ni = Getitem(dispatch, i); Parm *pi = Getattr(ni, "wrap:parms"); bool implicitconvtypecheckoff = GetFlag(ni, "implicitconvtypecheckoff") != 0; int num_required = emit_num_required(pi); int num_arguments = emit_num_arguments(pi); if (num_arguments > *maxargs) *maxargs = num_arguments; if (num_required == num_arguments) { Printf(f, "if (%s == %d) {\n", argc_template_string, num_required); } else { Printf(f, "if ((%s >= %d) && (%s <= %d)) {\n", argc_template_string, num_required, argc_template_string, num_arguments); } /* create a list with the wrappers that collide with the current one based on argument number */ List *coll = NewList(); for (int k = i + 1; k < nfunc; k++) { Node *nk = Getitem(dispatch, k); Parm *pk = Getattr(nk, "wrap:parms"); int nrk = emit_num_required(pk); int nak = emit_num_arguments(pk); if ((nrk >= num_required && nrk <= num_arguments) || (nak >= num_required && nak <= num_arguments) || (nrk <= num_required && nak >= num_arguments)) Append(coll, nk); } // printf("overload: %s coll=%d\n", Char(Getattr(n, "sym:name")), Len(coll)); bool emitcheck = false; int num_braces = 0; bool test = (Len(coll) > 0 && num_arguments); if (test) { int need_v = 1; j = 0; Parm *pj = pi; while (pj) { if (checkAttribute(pj, "tmap:in:numinputs", "0")) { pj = Getattr(pj, "tmap:in:next"); continue; } String *tm = Getattr(pj, "tmap:typecheck"); if (tm) { tm = Copy(tm); /* normalise for comparison later */ Replaceid(tm, Getattr(pj, "lname"), "_v"); /* if all the wrappers have the same type check on this argument we can optimize it out */ emitcheck = false; for (int k = 0; k < Len(coll) && !emitcheck; k++) { Node *nk = Getitem(coll, k); Parm *pk = Getattr(nk, "wrap:parms"); int nak = emit_num_arguments(pk); if (nak <= j) continue; int l = 0; Parm *pl = pk; /* finds arg j on the collider wrapper */ while (pl && l <= j) { if (checkAttribute(pl, "tmap:in:numinputs", "0")) { pl = Getattr(pl, "tmap:in:next"); continue; } if (l == j) { /* we are at arg j, so we compare the tmaps now */ String *tml = Getattr(pl, "tmap:typecheck"); /* normalise it before comparing */ if (tml) Replaceid(tml, Getattr(pl, "lname"), "_v"); if (!tml || Cmp(tm, tml)) emitcheck = true; //printf("tmap: %s[%d] (%d) => %s\n\n", // Char(Getattr(nk, "sym:name")), // l, emitcheck, tml?Char(tml):0); } Parm *pl1 = Getattr(pl, "tmap:in:next"); if (pl1) pl = pl1; else pl = nextSibling(pl); l++; } } if (emitcheck) { *check_emitted = true; if (need_v) { Printf(f, "int _v = 0;\n"); need_v = 0; } if (j >= num_required) { Printf(f, "if (%s > %d) {\n", argc_template_string, j); num_braces++; } String *tmp = NewStringf(argv_template_string, j); String *conv = Getattr(pj, "implicitconv"); if (conv && !implicitconvtypecheckoff) { Replaceall(tm, "$implicitconv", conv); } else { Replaceall(tm, "$implicitconv", "0"); } Replaceall(tm, "$input", tmp); Printv(f, "{\n", tm, "}\n", NIL); Delete(tm); fn = i + 1; Printf(f, "if (!_v) goto check_%d;\n", fn); } } if (!Getattr(pj, "tmap:in:SWIGTYPE") && Getattr(pj, "tmap:typecheck:SWIGTYPE")) { /* we emit a warning if the argument defines the 'in' typemap, but not the 'typecheck' one */ Swig_warning(WARN_TYPEMAP_TYPECHECK_UNDEF, Getfile(ni), Getline(ni), "Overloaded method %s with no explicit typecheck typemap for arg %d of type '%s'.\n", Swig_name_decl(n), j, SwigType_str(Getattr(pj, "type"), 0)); Swig_warning(WARN_TYPEMAP_TYPECHECK_UNDEF, Getfile(ni), Getline(ni), "Dispatching calls to this method may not work correctly, see the 'Typemaps and Overloading' section in the Typemaps chapter of the SWIG documentation.\n"); } Parm *pj1 = Getattr(pj, "tmap:in:next"); if (pj1) pj = pj1; else pj = nextSibling(pj); j++; } } /* close braces */ for ( /* empty */ ; num_braces > 0; num_braces--) Printf(f, "}\n"); // The language module may want to generate different code for last overloaded function called (with same number of arguments) String *lfmt = ReplaceFormat(!emitcheck && fmt_fastdispatch ? fmt_fastdispatch : fmt, num_arguments); Printf(f, Char(lfmt), Getattr(ni, "wrap:name")); Printf(f, "}\n"); /* braces closes "if" for this method */ if (fn) Printf(f, "check_%d:\n\n", fn); if (implicitconvtypecheckoff) Delattr(ni, "implicitconvtypecheckoff"); Delete(lfmt); Delete(coll); } Delete(dispatch); return f; } String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted, const_String_or_char_ptr fmt_fastdispatch) { if (fast_dispatch_mode || GetFlag(n, "feature:fastdispatch")) { return overload_dispatch_fast(n, fmt, maxargs, check_emitted, fmt_fastdispatch); } int i, j; *maxargs = 0; *check_emitted = false; String *f = NewString(""); /* Get a list of methods ranked by precedence values and argument count */ List *dispatch = Swig_overload_rank(n, true); int nfunc = Len(dispatch); /* Loop over the functions */ for (i = 0; i < nfunc; i++) { Node *ni = Getitem(dispatch, i); Parm *pi = Getattr(ni, "wrap:parms"); bool implicitconvtypecheckoff = GetFlag(ni, "implicitconvtypecheckoff") != 0; int num_required = emit_num_required(pi); int num_arguments = emit_num_arguments(pi); if (GetFlag(n, "wrap:this")) { num_required++; num_arguments++; } if (num_arguments > *maxargs) *maxargs = num_arguments; if (num_required == num_arguments) { Printf(f, "if (%s == %d) {\n", argc_template_string, num_required); } else { Printf(f, "if ((%s >= %d) && (%s <= %d)) {\n", argc_template_string, num_required, argc_template_string, num_arguments); } if (num_arguments) { Printf(f, "int _v = 0;\n"); } int num_braces = 0; j = 0; Parm *pj = pi; while (pj) { if (checkAttribute(pj, "tmap:in:numinputs", "0")) { pj = Getattr(pj, "tmap:in:next"); continue; } *check_emitted = true; if (j >= num_required) { String *lfmt = ReplaceFormat(fmt, num_arguments); Printf(f, "if (%s <= %d) {\n", argc_template_string, j); Printf(f, Char(lfmt), Getattr(ni, "wrap:name")); Printf(f, "}\n"); Delete(lfmt); } if (print_typecheck(f, (GetFlag(n, "wrap:this") ? j + 1 : j), pj, implicitconvtypecheckoff)) { Printf(f, "if (_v) {\n"); num_braces++; } if (!Getattr(pj, "tmap:in:SWIGTYPE") && Getattr(pj, "tmap:typecheck:SWIGTYPE")) { /* we emit a warning if the argument defines the 'in' typemap, but not the 'typecheck' one */ Swig_warning(WARN_TYPEMAP_TYPECHECK_UNDEF, Getfile(ni), Getline(ni), "Overloaded method %s with no explicit typecheck typemap for arg %d of type '%s'.\n", Swig_name_decl(n), j, SwigType_str(Getattr(pj, "type"), 0)); Swig_warning(WARN_TYPEMAP_TYPECHECK_UNDEF, Getfile(ni), Getline(ni), "Dispatching calls to this method may not work correctly, see the 'Typemaps and Overloading' section in the Typemaps chapter of the SWIG documentation.\n"); } Parm *pk = Getattr(pj, "tmap:in:next"); if (pk) pj = pk; else pj = nextSibling(pj); j++; } String *lfmt = ReplaceFormat(fmt, num_arguments); Printf(f, Char(lfmt), Getattr(ni, "wrap:name")); Delete(lfmt); /* close braces */ for ( /* empty */ ; num_braces > 0; num_braces--) Printf(f, "}\n"); Printf(f, "}\n"); /* braces closes "if" for this method */ if (implicitconvtypecheckoff) Delattr(ni, "implicitconvtypecheckoff"); } Delete(dispatch); return f; } /* ----------------------------------------------------------------------------- * Swig_overload_check() * ----------------------------------------------------------------------------- */ void Swig_overload_check(Node *n) { Swig_overload_rank(n, false); } swig-4.4.0/Source/Modules/java.cxx0000664000175000017500000055434015075443613016710 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * java.cxx * * Java language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include #include // for INT_MAX #include #include "javadoc.h" /* Hash type used for upcalls from C/C++ */ typedef DOH UpcallData; class JAVA:public Language { static const char *usage; const String *empty_string; const String *public_string; const String *protected_string; Hash *swig_types_hash; File *f_begin; File *f_runtime; File *f_runtime_h; File *f_header; File *f_wrappers; File *f_init; File *f_directors; File *f_directors_h; List *filenames_list; bool proxy_flag; // Flag for generating proxy classes bool nopgcpp_flag; // Flag for suppressing the premature garbage collection prevention parameter bool native_function_flag; // Flag for when wrapping a native function bool enum_constant_flag; // Flag for when wrapping an enum or constant bool static_flag; // Flag for when wrapping a static functions or member variables bool variable_wrapper_flag; // Flag for when wrapping a nonstatic member variable bool wrapping_member_flag; // Flag for when wrapping a member variable/enum/const bool global_variable_flag; // Flag for when wrapping a global variable bool old_variable_names; // Flag for old style variable names in the intermediary class bool member_func_flag; // flag set when wrapping a member function bool doxygen; //flag for converting found doxygen to javadoc bool comment_creation_chatter; //flag for getting information about where comments were created in java.cxx String *imclass_name; // intermediary class name String *module_class_name; // module class name String *constants_interface_name; // constants interface name String *imclass_class_code; // intermediary class code String *proxy_class_def; String *proxy_class_code; String *interface_class_code; // if %feature("interface") was declared for a class, here goes the interface declaration String *module_class_code; String *proxy_class_name; // proxy class name String *full_proxy_class_name;// fully qualified proxy class name when using nspace feature, otherwise same as proxy_class_name String *full_imclass_name; // fully qualified intermediary class name when using nspace feature, otherwise same as imclass_name String *variable_name; //Name of a variable being wrapped String *proxy_class_constants_code; String *module_class_constants_code; String *common_begin_code; String *enum_code; String *package; // Optional package name String *jnipackage; // Package name used in the JNI code String *package_path; // Package name used internally by JNI (slashes) String *imclass_imports; //intermediary class imports from %pragma String *module_imports; //module imports from %pragma String *imclass_baseclass; //inheritance for intermediary class class from %pragma String *imclass_package; //package in which to generate the intermediary class String *module_baseclass; //inheritance for module class from %pragma String *imclass_interfaces; //interfaces for intermediary class class from %pragma String *module_interfaces; //interfaces for module class from %pragma String *imclass_class_modifiers; //class modifiers for intermediary class overridden by %pragma String *module_class_modifiers; //class modifiers for module class overridden by %pragma String *constants_modifiers; //access modifiers for constants interface overridden by %pragma String *upcasts_code; //C++ casts for inheritance hierarchies C++ code String *imclass_cppcasts_code; //C++ casts up inheritance hierarchies intermediary class code String *imclass_directors; // Intermediate class director code String *destructor_call; //C++ destructor call if any String *destructor_throws_clause; //C++ destructor throws clause if any // Director method stuff: List *dmethods_seq; Hash *dmethods_table; int n_dmethods; int n_directors; int first_class_dmethod; int curr_class_dmethod; int nesting_depth; enum EnumFeature { SimpleEnum, TypeunsafeEnum, TypesafeEnum, ProperEnum }; public: /* ----------------------------------------------------------------------------- * JAVA() * ----------------------------------------------------------------------------- */ JAVA():empty_string(NewString("")), public_string(NewString("public")), protected_string(NewString("protected")), swig_types_hash(NULL), f_begin(NULL), f_runtime(NULL), f_runtime_h(NULL), f_header(NULL), f_wrappers(NULL), f_init(NULL), f_directors(NULL), f_directors_h(NULL), filenames_list(NULL), proxy_flag(true), nopgcpp_flag(false), native_function_flag(false), enum_constant_flag(false), static_flag(false), variable_wrapper_flag(false), wrapping_member_flag(false), global_variable_flag(false), old_variable_names(false), member_func_flag(false), doxygen(false), comment_creation_chatter(false), imclass_name(NULL), module_class_name(NULL), constants_interface_name(NULL), imclass_class_code(NULL), proxy_class_def(NULL), proxy_class_code(NULL), interface_class_code(NULL), module_class_code(NULL), proxy_class_name(NULL), full_proxy_class_name(NULL), full_imclass_name(NULL), variable_name(NULL), proxy_class_constants_code(NULL), module_class_constants_code(NULL), common_begin_code(NULL), enum_code(NULL), package(NULL), jnipackage(NULL), package_path(NULL), imclass_imports(NULL), module_imports(NULL), imclass_baseclass(NULL), imclass_package(NULL), module_baseclass(NULL), imclass_interfaces(NULL), module_interfaces(NULL), imclass_class_modifiers(NULL), module_class_modifiers(NULL), constants_modifiers(NULL), upcasts_code(NULL), imclass_cppcasts_code(NULL), imclass_directors(NULL), destructor_call(NULL), destructor_throws_clause(NULL), dmethods_seq(NULL), dmethods_table(NULL), n_dmethods(0), n_directors(0), first_class_dmethod(0), curr_class_dmethod(0), nesting_depth(0){ /* for now, multiple inheritance in directors is disabled, this should be easy to implement though */ director_multiple_inheritance = 0; directorLanguage(); } ~JAVA() { delete doxygenTranslator; } /* ----------------------------------------------------------------------------- * constructIntermediateClassName() * * Construct the fully qualified name of the intermediate class and set * the full_imclass_name attribute accordingly. * ----------------------------------------------------------------------------- */ void constructIntermediateClassName(Node *n) { String *nspace = Getattr(n, "sym:nspace"); if (imclass_package && package) full_imclass_name = NewStringf("%s.%s.%s", package, imclass_package, imclass_name); else if (package && nspace) full_imclass_name = NewStringf("%s.%s", package, imclass_name); else if (imclass_package) full_imclass_name = NewStringf("%s.%s", imclass_package, imclass_name); else full_imclass_name = NewStringf("%s", imclass_name); if (nspace && !package) { String *name = Getattr(n, "name") ? Getattr(n, "name") : NewString(""); Swig_warning(WARN_JAVA_NSPACE_WITHOUT_PACKAGE, Getfile(n), Getline(n), "The nspace feature is used on '%s' without -package. " "The generated code may not compile as Java does not support types declared in a named package accessing types declared in an unnamed package.\n", SwigType_namestr(name)); } } /* ----------------------------------------------------------------------------- * getProxyName() * * Test to see if a type corresponds to something wrapped with a proxy class. * Return NULL if not otherwise the proxy class name, fully qualified with * package name if the nspace feature is used, unless jnidescriptor is true as * the package name is handled differently (unfortunately for legacy reasons). * ----------------------------------------------------------------------------- */ String *getProxyName(SwigType *t, bool jnidescriptor = false) { String *proxyname = NULL; if (proxy_flag) { Node *n = classLookup(t); if (n) { proxyname = Getattr(n, "proxyname"); if (!proxyname || jnidescriptor) { String *nspace = Getattr(n, "sym:nspace"); String *symname = Copy(Getattr(n, "sym:name")); if (symname && !GetFlag(n, "feature:flatnested")) { for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) { if (String* name = Getattr(outer_class, "sym:name")) { Push(symname, jnidescriptor ? "$" : "."); Push(symname, name); } else return NULL; } } if (nspace) { if (package && !jnidescriptor) proxyname = NewStringf("%s.%s.%s", package, nspace, symname); else proxyname = NewStringf("%s.%s", nspace, symname); } else { proxyname = Copy(symname); } if (!jnidescriptor) { Setattr(n, "proxyname", proxyname); // Cache it Delete(proxyname); } Delete(symname); } } } return proxyname; } /* ----------------------------------------------------------------------------- * makeValidJniName() * ----------------------------------------------------------------------------- */ String *makeValidJniName(const String *name) { String *valid_jni_name = NewString(name); Replaceall(valid_jni_name, "_", "_1"); return valid_jni_name; } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { SWIG_library_directory("java"); int doxygen_translator_flags = 0; // Look for certain 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(""); Printf(package, argv[i + 1]); if (Len(package) == 0) { Delete(package); package = 0; } Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if ((strcmp(argv[i], "-doxygen") == 0)) { Swig_mark_arg(i); doxygen = true; scan_doxygen_comments = true; } else if ((strcmp(argv[i], "-debug-doxygen-translator") == 0)) { Swig_mark_arg(i); doxygen_translator_flags |= DoxygenTranslator::debug_translator; } else if ((strcmp(argv[i], "-debug-doxygen-parser") == 0)) { Swig_mark_arg(i); doxygen_translator_flags |= DoxygenTranslator::debug_parser; } else if ((strcmp(argv[i], "-noproxy") == 0)) { Swig_mark_arg(i); proxy_flag = false; } else if (strcmp(argv[i], "-nopgcpp") == 0) { Swig_mark_arg(i); nopgcpp_flag = true; } else if (strcmp(argv[i], "-oldvarnames") == 0) { Swig_mark_arg(i); old_variable_names = true; } else if (strcmp(argv[i], "-help") == 0) { Printf(stdout, "%s\n", usage); } } } if (doxygen) doxygenTranslator = new JavaDocConverter(doxygen_translator_flags); // Add a symbol to the parser for conditional compilation Preprocessor_define("SWIGJAVA 1", 0); SWIG_config_file("java.swg"); allow_overloading(); Swig_interface_feature_enable(); } /* --------------------------------------------------------------------- * top() * --------------------------------------------------------------------- */ virtual int top(Node *n) { // Get any options set in the module directive Node *module = Getattr(n, "module"); Node *optionsnode = Getattr(module, "options"); if (optionsnode) { if (Getattr(optionsnode, "jniclassname")) imclass_name = Copy(Getattr(optionsnode, "jniclassname")); /* check if directors are enabled for this module. note: this * is a "master" switch, without which no director code will be * emitted. %feature("director") statements are also required * to enable directors for individual classes or methods. * * use %module(directors="1") modulename at the start of the * interface file to enable director generation. */ if (Getattr(optionsnode, "directors")) { allow_directors(); } if (Getattr(optionsnode, "dirprot")) { allow_dirprot(); } allow_allprotected(GetFlag(optionsnode, "allprotected")); common_begin_code = Getattr(optionsnode, "javabegin"); if (common_begin_code) Printf(common_begin_code, "\n"); } /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = Getattr(n, "outfile_h"); if (!outfile) { Printf(stderr, "Unable to determine outfile\n"); Exit(EXIT_FAILURE); } f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } if (Swig_directors_enabled()) { if (!outfile_h) { Printf(stderr, "Unable to determine outfile_h\n"); Exit(EXIT_FAILURE); } f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); swig_types_hash = NewHash(); filenames_list = NewList(); // Make the intermediary class and module class names. The intermediary class name can be set in the module directive. if (!imclass_name) { imclass_name = NewStringf("%sJNI", Getattr(n, "name")); module_class_name = Copy(Getattr(n, "name")); } else { // Rename the module name if it is the same as intermediary class name - a backwards compatibility solution if (Cmp(imclass_name, Getattr(n, "name")) == 0) module_class_name = NewStringf("%sModule", Getattr(n, "name")); else module_class_name = Copy(Getattr(n, "name")); } constants_interface_name = NewStringf("%sConstants", module_class_name); // module class and intermediary classes are always created if (!addSymbol(imclass_name, module)) return SWIG_ERROR; if (!addSymbol(module_class_name, module)) return SWIG_ERROR; imclass_class_code = NewString(""); proxy_class_def = NewString(""); proxy_class_code = NewString(""); module_class_constants_code = NewString(""); imclass_baseclass = NewString(""); imclass_package = NULL; imclass_interfaces = NewString(""); imclass_class_modifiers = NewString(""); module_class_code = NewString(""); module_baseclass = NewString(""); module_interfaces = NewString(""); module_imports = NewString(""); module_class_modifiers = NewString(""); constants_modifiers = NewString(""); imclass_imports = NewString(""); imclass_cppcasts_code = NewString(""); imclass_directors = NewString(""); upcasts_code = NewString(""); dmethods_seq = NewList(); dmethods_table = NewHash(); n_dmethods = 0; n_directors = 0; jnipackage = NewString(""); package_path = NewString(""); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "JAVA"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); /* Emit initial director header and director code: */ Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module_class_name); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module_class_name); Printf(f_directors, "\n\n"); Printf(f_directors, "/* ---------------------------------------------------\n"); Printf(f_directors, " * C++ director class methods\n"); Printf(f_directors, " * --------------------------------------------------- */\n\n"); if (outfile_h) { String *filename = Swig_file_filename(outfile_h); Printf(f_directors, "#include \"%s\"\n\n", filename); Delete(filename); } } Printf(f_runtime, "\n"); String *wrapper_name = NewString(""); if (package) { String *jniname = makeValidJniName(package); Printv(jnipackage, jniname, NIL); Delete(jniname); Replaceall(jnipackage, ".", "_"); Append(jnipackage, "_"); Printv(package_path, package, NIL); Replaceall(package_path, ".", "/"); } String *jniname = makeValidJniName(imclass_name); Printf(wrapper_name, "Java_%s%s_%%f", jnipackage, jniname); Delete(jniname); Swig_name_register("wrapper", Char(wrapper_name)); if (old_variable_names) { Swig_name_register("set", "set_%n%v"); Swig_name_register("get", "get_%n%v"); } Delete(wrapper_name); Printf(f_wrappers, "\n#ifdef __cplusplus\n"); Printf(f_wrappers, "extern \"C\" {\n"); Printf(f_wrappers, "#endif\n\n"); /* Emit code */ Language::top(n); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (make it occur before %header section) Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } // Generate the intermediary class { String *filen = NewStringf("%s%s.java", outputDirectory(imclass_package), imclass_name); File *f_im = NewFile(filen, "w", SWIG_output_files()); if (!f_im) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; // Start writing out the intermediary class file emitBanner(f_im); if (imclass_package && package) Printf(f_im, "package %s.%s;", package, imclass_package); else if (imclass_package) Printf(f_im, "package %s;", imclass_package); else if (package) Printf(f_im, "package %s;\n", package); if (imclass_imports) Printf(f_im, "%s\n", imclass_imports); if (Len(imclass_class_modifiers) > 0) Printf(f_im, "%s ", imclass_class_modifiers); Printf(f_im, "%s ", imclass_name); if (imclass_baseclass && *Char(imclass_baseclass)) Printf(f_im, "extends %s ", imclass_baseclass); if (Len(imclass_interfaces) > 0) Printv(f_im, "implements ", imclass_interfaces, " ", NIL); Printf(f_im, "{\n"); // Add the intermediary class methods Replaceall(imclass_class_code, "$module", module_class_name); Replaceall(imclass_class_code, "$imclassname", imclass_name); Printv(f_im, imclass_class_code, NIL); Printv(f_im, imclass_cppcasts_code, NIL); if (Len(imclass_directors) > 0) Printv(f_im, "\n", imclass_directors, NIL); if (n_dmethods > 0) { Putc('\n', f_im); Printf(f_im, " private final static native void swig_module_init();\n"); Printf(f_im, " static {\n"); Printf(f_im, " swig_module_init();\n"); Printf(f_im, " }\n"); } // Finish off the class Printf(f_im, "}\n"); Delete(f_im); } // Generate the Java module class { String *filen = NewStringf("%s%s.java", SWIG_output_directory(), module_class_name); File *f_module = NewFile(filen, "w", SWIG_output_files()); if (!f_module) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; // Start writing out the module class file emitBanner(f_module); if (package) Printf(f_module, "package %s;\n", package); if (module_imports) Printf(f_module, "%s\n", module_imports); if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, 0); if (comment_creation_chatter) Printf(f_module, "/* This was generated from top() */\n"); Printv(f_module, Char(doxygen_comments), NIL); Delete(doxygen_comments); } if (Len(module_class_modifiers) > 0) Printf(f_module, "%s ", module_class_modifiers); Printf(f_module, "%s ", module_class_name); if (module_baseclass && *Char(module_baseclass)) Printf(f_module, "extends %s ", module_baseclass); if (Len(module_interfaces) > 0) { if (Len(module_class_constants_code) != 0) Printv(f_module, "implements ", constants_interface_name, ", ", module_interfaces, " ", NIL); else Printv(f_module, "implements ", module_interfaces, " ", NIL); } else { if (Len(module_class_constants_code) != 0) Printv(f_module, "implements ", constants_interface_name, " ", NIL); } Printf(f_module, "{\n"); Replaceall(module_class_code, "$module", module_class_name); Replaceall(module_class_constants_code, "$module", module_class_name); Replaceall(module_class_code, "$imclassname", imclass_name); Replaceall(module_class_constants_code, "$imclassname", imclass_name); // Add the wrapper methods Printv(f_module, module_class_code, NIL); // Finish off the class Printf(f_module, "}\n"); Delete(f_module); } // Generate the Java constants interface if (Len(module_class_constants_code) != 0) { String *filen = NewStringf("%s%s.java", SWIG_output_directory(), constants_interface_name); File *f_module = NewFile(filen, "w", SWIG_output_files()); if (!f_module) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; // Start writing out the Java constants interface file emitBanner(f_module); if (package) Printf(f_module, "package %s;\n", package); if (module_imports) Printf(f_module, "%s\n", module_imports); if (Len(constants_modifiers) > 0) Printf(f_module, "%s ", constants_modifiers); Printf(f_module, "%s {\n", constants_interface_name); // Write out all the global constants Printv(f_module, module_class_constants_code, NIL); // Finish off the Java interface Printf(f_module, "}\n"); Delete(f_module); } if (upcasts_code) Printv(f_wrappers, upcasts_code, NIL); emitDirectorUpcalls(); Printf(f_wrappers, "#ifdef __cplusplus\n"); Printf(f_wrappers, "}\n"); Printf(f_wrappers, "#endif\n"); // Output a Java type wrapper class for each SWIG type for (Iterator swig_type = First(swig_types_hash); swig_type.key; swig_type = Next(swig_type)) { emitTypeWrapperClass(swig_type.key, swig_type.item); } // Check for overwriting file problems on filesystems that are case insensitive Iterator it1; Iterator it2; for (it1 = First(filenames_list); it1.item; it1 = Next(it1)) { String *item1_lower = Swig_string_lower(it1.item); for (it2 = Next(it1); it2.item; it2 = Next(it2)) { String *item2_lower = Swig_string_lower(it2.item); if (it1.item && it2.item) { if (Strcmp(item1_lower, item2_lower) == 0) { Swig_warning(WARN_LANG_PORTABILITY_FILENAME, input_file, line_number, "Portability warning: File %s will be overwritten by %s on case insensitive filesystems such as " "Windows' FAT32 and NTFS unless the class/module name is renamed\n", it1.item, it2.item); } } Delete(item2_lower); } Delete(item1_lower); } Delete(swig_types_hash); swig_types_hash = NULL; Delete(filenames_list); filenames_list = NULL; Delete(imclass_name); imclass_name = NULL; Delete(imclass_class_code); imclass_class_code = NULL; Delete(proxy_class_def); proxy_class_def = NULL; Delete(proxy_class_code); proxy_class_code = NULL; Delete(module_class_constants_code); module_class_constants_code = NULL; Delete(imclass_baseclass); imclass_baseclass = NULL; Delete(imclass_package); imclass_package = NULL; Delete(imclass_interfaces); imclass_interfaces = NULL; Delete(imclass_class_modifiers); imclass_class_modifiers = NULL; Delete(module_class_name); module_class_name = NULL; Delete(constants_interface_name); constants_interface_name = NULL; Delete(module_class_code); module_class_code = NULL; Delete(module_baseclass); module_baseclass = NULL; Delete(module_interfaces); module_interfaces = NULL; Delete(module_imports); module_imports = NULL; Delete(module_class_modifiers); module_class_modifiers = NULL; Delete(constants_modifiers); constants_modifiers = NULL; Delete(imclass_imports); imclass_imports = NULL; Delete(imclass_cppcasts_code); imclass_cppcasts_code = NULL; Delete(imclass_directors); imclass_directors = NULL; Delete(upcasts_code); upcasts_code = NULL; Delete(package); package = NULL; Delete(jnipackage); jnipackage = NULL; Delete(package_path); package_path = NULL; Delete(dmethods_seq); dmethods_seq = NULL; Delete(dmethods_table); dmethods_table = NULL; n_dmethods = 0; /* Close all of the files */ Dump(f_header, f_runtime); if (Swig_directors_enabled()) { Dump(f_directors, f_runtime); Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); Delete(f_runtime_h); f_runtime_h = NULL; Delete(f_directors); f_directors = NULL; Delete(f_directors_h); f_directors_h = NULL; } Dump(f_wrappers, f_runtime); Wrapper_pretty_print(f_init, f_runtime); Delete(f_header); Delete(f_wrappers); Delete(f_init); Dump(f_runtime, f_begin); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* ----------------------------------------------------------------------------- * emitBanner() * ----------------------------------------------------------------------------- */ void emitBanner(File *f) { Printf(f, "/* ----------------------------------------------------------------------------\n"); Swig_banner_target_lang(f, " *"); Printf(f, " * ----------------------------------------------------------------------------- */\n\n"); Printv(f, common_begin_code, NIL); } /*----------------------------------------------------------------------- * Add new director upcall signature *----------------------------------------------------------------------*/ UpcallData *addUpcallMethod(String *imclass_method, String *class_method, String *imclass_desc, String *class_desc, String *decl) { String *key = NewStringf("%s|%s", imclass_method, decl); ++curr_class_dmethod; String *imclass_methodidx = NewStringf("%d", n_dmethods); String *class_methodidx = NewStringf("%d", n_dmethods - first_class_dmethod); n_dmethods++; Hash *new_udata = NewHash(); Append(dmethods_seq, new_udata); Setattr(dmethods_table, key, new_udata); Setattr(new_udata, "method", Copy(class_method)); Setattr(new_udata, "fdesc", Copy(class_desc)); Setattr(new_udata, "imclass_method", Copy(imclass_method)); Setattr(new_udata, "imclass_fdesc", Copy(imclass_desc)); Setattr(new_udata, "imclass_methodidx", imclass_methodidx); Setattr(new_udata, "class_methodidx", class_methodidx); Setattr(new_udata, "decl", Copy(decl)); Delete(key); return new_udata; } /*----------------------------------------------------------------------- * Get director upcall signature *----------------------------------------------------------------------*/ UpcallData *getUpcallMethodData(String *director_class, String *decl) { String *key = NewStringf("%s|%s", director_class, decl); UpcallData *udata = Getattr(dmethods_table, key); Delete(key); return udata; } /* ---------------------------------------------------------------------- * nativeWrapper() * ---------------------------------------------------------------------- */ virtual int nativeWrapper(Node *n) { String *wrapname = Getattr(n, "wrap:name"); if (!addSymbol(wrapname, n, imclass_name)) return SWIG_ERROR; if (Getattr(n, "type")) { Swig_save("nativeWrapper", n, "name", NIL); Setattr(n, "name", wrapname); native_function_flag = true; functionWrapper(n); Swig_restore(n); native_function_flag = false; } else { Swig_error(input_file, line_number, "No return type for %%native method %s.\n", Getattr(n, "wrap:name")); } return SWIG_OK; } /* ---------------------------------------------------------------------- * functionWrapper() * ---------------------------------------------------------------------- */ virtual int functionWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *c_return_type = NewString(""); String *im_return_type = NewString(""); String *cleanup = NewString(""); String *outarg = NewString(""); String *body = NewString(""); int num_arguments = 0; int gencomma = 0; bool is_void_return; String *overloaded_name = getOverloadedName(n); String *nondir_args = NewString(""); bool is_destructor = (Cmp(Getattr(n, "nodeType"), "destructor") == 0); if (!Getattr(n, "sym:overloaded")) { if (!addSymbol(symname, n, imclass_name)) return SWIG_ERROR; } /* The rest of this function deals with generating the intermediary class wrapper function (that wraps a c/c++ function) and generating the JNI c code. Each Java wrapper function has a matching JNI c function call. */ // A new wrapper function object Wrapper *f = NewWrapper(); // Make a wrapper name for this function String *jniname = makeValidJniName(overloaded_name); String *wname = Swig_name_wrapper(jniname); Delete(jniname); /* Attach the non-standard typemaps to the parameter list. */ Swig_typemap_attach_parms("jni", l, f); Swig_typemap_attach_parms("jtype", l, f); Swig_typemap_attach_parms("jstype", l, f); /* Get return types */ if ((tm = Swig_typemap_lookup("jni", n, "", 0))) { Printf(c_return_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JNI_UNDEF, input_file, line_number, "No jni typemap defined for %s\n", SwigType_str(returntype, 0)); } if ((tm = Swig_typemap_lookup("jtype", n, "", 0))) { Printf(im_return_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JTYPE_UNDEF, input_file, line_number, "No jtype typemap defined for %s\n", SwigType_str(returntype, 0)); } is_void_return = Cmp(c_return_type, "void") == 0; if (!is_void_return) Wrapper_add_localv(f, "jresult", c_return_type, "jresult = 0", NIL); Printv(f->def, "SWIGEXPORT ", c_return_type, " JNICALL ", wname, "(JNIEnv *jenv, jclass jcls", NIL); // Usually these function parameters are unused - The code below ensures // that compilers do not issue such a warning if configured to do so. Printv(f->code, " (void)jenv;\n", NIL); Printv(f->code, " (void)jcls;\n", NIL); // Emit all of the local variables for holding arguments. emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); // Parameter overloading Setattr(n, "wrap:parms", l); Setattr(n, "wrap:name", wname); // Wrappers not wanted for some methods where the parameters cannot be overloaded in Java if (Getattr(n, "sym:overloaded")) { // Emit warnings for the few cases that can't be overloaded in Java and give up on generating wrapper Swig_overload_check(n); if (Getattr(n, "overload:ignore")) { DelWrapper(f); return SWIG_OK; } } Printf(imclass_class_code, " public final static native %s %s(", im_return_type, overloaded_name); num_arguments = emit_num_arguments(l); // Now walk the function parameter list and generate code to get arguments for (i = 0, p = l; i < num_arguments; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); String *im_param_type = NewString(""); String *c_param_type = NewString(""); String *arg = NewString(""); Printf(arg, "j%s", ln); /* Get the JNI C types of the parameter */ if ((tm = Getattr(p, "tmap:jni"))) { Printv(c_param_type, tm, NIL); } else { Swig_warning(WARN_JAVA_TYPEMAP_JNI_UNDEF, input_file, line_number, "No jni typemap defined for %s\n", SwigType_str(pt, 0)); } /* Get the intermediary class parameter types of the parameter */ if ((tm = Getattr(p, "tmap:jtype"))) { Printv(im_param_type, tm, NIL); } else { Swig_warning(WARN_JAVA_TYPEMAP_JTYPE_UNDEF, input_file, line_number, "No jtype typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to intermediary class method */ if (gencomma) Printf(imclass_class_code, ", "); Printf(imclass_class_code, "%s %s", im_param_type, arg); // Add parameter to C function Printv(f->def, ", ", c_param_type, " ", arg, NIL); ++gencomma; // Premature garbage collection prevention parameter if (!is_destructor) { String *pgc_parameter = prematureGarbageCollectionPreventionParameter(pt, p); if (pgc_parameter) { Printf(imclass_class_code, ", %s %s_", pgc_parameter, arg); Printf(f->def, ", jobject %s_", arg); Printf(f->code, " (void)%s_;\n", arg); } } // Get typemap for this argument if ((tm = Getattr(p, "tmap:in"))) { addThrows(n, "tmap:in", p); Replaceall(tm, "$arg", arg); /* deprecated? */ Replaceall(tm, "$input", arg); Setattr(p, "emit:input", arg); Printf(nondir_args, "%s\n", tm); p = Getattr(p, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); p = nextSibling(p); } Delete(im_param_type); Delete(c_param_type); Delete(arg); } Printv(f->code, nondir_args, NIL); Delete(nondir_args); /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { addThrows(n, "tmap:check", p); Replaceall(tm, "$arg", Getattr(p, "emit:input")); /* deprecated? */ Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { addThrows(n, "tmap:freearg", p); Replaceall(tm, "$arg", Getattr(p, "emit:input")); /* deprecated? */ Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { addThrows(n, "tmap:argout", p); Replaceall(tm, "$arg", Getattr(p, "emit:input")); /* deprecated? */ Replaceall(tm, "$result", "jresult"); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } // Get any Java exception classes in the throws typemap ParmList *throw_parm_list = NULL; if ((throw_parm_list = Getattr(n, "catchlist"))) { Swig_typemap_attach_parms("throws", throw_parm_list, f); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { addThrows(n, "tmap:throws", p); } } } // Now write code to make the function call if (!native_function_flag) { Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); // Handle exception classes specified in the "except" feature's "throws" attribute addThrows(n, "feature:except", n); /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { addThrows(n, "tmap:out", n); Replaceall(tm, "$result", "jresult"); if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "1"); else Replaceall(tm, "$owner", "0"); Printf(f->code, "%s", tm); if (Len(tm)) Printf(f->code, "\n"); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), Getattr(n, "name")); } emit_return_variable(n, returntype, f); } /* Output argument output code */ Printv(f->code, outarg, NIL); /* Output cleanup code */ Printv(f->code, cleanup, NIL); /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { addThrows(n, "tmap:newfree", n); Printf(f->code, "%s\n", tm); } } /* See if there is any return cleanup code */ if (!native_function_flag) { if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { addThrows(n, "tmap:ret", n); Printf(f->code, "%s\n", tm); } } /* Finish C function and intermediary class function definitions */ Printf(imclass_class_code, ")"); generateThrowsClause(n, imclass_class_code); Printf(imclass_class_code, ";\n"); Printf(f->def, ") {"); if (!is_void_return) Printv(f->code, " return jresult;\n", NIL); Printf(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); Replaceall(f->code, "$isvoid", is_void_return ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", symname); /* Contract macro modification */ Replaceall(f->code, "SWIG_contract_assert(", "SWIG_contract_assert($null, "); if (!is_void_return) Replaceall(f->code, "$null", "0"); else Replaceall(f->code, "$null", ""); /* Dump the function out */ if (!native_function_flag) Wrapper_print(f, f_wrappers); if (!(proxy_flag && is_wrapping_class()) && !enum_constant_flag) { moduleClassFunctionHandler(n); } /* * Generate the proxy class getters/setters for public member variables. * Not for enums and constants. */ if (proxy_flag && wrapping_member_flag && !enum_constant_flag) { // Capitalize the first letter in the variable to create a JavaBean type getter/setter function name bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) != 0; String *getter_setter_name = NewString(""); if (!getter_flag) Printf(getter_setter_name, "set"); else Printf(getter_setter_name, "get"); Putc(toupper((int) *Char(variable_name)), getter_setter_name); Printf(getter_setter_name, "%s", Char(variable_name) + 1); Setattr(n, "proxyfuncname", getter_setter_name); Setattr(n, "imfuncname", symname); proxyClassFunctionHandler(n); Delete(getter_setter_name); } Delete(c_return_type); Delete(im_return_type); Delete(cleanup); Delete(outarg); Delete(body); Delete(overloaded_name); DelWrapper(f); return SWIG_OK; } /* ----------------------------------------------------------------------- * variableWrapper() * ----------------------------------------------------------------------- */ virtual int variableWrapper(Node *n) { variable_wrapper_flag = true; Language::variableWrapper(n); /* Default to functions */ variable_wrapper_flag = false; return SWIG_OK; } /* ----------------------------------------------------------------------- * globalvariableHandler() * ------------------------------------------------------------------------ */ virtual int globalvariableHandler(Node *n) { variable_name = Getattr(n, "sym:name"); global_variable_flag = true; int ret = Language::globalvariableHandler(n); global_variable_flag = false; return ret; } String *getCurrentScopeName(String *nspace) { String *scope = 0; if (nspace || getCurrentClass()) { scope = NewString(""); if (nspace) Printf(scope, "%s", nspace); if (Node* cls = getCurrentClass()) { if (Node *outer = Getattr(cls, "nested:outer")) { String *outerClassesPrefix = Copy(Getattr(outer, "sym:name")); for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) { Push(outerClassesPrefix, "."); Push(outerClassesPrefix, Getattr(outer, "sym:name")); } Printv(scope, nspace ? "." : "", outerClassesPrefix, ".", proxy_class_name, NIL); Delete(outerClassesPrefix); } else Printv(scope, nspace ? "." : "", proxy_class_name, NIL); } } return scope; } /* ---------------------------------------------------------------------- * enumDeclaration() * * C/C++ enums can be mapped in one of 4 ways, depending on the java:enum feature specified: * 1) Simple enums - simple constant within the proxy class or module class * 2) Typeunsafe enums - simple constant in a Java class (class named after the c++ enum name) * 3) Typesafe enum - typesafe enum pattern (class named after the c++ enum name) * 4) Proper enums - proper Java enum * Anonymous enums always default to 1) * ---------------------------------------------------------------------- */ virtual int enumDeclaration(Node *n) { if (!ImportMode) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; String *nspace = Getattr(n, "sym:nspace"); // NSpace/getNSpace() only works during Language::enumDeclaration call enum_code = NewString(""); String *symname = Getattr(n, "sym:name"); String *constants_code = (proxy_flag && is_wrapping_class())? proxy_class_constants_code : module_class_constants_code; EnumFeature enum_feature = decodeEnumFeature(n); String *typemap_lookup_type = Getattr(n, "name"); if ((enum_feature != SimpleEnum) && symname && typemap_lookup_type) { // Wrap (non-anonymous) C/C++ enum within a typesafe, typeunsafe or proper Java enum if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, 0); if (comment_creation_chatter) Printf(enum_code, "/* This was generated from enumDeclaration() */\n"); Printv(enum_code, Char(doxygen_comments), NIL); Delete(doxygen_comments); } String *scope = getCurrentScopeName(nspace); if (!addSymbol(symname, n, scope)) return SWIG_ERROR; // Pure Java baseclass and interfaces const String *pure_baseclass = typemapLookup(n, "javabase", typemap_lookup_type, WARN_NONE); const String *pure_interfaces = typemapLookup(n, "javainterfaces", typemap_lookup_type, WARN_NONE); // Emit the enum Printv(enum_code, typemapLookup(n, "javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers (enum modifiers really) " ", symname, *Char(pure_baseclass) ? // Bases " extends " : "", pure_baseclass, *Char(pure_interfaces) ? // Interfaces " implements " : "", pure_interfaces, " {\n", NIL); if (proxy_flag && is_wrapping_class()) Replaceall(enum_code, "$static ", "static "); else Replaceall(enum_code, "$static ", ""); Delete(scope); } else { if (symname && !Getattr(n, "unnamedinstance")) Printf(constants_code, " // %s \n", symname); // Translate and write javadoc comment for the enum itself if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, " "); if (comment_creation_chatter) Printf(constants_code, "/* This was generated from enumDeclaration() */\n"); Printf(constants_code, Char(doxygen_comments)); Printf(constants_code, "\n"); Delete(doxygen_comments); } } if (proxy_flag && !is_wrapping_class()) { // Global enums / enums in a namespace assert(!full_imclass_name); constructIntermediateClassName(n); } // Emit each enum item Language::enumDeclaration(n); if (proxy_flag && !is_wrapping_class()) { Delete(full_imclass_name); full_imclass_name = 0; } if ((enum_feature != SimpleEnum) && symname && typemap_lookup_type) { // Wrap (non-anonymous) C/C++ enum within a typesafe, typeunsafe or proper Java enum // Finish the enum declaration // Typemaps are used to generate the enum definition in a similar manner to proxy classes. Printv(enum_code, (enum_feature == ProperEnum) ? ";\n" : "", typemapLookup(n, "javabody", typemap_lookup_type, WARN_JAVA_TYPEMAP_JAVABODY_UNDEF), // main body of class typemapLookup(n, "javacode", typemap_lookup_type, WARN_NONE), // extra Java code "}", NIL); Replaceall(enum_code, "$javaclassname", symname); // Substitute $enumvalues - intended usage is for typesafe enums if (Getattr(n, "enumvalues")) Replaceall(enum_code, "$enumvalues", Getattr(n, "enumvalues")); else Replaceall(enum_code, "$enumvalues", ""); if (proxy_flag && is_wrapping_class()) { // Enums defined within the C++ class are defined within the proxy class // Add extra indentation Replaceall(enum_code, "\n", "\n "); Replaceall(enum_code, " \n", "\n"); Printv(proxy_class_constants_code, " ", enum_code, "\n\n", NIL); } else { // Global enums are defined in their own file String *output_directory = outputDirectory(nspace); String *filen = NewStringf("%s%s.java", output_directory, symname); File *f_enum = NewFile(filen, "w", SWIG_output_files()); if (!f_enum) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; // Start writing out the enum file emitBanner(f_enum); if (package || nspace) { Printf(f_enum, "package "); if (package) Printv(f_enum, package, nspace ? "." : "", NIL); if (nspace) Printv(f_enum, nspace, NIL); Printf(f_enum, ";\n"); } Printv(f_enum, typemapLookup(n, "javaimports", typemap_lookup_type, WARN_NONE), // Import statements "\n", enum_code, "\n", NIL); Printf(f_enum, "\n"); Delete(f_enum); Delete(output_directory); } } else { // Wrap C++ enum with simple constant Printf(enum_code, "\n"); if (proxy_flag && is_wrapping_class()) Printv(proxy_class_constants_code, enum_code, NIL); else Printv(module_class_constants_code, enum_code, NIL); } Delete(enum_code); enum_code = NULL; } return SWIG_OK; } /* ---------------------------------------------------------------------- * enumvalueDeclaration() * ---------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; Swig_require("enumvalueDeclaration", n, "*name", "?value", NIL); String *symname = Getattr(n, "sym:name"); String *value = Getattr(n, "value"); String *name = Getattr(n, "name"); Node *parent = parentNode(n); int unnamedinstance = GetFlag(parent, "unnamedinstance"); String *parent_name = Getattr(parent, "name"); String *nspace = getNSpace(); String *newsymname = 0; String *tmpValue; // Strange hack from parent method if (value) tmpValue = NewString(value); else tmpValue = NewString(name); // Note that this is used in enumValue() amongst other places Setattr(n, "value", tmpValue); // Deal with enum values that are not int int swigtype = SwigType_type(Getattr(n, "type")); if (swigtype == T_CHAR) { if (Getattr(n, "enumstringval")) { String *val = NewStringf("'%(escape)s'", Getattr(n, "enumstringval")); Setattr(n, "enumvalue", val); Delete(val); } } else { String *numval = Getattr(n, "enumnumval"); if (numval) { const char *p = Char(numval); if (isdigit(p[0])) { char *e; errno = 0; unsigned long long value = strtoull(p, &e, 0); if (errno != ERANGE && *e == '\0' && value >= 0x80000000) { // Use hex for larger unsigned integer constants in Java code since // Java allows implicit conversion to a signed integer value. String *hexval = NewStringf("0x%llx", value); Setattr(n, "enumvalue", hexval); Delete(hexval); } else { Setattr(n, "enumvalue", numval); } } else { // Emit negative values as-is. Setattr(n, "enumvalue", numval); } } } { EnumFeature enum_feature = decodeEnumFeature(parent); if ((enum_feature == SimpleEnum) && GetFlag(parent, "scopedenum")) { newsymname = Swig_name_member(0, Getattr(parent, "sym:name"), symname); symname = newsymname; } // Add to language symbol table String *scope = 0; if (unnamedinstance || !parent_name || enum_feature == SimpleEnum) { String *enumClassPrefix = getEnumClassPrefix(); if (enumClassPrefix) { scope = NewString(""); if (nspace) Printf(scope, "%s.", nspace); Printf(scope, "%s", enumClassPrefix); } else { scope = Copy(constants_interface_name); } } else { scope = getCurrentScopeName(nspace); if (!scope) scope = Copy(Getattr(parent, "sym:name")); else Printf(scope, ".%s", Getattr(parent, "sym:name")); } if (!addSymbol(symname, n, scope)) return SWIG_ERROR; if ((enum_feature == ProperEnum) && parent_name && !unnamedinstance) { if (!GetFlag(n, "firstenumitem")) Printf(enum_code, ",\n"); } // Translate and write javadoc comment if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, " "); if (comment_creation_chatter) Printf(enum_code, "/* This was generated from enumvalueDeclaration() */\n"); Printv(enum_code, Char(doxygen_comments), NIL); Delete(doxygen_comments); } if ((enum_feature == ProperEnum) && parent_name && !unnamedinstance) { // Wrap (non-anonymous) C/C++ enum with a proper Java enum // Emit the enum item. Printf(enum_code, " %s", symname); if (Getattr(n, "enumvalue")) { String *value = enumValue(n); Printf(enum_code, "(%s)", value); Delete(value); } } else { // Wrap C/C++ enums with constant integers or use the typesafe enum pattern SwigType *typemap_lookup_type = parent_name ? parent_name : NewString("enum "); Setattr(n, "type", typemap_lookup_type); const String *tm = typemapLookup(n, "jstype", typemap_lookup_type, WARN_JAVA_TYPEMAP_JSTYPE_UNDEF); String *return_type = Copy(tm); substituteClassname(typemap_lookup_type, return_type); const String *methodmods = Getattr(n, "feature:java:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); if ((enum_feature == TypesafeEnum) && parent_name && !unnamedinstance) { // Wrap (non-anonymous) enum using the typesafe enum pattern if (Getattr(n, "enumvalue")) { String *value = enumValue(n); Printf(enum_code, " %s final static %s %s = new %s(\"%s\", %s);\n", methodmods, return_type, symname, return_type, symname, value); Delete(value); } else { Printf(enum_code, " %s final static %s %s = new %s(\"%s\");\n", methodmods, return_type, symname, return_type, symname); } } else { // Simple integer constants // Note these are always generated for anonymous enums, no matter what enum_feature is specified // Code generated is the same for SimpleEnum and TypeunsafeEnum -> the class it is generated into is determined later String *value = enumValue(n); Printf(enum_code, " %s final static %s %s = %s;\n", methodmods, return_type, symname, value); Delete(value); } Delete(return_type); } // Add the enum value to the comma separated list being constructed in the enum declaration. String *enumvalues = Getattr(parent, "enumvalues"); if (!enumvalues) Setattr(parent, "enumvalues", Copy(symname)); else Printv(enumvalues, ", ", symname, NIL); Delete(scope); } Delete(newsymname); Delete(tmpValue); Swig_restore(n); return SWIG_OK; } /* ----------------------------------------------------------------------- * constantWrapper() * Used for wrapping constants - #define or %constant. * Also for inline initialised const static primitive type member variables (short, int, double, enums etc). * Java static final variables are generated for these. * If the %javaconst(1) feature is used then the C constant value is used to initialise the Java final variable. * If not, a JNI method is generated to get the C constant value for initialisation of the Java final variable. * However, if the %javaconstvalue feature is used, it overrides all other ways to generate the initialisation. * Also note that this method might be called for wrapping enum items (when the enum is using %javaconst(0)). * ------------------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; String *return_type = NewString(""); String *constants_code = NewString(""); // Translate and write javadoc comment if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, " "); if (comment_creation_chatter) Printf(constants_code, "/* This was generated from constantWrapper() */\n"); Printv(constants_code, Char(doxygen_comments), NIL); Delete(doxygen_comments); } bool is_enum_item = (Cmp(nodeType(n), "enumitem") == 0); const String *itemname = (proxy_flag && wrapping_member_flag) ? variable_name : symname; if (!is_enum_item) { String *scope = 0; if (proxy_class_name) { String *nspace = getNSpace(); scope = NewString(""); if (nspace) Printf(scope, "%s.", nspace); Printf(scope, "%s", proxy_class_name); } else { scope = Copy(constants_interface_name); } if (!addSymbol(itemname, n, scope)) return SWIG_ERROR; Delete(scope); } // The %javaconst feature determines how the constant value is obtained int const_feature_flag = GetFlag(n, "feature:java:const"); /* Adjust the enum type for the Swig_typemap_lookup. * We want the same jstype typemap for all the enum items so we use the enum type (parent node). */ if (is_enum_item) { t = Getattr(parentNode(n), "enumtype"); Setattr(n, "type", t); } /* Attach the non-standard typemaps to the parameter list. */ Swig_typemap_attach_parms("jstype", l, NULL); /* Get Java return types */ bool classname_substituted_flag = false; if ((tm = Swig_typemap_lookup("jstype", n, "", 0))) { classname_substituted_flag = substituteClassname(t, tm); Printf(return_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JSTYPE_UNDEF, input_file, line_number, "No jstype typemap defined for %s\n", SwigType_str(t, 0)); } const String *methodmods = Getattr(n, "feature:java:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); Printf(constants_code, " %s final static %s %s = ", methodmods, return_type, itemname); // Check for the %javaconstvalue feature String *value = Getattr(n, "feature:java:constvalue"); if (value) { Printf(constants_code, "%s;\n", value); } else if (!const_feature_flag) { // Default enum and constant handling will work with any type of C constant and initialises the Java variable from C through a JNI call. if (classname_substituted_flag) { if (SwigType_isenum(t)) { // This handles wrapping of inline initialised const enum static member variables (not when wrapping enum items - ignored later on) Printf(constants_code, "%s.swigToEnum(%s.%s());\n", return_type, full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } else { // This handles function pointers using the %constant directive Printf(constants_code, "new %s(%s.%s(), false);\n", return_type, full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } } else { Printf(constants_code, "%s.%s();\n", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } // Each constant and enum value is wrapped with a separate JNI function call SetFlag(n, "feature:immutable"); enum_constant_flag = true; variableWrapper(n); enum_constant_flag = false; } else { // Alternative constant handling will use the C syntax to make a true Java constant and hope that it compiles as Java code if (Getattr(n, "wrappedasconstant")) { Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); } else { Printf(constants_code, "%s;\n", Getattr(n, "value")); } } // Emit the generated code to appropriate place // Enums only emit the intermediate and JNI methods, so no proxy or module class wrapper methods needed if (!is_enum_item) { if (proxy_flag && wrapping_member_flag) Printv(proxy_class_constants_code, constants_code, NIL); else Printv(module_class_constants_code, constants_code, NIL); } // Cleanup Swig_restore(n); Delete(return_type); Delete(constants_code); return SWIG_OK; } /* ----------------------------------------------------------------------------- * insertDirective() * ----------------------------------------------------------------------------- */ virtual int insertDirective(Node *n) { int ret = SWIG_OK; String *code = Getattr(n, "code"); String *section = Getattr(n, "section"); Replaceall(code, "$module", module_class_name); Replaceall(code, "$imclassname", imclass_name); if (!ImportMode && (Cmp(section, "proxycode") == 0)) { if (proxy_class_code) { Swig_typemap_replace_embedded_typemap(code, n); int offset = Len(code) > 0 && *Char(code) == '\n' ? 1 : 0; Printv(proxy_class_code, Char(code) + offset, "\n", NIL); } } else { ret = Language::insertDirective(n); } return ret; } /* ----------------------------------------------------------------------------- * pragmaDirective() * * Valid Pragmas: * jniclassbase - base (extends) for the intermediary class * jniclasspackage - package in which to generate the intermediary class * jniclassclassmodifiers - class modifiers for the intermediary class * jniclasscode - text (java code) is copied verbatim to the intermediary class * jniclassimports - import statements for the intermediary class * jniclassinterfaces - interface (implements) for the intermediary class * * modulebase - base (extends) for the module class * moduleclassmodifiers - class modifiers for the module class * modulecode - text (java code) is copied verbatim to the module class * moduleimports - import statements for the module class * moduleinterfaces - interface (implements) for the module class * * constantsmodifiers - access modifiers for the constants interface * ----------------------------------------------------------------------------- */ virtual int pragmaDirective(Node *n) { if (!ImportMode) { String *lang = Getattr(n, "lang"); String *code = Getattr(n, "name"); String *value = Getattr(n, "value"); if (Strcmp(lang, "java") == 0) { String *strvalue = NewString(value); Replaceall(strvalue, "\\\"", "\""); if (Strcmp(code, "jniclassbase") == 0) { Delete(imclass_baseclass); imclass_baseclass = Copy(strvalue); } else if (Strcmp(code, "jniclasspackage") == 0) { Delete(imclass_package); imclass_package = Copy(strvalue); String *imclass_class_package_jniname = makeValidJniName(imclass_package); Printv(jnipackage, imclass_class_package_jniname, NIL); Delete(imclass_class_package_jniname); Replaceall(jnipackage, NSPACE_SEPARATOR, "_"); Append(jnipackage, "_"); String *wrapper_name = NewString(""); String *imclass_class_jniname = makeValidJniName(imclass_name); Printf(wrapper_name, "Java_%s%s_%%f", jnipackage, imclass_class_jniname); Delete(imclass_class_jniname); Swig_name_unregister("wrapper"); Swig_name_register("wrapper", Char(wrapper_name)); Delete(wrapper_name); } else if (Strcmp(code, "jniclassclassmodifiers") == 0) { Delete(imclass_class_modifiers); imclass_class_modifiers = Copy(strvalue); } else if (Strcmp(code, "jniclasscode") == 0) { Printf(imclass_class_code, "%s\n", strvalue); } else if (Strcmp(code, "jniclassimports") == 0) { Delete(imclass_imports); imclass_imports = Copy(strvalue); } else if (Strcmp(code, "jniclassinterfaces") == 0) { Delete(imclass_interfaces); imclass_interfaces = Copy(strvalue); } else if (Strcmp(code, "modulebase") == 0) { Delete(module_baseclass); module_baseclass = Copy(strvalue); } else if (Strcmp(code, "moduleclassmodifiers") == 0) { Delete(module_class_modifiers); module_class_modifiers = Copy(strvalue); } else if (Strcmp(code, "modulecode") == 0) { Printf(module_class_code, "%s\n", strvalue); } else if (Strcmp(code, "moduleimports") == 0) { Delete(module_imports); module_imports = Copy(strvalue); } else if (Strcmp(code, "moduleinterfaces") == 0) { Delete(module_interfaces); module_interfaces = Copy(strvalue); } else if (Strcmp(code, "constantsmodifiers") == 0) { Delete(constants_modifiers); constants_modifiers = Copy(strvalue); } else { Swig_error(input_file, line_number, "Unrecognized pragma.\n"); } Delete(strvalue); } } return Language::pragmaDirective(n); } /* ----------------------------------------------------------------------------- * getQualifiedInterfaceName() * ----------------------------------------------------------------------------- */ String *getQualifiedInterfaceName(Node *n) { String *ret = Getattr(n, "interface:qname"); if (!ret) { String *nspace = Getattr(n, "sym:nspace"); String *symname = Getattr(n, "interface:name"); if (nspace) { if (package) ret = NewStringf("%s.%s.%s", package, nspace, symname); else ret = NewStringf("%s.%s", nspace, symname); } else { ret = Copy(symname); } Setattr(n, "interface:qname", ret); } return ret; } /* ----------------------------------------------------------------------------- * getInterfaceName() * ----------------------------------------------------------------------------- */ String *getInterfaceName(SwigType *t, bool qualified) { String *interface_name = NULL; if (proxy_flag) { Node *n = classLookup(t); if (n && Getattr(n, "interface:name")) interface_name = qualified ? getQualifiedInterfaceName(n) : Getattr(n, "interface:name"); } return interface_name; } /* ----------------------------------------------------------------------------- * addInterfaceNameAndUpcasts() * ----------------------------------------------------------------------------- */ void addInterfaceNameAndUpcasts(SwigType *smart, String *interface_list, String *interface_upcasts, List *base_list, SwigType *c_classname) { for (Iterator it = First(base_list); it.item; it = Next(it)) { Node *base = it.item; SwigType *c_baseclassname = Getattr(base, "name"); String *interface_name = Getattr(base, "interface:name"); SwigType *bsmart = Getattr(base, "smart"); if (Len(interface_list)) Append(interface_list, ", "); Append(interface_list, interface_name); Node *attributes = NewHash(); String *interface_code = Copy(typemapLookup(base, "javainterfacecode", Getattr(base, "classtypeobj"), WARN_JAVA_TYPEMAP_INTERFACECODE_UNDEF, attributes)); String *cptr_method_name = 0; if (interface_code) { Replaceall(interface_code, "$interfacename", interface_name); Printv(interface_upcasts, interface_code, NIL); cptr_method_name = Copy(Getattr(attributes, "tmap:javainterfacecode:cptrmethod")); } if (!cptr_method_name) cptr_method_name = NewStringf("%s_GetInterfaceCPtr", interface_name); Replaceall(cptr_method_name, ".", "_"); Replaceall(cptr_method_name, "$interfacename", interface_name); String *upcast_method_name = Swig_name_member(getNSpace(), getClassPrefix(), cptr_method_name); upcastsCode(smart, bsmart, upcast_method_name, c_classname, c_baseclassname); Delete(upcast_method_name); Delete(cptr_method_name); Delete(interface_code); } } /* ----------------------------------------------------------------------------- * upcastsCode() * * Add code for C++ casting to base class * ----------------------------------------------------------------------------- */ void upcastsCode(SwigType *smart, SwigType *bsmart, String *upcast_method_name, SwigType *c_classname, SwigType *c_baseclassname) { String *jniname = makeValidJniName(upcast_method_name); String *wname = Swig_name_wrapper(jniname); Printf(imclass_cppcasts_code, " public final static native long %s(long jarg1);\n", upcast_method_name); if (smart) { if (bsmart) { String *smartnamestr = SwigType_namestr(smart); String *bsmartnamestr = SwigType_namestr(bsmart); Printv(upcasts_code, "SWIGEXPORT jlong JNICALL ", wname, "(JNIEnv *jenv, jclass jcls, jlong jarg1) {\n", " jlong baseptr = 0;\n" " ", smartnamestr, " *argp1;\n" " (void)jenv;\n" " (void)jcls;\n" " argp1 = *(", smartnamestr, " **)&jarg1;\n" " *(", bsmartnamestr, " **)&baseptr = argp1 ? new ", bsmartnamestr, "(*argp1) : 0;\n" " return baseptr;\n" "}\n", "\n", NIL); Delete(bsmartnamestr); Delete(smartnamestr); } } else { String *classname = SwigType_namestr(c_classname); String *baseclassname = SwigType_namestr(c_baseclassname); Printv(upcasts_code, "SWIGEXPORT jlong JNICALL ", wname, "(JNIEnv *jenv, jclass jcls, jlong jarg1) {\n", " jlong baseptr = 0;\n" " (void)jenv;\n" " (void)jcls;\n" " *(", baseclassname, " **)&baseptr = *(", classname, " **)&jarg1;\n" " return baseptr;\n" "}\n", "\n", NIL); Delete(baseclassname); Delete(classname); } Delete(wname); Delete(jniname); } /* ----------------------------------------------------------------------------- * emitProxyClassDefAndCPPCasts() * ----------------------------------------------------------------------------- */ void emitProxyClassDefAndCPPCasts(Node *n) { SwigType *c_classname = Getattr(n, "name"); SwigType *c_baseclassname = NULL; String *baseclass = NULL; String *interface_list = NewStringEmpty(); String *interface_upcasts = NewStringEmpty(); SwigType *typemap_lookup_type = Getattr(n, "classtypeobj"); bool feature_director = Swig_directorclass(n) ? true : false; bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested"); SwigType *smart = Getattr(n, "smart"); SwigType *bsmart = 0; // Inheritance from pure Java classes Node *attributes = NewHash(); const String *pure_baseclass = typemapLookup(n, "javabase", typemap_lookup_type, WARN_NONE, attributes); bool purebase_replace = GetFlag(attributes, "tmap:javabase:replace") ? true : false; bool purebase_notderived = GetFlag(attributes, "tmap:javabase:notderived") ? true : false; Delete(attributes); // C++ inheritance if (!purebase_replace) { List *baselist = Getattr(n, "bases"); if (baselist) { Iterator base = First(baselist); while (base.item) { if (!(GetFlag(base.item, "feature:ignore") || GetFlag(base.item, "feature:interface"))) { SwigType *baseclassname = Getattr(base.item, "name"); if (!c_baseclassname) { String *name = getProxyName(baseclassname); if (name) { c_baseclassname = baseclassname; baseclass = name; bsmart = Getattr(base.item, "smart"); } } else { /* Warn about multiple inheritance for additional base class(es) */ String *proxyclassname = Getattr(n, "classtypeobj"); Swig_warning(WARN_JAVA_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, base %s ignored. Multiple inheritance is not supported in Java.\n", SwigType_namestr(proxyclassname), SwigType_namestr(baseclassname)); } } base = Next(base); } } } List *interface_bases = Getattr(n, "interface:bases"); if (interface_bases) addInterfaceNameAndUpcasts(smart, interface_list, interface_upcasts, interface_bases, c_classname); bool derived = baseclass != 0; if (derived && purebase_notderived) pure_baseclass = empty_string; const String *wanted_base = baseclass ? baseclass : pure_baseclass; if (purebase_replace) { wanted_base = pure_baseclass; derived = false; baseclass = NULL; if (purebase_notderived) Swig_error(Getfile(n), Getline(n), "The javabase typemap for proxy %s must contain just one of the 'replace' or 'notderived' attributes.\n", typemap_lookup_type); } else if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) { Swig_warning(WARN_JAVA_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, base %s ignored. Multiple inheritance is not supported in Java. " "Perhaps you need one of the 'replace' or 'notderived' attributes in the javabase typemap?\n", typemap_lookup_type, pure_baseclass); } // Pure Java interfaces const String *pure_interfaces = typemapLookup(n, "javainterfaces", typemap_lookup_type, WARN_NONE); if (*Char(interface_list) && *Char(pure_interfaces)) Append(interface_list, ", "); Append(interface_list, pure_interfaces); // Start writing the proxy class if (!has_outerclass) // Import statements Printv(proxy_class_def, typemapLookup(n, "javaimports", typemap_lookup_type, WARN_NONE),"\n", NIL); // Translate and write javadoc comment if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, 0); if (comment_creation_chatter) Printf(proxy_class_def, "/* This was generated from emitProxyClassDefAndCPPCasts() */\n"); Printv(proxy_class_def, Char(doxygen_comments), NIL); Delete(doxygen_comments); } if (has_outerclass) Printv(proxy_class_def, "static ", NIL); // C++ nested classes correspond to static java classes Printv(proxy_class_def, typemapLookup(n, "javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers " $javaclassname", // Class name and bases (*Char(wanted_base)) ? " extends " : "", wanted_base, *Char(interface_list) ? // Pure Java interfaces " implements " : "", interface_list, " {", derived ? typemapLookup(n, "javabody_derived", typemap_lookup_type, WARN_JAVA_TYPEMAP_JAVABODY_UNDEF) : // main body of class typemapLookup(n, "javabody", typemap_lookup_type, WARN_JAVA_TYPEMAP_JAVABODY_UNDEF), // main body of class NIL); // C++ destructor is wrapped by the delete method // Note that the method name is specified in a typemap attribute called methodname String *destruct = NewString(""); const String *tm = NULL; attributes = NewHash(); const String *destruct_methodname = NULL; const String *destruct_methodmodifiers = NULL; const String *destruct_parameters = NULL; if (derived) { tm = typemapLookup(n, "javadestruct_derived", typemap_lookup_type, WARN_NONE, attributes); destruct_methodname = Getattr(attributes, "tmap:javadestruct_derived:methodname"); destruct_methodmodifiers = Getattr(attributes, "tmap:javadestruct_derived:methodmodifiers"); destruct_parameters = Getattr(attributes, "tmap:javadestruct_derived:parameters"); } else { tm = typemapLookup(n, "javadestruct", typemap_lookup_type, WARN_NONE, attributes); destruct_methodname = Getattr(attributes, "tmap:javadestruct:methodname"); destruct_methodmodifiers = Getattr(attributes, "tmap:javadestruct:methodmodifiers"); destruct_parameters = Getattr(attributes, "tmap:javadestruct:parameters"); } if (tm && *Char(tm)) { if (!destruct_methodname) { Swig_error(Getfile(n), Getline(n), "No methodname attribute defined in javadestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name); } if (!destruct_methodmodifiers) { Swig_error(Getfile(n), Getline(n), "No methodmodifiers attribute defined in javadestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name); } if (!destruct_parameters) destruct_parameters = empty_string; } // Emit the finalize and delete methods if (tm) { // Finalize method if (*Char(destructor_call)) { Printv(proxy_class_def, typemapLookup(n, "javafinalize", typemap_lookup_type, WARN_NONE), NIL); } // delete method Printv(destruct, tm, NIL); if (*Char(destructor_call)) Replaceall(destruct, "$jnicall", destructor_call); else Replaceall(destruct, "$jnicall", "throw new UnsupportedOperationException(\"C++ destructor does not have public access\")"); if (*Char(destruct)) { Printv(proxy_class_def, "\n ", NIL); const String *methodmods = Getattr(n, "destructmethodmodifiers"); if (methodmods) Printv(proxy_class_def, methodmods, NIL); else Printv(proxy_class_def, destruct_methodmodifiers, NIL); Printv(proxy_class_def, " void ", destruct_methodname, "(", destruct_parameters, ")", destructor_throws_clause, " ", destruct, "\n", NIL); } } if (*Char(interface_upcasts)) Printv(proxy_class_def, interface_upcasts, NIL); /* Insert directordisconnect typemap, if this class has directors enabled */ /* Also insert the swigTakeOwnership and swigReleaseOwnership methods */ if (feature_director) { String *destruct_jnicall, *release_jnicall, *take_jnicall; String *changeown_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "change_ownership"); destruct_jnicall = NewStringf("%s()", destruct_methodname); release_jnicall = NewStringf("%s.%s(this, swigCPtr, false)", full_imclass_name, changeown_method_name); take_jnicall = NewStringf("%s.%s(this, swigCPtr, true)", full_imclass_name, changeown_method_name); emitCodeTypemap(n, false, typemap_lookup_type, "directordisconnect", "methodname", destruct_jnicall); emitCodeTypemap(n, false, typemap_lookup_type, "directorowner_release", "methodname", release_jnicall); emitCodeTypemap(n, false, typemap_lookup_type, "directorowner_take", "methodname", take_jnicall); Delete(destruct_jnicall); Delete(changeown_method_name); Delete(release_jnicall); Delete(take_jnicall); } Delete(interface_upcasts); Delete(interface_list); Delete(attributes); Delete(destruct); // Emit extra user code Printv(proxy_class_def, typemapLookup(n, "javacode", typemap_lookup_type, WARN_NONE), // extra Java code "\n", NIL); if (derived) { String *upcast_method_name = Swig_name_member(getNSpace(), getClassPrefix(), smart != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast"); upcastsCode(smart, bsmart, upcast_method_name, c_classname, c_baseclassname); Delete(upcast_method_name); } } /* ---------------------------------------------------------------------- * emitInterfaceDeclaration() * ---------------------------------------------------------------------- */ void emitInterfaceDeclaration(Node *n, String *interface_name, File *f_interface, String *nspace) { if (package || nspace) { Printf(f_interface, "package "); if (package) Printv(f_interface, package, nspace ? "." : "", NIL); if (nspace) Printv(f_interface, nspace, NIL); Printf(f_interface, ";\n"); } Printv(f_interface, typemapLookup(n, "javaimports", Getattr(n, "classtypeobj"), WARN_NONE), "\n", NIL); Printv(f_interface, typemapLookup(n, "javainterfacemodifiers", Getattr(n, "classtypeobj"), WARN_JAVA_TYPEMAP_INTERFACEMODIFIERS_UNDEF), NIL); Printf(f_interface, " %s", interface_name); String *additional = Getattr(n, "feature:interface:additional"); String *bases = additional ? Copy(additional) : 0; if (List *baselist = Getattr(n, "bases")) { for (Iterator base = First(baselist); base.item; base = Next(base)) { if (GetFlag(base.item, "feature:ignore") || !GetFlag(base.item, "feature:interface")) continue; // TODO: warn about skipped non-interface bases String *base_iname = Getattr(base.item, "interface:name"); if (!bases) bases = Copy(base_iname); else { Append(bases, ", "); Append(bases, base_iname); } } } if (bases) { Printv(f_interface, " extends ", bases, NIL); Delete(bases); } Printf(f_interface, " {\n"); Node *attributes = NewHash(); String *interface_code = Copy(typemapLookup(n, "javainterfacecode", Getattr(n, "classtypeobj"), WARN_JAVA_TYPEMAP_INTERFACECODE_UNDEF, attributes)); if (interface_code) { String *interface_declaration = Copy(Getattr(attributes, "tmap:javainterfacecode:declaration")); if (interface_declaration) { Replaceall(interface_declaration, "$interfacename", interface_name); Printv(f_interface, interface_declaration, NIL); Delete(interface_declaration); } Delete(interface_code); } } /* ---------------------------------------------------------------------- * classDeclaration() * ---------------------------------------------------------------------- */ int classDeclaration(Node *n) { return Language::classDeclaration(n); } /* ---------------------------------------------------------------------- * classHandler() * ---------------------------------------------------------------------- */ virtual int classHandler(Node *n) { File *f_proxy = NULL; File *f_interface = NULL; String *old_proxy_class_name = proxy_class_name; String *old_full_proxy_class_name = full_proxy_class_name; String *old_full_imclass_name = full_imclass_name; String *old_destructor_call = destructor_call; String *old_destructor_throws_clause = destructor_throws_clause; String *old_proxy_class_constants_code = proxy_class_constants_code; String *old_proxy_class_def = proxy_class_def; String *old_proxy_class_code = proxy_class_code; bool has_outerclass = Getattr(n, "nested:outer") && !GetFlag(n, "feature:flatnested"); String *old_interface_class_code = interface_class_code; interface_class_code = 0; if (proxy_flag) { proxy_class_name = NewString(Getattr(n, "sym:name")); String *nspace = getNSpace(); constructIntermediateClassName(n); String *outerClassesPrefix = 0; if (Node *outer = Getattr(n, "nested:outer")) { outerClassesPrefix = Copy(Getattr(outer, "sym:name")); for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) { Push(outerClassesPrefix, "."); Push(outerClassesPrefix, Getattr(outer, "sym:name")); } } if (!nspace) { full_proxy_class_name = outerClassesPrefix ? NewStringf("%s.%s", outerClassesPrefix, proxy_class_name) : NewStringf("%s", proxy_class_name); if (Cmp(proxy_class_name, imclass_name) == 0) { Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name); Exit(EXIT_FAILURE); } if (Cmp(proxy_class_name, module_class_name) == 0) { Printf(stderr, "Class name cannot be equal to module class name: %s\n", proxy_class_name); Exit(EXIT_FAILURE); } } else { if (outerClassesPrefix) { if (package) full_proxy_class_name = NewStringf("%s.%s.%s.%s", package, nspace, outerClassesPrefix, proxy_class_name); else full_proxy_class_name = NewStringf("%s.%s.%s", nspace, outerClassesPrefix, proxy_class_name); } else { if (package) full_proxy_class_name = NewStringf("%s.%s.%s", package, nspace, proxy_class_name); else full_proxy_class_name = NewStringf("%s.%s", nspace, proxy_class_name); } } String *interface_name = GetFlag(n, "feature:interface") ? Getattr(n, "interface:name") : 0; if (outerClassesPrefix) { String *fnspace = nspace ? NewStringf("%s.%s", nspace, outerClassesPrefix) : outerClassesPrefix; if (!addSymbol(proxy_class_name, n, fnspace)) return SWIG_ERROR; if (interface_name && !addInterfaceSymbol(interface_name, n, fnspace)) return SWIG_ERROR; if (nspace) Delete(fnspace); Delete(outerClassesPrefix); } else { if (!addSymbol(proxy_class_name, n, nspace)) return SWIG_ERROR; if (interface_name && !addInterfaceSymbol(interface_name, n, nspace)) return SWIG_ERROR; } // Each outer proxy class goes into a separate file if (!has_outerclass) { String *output_directory = outputDirectory(nspace); String *filen = NewStringf("%s%s.java", output_directory, proxy_class_name); f_proxy = NewFile(filen, "w", SWIG_output_files()); if (!f_proxy) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); Delete(output_directory); // Start writing out the proxy class file emitBanner(f_proxy); if (package || nspace) { Printf(f_proxy, "package "); if (package) Printv(f_proxy, package, nspace ? "." : "", NIL); if (nspace) Printv(f_proxy, nspace, NIL); Printf(f_proxy, ";\n"); } } else ++nesting_depth; proxy_class_def = NewString(""); proxy_class_code = NewString(""); destructor_call = NewString(""); destructor_throws_clause = NewString(""); proxy_class_constants_code = NewString(""); if (GetFlag(n, "feature:interface")) { interface_class_code = NewString(""); String *output_directory = outputDirectory(nspace); String *filen = NewStringf("%s%s.java", output_directory, interface_name); f_interface = NewFile(filen, "w", SWIG_output_files()); if (!f_interface) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, filen); // file name ownership goes to the list emitBanner(f_interface); emitInterfaceDeclaration(n, interface_name, interface_class_code, nspace); Delete(filen); Delete(output_directory); } } Language::classHandler(n); if (proxy_flag) { emitProxyClassDefAndCPPCasts(n); String *javaclazzname = Swig_name_member(getNSpace(), getClassPrefix(), ""); // mangled full proxy class name Replaceall(proxy_class_def, "$javaclassname", proxy_class_name); Replaceall(proxy_class_code, "$javaclassname", proxy_class_name); Replaceall(proxy_class_constants_code, "$javaclassname", proxy_class_name); Replaceall(interface_class_code, "$javaclassname", proxy_class_name); Replaceall(proxy_class_def, "$javaclazzname", javaclazzname); Replaceall(proxy_class_code, "$javaclazzname", javaclazzname); Replaceall(proxy_class_constants_code, "$javaclazzname", javaclazzname); Replaceall(interface_class_code, "$javaclazzname", javaclazzname); Replaceall(proxy_class_def, "$module", module_class_name); Replaceall(proxy_class_code, "$module", module_class_name); Replaceall(proxy_class_constants_code, "$module", module_class_name); Replaceall(interface_class_code, "$module", module_class_name); Replaceall(proxy_class_def, "$imclassname", full_imclass_name); Replaceall(proxy_class_code, "$imclassname", full_imclass_name); Replaceall(proxy_class_constants_code, "$imclassname", full_imclass_name); Replaceall(interface_class_code, "$imclassname", full_imclass_name); if (!has_outerclass) Printv(f_proxy, proxy_class_def, proxy_class_code, NIL); else { Swig_offset_string(proxy_class_def, nesting_depth); Append(old_proxy_class_code, proxy_class_def); Swig_offset_string(proxy_class_code, nesting_depth); Append(old_proxy_class_code, proxy_class_code); } // Write out all the constants if (Len(proxy_class_constants_code) != 0) { if (!has_outerclass) Printv(f_proxy, proxy_class_constants_code, NIL); else { Swig_offset_string(proxy_class_constants_code, nesting_depth); Append(old_proxy_class_code, proxy_class_constants_code); } } if (!has_outerclass) { Printf(f_proxy, "}\n"); Delete(f_proxy); f_proxy = NULL; } else { for (int i = 0; i < nesting_depth; ++i) Append(old_proxy_class_code, " "); Append(old_proxy_class_code, "}\n\n"); --nesting_depth; } if (f_interface) { Printv(f_interface, interface_class_code, "}\n", NIL); Delete(f_interface); f_interface = 0; } emitDirectorExtraMethods(n); Delete(interface_class_code); interface_class_code = old_interface_class_code; Delete(javaclazzname); Delete(proxy_class_name); proxy_class_name = old_proxy_class_name; Delete(full_proxy_class_name); full_proxy_class_name = old_full_proxy_class_name; Delete(full_imclass_name); full_imclass_name = old_full_imclass_name; Delete(destructor_call); destructor_call = old_destructor_call; Delete(destructor_throws_clause); destructor_throws_clause = old_destructor_throws_clause; Delete(proxy_class_constants_code); proxy_class_constants_code = old_proxy_class_constants_code; Delete(proxy_class_def); proxy_class_def = old_proxy_class_def; Delete(proxy_class_code); proxy_class_code = old_proxy_class_code; } return SWIG_OK; } /* ---------------------------------------------------------------------- * memberfunctionHandler() * ---------------------------------------------------------------------- */ virtual int memberfunctionHandler(Node *n) { member_func_flag = true; Language::memberfunctionHandler(n); if (proxy_flag) { String *overloaded_name = getOverloadedName(n); String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name); Setattr(n, "proxyfuncname", Getattr(n, "sym:name")); Setattr(n, "imfuncname", intermediary_function_name); proxyClassFunctionHandler(n); Delete(overloaded_name); } member_func_flag = false; return SWIG_OK; } /* ---------------------------------------------------------------------- * staticmemberfunctionHandler() * ---------------------------------------------------------------------- */ virtual int staticmemberfunctionHandler(Node *n) { static_flag = true; member_func_flag = true; Language::staticmemberfunctionHandler(n); if (proxy_flag) { String *overloaded_name = getOverloadedName(n); String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name); Setattr(n, "proxyfuncname", Getattr(n, "sym:name")); Setattr(n, "imfuncname", intermediary_function_name); proxyClassFunctionHandler(n); Delete(overloaded_name); } static_flag = false; member_func_flag = false; return SWIG_OK; } /* ----------------------------------------------------------------------------- * proxyClassFunctionHandler() * * Function called for creating a Java wrapper function around a c++ function in the * proxy class. Used for both static and non-static C++ class functions. * C++ class static functions map to Java static functions. * Two extra attributes in the Node must be available. These are "proxyfuncname" - * the name of the Java class proxy function, which in turn will call "imfuncname" - * the intermediary (JNI) function name in the intermediary class. * ----------------------------------------------------------------------------- */ void proxyClassFunctionHandler(Node *n) { SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *intermediary_function_name = Getattr(n, "imfuncname"); String *proxy_function_name = Getattr(n, "proxyfuncname"); String *tm; Parm *p; int i; String *imcall = NewString(""); String *return_type = NewString(""); String *function_code = NewString(""); bool setter_flag = false; String *pre_code = NewString(""); String *post_code = NewString(""); bool is_interface = GetFlag(parentNode(n), "feature:interface") && !checkAttribute(n, "kind", "variable") && !static_flag && Getattr(n, "interface:owner") == 0; if (!proxy_flag) return; // Wrappers not wanted for some methods where the parameters cannot be overloaded in Java if (Getattr(n, "overload:ignore")) return; // Don't generate proxy method for additional explicitcall method used in directors if (GetFlag(n, "explicitcall")) return; if (l) { if (SwigType_type(Getattr(l, "type")) == T_VOID) { l = nextSibling(l); } } /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("in", l, NULL); Swig_typemap_attach_parms("jtype", l, NULL); Swig_typemap_attach_parms("jstype", l, NULL); Swig_typemap_attach_parms("javain", l, NULL); /* Get return types */ if ((tm = Swig_typemap_lookup("jstype", n, "", 0))) { // Note that in the case of polymorphic (covariant) return types, the method's return type is changed to be the base of the C++ return type SwigType *covariant = Getattr(n, "covariant"); substituteClassname(covariant ? covariant : t, tm); Printf(return_type, "%s", tm); if (covariant) Swig_warning(WARN_JAVA_COVARIANT_RET, input_file, line_number, "Covariant return types not supported in Java. Proxy method will return %s.\n", SwigType_str(covariant, 0)); } else { Swig_warning(WARN_JAVA_TYPEMAP_JSTYPE_UNDEF, input_file, line_number, "No jstype typemap defined for %s\n", SwigType_str(t, 0)); } if (wrapping_member_flag && !enum_constant_flag) { // For wrapping member variables (Javabean setter) setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) == 0); } // Translate and write javadoc comment if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, " "); if (comment_creation_chatter) Printf(function_code, "/* This was generated from proxyclassfunctionhandler() */\n"); Printv(function_code, Char(doxygen_comments), NIL); Delete(doxygen_comments); } /* Start generating the proxy function */ const String *methodmods = Getattr(n, "feature:java:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); Printf(function_code, " %s ", methodmods); if (static_flag) Printf(function_code, "static "); Printf(function_code, "%s %s(", return_type, proxy_function_name); if (is_interface) Printf(interface_class_code, " %s %s(", return_type, proxy_function_name); Printv(imcall, full_imclass_name, ".$imfuncname(", NIL); if (!static_flag) { Printf(imcall, "swigCPtr"); String *this_type = Copy(getClassType()); String *name = NewString("jself"); String *qualifier = Getattr(n, "qualifier"); if (qualifier) SwigType_push(this_type, qualifier); SwigType_add_pointer(this_type); Parm *this_parm = NewParm(this_type, name, n); Swig_typemap_attach_parms("jtype", this_parm, NULL); Swig_typemap_attach_parms("jstype", this_parm, NULL); if (prematureGarbageCollectionPreventionParameter(this_type, this_parm)) Printf(imcall, ", this"); Delete(this_parm); Delete(name); Delete(this_type); } emit_mark_varargs(l); int gencomma = !static_flag; /* Output each parameter */ for (i = 0, p = l; p; i++) { /* Ignored varargs */ if (checkAttribute(p, "varargs:ignore", "1")) { p = nextSibling(p); continue; } /* Ignored parameters */ if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } /* Ignore the 'this' argument for variable wrappers */ if (!(variable_wrapper_flag && i == 0) || static_flag) { SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); /* Get the Java parameter type */ if ((tm = Getattr(p, "tmap:jstype"))) { substituteClassname(pt, tm); Printf(param_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JSTYPE_UNDEF, input_file, line_number, "No jstype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, setter_flag); // Use typemaps to transform type used in Java proxy wrapper (in proxy class) to type used in JNI function (in intermediary class) if ((tm = Getattr(p, "tmap:javain"))) { addThrows(n, "tmap:javain", p); substituteClassname(pt, tm); Replaceall(tm, "$javainput", arg); String *pre = Getattr(p, "tmap:javain:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$javainput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:javain:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$javainput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_JAVA_TYPEMAP_JAVAIN_UNDEF, input_file, line_number, "No javain typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to proxy function */ if (gencomma >= 2) { Printf(function_code, ", "); if (is_interface) Printf(interface_class_code, ", "); } gencomma = 2; Printf(function_code, "%s %s", param_type, arg); if (is_interface) Printf(interface_class_code, "%s %s", param_type, arg); if (prematureGarbageCollectionPreventionParameter(pt, p)) { String *pgcppname = Getattr(p, "tmap:javain:pgcppname"); if (pgcppname) { String *argname = Copy(pgcppname); Replaceall(argname, "$javainput", arg); Printf(imcall, ", %s", argname); Delete(argname); } else { Printf(imcall, ", %s", arg); } } Delete(arg); Delete(param_type); } p = Getattr(p, "tmap:in:next"); } Printf(imcall, ")"); Printf(function_code, ")"); // Transform return type used in JNI function (in intermediary class) to type used in Java wrapper function (in proxy class) if ((tm = Swig_typemap_lookup("javaout", n, "", 0))) { addThrows(n, "tmap:javaout", n); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; if (is_pre_code || is_post_code) { Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap if (is_post_code) { Insert(tm, 0, "\n try "); Printv(tm, " finally {\n", post_code, "\n }", NIL); } else { Insert(tm, 0, "\n "); } if (is_pre_code) { Insert(tm, 0, pre_code); Insert(tm, 0, "\n"); } Insert(tm, 0, "{"); Printf(tm, "\n }"); } if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); substituteClassname(t, tm); // For director methods: generate code to selectively make a normal polymorphic call or // an explicit method call - needed to prevent infinite recursion calls in director methods. Node *explicit_n = Getattr(n, "explicitcallnode"); if (explicit_n) { String *ex_overloaded_name = getOverloadedName(explicit_n); String *ex_intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), ex_overloaded_name); String *ex_imcall = Copy(imcall); Replaceall(ex_imcall, "$imfuncname", ex_intermediary_function_name); Replaceall(imcall, "$imfuncname", intermediary_function_name); String *excode = NewString(""); if (!Cmp(return_type, "void")) Printf(excode, "if (getClass() == %s.class) %s; else %s", proxy_class_name, imcall, ex_imcall); else Printf(excode, "(getClass() == %s.class) ? %s : %s", proxy_class_name, imcall, ex_imcall); Clear(imcall); Printv(imcall, excode, NIL); Delete(ex_overloaded_name); Delete(excode); } else { Replaceall(imcall, "$imfuncname", intermediary_function_name); } Replaceall(tm, "$imfuncname", intermediary_function_name); Replaceall(tm, "$jnicall", imcall); } else { Swig_warning(WARN_JAVA_TYPEMAP_JAVAOUT_UNDEF, input_file, line_number, "No javaout typemap defined for %s\n", SwigType_str(t, 0)); } if (is_interface) { Printf(interface_class_code, ")"); generateThrowsClause(n, interface_class_code); Printf(interface_class_code, ";\n"); } generateThrowsClause(n, function_code); Printf(function_code, " %s\n\n", tm ? tm : empty_string); Printv(proxy_class_code, function_code, NIL); Delete(pre_code); Delete(post_code); Delete(function_code); Delete(return_type); Delete(imcall); } /* ---------------------------------------------------------------------- * constructorHandler() * ---------------------------------------------------------------------- */ virtual int constructorHandler(Node *n) { ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *function_code = NewString(""); String *helper_code = NewString(""); // Holds code for the constructor helper method generated only when the javain typemap has code in the pre or post attributes String *helper_args = NewString(""); String *pre_code = NewString(""); String *post_code = NewString(""); String *im_return_type = NewString(""); bool feature_director = (parentNode(n) && Swig_directorclass(n)); Language::constructorHandler(n); // Wrappers not wanted for some methods where the parameters cannot be overloaded in Java if (Getattr(n, "overload:ignore")) return SWIG_OK; if (proxy_flag) { String *overloaded_name = getOverloadedName(n); String *mangled_overname = Swig_name_construct(getNSpace(), overloaded_name); String *imcall = NewString(""); #if 0 // TODO: add full support for Java annotations - implementation should be same as C# attributes const String *annotations = Getattr(n, "feature:java:annotations"); #else const String *annotations = NULL; #endif // Default annotation for director constructors is a warning suppression static const String *suppress_warning_this_escape = NewString("@SuppressWarnings(\"this-escape\")"); if (!annotations && feature_director) annotations = suppress_warning_this_escape; if (annotations) { Printf(function_code, " %s\n", annotations); Printf(helper_code, " %s\n", annotations); } const String *methodmods = Getattr(n, "feature:java:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); tm = Getattr(n, "tmap:jtype"); // typemaps were attached earlier to the node Printf(im_return_type, "%s", tm); // Translate and write javadoc comment if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, " "); if (comment_creation_chatter) Printf(function_code, "/* This was generated from constructionhandler() */\n"); Printv(function_code, Char(doxygen_comments), NIL); Delete(doxygen_comments); } Printf(function_code, " %s %s(", methodmods, proxy_class_name); Printf(helper_code, " static private %s SwigConstruct%s(", im_return_type, proxy_class_name); Printv(imcall, full_imclass_name, ".", mangled_overname, "(", NIL); /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("in", l, NULL); Swig_typemap_attach_parms("jtype", l, NULL); Swig_typemap_attach_parms("jstype", l, NULL); Swig_typemap_attach_parms("javain", l, NULL); emit_mark_varargs(l); int gencomma = 0; /* Output each parameter */ for (i = 0, p = l; p; i++) { /* Ignored varargs */ if (checkAttribute(p, "varargs:ignore", "1")) { p = nextSibling(p); continue; } /* Ignored parameters */ if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); /* Get the Java parameter type */ if ((tm = Getattr(p, "tmap:jstype"))) { substituteClassname(pt, tm); Printf(param_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JSTYPE_UNDEF, input_file, line_number, "No jstype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, false); // Use typemaps to transform type used in Java wrapper function (in proxy class) to type used in JNI function (in intermediary class) if ((tm = Getattr(p, "tmap:javain"))) { addThrows(n, "tmap:javain", p); substituteClassname(pt, tm); Replaceall(tm, "$javainput", arg); String *pre = Getattr(p, "tmap:javain:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$javainput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:javain:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$javainput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_JAVA_TYPEMAP_JAVAIN_UNDEF, input_file, line_number, "No javain typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to proxy function */ if (gencomma) { Printf(function_code, ", "); Printf(helper_code, ", "); Printf(helper_args, ", "); } Printf(function_code, "%s %s", param_type, arg); Printf(helper_code, "%s %s", param_type, arg); Printf(helper_args, "%s", arg); ++gencomma; if (prematureGarbageCollectionPreventionParameter(pt, p)) { String *pgcppname = Getattr(p, "tmap:javain:pgcppname"); if (pgcppname) { String *argname = Copy(pgcppname); Replaceall(argname, "$javainput", arg); Printf(imcall, ", %s", argname); Delete(argname); } else { Printf(imcall, ", %s", arg); } } Delete(arg); Delete(param_type); p = Getattr(p, "tmap:in:next"); } Printf(imcall, ")"); Printf(function_code, ")"); Printf(helper_code, ")"); generateThrowsClause(n, function_code); /* Insert the javaconstruct typemap, doing the replacement for $directorconnect, as needed */ Hash *attributes = NewHash(); String *typemap_lookup_type = Getattr(getCurrentClass(), "classtypeobj"); String *construct_tm = Copy(typemapLookup(n, "javaconstruct", typemap_lookup_type, WARN_JAVA_TYPEMAP_JAVACONSTRUCT_UNDEF, attributes)); if (construct_tm) { if (!feature_director) { Replaceall(construct_tm, "$directorconnect", ""); } else { String *connect_attr = Getattr(attributes, "tmap:javaconstruct:directorconnect"); if (connect_attr) { Replaceall(construct_tm, "$directorconnect", connect_attr); } else { Swig_warning(WARN_JAVA_NO_DIRECTORCONNECT_ATTR, input_file, line_number, "\"directorconnect\" attribute missing in %s \"javaconstruct\" typemap.\n", Getattr(n, "name")); Replaceall(construct_tm, "$directorconnect", ""); } } Printv(function_code, " ", construct_tm, "\n", NIL); } bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; if (is_pre_code || is_post_code) { generateThrowsClause(n, helper_code); Printf(helper_code, " {\n"); if (is_pre_code) { Printv(helper_code, pre_code, "\n", NIL); } if (is_post_code) { Printf(helper_code, " try {\n"); Printv(helper_code, " return ", imcall, ";\n", NIL); Printv(helper_code, " } finally {\n", post_code, "\n }", NIL); } else { Printv(helper_code, " return ", imcall, ";", NIL); } Printf(helper_code, "\n }\n"); String *helper_name = NewStringf("%s.SwigConstruct%s(%s)", proxy_class_name, proxy_class_name, helper_args); Printv(proxy_class_code, helper_code, "\n", NIL); Replaceall(function_code, "$imcall", helper_name); Delete(helper_name); } else { Replaceall(function_code, "$imcall", imcall); } Printv(proxy_class_code, function_code, "\n", NIL); Delete(helper_args); Delete(im_return_type); Delete(pre_code); Delete(post_code); Delete(construct_tm); Delete(attributes); Delete(overloaded_name); Delete(imcall); } return SWIG_OK; } /* ---------------------------------------------------------------------- * destructorHandler() * ---------------------------------------------------------------------- */ virtual int destructorHandler(Node *n) { Language::destructorHandler(n); String *symname = Getattr(n, "sym:name"); if (proxy_flag) { Printv(destructor_call, full_imclass_name, ".", Swig_name_destroy(getNSpace(), symname), "(swigCPtr)", NIL); generateThrowsClause(n, destructor_throws_clause); const String *methodmods = Getattr(n, "feature:java:methodmodifiers"); if (methodmods) Setattr(getCurrentClass(), "destructmethodmodifiers", methodmods); } return SWIG_OK; } /* ---------------------------------------------------------------------- * membervariableHandler() * ---------------------------------------------------------------------- */ virtual int membervariableHandler(Node *n) { variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; variable_wrapper_flag = true; Language::membervariableHandler(n); wrapping_member_flag = false; variable_wrapper_flag = false; return SWIG_OK; } /* ---------------------------------------------------------------------- * staticmembervariableHandler() * ---------------------------------------------------------------------- */ virtual int staticmembervariableHandler(Node *n) { variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; static_flag = true; Language::staticmembervariableHandler(n); wrapping_member_flag = false; static_flag = false; return SWIG_OK; } /* ---------------------------------------------------------------------- * memberconstantHandler() * ---------------------------------------------------------------------- */ virtual int memberconstantHandler(Node *n) { variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; Language::memberconstantHandler(n); wrapping_member_flag = false; return SWIG_OK; } /* ----------------------------------------------------------------------------- * getOverloadedName() * ----------------------------------------------------------------------------- */ String *getOverloadedName(Node *n) { /* Although JNI functions are designed to handle overloaded Java functions, * a Java long is used for all classes in the SWIG intermediary class. * The intermediary class methods are thus mangled when overloaded to give * a unique name. */ String *overloaded_name = Copy(Getattr(n, "sym:name")); if (Getattr(n, "sym:overloaded")) { Printv(overloaded_name, Getattr(n, "sym:overname"), NIL); } return overloaded_name; } /* ----------------------------------------------------------------------------- * moduleClassFunctionHandler() * ----------------------------------------------------------------------------- */ void moduleClassFunctionHandler(Node *n) { SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *imcall = NewString(""); String *return_type = NewString(""); String *function_code = NewString(""); int num_arguments = 0; String *overloaded_name = getOverloadedName(n); String *func_name = NULL; bool setter_flag = false; String *pre_code = NewString(""); String *post_code = NewString(""); // Translate and write javadoc comment if flagged if (doxygen && doxygenTranslator->hasDocumentation(n)) { String *doxygen_comments = doxygenTranslator->getDocumentation(n, " "); if (comment_creation_chatter) Printf(function_code, "/* This was generated from moduleClassFunctionHandler() */\n"); Printv(function_code, doxygen_comments, NIL); Delete(doxygen_comments); } if (l) { if (SwigType_type(Getattr(l, "type")) == T_VOID) { l = nextSibling(l); } } /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("jstype", l, NULL); Swig_typemap_attach_parms("javain", l, NULL); /* Get return types */ if ((tm = Swig_typemap_lookup("jstype", n, "", 0))) { substituteClassname(t, tm); Printf(return_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JSTYPE_UNDEF, input_file, line_number, "No jstype typemap defined for %s\n", SwigType_str(t, 0)); } /* Change function name for global variables */ if (proxy_flag && global_variable_flag) { // Capitalize the first letter in the variable to create a JavaBean type getter/setter function name func_name = NewString(""); setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), variable_name)) == 0); if (setter_flag) Printf(func_name, "set"); else Printf(func_name, "get"); Putc(toupper((int) *Char(variable_name)), func_name); Printf(func_name, "%s", Char(variable_name) + 1); } else { func_name = Copy(Getattr(n, "sym:name")); } /* Start generating the function */ const String *methodmods = Getattr(n, "feature:java:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); Printf(function_code, " %s static %s %s(", methodmods, return_type, func_name); Printv(imcall, imclass_name, ".", overloaded_name, "(", NIL); /* Get number of required and total arguments */ num_arguments = emit_num_arguments(l); bool global_or_member_variable = global_variable_flag || (wrapping_member_flag && !enum_constant_flag); int gencomma = 0; /* Output each parameter */ for (i = 0, p = l; i < num_arguments; i++) { /* Ignored parameters */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); /* Get the Java parameter type */ if ((tm = Getattr(p, "tmap:jstype"))) { substituteClassname(pt, tm); Printf(param_type, "%s", tm); } else { Swig_warning(WARN_JAVA_TYPEMAP_JSTYPE_UNDEF, input_file, line_number, "No jstype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, global_or_member_variable); // Use typemaps to transform type used in Java wrapper function (in proxy class) to type used in JNI function (in intermediary class) if ((tm = Getattr(p, "tmap:javain"))) { addThrows(n, "tmap:javain", p); substituteClassname(pt, tm); Replaceall(tm, "$javainput", arg); String *pre = Getattr(p, "tmap:javain:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$javainput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:javain:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$javainput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_JAVA_TYPEMAP_JAVAIN_UNDEF, input_file, line_number, "No javain typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to module class function */ if (gencomma >= 2) Printf(function_code, ", "); gencomma = 2; Printf(function_code, "%s %s", param_type, arg); if (prematureGarbageCollectionPreventionParameter(pt, p)) { String *pgcppname = Getattr(p, "tmap:javain:pgcppname"); if (pgcppname) { String *argname = Copy(pgcppname); Replaceall(argname, "$javainput", arg); Printf(imcall, ", %s", argname); Delete(argname); } else { Printf(imcall, ", %s", arg); } } p = Getattr(p, "tmap:in:next"); Delete(arg); Delete(param_type); } Printf(imcall, ")"); Printf(function_code, ")"); // Transform return type used in JNI function (in intermediary class) to type used in Java wrapper function (in module class) if ((tm = Swig_typemap_lookup("javaout", n, "", 0))) { addThrows(n, "tmap:javaout", n); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; if (is_pre_code || is_post_code) { Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap if (is_post_code) { Insert(tm, 0, "\n try "); Printv(tm, " finally {\n", post_code, "\n }", NIL); } else { Insert(tm, 0, "\n "); } if (is_pre_code) { Insert(tm, 0, pre_code); Insert(tm, 0, "\n"); } Insert(tm, 0, "{"); Printf(tm, "\n }"); } if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); substituteClassname(t, tm); Replaceall(tm, "$imfuncname", overloaded_name); Replaceall(tm, "$jnicall", imcall); } else { Swig_warning(WARN_JAVA_TYPEMAP_JAVAOUT_UNDEF, input_file, line_number, "No javaout typemap defined for %s\n", SwigType_str(t, 0)); } generateThrowsClause(n, function_code); Printf(function_code, " %s\n\n", tm ? tm : empty_string); Printv(module_class_code, function_code, NIL); Delete(pre_code); Delete(post_code); Delete(function_code); Delete(return_type); Delete(imcall); Delete(func_name); } /*---------------------------------------------------------------------- * replaceSpecialVariables() *--------------------------------------------------------------------*/ virtual void replaceSpecialVariables(String *method, String *tm, Parm *parm) { (void)method; SwigType *type = Getattr(parm, "type"); substituteClassname(type, tm); } /*---------------------------------------------------------------------- * decodeEnumFeature() * Decode the possible enum features, which are one of: * %javaenum(simple) * %javaenum(typeunsafe) - default * %javaenum(typesafe) * %javaenum(proper) *--------------------------------------------------------------------*/ EnumFeature decodeEnumFeature(Node *n) { EnumFeature enum_feature = TypeunsafeEnum; String *feature = Getattr(n, "feature:java:enum"); if (feature) { if (Cmp(feature, "simple") == 0) enum_feature = SimpleEnum; else if (Cmp(feature, "typesafe") == 0) enum_feature = TypesafeEnum; else if (Cmp(feature, "proper") == 0) enum_feature = ProperEnum; } return enum_feature; } /* ----------------------------------------------------------------------- * enumValue() * This method will return a string with an enum value to use in Java generated * code. If the %javaconst feature is not used, the string will contain the intermediary * class call to obtain the enum value. The intermediary class and JNI methods to obtain * the enum value will be generated. Otherwise the C/C++ enum value will be used if there * is one and hopefully it will compile as Java code - e.g. 20 as in: enum E{e=20}; * The %javaconstvalue feature overrides all other ways to generate the constant value. * The caller must delete memory allocated for the returned string. * ------------------------------------------------------------------------ */ String *enumValue(Node *n) { String *symname = Getattr(n, "sym:name"); // Check for the %javaconstvalue feature String *value = Getattr(n, "feature:java:constvalue"); if (!value) { // The %javaconst feature determines how the constant value is obtained int const_feature_flag = GetFlag(n, "feature:java:const"); if (const_feature_flag) { // Use the C syntax to make a true Java constant and hope that it compiles as Java code value = Getattr(n, "enumvalue") ? Copy(Getattr(n, "enumvalue")) : Copy(Getattr(n, "enumvalueex")); } else { String *newsymname = 0; if (!getCurrentClass() || !proxy_flag) { String *enumClassPrefix = getEnumClassPrefix(); if (enumClassPrefix) { // A global scoped enum newsymname = Swig_name_member(0, enumClassPrefix, symname); symname = newsymname; } } // Get the enumvalue from a JNI call if (!getCurrentClass() || !cparse_cplusplus || !proxy_flag) { // Strange hack to change the name Setattr(n, "name", Getattr(n, "value")); /* for wrapping of enums in a namespace when emit_action is used */ constantWrapper(n); value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } else { memberconstantHandler(n); value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), Swig_name_member(0, getEnumClassPrefix(), symname))); } Delete(newsymname); } } return value; } /* ----------------------------------------------------------------------------- * getEnumName() * * If jnidescriptor is set, inner class names are separated with '$' otherwise a '.' * and the package is also not added to the name. * ----------------------------------------------------------------------------- */ String *getEnumName(SwigType *t, bool jnidescriptor) { Node *enumname = NULL; Node *n = enumLookup(t); if (n) { enumname = Getattr(n, "enumname"); if (!enumname || jnidescriptor) { String *symname = Getattr(n, "sym:name"); if (symname) { // Add in class scope when referencing enum if not a global enum String *scopename_prefix = Swig_scopename_prefix(Getattr(n, "name")); String *proxyname = 0; if (scopename_prefix) { proxyname = getProxyName(scopename_prefix, jnidescriptor); } if (proxyname) { const char *class_separator = jnidescriptor ? "$" : "."; enumname = NewStringf("%s%s%s", proxyname, class_separator, symname); } else { // global enum or enum in a namespace String *nspace = Getattr(n, "sym:nspace"); if (nspace) { if (package && !jnidescriptor) enumname = NewStringf("%s.%s.%s", package, nspace, symname); else enumname = NewStringf("%s.%s", nspace, symname); } else { enumname = Copy(symname); } } if (!jnidescriptor) { Setattr(n, "enumname", enumname); // Cache it Delete(enumname); } Delete(scopename_prefix); } } } return enumname; } /* ----------------------------------------------------------------------------- * substituteClassname() * * Substitute the special variable $javaclassname with the proxy class name for classes/structs/unions * that SWIG knows about. Also substitutes enums with enum name. * Otherwise use the $descriptor name for the Java class name. Note that the $&javaclassname substitution * is the same as a $&descriptor substitution, ie one pointer added to descriptor name. * Note that the path separator is a '.' unless jnidescriptor is set. * Inputs: * pt - parameter type * tm - typemap contents that might contain the special variable to be replaced * jnidescriptor - if set, inner class names are separated with '$' otherwise a '/' is used for the path separator * Outputs: * tm - typemap contents complete with the special variable substitution * Return: * substitution_performed - flag indicating if a substitution was performed * ----------------------------------------------------------------------------- */ bool substituteClassname(SwigType *pt, String *tm, bool jnidescriptor = false) { bool substitution_performed = false; SwigType *type = Copy(SwigType_typedef_resolve_all(pt)); SwigType *strippedtype = SwigType_strip_qualifiers(type); if (Strstr(tm, "$javaclassname")) { SwigType *classnametype = Copy(strippedtype); substituteClassnameSpecialVariable(classnametype, tm, "$javaclassname", jnidescriptor); substitution_performed = true; Delete(classnametype); } if (Strstr(tm, "$*javaclassname")) { SwigType *classnametype = Copy(strippedtype); Delete(SwigType_pop(classnametype)); if (Len(classnametype) > 0) { substituteClassnameSpecialVariable(classnametype, tm, "$*javaclassname", jnidescriptor); substitution_performed = true; } Delete(classnametype); } if (Strstr(tm, "$&javaclassname")) { SwigType *classnametype = Copy(strippedtype); SwigType_add_pointer(classnametype); substituteClassnameSpecialVariable(classnametype, tm, "$&javaclassname", jnidescriptor); substitution_performed = true; Delete(classnametype); } if (Strstr(tm, "$javainterfacename")) { SwigType *interfacenametype = Copy(strippedtype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$javainterfacename", jnidescriptor, true); substitution_performed = true; Delete(interfacenametype); } if (Strstr(tm, "$*javainterfacename")) { SwigType *interfacenametype = Copy(strippedtype); Delete(SwigType_pop(interfacenametype)); if (Len(interfacenametype) > 0) { substituteInterfacenameSpecialVariable(interfacenametype, tm, "$*javainterfacename", jnidescriptor, true); substitution_performed = true; } Delete(interfacenametype); } if (Strstr(tm, "$&javainterfacename")) { SwigType *interfacenametype = Copy(strippedtype); SwigType_add_pointer(interfacenametype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$&javainterfacename", jnidescriptor, true); substitution_performed = true; Delete(interfacenametype); } if (Strstr(tm, "$interfacename")) { SwigType *interfacenametype = Copy(strippedtype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$interfacename", jnidescriptor, false); substitution_performed = true; Delete(interfacenametype); } if (Strstr(tm, "$*interfacename")) { SwigType *interfacenametype = Copy(strippedtype); Delete(SwigType_pop(interfacenametype)); if (Len(interfacenametype) > 0) { substituteInterfacenameSpecialVariable(interfacenametype, tm, "$*interfacename", jnidescriptor, false); substitution_performed = true; } Delete(interfacenametype); } if (Strstr(tm, "$&interfacename")) { SwigType *interfacenametype = Copy(strippedtype); SwigType_add_pointer(interfacenametype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$&interfacename", jnidescriptor, false); substitution_performed = true; Delete(interfacenametype); } Delete(strippedtype); Delete(type); return substitution_performed; } /* ----------------------------------------------------------------------------- * substituteClassnameSpecialVariable() * ----------------------------------------------------------------------------- */ void substituteClassnameSpecialVariable(SwigType *classnametype, String *tm, const char *classnamespecialvariable, bool jnidescriptor) { String *replacementname; if (SwigType_isenum(classnametype)) { String *enumname = getEnumName(classnametype, jnidescriptor); if (enumname) { replacementname = Copy(enumname); } else { bool anonymous_enum = (Cmp(classnametype, "enum ") == 0); if (anonymous_enum) { replacementname = NewString("int"); } else { // An unknown enum - one that has not been parsed (neither a C enum forward reference nor a definition) or an ignored enum replacementname = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype)); Replace(replacementname, "enum ", "", DOH_REPLACE_ANY); Setattr(swig_types_hash, replacementname, classnametype); } } } else { String *classname = getProxyName(classnametype, jnidescriptor); // getProxyName() works for pointers to classes too if (classname) { replacementname = Copy(classname); } else { // use $descriptor if SWIG does not know anything about this type. Note that any typedefs are resolved. replacementname = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype)); // Add to hash table so that the type wrapper classes can be created later Setattr(swig_types_hash, replacementname, classnametype); } } if (jnidescriptor) Replaceall(replacementname,".","/"); Replaceall(tm, classnamespecialvariable, replacementname); Delete(replacementname); } /* ----------------------------------------------------------------------------- * substituteInterfacenameSpecialVariable() * ----------------------------------------------------------------------------- */ void substituteInterfacenameSpecialVariable(SwigType *interfacenametype, String *tm, const char *interfacenamespecialvariable, bool jnidescriptor, bool qualified) { String *interfacename = getInterfaceName(interfacenametype/*, jnidescriptor*/, qualified); if (interfacename) { String *replacementname = Copy(interfacename); if (jnidescriptor) Replaceall(replacementname,".","/"); Replaceall(tm, interfacenamespecialvariable, replacementname); Delete(replacementname); } } /* ----------------------------------------------------------------------------- * emitTypeWrapperClass() * ----------------------------------------------------------------------------- */ void emitTypeWrapperClass(String *classname, SwigType *type) { Node *n = NewHash(); Setfile(n, input_file); Setline(n, line_number); String *swigtype = NewString(""); String *filen = NewStringf("%s%s.java", SWIG_output_directory(), classname); File *f_swigtype = NewFile(filen, "w", SWIG_output_files()); if (!f_swigtype) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; // Start writing out the type wrapper class file emitBanner(f_swigtype); if (package) Printf(f_swigtype, "package %s;\n", package); // Pure Java baseclass and interfaces const String *pure_baseclass = typemapLookup(n, "javabase", type, WARN_NONE); const String *pure_interfaces = typemapLookup(n, "javainterfaces", type, WARN_NONE); // Emit the class Printv(swigtype, typemapLookup(n, "javaimports", type, WARN_NONE), // Import statements "\n", typemapLookup(n, "javaclassmodifiers", type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers " $javaclassname", // Class name and bases *Char(pure_baseclass) ? " extends " : "", pure_baseclass, *Char(pure_interfaces) ? // Interfaces " implements " : "", pure_interfaces, " {", typemapLookup(n, "javabody", type, WARN_JAVA_TYPEMAP_JAVABODY_UNDEF), // main body of class typemapLookup(n, "javacode", type, WARN_NONE), // extra Java code "}\n", "\n", NIL); Replaceall(swigtype, "$javaclassname", classname); Replaceall(swigtype, "$module", module_class_name); Replaceall(swigtype, "$imclassname", imclass_name); // For unknown enums Replaceall(swigtype, "$static ", ""); Replaceall(swigtype, "$enumvalues", ""); Printv(f_swigtype, swigtype, NIL); Delete(f_swigtype); Delete(swigtype); Delete(n); } /* ----------------------------------------------------------------------------- * typemapLookup() * n - for input only and must contain info for Getfile(n) and Getline(n) to work * tmap_method - typemap method name * type - typemap type to lookup * warning - warning number to issue if no typemaps found * typemap_attributes - the typemap attributes are attached to this node and will * also be used for temporary storage if non null * return is never NULL, unlike Swig_typemap_lookup() * ----------------------------------------------------------------------------- */ const String *typemapLookup(Node *n, const_String_or_char_ptr tmap_method, SwigType *type, int warning, Node *typemap_attributes = 0) { Node *node = !typemap_attributes ? NewHash() : typemap_attributes; Setattr(node, "type", type); Setfile(node, Getfile(n)); Setline(node, Getline(n)); const String *tm = Swig_typemap_lookup(tmap_method, node, "", 0); if (!tm) { tm = empty_string; if (warning != WARN_NONE) Swig_warning(warning, Getfile(n), Getline(n), "No %s typemap defined for %s\n", tmap_method, SwigType_str(type, 0)); } if (!typemap_attributes) Delete(node); return tm; } /* ----------------------------------------------------------------------------- * addThrows() * * Adds exception classes to a throws list. The throws list is the list of classes * that will form the Java throws clause. Mainly for checked exceptions. * ----------------------------------------------------------------------------- */ void addThrows(Node *n, const String *attribute, Node *parameter) { // Get the comma separated exception classes for the throws clause - held in typemap/feature's "throws" attribute String *throws_attribute = NewStringf("%s:throws", attribute); String *throws = Getattr(parameter, throws_attribute); if (throws && Len(throws) > 0) { String *throws_list = Getattr(n, "java:throwslist"); if (!throws_list) { throws_list = NewList(); Setattr(n, "java:throwslist", throws_list); } // Put the exception classes in the throws clause into a temporary List List *temp_classes_list = Split(throws, ',', INT_MAX); // Add the exception classes to the node throws list, but don't duplicate if already in list if (temp_classes_list && Len(temp_classes_list) > 0) { for (Iterator cls = First(temp_classes_list); cls.item; cls = Next(cls)) { String *exception_class = NewString(cls.item); Replaceall(exception_class, " ", ""); // remove spaces Replaceall(exception_class, "\t", ""); // remove tabs if (Len(exception_class) > 0) { // $javaclassname substitution SwigType *pt = Getattr(parameter, "type"); substituteClassname(pt, exception_class); // Don't duplicate the Java exception class in the throws clause bool found_flag = false; for (Iterator item = First(throws_list); item.item; item = Next(item)) { if (Strcmp(item.item, exception_class) == 0) found_flag = true; } if (!found_flag) Append(throws_list, exception_class); } Delete(exception_class); } } Delete(temp_classes_list); } Delete(throws_attribute); } /* ----------------------------------------------------------------------------- * generateThrowsClause() * * Generates throws clause for checked exception * ----------------------------------------------------------------------------- */ void generateThrowsClause(Node *n, String *code) { // Add the throws clause into code List *throws_list = Getattr(n, "java:throwslist"); if (throws_list) { Iterator cls = First(throws_list); Printf(code, " throws %s", cls.item); while ((cls = Next(cls)).item) Printf(code, ", %s", cls.item); } } /* ----------------------------------------------------------------------------- * prematureGarbageCollectionPreventionParameter() * * Get the proxy class name for use in an additional generated parameter. The * additional parameter is added to a native method call purely to prevent * premature garbage collection of proxy classes which pass their C++ class pointer * in a Java long to the JNI layer. * ----------------------------------------------------------------------------- */ String *prematureGarbageCollectionPreventionParameter(SwigType *t, Parm *p) { String *pgcpp_java_type = 0; String *jtype = NewString(Getattr(p, "tmap:jtype")); // Strip C comments String *stripped_jtype = Swig_strip_c_comments(jtype); if (stripped_jtype) { Delete(jtype); jtype = stripped_jtype; } // Remove whitespace Replaceall(jtype, " ", ""); Replaceall(jtype, "\t", ""); if (Cmp(jtype, "long") == 0) { if (proxy_flag) { if (!GetFlag(p, "tmap:jtype:nopgcpp") && !nopgcpp_flag) { String *interface_name = getInterfaceName(t, true); pgcpp_java_type = interface_name ? interface_name : getProxyName(t); if (!pgcpp_java_type) { // Look for proxy class parameters passed to C++ layer using non-default typemaps, ie not one of above types String *jstype = NewString(Getattr(p, "tmap:jstype")); if (jstype) { Hash *classes = getClassHash(); if (classes) { // Strip C comments String *stripped_jstype = Swig_strip_c_comments(jstype); if (stripped_jstype) { Delete(jstype); jstype = stripped_jstype; } // Remove whitespace Replaceall(jstype, " ", ""); Replaceall(jstype, "\t", ""); Iterator ki; for (ki = First(classes); ki.key; ki = Next(ki)) { Node *cls = ki.item; if (cls && !Getattr(cls, "feature:ignore")) { String *symname = Getattr(cls, "sym:name"); if (symname && Strcmp(symname, jstype) == 0) { pgcpp_java_type = symname; } } } } } Delete(jstype); } } } } Delete(jtype); return pgcpp_java_type; } /* ----------------------------------------------------------------------------- * outputDirectory() * * Return the directory to use for generating Java classes/enums and create the * subdirectory (does not create if language specific outdir does not exist). * ----------------------------------------------------------------------------- */ String *outputDirectory(String *nspace) { String *output_directory = Copy(SWIG_output_directory()); if (nspace) { String *nspace_subdirectory = Copy(nspace); Replaceall(nspace_subdirectory, ".", SWIG_FILE_DELIMITER); String *newdir_error = Swig_new_subdirectory(output_directory, nspace_subdirectory); if (newdir_error) { Printf(stderr, "%s\n", newdir_error); Delete(newdir_error); Exit(EXIT_FAILURE); } Printv(output_directory, nspace_subdirectory, SWIG_FILE_DELIMITER, 0); Delete(nspace_subdirectory); } return output_directory; } /*---------------------------------------------------------------------- * Start of director methods *--------------------------------------------------------------------*/ /*---------------------------------------------------------------------- * getUpcallJNIMethod() *--------------------------------------------------------------------*/ String *getUpcallJNIMethod(String *descrip) { static struct { char code; const char *method; } upcall_methods[] = { { 'B', "CallStaticByteMethod"}, { 'C', "CallStaticCharMethod"}, { 'D', "CallStaticDoubleMethod"}, { 'F', "CallStaticFloatMethod"}, { 'I', "CallStaticIntMethod"}, { 'J', "CallStaticLongMethod"}, { 'L', "CallStaticObjectMethod"}, { 'S', "CallStaticShortMethod"}, { 'V', "CallStaticVoidMethod"}, { 'Z', "CallStaticBooleanMethod"}, { '[', "CallStaticObjectMethod"} }; char code; int i; code = *Char(descrip); for (i = 0; i < (int) (sizeof(upcall_methods) / sizeof(upcall_methods[0])); ++i) if (code == upcall_methods[i].code) return NewString(upcall_methods[i].method); return NULL; } /*---------------------------------------------------------------------- * emitDirectorUpcalls() *--------------------------------------------------------------------*/ void emitDirectorUpcalls() { if (n_dmethods) { Wrapper *w = NewWrapper(); String *jni_imclass_name = makeValidJniName(imclass_name); String *swig_module_init = NewString("swig_module_init"); String *swig_module_init_jni = makeValidJniName(swig_module_init); String *dmethod_data = NewString(""); int n_methods = 0; Iterator udata_iter; udata_iter = First(dmethods_seq); while (udata_iter.item) { UpcallData *udata = udata_iter.item; Printf(dmethod_data, " { \"%s\", \"%s\" }", Getattr(udata, "imclass_method"), Getattr(udata, "imclass_fdesc")); ++n_methods; udata_iter = Next(udata_iter); if (udata_iter.item) Putc(',', dmethod_data); Putc('\n', dmethod_data); } Printf(f_runtime, "namespace Swig {\n"); Printf(f_runtime, " namespace {\n"); Printf(f_runtime, " jclass jclass_%s = NULL;\n", imclass_name); Printf(f_runtime, " jmethodID director_method_ids[%d];\n", n_methods); Printf(f_runtime, " }\n"); Printf(f_runtime, "}\n"); Printf(w->def, "SWIGEXPORT void JNICALL Java_%s%s_%s(JNIEnv *jenv, jclass jcls) {", jnipackage, jni_imclass_name, swig_module_init_jni); Printf(w->code, "static struct {\n"); Printf(w->code, " const char *method;\n"); Printf(w->code, " const char *signature;\n"); Printf(w->code, "} methods[%d] = {\n", n_methods); Printv(w->code, dmethod_data, NIL); Printf(w->code, "};\n"); Wrapper_add_local(w, "i", "int i"); Printf(w->code, "Swig::jclass_%s = (jclass) jenv->NewGlobalRef(jcls);\n", imclass_name); Printf(w->code, "if (!Swig::jclass_%s) return;\n", imclass_name); Printf(w->code, "for (i = 0; i < (int) (sizeof(methods)/sizeof(methods[0])); ++i) {\n"); Printf(w->code, " Swig::director_method_ids[i] = jenv->GetStaticMethodID(jcls, methods[i].method, methods[i].signature);\n"); Printf(w->code, " if (!Swig::director_method_ids[i]) return;\n"); Printf(w->code, "}\n"); Printf(w->code, "}\n"); Wrapper_print(w, f_wrappers); Delete(dmethod_data); Delete(swig_module_init_jni); Delete(swig_module_init); Delete(jni_imclass_name); DelWrapper(w); } } /*---------------------------------------------------------------------- * emitDirectorExtraMethods() * * This is where the director connect method is generated. *--------------------------------------------------------------------*/ void emitDirectorExtraMethods(Node *n) { if (!Swig_directorclass(n)) return; // Output the director connect method: String *jni_imclass_name = makeValidJniName(imclass_name); String *norm_name = SwigType_namestr(Getattr(n, "name")); String *swig_director_connect = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect"); String *swig_director_connect_jni = makeValidJniName(swig_director_connect); SwigType *smart = Getattr(n, "smart"); String *smartptr = smart ? SwigType_namestr(smart) : 0; String *dirClassName = directorClassName(n); Wrapper *code_wrap; Printf(imclass_class_code, " public final static native void %s(%s obj, long cptr, boolean mem_own, boolean weak_global);\n", swig_director_connect, full_proxy_class_name); code_wrap = NewWrapper(); Printf(code_wrap->def, "SWIGEXPORT void JNICALL Java_%s%s_%s(JNIEnv *jenv, jclass jcls, jobject jself, jlong objarg, jboolean jswig_mem_own, " "jboolean jweak_global) {\n", jnipackage, jni_imclass_name, swig_director_connect_jni); if (smartptr) { Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", smartptr, smartptr); Printf(code_wrap->code, " (void)jcls;\n"); Printf(code_wrap->code, " // Keep a local instance of the smart pointer around while we are using the raw pointer\n"); Printf(code_wrap->code, " // Avoids using smart pointer specific API.\n"); Printf(code_wrap->code, " %s *director = static_cast<%s *>(obj->operator->());\n", dirClassName, dirClassName); } else { Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", norm_name, norm_name); Printf(code_wrap->code, " (void)jcls;\n"); Printf(code_wrap->code, " %s *director = static_cast<%s *>(obj);\n", dirClassName, dirClassName); } Printf(code_wrap->code, " director->swig_connect_director(jenv, jself, jenv->GetObjectClass(jself), " "(jswig_mem_own == JNI_TRUE), (jweak_global == JNI_TRUE));\n"); Printf(code_wrap->code, "}\n"); Wrapper_print(code_wrap, f_wrappers); DelWrapper(code_wrap); Delete(swig_director_connect_jni); Delete(swig_director_connect); // Output the swigReleaseOwnership, swigTakeOwnership methods: String *changeown_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "change_ownership"); String *changeown_jnimethod_name = makeValidJniName(changeown_method_name); Printf(imclass_class_code, " public final static native void %s(%s obj, long cptr, boolean take_or_release);\n", changeown_method_name, full_proxy_class_name); code_wrap = NewWrapper(); Printf(code_wrap->def, "SWIGEXPORT void JNICALL Java_%s%s_%s(JNIEnv *jenv, jclass jcls, jobject jself, jlong objarg, jboolean jtake_or_release) {\n", jnipackage, jni_imclass_name, changeown_jnimethod_name); if (smartptr) { Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", smartptr, smartptr); Printf(code_wrap->code, " // Keep a local instance of the smart pointer around while we are using the raw pointer\n"); Printf(code_wrap->code, " // Avoids using smart pointer specific API.\n"); Printf(code_wrap->code, " %s *director = dynamic_cast<%s *>(obj->operator->());\n", dirClassName, dirClassName); } else { Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", norm_name, norm_name); Printf(code_wrap->code, " %s *director = dynamic_cast<%s *>(obj);\n", dirClassName, dirClassName); } Printf(code_wrap->code, " (void)jcls;\n"); Printf(code_wrap->code, " if (director) {\n"); Printf(code_wrap->code, " director->swig_java_change_ownership(jenv, jself, jtake_or_release ? true : false);\n"); Printf(code_wrap->code, " }\n"); Printf(code_wrap->code, "}\n"); Wrapper_print(code_wrap, f_wrappers); DelWrapper(code_wrap); Delete(changeown_method_name); Delete(changeown_jnimethod_name); Delete(norm_name); Delete(dirClassName); Delete(jni_imclass_name); } /*---------------------------------------------------------------------- * emitCodeTypemap() * * Output a code typemap that uses $methodname and $jnicall, as used * in the directordisconnect, director_release and director_take * typemaps. *--------------------------------------------------------------------*/ void emitCodeTypemap(Node *n, bool derived, SwigType *lookup_type, const String *typemap, const String *methodname, const String *jnicall) { const String *tm = NULL; Node *tmattrs = NewHash(); String *lookup_tmname = NewString(typemap); String *method_attr_name; String *method_attr; if (derived) { Append(lookup_tmname, "_derived"); } tm = typemapLookup(n, lookup_tmname, lookup_type, WARN_NONE, tmattrs); method_attr_name = NewStringf("tmap:%s:%s", lookup_tmname, methodname); method_attr = Getattr(tmattrs, method_attr_name); if (*Char(tm)) { if (method_attr) { String *codebody = Copy(tm); Replaceall(codebody, "$methodname", method_attr); Replaceall(codebody, "$jnicall", jnicall); Append(proxy_class_def, codebody); Delete(codebody); } else { Swig_error(input_file, line_number, "No %s method name attribute for %s\n", lookup_tmname, proxy_class_name); } } else { Swig_error(input_file, line_number, "No %s typemap for %s\n", lookup_tmname, proxy_class_name); } Delete(tmattrs); Delete(lookup_tmname); // Delete(method_attr); } /* ----------------------------------------------------------------------------- * substitutePackagePath() * * Replace $packagepath using the javapackage typemap associated with passed * parm or global package if p is 0. "$packagepath/" is replaced with "" if * no package is set. Note that the path separator is a '/'. * ----------------------------------------------------------------------------- */ void substitutePackagePath(String *text, Parm *p) { String *pkg_path= 0; if (p) pkg_path = Swig_typemap_lookup("javapackage", p, "", 0); if (!pkg_path || Len(pkg_path) == 0) pkg_path = Copy(package_path); if (Len(pkg_path) > 0) { Replaceall(pkg_path, ".", "/"); Replaceall(text, "$packagepath", pkg_path); } else { Replaceall(text, "$packagepath/", empty_string); Replaceall(text, "$packagepath", empty_string); } Delete(pkg_path); } /* --------------------------------------------------------------- * Canonicalize the JNI field descriptor * * Replace the $packagepath and $javaclassname family of special * variables with the desired package and Java proxy name as * required in the JNI field descriptors. * * !!SFM!! If $packagepath occurs in the field descriptor, but * package_path isn't set (length == 0), then strip it and the * optional trailing '/' from the resulting name. * * --------------------------------------------------------------- */ String *canonicalizeJNIDescriptor(String *descriptor_in, Parm *p) { SwigType *type = Getattr(p, "type"); String *descriptor_out = Copy(descriptor_in); substituteClassname(type, descriptor_out, true); substitutePackagePath(descriptor_out, p); return descriptor_out; } /* --------------------------------------------------------------- * classDirectorMethod() * * Emit a virtual director method to pass a method call on to the * underlying Java object. * * --------------------------------------------------------------- */ int classDirectorMethod(Node *n, Node *parent, String *super) { String *c_classname = Getattr(parent, "name"); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); String *overloaded_name = 0; String *storage = Getattr(n, "storage"); String *value = Getattr(n, "value"); String *decl = Getattr(n, "decl"); String *declaration = NewString(""); String *tm; Parm *p; int i; Wrapper *w = NewWrapper(); ParmList *l = Getattr(n, "parms"); bool is_void = !(Cmp(returntype, "void")); String *qualified_return = 0; bool pure_virtual = (!(Cmp(storage, "virtual")) && !(Cmp(value, "0"))); int status = SWIG_OK; bool output_director = true; String *dirclassname = directorClassName(parent); String *qualified_name = NewStringf("%s::%s", dirclassname, name); String *jnidesc = NewString(""); String *classdesc = NewString(""); String *jniret_desc = NewString(""); String *classret_desc = NewString(""); SwigType *c_ret_type = NULL; String *jupcall_args = NewString("swigjobj"); String *imclass_dmethod = 0; String *callback_def = NewString(""); String *callback_code = NewString(""); String *imcall_args = NewString(""); int classmeth_off = curr_class_dmethod - first_class_dmethod; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; String *qualified_classname = getProxyName(getClassName()); // Kludge Alert: functionWrapper sets sym:overload properly, but it // isn't at this point, so we have to manufacture it ourselves. At least // we're consistent with the sym:overload name in functionWrapper. (?? when // does the overloaded method name get set?) if (!ignored_method) { overloaded_name = getOverloadedName(n); imclass_dmethod = Swig_name_member(getNSpace(), dirclassname, overloaded_name); } qualified_return = SwigType_rcaststr(returntype, "c_result"); if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { String *base_typename = SwigType_base(returntype); String *resolved_typename = SwigType_typedef_resolve_all(base_typename); Symtab *symtab = Getattr(n, "sym:symtab"); Node *typenode = Swig_symbol_clookup(resolved_typename, symtab); if (SwigType_ispointer(returntype) || (typenode && Getattr(typenode, "abstracts"))) { /* initialize pointers to something sane. Same for abstract classes when a reference is returned. */ Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } else { /* If returning a reference, initialize the pointer to a sane default - if a Java exception occurs, then the pointer returns something other than a NULL-initialized reference. */ SwigType *noref_type = SwigType_del_reference(Copy(returntype)); String *noref_ltype = SwigType_lstr(noref_type, 0); String *return_ltype = SwigType_lstr(returntype, 0); Wrapper_add_localv(w, "result_default", "static", noref_ltype, "result_default", NIL); Wrapper_add_localv(w, "c_result", return_ltype, "c_result", NIL); Printf(w->code, "result_default = SwigValueInit< %s >();\n", noref_ltype); Printf(w->code, "c_result = &result_default;\n"); Delete(return_ltype); Delete(noref_ltype); Delete(noref_type); } Delete(base_typename); Delete(resolved_typename); } } else { SwigType *vt; vt = cplus_value_type(returntype); if (!vt) { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), NIL); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(vt, "c_result"), NIL); Delete(vt); } } } if (!ignored_method) { /* Create the intermediate class wrapper */ tm = Swig_typemap_lookup("jtype", n, "", 0); if (tm) { Printf(callback_def, " public static %s %s(%s jself", tm, imclass_dmethod, qualified_classname); } else { Swig_warning(WARN_JAVA_TYPEMAP_JTYPE_UNDEF, input_file, line_number, "No jtype typemap defined for %s\n", SwigType_str(returntype, 0)); } } String *cdesc = NULL; SwigType *covariant = Getattr(n, "covariant"); SwigType *adjustedreturntype = covariant ? covariant : returntype; Parm *adjustedreturntypeparm = NewParmNode(adjustedreturntype, n); if (Swig_typemap_lookup("directorin", adjustedreturntypeparm, "", 0) && (cdesc = Getattr(adjustedreturntypeparm, "tmap:directorin:descriptor"))) { // Note that in the case of polymorphic (covariant) return types, the // method's return type is changed to be the base of the C++ return // type String *jnidesc_canon = canonicalizeJNIDescriptor(cdesc, adjustedreturntypeparm); Append(classret_desc, jnidesc_canon); Delete(jnidesc_canon); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } /* Get the JNI field descriptor for this return type, add the JNI field descriptor to jniret_desc */ if ((c_ret_type = Swig_typemap_lookup("jni", n, "", 0))) { Parm *tp = NewParmNode(c_ret_type, n); if (!is_void && !ignored_method) { String *jretval_decl = NewStringf("%s jresult", c_ret_type); Wrapper_add_localv(w, "jresult", jretval_decl, "= 0", NIL); Delete(jretval_decl); } String *jdesc = NULL; if (Swig_typemap_lookup("directorin", tp, "", 0) && (jdesc = Getattr(tp, "tmap:directorin:descriptor"))) { // Objects marshalled passing a Java class across JNI boundary use jobject - the nouse flag indicates this // We need the specific Java class name instead of the generic 'Ljava/lang/Object;' if (GetFlag(tp, "tmap:directorin:nouse")) jdesc = cdesc; String *jnidesc_canon = canonicalizeJNIDescriptor(jdesc, tp); Append(jniret_desc, jnidesc_canon); Delete(jnidesc_canon); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(c_ret_type, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Delete(tp); } else { Swig_warning(WARN_JAVA_TYPEMAP_JNI_UNDEF, input_file, line_number, "No jni typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Delete(adjustedreturntypeparm); Swig_director_parms_fixup(l); /* Attach the standard typemaps */ Swig_typemap_attach_parms("out", l, 0); Swig_typemap_attach_parms("jni", l, 0); Swig_typemap_attach_parms("jtype", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("javadirectorin", l, 0); Swig_typemap_attach_parms("directorargout", l, w); if (!ignored_method) { /* Add Java environment pointer to wrapper */ String *jenvstr = NewString("jenv"); String *jobjstr = NewString("swigjobj"); Wrapper_add_localv(w, "swigjnienv", "JNIEnvWrapper", "swigjnienv(this)", NIL, NIL); Wrapper_add_localv(w, jenvstr, "JNIEnv *", jenvstr, "= swigjnienv.getJNIEnv()", NIL); Wrapper_add_localv(w, jobjstr, "jobject", jobjstr, "= (jobject) NULL", NIL); Delete(jenvstr); Delete(jobjstr); /* Preamble code */ Printf(w->code, "if (!swig_override[%d]) {\n", classmeth_off); } if (!pure_virtual) { String *super_call = Swig_method_call(super, l); if (is_void) { Printf(w->code, "%s;\n", super_call); if (!ignored_method) Printf(w->code, "return;\n"); } else { Printf(w->code, "return %s;\n", super_call); } Delete(super_call); } else { Printf(w->code, "SWIG_JavaThrowException(JNIEnvWrapper(this).getJNIEnv(), SWIG_JavaDirectorPureVirtual, "); Printf(w->code, "\"Attempted to invoke pure virtual method %s::%s.\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); /* Make sure that we return something in the case of a pure * virtual method call for syntactical reasons. */ if (!is_void) Printf(w->code, "return %s;", qualified_return); else if (!ignored_method) Printf(w->code, "return;\n"); } if (!ignored_method) { Printf(w->code, "}\n"); Printf(w->code, "swigjobj = swig_get_self(jenv);\n"); Printf(w->code, "if (swigjobj && jenv->IsSameObject(swigjobj, NULL) == JNI_FALSE) {\n"); } /* Start the Java field descriptor for the intermediate class's upcall (insert jself object) */ Parm *tp = NewParmNode(c_classname, n); String *jdesc; if ((tm = Swig_typemap_lookup("directorin", tp, "", 0)) && (jdesc = Getattr(tp, "tmap:directorin:descriptor"))) { String *jni_canon = canonicalizeJNIDescriptor(jdesc, tp); Append(jnidesc, jni_canon); Delete(jni_canon); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap for type %s for use in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Delete(tp); /* Go through argument list, convert from native to Java */ for (i = 0, p = l; p; ++i) { /* Is this superfluous? */ while (checkAttribute(p, "tmap:directorin:numinputs", "0")) { p = Getattr(p, "tmap:directorin:next"); } SwigType *pt = Getattr(p, "type"); String *ln = makeParameterName(n, p, i, false); String *c_param_type = NULL; String *c_decl = NewString(""); String *arg = NewString(""); Printf(arg, "j%s", ln); /* Add various typemap's 'throws' clauses */ addThrows(n, "tmap:directorin", p); addThrows(n, "tmap:out", p); /* And add to the upcall args */ Printf(jupcall_args, ", %s", arg); /* Get parameter's intermediary C type */ if ((c_param_type = Getattr(p, "tmap:jni"))) { Parm *tp = NewParm(c_param_type, Getattr(p, "name"), n); String *desc_tm = NULL, *jdesc = NULL, *cdesc = NULL; /* Add to local variables */ Printf(c_decl, "%s %s", c_param_type, arg); if (!ignored_method) Wrapper_add_localv(w, arg, c_decl, (!(SwigType_ispointer(pt) || SwigType_isreference(pt)) ? "" : "= 0"), NIL); /* Add input marshalling code and update JNI field descriptor */ if ((desc_tm = Swig_typemap_lookup("directorin", tp, "", 0)) && (jdesc = Getattr(tp, "tmap:directorin:descriptor")) && (tm = Getattr(p, "tmap:directorin")) && (cdesc = Getattr(p, "tmap:directorin:descriptor"))) { // Objects marshalled by passing a Java class across the JNI boundary use jobject as the JNI type - // the nouse flag indicates this. We need the specific Java class name instead of the generic 'Ljava/lang/Object;' if (GetFlag(tp, "tmap:directorin:nouse")) jdesc = cdesc; String *jni_canon = canonicalizeJNIDescriptor(jdesc, tp); Append(jnidesc, jni_canon); Delete(jni_canon); Setattr(p, "emit:directorinput", arg); Replaceall(tm, "$input", arg); Replaceall(tm, "$owner", "0"); if (Len(tm)) if (!ignored_method) Printf(w->code, "%s\n", tm); /* Add parameter to the intermediate class code if generating the * intermediate's upcall code */ if ((tm = Getattr(p, "tmap:jtype"))) { String *din = Copy(Getattr(p, "tmap:javadirectorin")); addThrows(n, "tmap:javadirectorin", p); if (din) { Replaceall(din, "$module", module_class_name); Replaceall(din, "$imclassname", imclass_name); substituteClassname(pt, din); Replaceall(din, "$jniinput", ln); if (i > 0) Printf(imcall_args, ", "); Printf(callback_def, ", %s %s", tm, ln); if (Cmp(din, ln)) { Printv(imcall_args, din, NIL); } else Printv(imcall_args, ln, NIL); jni_canon = canonicalizeJNIDescriptor(cdesc, p); Append(classdesc, jni_canon); Delete(jni_canon); } else { Swig_warning(WARN_JAVA_TYPEMAP_JAVADIRECTORIN_UNDEF, input_file, line_number, "No javadirectorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } } else { Swig_warning(WARN_JAVA_TYPEMAP_JTYPE_UNDEF, input_file, line_number, "No jtype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } p = Getattr(p, "tmap:directorin:next"); Delete(desc_tm); } else { if (!desc_tm) { Swig_warning(WARN_JAVA_TYPEMAP_JAVADIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(c_param_type, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); p = nextSibling(p); } else if (!jdesc) { Swig_warning(WARN_JAVA_TYPEMAP_DIRECTORIN_NODESC, input_file, line_number, "Missing JNI descriptor in directorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(c_param_type, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); p = Getattr(p, "tmap:directorin:next"); } else if (!tm) { Swig_warning(WARN_JAVA_TYPEMAP_JAVADIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap defined for argument %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); p = nextSibling(p); } else if (!cdesc) { Swig_warning(WARN_JAVA_TYPEMAP_DIRECTORIN_NODESC, input_file, line_number, "Missing JNI descriptor in directorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); p = Getattr(p, "tmap:directorin:next"); } output_director = false; } } else { Swig_warning(WARN_JAVA_TYPEMAP_JNI_UNDEF, input_file, line_number, "No jni typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; p = nextSibling(p); } Delete(arg); Delete(c_decl); Delete(ln); } /* header declaration, start wrapper definition */ String *target; SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Add any exception specifications to the methods in the director class // Get any Java exception classes in the throws typemap ParmList *throw_parm_list = NULL; // May need to add Java throws clause to director methods if %catches defined // Get any Java exception classes in the throws typemap ParmList *catches_list = Getattr(n, "catchlist"); if (catches_list) { Swig_typemap_attach_parms("throws", catches_list, 0); Swig_typemap_attach_parms("directorthrows", catches_list, 0); for (p = catches_list; p; p = nextSibling(p)) { addThrows(n, "tmap:throws", p); } } if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) { Swig_typemap_attach_parms("throws", throw_parm_list, 0); Swig_typemap_attach_parms("directorthrows", throw_parm_list, 0); } for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { // %catches replaces the specified exception specification if (!catches_list) { addThrows(n, "tmap:throws", p); } if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } Printf(w->def, "%s", SwigType_str(Getattr(p, "type"), 0)); Printf(declaration, "%s", SwigType_str(Getattr(p, "type"), 0)); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); /* Emit the intermediate class's upcall to the actual class */ String *upcall = NewStringf("jself.%s(%s)", symname, imcall_args); // Handle exception classes specified in the "except" feature's "throws" attribute addThrows(n, "feature:except", n); if (!is_void) { if ((tm = Swig_typemap_lookup("javadirectorout", n, "", 0))) { addThrows(n, "tmap:javadirectorout", n); substituteClassname(returntype, tm); Replaceall(tm, "$javacall", upcall); Printf(callback_code, " return %s;\n", tm); } if ((tm = Swig_typemap_lookup("out", n, "", 0))) addThrows(n, "tmap:out", n); Delete(tm); } else Printf(callback_code, " %s;\n", upcall); Printf(callback_code, " }\n"); Delete(upcall); /* Finish off the inherited upcall's definition */ Putc(')', callback_def); generateThrowsClause(n, callback_def); Printf(callback_def, " {\n"); if (!ignored_method) { /* Emit the actual upcall through */ String *imclass_desc = NewStringf("(%s)%s", jnidesc, jniret_desc); String *class_desc = NewStringf("(%s)%s", classdesc, classret_desc); UpcallData *udata = addUpcallMethod(imclass_dmethod, symname, imclass_desc, class_desc, decl); String *methid = Getattr(udata, "imclass_methodidx"); String *methop = getUpcallJNIMethod(jniret_desc); if (!is_void) Printf(w->code, "jresult = (%s) ", c_ret_type); Printf(w->code, "jenv->%s(Swig::jclass_%s, Swig::director_method_ids[%s], %s);\n", methop, imclass_name, methid, jupcall_args); // Generate code to handle any Java exception thrown by director delegation directorExceptHandler(n, catches_list ? catches_list : throw_parm_list, w); if (!is_void) { String *jresult_str = NewString("jresult"); String *result_str = NewString("c_result"); /* Copy jresult into c_result... */ if ((tm = Swig_typemap_lookup("directorout", n, result_str, w))) { addThrows(n, "tmap:directorout", n); Replaceall(tm, "$input", jresult_str); Replaceall(tm, "$result", result_str); Printf(w->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s used in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Delete(jresult_str); Delete(result_str); } /* Marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout"))) { addThrows(n, "tmap:directorargout", p); Replaceall(tm, "$result", makeParameterName(n, p, i, false)); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } Delete(imclass_desc); Delete(class_desc); /* Terminate wrapper code */ Printf(w->code, "} else {\n"); Printf(w->code, "SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, \"null upcall object in %s::%s \");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); Printf(w->code, "}\n"); Printf(w->code, "if (swigjobj) jenv->DeleteLocalRef(swigjobj);\n"); if (!is_void) Printf(w->code, "return %s;", qualified_return); } Printf(w->code, "}"); // We expose virtual protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK && output_director) { if (!is_void) { Replaceall(w->code, "$null", qualified_return); } else { Replaceall(w->code, "$null", ""); } Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!GetFlag(n, "feature:ignore")) Printv(imclass_directors, callback_def, callback_code, NIL); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } Delete(inline_extra_method); Delete(qualified_return); Delete(jnidesc); Delete(c_ret_type); Delete(jniret_desc); Delete(declaration); Delete(callback_def); Delete(callback_code); DelWrapper(w); return status; } /* ------------------------------------------------------------ * directorExceptHandler() * * Emit code to map Java exceptions back to C++ exceptions when * feature("director:except") is applied to a method node. * This is generated after the Java method upcall. * ------------------------------------------------------------ */ void directorExceptHandler(Node *n, ParmList *throw_parm_list, Wrapper *w) { String *directorexcept = Getattr(n, "feature:director:except"); if (!directorexcept) { directorexcept = NewString(""); Printf(directorexcept, "jthrowable $error = jenv->ExceptionOccurred();\n"); Printf(directorexcept, "if ($error) {"); Printf(directorexcept, "$directorthrowshandlers\n"); Printf(directorexcept, " Swig::DirectorException::raise(jenv, $error);\n"); Printf(directorexcept, "}\n"); } else { directorexcept = Copy(directorexcept); } // Can explicitly disable director:except by setting to "" or "0" if (Len(directorexcept) > 0 && Cmp(directorexcept, "0") != 0) { // Replace $packagepath substitutePackagePath(directorexcept, 0); // Replace $directorthrowshandlers with any defined typemap handlers (or nothing) if (Strstr(directorexcept, "$directorthrowshandlers")) { String *directorthrowshandlers_code = NewString(""); for (Parm *p = throw_parm_list; p; p = nextSibling(p)) { String *tm = Getattr(p, "tmap:directorthrows"); if (tm) { // replace $packagepath/$javaclassname String *directorthrows = canonicalizeJNIDescriptor(tm, p); Printv(directorthrowshandlers_code, directorthrows, NIL); Delete(directorthrows); } else { String *t = Getattr(p,"type"); Swig_warning(WARN_TYPEMAP_DIRECTORTHROWS_UNDEF, Getfile(n), Getline(n), "No directorthrows typemap defined for %s\n", SwigType_str(t, 0)); } } Replaceall(directorexcept, "$directorthrowshandlers", directorthrowshandlers_code); Delete(directorthrowshandlers_code); } Replaceall(directorexcept, "$error", "swigerror"); Printf(w->code, " %s\n", directorexcept); } Delete(directorexcept); } /* ------------------------------------------------------------ * directorPrefixArgs() * ------------------------------------------------------------ */ void directorPrefixArgs(Node *n) { Parm *p; /* Need to prepend 'jenv' to the director constructor's argument list */ String *jenv_type = NewString("JNIEnv"); SwigType_add_pointer(jenv_type); p = NewParm(jenv_type, NewString("jenv"), n); Setattr(p, "arg:byname", "1"); set_nextSibling(p, NULL); Setattr(n, "director:prefix_args", p); } /* ------------------------------------------------------------ * classDirectorConstructor() * ------------------------------------------------------------ */ int classDirectorConstructor(Node *n) { Node *parent = parentNode(n); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *dirclassname = directorClassName(parent); String *sub = NewString(""); Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms; int argidx = 0; /* Assign arguments to superclass's parameters, if not already done */ for (p = superparms; p; p = nextSibling(p)) { String *pname = Getattr(p, "name"); if (!pname) { pname = NewStringf("arg%d", argidx++); Setattr(p, "name", pname); } } /* insert jenv prefix argument */ parms = CopyParmList(superparms); String *jenv_type = NewString("JNIEnv"); SwigType_add_pointer(jenv_type); p = NewParm(jenv_type, NewString("jenv"), n); set_nextSibling(p, parms); parms = p; directorPrefixArgs(n); if (!Getattr(n, "defaultargs")) { /* constructor */ { String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, dirclassname, parms, 0); String *call = Swig_csuperclass_call(0, basetype, superparms); String *classtype = SwigType_namestr(Getattr(n, "name")); Printf(f_directors, "%s::%s : %s, %s {\n", dirclassname, target, call, Getattr(parent, "director:ctor")); Printf(f_directors, "}\n\n"); Delete(classtype); Delete(target); Delete(call); } /* constructor header */ { String *target = Swig_method_decl(0, decl, dirclassname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(supername); Delete(jenv_type); Delete(parms); Delete(dirclassname); return Language::classDirectorConstructor(n); } /* ------------------------------------------------------------ * classDirectorDefaultConstructor() * ------------------------------------------------------------ */ int classDirectorDefaultConstructor(Node *n) { String *classname = Swig_class_name(n); String *classtype = SwigType_namestr(Getattr(n, "name")); String *dirClassName = directorClassName(n); Wrapper *w = NewWrapper(); Printf(w->def, "%s::%s(JNIEnv *jenv) : %s {", dirClassName, dirClassName, Getattr(n, "director:ctor")); Printf(w->code, "}\n"); Wrapper_print(w, f_directors); Printf(f_directors_h, " %s(JNIEnv *jenv);\n", dirClassName); DelWrapper(w); Delete(classtype); Delete(classname); Delete(dirClassName); directorPrefixArgs(n); return Language::classDirectorDefaultConstructor(n); } /* ------------------------------------------------------------ * classDirectorInit() * ------------------------------------------------------------ */ int classDirectorInit(Node *n) { Delete(none_comparison); none_comparison = NewString(""); // not used Delete(director_ctor_code); director_ctor_code = NewString("$director_new"); directorDeclaration(n); Printf(f_directors_h, "%s {\n", Getattr(n, "director:decl")); Printf(f_directors_h, "\npublic:\n"); Printf(f_directors_h, " void swig_connect_director(JNIEnv *jenv, jobject jself, jclass jcls, bool swig_mem_own, bool weak_global);\n"); /* Keep track of the director methods for this class */ first_class_dmethod = curr_class_dmethod = n_dmethods; return Language::classDirectorInit(n); } /* ---------------------------------------------------------------------- * classDirectorDestructor() * ---------------------------------------------------------------------- */ int classDirectorDestructor(Node *n) { Node *current_class = getCurrentClass(); String *full_classname = Getattr(current_class, "name"); String *classname = Swig_class_name(current_class); String *dirClassName = directorClassName(current_class); Wrapper *w = NewWrapper(); if (Getattr(n, "noexcept")) { Printf(f_directors_h, " virtual ~%s() noexcept;\n", dirClassName); Printf(w->def, "%s::~%s() noexcept {\n", dirClassName, dirClassName); } else if (Getattr(n, "throw")) { Printf(f_directors_h, " virtual ~%s() throw();\n", dirClassName); Printf(w->def, "%s::~%s() throw() {\n", dirClassName, dirClassName); } else { Printf(f_directors_h, " virtual ~%s();\n", dirClassName); Printf(w->def, "%s::~%s() {\n", dirClassName, dirClassName); } /* Ensure that correct directordisconnect typemap's method name is called * here: */ Node *disconn_attr = NewHash(); String *disconn_methodname = NULL; typemapLookup(n, "directordisconnect", full_classname, WARN_NONE, disconn_attr); disconn_methodname = Getattr(disconn_attr, "tmap:directordisconnect:methodname"); Printv(w->code, " swig_disconnect_director_self(\"", disconn_methodname, "\");\n", "}\n", NIL); Wrapper_print(w, f_directors); DelWrapper(w); Delete(disconn_attr); Delete(classname); Delete(dirClassName); return SWIG_OK; } /* ------------------------------------------------------------ * classDirectorEnd() * ------------------------------------------------------------ */ int classDirectorEnd(Node *n) { String *full_classname = Getattr(n, "name"); String *classname = getProxyName(full_classname, true); String *director_classname = directorClassName(n); String *internal_classname; Wrapper *w = NewWrapper(); if (Len(package_path) > 0) internal_classname = NewStringf("%s/%s", package_path, classname); else internal_classname = NewStringf("%s", classname); // If the namespace is multiple levels, the result of getNSpace() will have inserted // .'s to delimit namespaces, so we need to replace those with /'s Replace(internal_classname, NSPACE_SEPARATOR, "/", DOH_REPLACE_ANY); Printf(w->def, "void %s::swig_connect_director(JNIEnv *jenv, jobject jself, jclass jcls, bool swig_mem_own, bool weak_global) {", director_classname); Printf(w->def, "static jclass baseclass = swig_new_global_ref(jenv, \"%s\");\n", internal_classname); Printf(w->def, "if (!baseclass) return;\n"); Printf(w->code, "if (swig_set_self(jenv, jself, swig_mem_own, weak_global)) {\n"); int n_methods = curr_class_dmethod - first_class_dmethod; if (n_methods) { /* Emit the swig_overrides() method and the swig_override array */ Printf(f_directors_h, "public:\n"); Printf(f_directors_h, " bool swig_overrides(int n) {\n"); Printf(f_directors_h, " return (n < %d ? swig_override[n] : false);\n", n_methods); Printf(f_directors_h, " }\n"); Printf(f_directors_h, "protected:\n"); Printf(f_directors_h, " Swig::BoolArray<%d> swig_override;\n", n_methods); /* Emit the code to look up the class's methods, initialize the override array */ Printf(w->code, " bool derived = (jenv->IsSameObject(baseclass, jcls) ? false : true);\n"); Printf(w->code, " for (int i = 0; i < %d; ++i) {\n", n_methods); // Generally, derived classes have a mix of overridden and // non-overridden methods and it is worth making a GetMethodID // check during initialization to determine if each method is // overridden, thus avoiding unnecessary calls into Java. // // On the other hand, when derived classes are // expected to override all director methods then the // GetMethodID calls are inefficient, and it is better to let // the director unconditionally call up into Java. The resulting code // will still behave correctly (though less efficiently) when Java // code doesn't override a given method. // // The assumeoverride feature on a director controls whether or not // overrides are assumed. if (GetFlag(n, "feature:director:assumeoverride")) { Printf(w->code, " swig_override[i] = derived;\n"); } else { Printf(w->def, "static SwigDirectorMethod methods[] = {\n"); for (int i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); Printf(w->def, "SwigDirectorMethod(jenv, baseclass, \"%s\", \"%s\")", Getattr(udata, "method"), Getattr(udata, "fdesc")); if (i != curr_class_dmethod - 1) Putc(',', w->def); Putc('\n', w->def); } Printf(w->def, "};"); Printf(w->code, " swig_override[i] = false;\n"); Printf(w->code, " if (derived) {\n"); Printf(w->code, " jmethodID methid = jenv->GetMethodID(jcls, methods[i].name, methods[i].desc);\n"); Printf(w->code, " swig_override[i] = methods[i].methid && (methid != methods[i].methid);\n"); Printf(w->code, " jenv->ExceptionClear();\n"); Printf(w->code, " }\n"); } Printf(w->code, "}\n"); } else { Printf(f_directors_h, "public:\n"); Printf(f_directors_h, " bool swig_overrides(int n) {\n"); Printf(f_directors_h, " return false;\n"); Printf(f_directors_h, " }\n"); } Printf(f_directors_h, "};\n\n"); Printf(w->code, "}\n"); Printf(w->code, "}\n"); Wrapper_print(w, f_directors); DelWrapper(w); Delete(internal_classname); return Language::classDirectorEnd(n); } /* -------------------------------------------------------------------- * classDirectorDisown() * ------------------------------------------------------------------*/ virtual int classDirectorDisown(Node *n) { (void) n; return SWIG_OK; } /*---------------------------------------------------------------------- * extraDirectorProtectedCPPMethodsRequired() *--------------------------------------------------------------------*/ bool extraDirectorProtectedCPPMethodsRequired() const { return false; } /*---------------------------------------------------------------------- * directorDeclaration() * * Generate the director class's declaration * e.g. "class SwigDirector_myclass : public myclass, public Swig::Director {" *--------------------------------------------------------------------*/ void directorDeclaration(Node *n) { String *base = Getattr(n, "classtype"); String *class_ctor = NewString("Swig::Director(jenv)"); String *directorname = directorClassName(n); String *declaration = Swig_class_declaration(n, directorname); Printf(declaration, " : public %s, public Swig::Director", base); // Stash stuff for later. Setattr(n, "director:decl", declaration); Setattr(n, "director:ctor", class_ctor); } /*---------------------------------------------------------------------- * nestedClassesSupport() *--------------------------------------------------------------------*/ NestedClassSupport nestedClassesSupport() const { return NCS_Full; } }; /* class JAVA */ /* ----------------------------------------------------------------------------- * swig_java() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_java() { return new JAVA(); } extern "C" Language *swig_java(void) { return new_swig_java(); } /* ----------------------------------------------------------------------------- * Static member variables * ----------------------------------------------------------------------------- */ const char *JAVA::usage = "\ Java Options (available with -java)\n\ -doxygen - Convert C++ doxygen comments to JavaDoc comments in proxy classes\n\ -debug-doxygen-parser - Display doxygen parser module debugging information\n\ -debug-doxygen-translator - Display doxygen translator module debugging information\n\ -nopgcpp - Suppress premature garbage collection prevention parameter\n\ -noproxy - Generate the low-level functional interface instead\n\ of proxy classes\n\ -oldvarnames - Old intermediary method names for variable wrappers\n\ -package - Set name of the Java package to \n\ \n"; swig-4.4.0/Source/Modules/swigmain.cxx0000664000175000017500000002170615075443613017600 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigmain.cxx * * Simplified Wrapper and Interface Generator (SWIG) * * This file is the main entry point to SWIG. It collects the command * line options, registers built-in language modules, and instantiates * a module for code generation. If adding new language modules * to SWIG, you would modify this file. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include /* Module factories. These functions are used to instantiate the built-in language modules. If adding a new language module to SWIG, place a similar function here. Make sure the function has "C" linkage. This is required so that modules can be dynamically loaded in future versions. */ extern "C" { Language *swig_c(void); Language *swig_csharp(void); Language *swig_d(void); Language *swig_go(void); Language *swig_guile(void); Language *swig_java(void); Language *swig_javascript(void); Language *swig_lua(void); Language *swig_ocaml(void); Language *swig_octave(void); Language *swig_perl5(void); Language *swig_php(void); Language *swig_python(void); Language *swig_r(void); Language *swig_ruby(void); Language *swig_scilab(void); Language *swig_tcl(void); Language *swig_xml(void); } /* Association of command line options to language modules. Place an entry for new language modules here, keeping the list sorted alphabetically. */ static TargetLanguageModule modules[] = { {"-allegrocl", NULL, "ALLEGROCL", Disabled}, {"-c", swig_c, "C", Experimental}, {"-chicken", NULL, "CHICKEN", Disabled}, {"-clisp", NULL, "CLISP", Disabled}, {"-csharp", swig_csharp, "C#", Supported}, {"-d", swig_d, "D", Supported}, {"-go", swig_go, "Go", Supported}, {"-guile", swig_guile, "Guile", Supported}, {"-java", swig_java, "Java", Supported}, {"-javascript", swig_javascript, "Javascript", Supported}, {"-lua", swig_lua, "Lua", Supported}, {"-modula3", NULL, "Modula 3", Disabled}, {"-ocaml", swig_ocaml, "OCaml", Experimental}, {"-octave", swig_octave, "Octave", Supported}, {"-perl", swig_perl5, NULL, Supported}, {"-perl5", swig_perl5, "Perl 5", Supported}, {"-php", swig_php, NULL, Supported}, {"-php5", NULL, "PHP 5", Disabled}, {"-php7", swig_php, "PHP 8 or later", Supported}, {"-pike", NULL, "Pike", Disabled}, {"-python", swig_python, "Python", Supported}, {"-r", swig_r, "R (aka GNU S)", Supported}, {"-ruby", swig_ruby, "Ruby", Supported}, {"-scilab", swig_scilab, "Scilab", Supported}, {"-sexp", NULL, "Lisp S-Expressions", Disabled}, {"-tcl", swig_tcl, NULL, Supported}, {"-tcl8", swig_tcl, "Tcl 8", Supported}, {"-uffi", NULL, "Common Lisp / UFFI", Disabled}, {"-xml", swig_xml, "XML", Supported}, {NULL, NULL, NULL, Disabled} }; //----------------------------------------------------------------- // main() // // Main program. Initializes the files and starts the parser. //----------------------------------------------------------------- static void SWIG_merge_envopt(const char *env, int oargc, char *oargv[], int *nargc, char ***nargv) { if (!env) { *nargc = oargc; *nargv = (char **)Malloc(sizeof(char *) * (oargc + 1)); memcpy(*nargv, oargv, sizeof(char *) * (oargc + 1)); return; } int argc = 1; int arge = oargc + 1024; char **argv = (char **) Malloc(sizeof(char *) * (arge + 1)); char *buffer = (char *) Malloc(2048); char *b = buffer; char *be = b + 1023; const char *c = env; while ((b != be) && *c && (argc < arge)) { while (isspace(*c) && *c) ++c; if (*c) { argv[argc] = b; ++argc; } while ((b != be) && *c && !isspace(*c)) { *(b++) = *(c++); } *b++ = 0; } argv[0] = oargv[0]; for (int i = 1; (i < oargc) && (argc < arge); ++i, ++argc) { argv[argc] = oargv[i]; } argv[argc] = NULL; *nargc = argc; *nargv = argv; } static void insert_option(int *argc, char ***argv, int index, char const *start, char const *end) { int new_argc = *argc; char **new_argv = *argv; size_t option_len = end - start; // Preserve the NULL pointer at argv[argc] new_argv = (char **)Realloc(new_argv, (new_argc + 2) * sizeof(char *)); memmove(&new_argv[index + 1], &new_argv[index], sizeof(char *) * (new_argc + 1 - index)); new_argc++; new_argv[index] = (char *)Malloc(option_len + 1); memcpy(new_argv[index], start, option_len); new_argv[index][option_len] = '\0'; *argc = new_argc; *argv = new_argv; } static void merge_options_files(int *argc, char ***argv) { static const int BUFFER_SIZE = 4096; char buffer[BUFFER_SIZE]; int i; int insert; char **new_argv = *argv; int new_argc = *argc; FILE *f; i = 1; while (i < new_argc) { if (new_argv[i] && new_argv[i][0] == '@' && (f = fopen(&new_argv[i][1], "r"))) { int ci; char *b; char *be = &buffer[BUFFER_SIZE]; int quote = 0; bool escape = false; new_argc--; memmove(&new_argv[i], &new_argv[i + 1], sizeof(char *) * (new_argc - i)); insert = i; b = buffer; while ((ci = fgetc(f)) != EOF) { const char c = static_cast(ci); if (escape) { if (b != be) { *b = c; ++b; } escape = false; } else if (c == '\\') { escape = true; } else if (!quote && (c == '\'' || c == '"')) { quote = c; } else if (quote && c == quote) { quote = 0; } else if (isspace(c) && !quote) { if (b != buffer) { insert_option(&new_argc, &new_argv, insert, buffer, b); insert++; b = buffer; } } else if (b != be) { *b = c; ++b; } } if (b != buffer) insert_option(&new_argc, &new_argv, insert, buffer, b); fclose(f); } else { ++i; } } *argv = new_argv; *argc = new_argc; } int main(int margc, char **margv) { int i; const TargetLanguageModule *language_module = 0; int argc; char **argv; /* Check for SWIG_FEATURES environment variable */ SWIG_merge_envopt(getenv("SWIG_FEATURES"), margc, margv, &argc, &argv); merge_options_files(&argc, &argv); Swig_init_args(argc, argv); /* Get options */ for (i = 1; i < argc; i++) { if (argv[i]) { bool is_target_language_module = false; for (int j = 0; modules[j].name; j++) { if (strcmp(modules[j].name, argv[i]) == 0) { if (!language_module) { language_module = &modules[j]; is_target_language_module = true; } else { Printf(stderr, "Only one target language can be supported at a time (both %s and %s were specified).\n", language_module->name, argv[i]); Exit(EXIT_FAILURE); } } } if (is_target_language_module) { Swig_mark_arg(i); if (language_module->status == Disabled) { if (language_module->help) Printf(stderr, "Target language option %s (%s) is no longer supported.\n", language_module->name, language_module->help); else Printf(stderr, "Target language option %s is no longer supported.\n", language_module->name); Exit(EXIT_FAILURE); } } else if ((strcmp(argv[i], "-help") == 0) || (strcmp(argv[i], "--help") == 0)) { if (strcmp(argv[i], "--help") == 0) strcpy(argv[i], "-help"); Printf(stdout, "Supported Target Language Options\n"); int experimental_count = 0, deprecated_count = 0; for (int j = 0; modules[j].name; j++) { if (modules[j].help) { switch (modules[j].status) { case Supported: Printf(stdout, " %-15s - Generate %s wrappers\n", modules[j].name, modules[j].help); break; case Experimental: ++experimental_count; break; case Deprecated: ++deprecated_count; break; case Disabled: // Avoids -Wswitch GCC warning. break; } } } if (experimental_count) { Printf(stdout, "\nExperimental Target Language Options\n"); for (int j = 0; modules[j].name; j++) { if (modules[j].help && modules[j].status == Experimental) { Printf(stdout, " %-15s - Generate %s wrappers\n", modules[j].name, modules[j].help); } } } if (deprecated_count) { Printf(stdout, "\nDeprecated Target Language Options\n"); for (int j = 0; modules[j].name; j++) { if (modules[j].help && modules[j].status == Deprecated) { Printf(stdout, " %-15s - Generate %s wrappers\n", modules[j].name, modules[j].help); } } } // Swig_mark_arg not called as the general -help options also need to be displayed later on } } } int res = SWIG_main(argc, argv, language_module); return res; } swig-4.4.0/Source/Modules/interface.cxx0000664000175000017500000002033115075443613017713 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * interface.cxx * * This module contains support for the interface feature. * This feature is used in language modules where the target language does not * naturally support C++ style multiple inheritance, but does support inheritance * from multiple interfaces. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" static bool interface_feature_enabled = false; /* ----------------------------------------------------------------------------- * collect_interface_methods() * * Create a list of all the methods from the base classes of class n that are * marked as an interface. The resulting list is thus the list of methods that * need to be implemented in order for n to be non-abstract. * ----------------------------------------------------------------------------- */ static List *collect_interface_methods(Node *n) { List *methods = NewList(); if (List *bases = Getattr(n, "interface:bases")) { for (Iterator base = First(bases); base.item; base = Next(base)) { Node *cls = base.item; if (cls == n) continue; for (Node *child = firstChild(cls); child; child = nextSibling(child)) { if (Cmp(nodeType(child), "cdecl") == 0) { if (GetFlag(child, "feature:ignore") || Getattr(child, "interface:owner")) continue; // skip methods propagated to bases if (!checkAttribute(child, "kind", "function")) continue; if (checkAttribute(child, "storage", "static")) continue; // accept virtual methods, non-virtual methods too... mmm??. Warn that the interface class has something that is not a virtual method? Node *nn = copyNode(child); Setattr(nn, "interface:owner", cls); ParmList *parms = CopyParmList(Getattr(child, "parms")); Setattr(nn, "parms", parms); Delete(parms); ParmList *throw_parm_list = Getattr(child, "throws"); if (throw_parm_list) Setattr(nn, "throws", CopyParmList(throw_parm_list)); Append(methods, nn); } } } } return methods; } /* ----------------------------------------------------------------------------- * collect_interface_bases * ----------------------------------------------------------------------------- */ static void collect_interface_bases(List *bases, Node *n) { if (GetFlag(n, "feature:interface")) { if (!Swig_item_in_list(bases, n)) Append(bases, n); } if (List *baselist = Getattr(n, "bases")) { for (Iterator base = First(baselist); base.item; base = Next(base)) { if (!GetFlag(base.item, "feature:ignore")) { if (GetFlag(base.item, "feature:interface")) collect_interface_bases(bases, base.item); } } } } /* ----------------------------------------------------------------------------- * collect_interface_base_classes() * * Create a list containing all the classes up the inheritance hierarchy * marked with feature:interface (including this class n). * Stops going up the inheritance chain as soon as a class is found without * feature:interface. * Remove duplicate bases (in the event of multiple inheritance). * The idea is to find all the base interfaces that a class must implement. * ----------------------------------------------------------------------------- */ static void collect_interface_base_classes(Node *n) { if (GetFlag(n, "feature:interface")) { // check all bases are also interfaces if (List *baselist = Getattr(n, "bases")) { for (Iterator base = First(baselist); base.item; base = Next(base)) { if (!GetFlag(base.item, "feature:ignore")) { if (!GetFlag(base.item, "feature:interface")) { Swig_error(Getfile(n), Getline(n), "Base class '%s' of '%s' is not similarly marked as an interface.\n", SwigType_namestr(Getattr(base.item, "name")), SwigType_namestr(Getattr(n, "name"))); Exit(EXIT_FAILURE); } } } } } List *interface_bases = NewList(); collect_interface_bases(interface_bases, n); if (Len(interface_bases) == 0) Delete(interface_bases); else Setattr(n, "interface:bases", interface_bases); } /* ----------------------------------------------------------------------------- * process_interface_name() * ----------------------------------------------------------------------------- */ static void process_interface_name(Node *n) { if (GetFlag(n, "feature:interface")) { String *interface_name = Getattr(n, "feature:interface:name"); if (!Len(interface_name)) { Swig_error(Getfile(n), Getline(n), "The interface feature for '%s' is missing the name attribute.\n", SwigType_namestr(Getattr(n, "name"))); Exit(EXIT_FAILURE); } if (Strchr(interface_name, '%')) { String *name = NewStringf(interface_name, Getattr(n, "sym:name")); Setattr(n, "interface:name", name); } else { Setattr(n, "interface:name", interface_name); } } } /* ----------------------------------------------------------------------------- * Swig_interface_propagate_methods() * * Find all the base classes marked as an interface (with feature:interface) for * class node n. For each of these, add all of its methods as methods of n so that * n is not abstract. If class n is also marked as an interface, it will remain * abstract and not have any methods added. * ----------------------------------------------------------------------------- */ void Swig_interface_propagate_methods(Node *n) { if (interface_feature_enabled) { process_interface_name(n); collect_interface_base_classes(n); List *methods = collect_interface_methods(n); bool is_interface = GetFlag(n, "feature:interface") ? true : false; for (Iterator mi = First(methods); mi.item; mi = Next(mi)) { if (!is_interface && GetFlag(mi.item, "abstract")) continue; String *this_decl = Getattr(mi.item, "decl"); String *this_decl_resolved = SwigType_typedef_resolve_all(this_decl); bool identically_overloaded_method = false; // true when a base class' method is implemented in n if (SwigType_isfunction(this_decl_resolved)) { String *name = Getattr(mi.item, "name"); for (Node *child = firstChild(n); child; child = nextSibling(child)) { if (Getattr(child, "interface:owner")) break; // at the end of the list are newly appended methods if (Cmp(nodeType(child), "cdecl") == 0) { if (checkAttribute(child, "name", name)) { String *decl = SwigType_typedef_resolve_all(Getattr(child, "decl")); identically_overloaded_method = Strcmp(decl, this_decl_resolved) == 0; Delete(decl); if (identically_overloaded_method) break; } } } } Delete(this_decl_resolved); if (!identically_overloaded_method) { // Add method copied from base class to this derived class Node *cn = mi.item; Delattr(cn, "sym:overname"); String *prefix = Getattr(n, "name"); String *name = Getattr(cn, "name"); String *decl = Getattr(cn, "decl"); String *oldname = Getattr(cn, "sym:name"); String *symname = Swig_name_make(cn, prefix, name, decl, oldname); if (Strcmp(symname, "$ignore") != 0) { Symtab *oldscope = Swig_symbol_setscope(Getattr(n, "symtab")); Node *on = Swig_symbol_add(symname, cn); (void)on; assert(on == cn); // Features from the copied base class method are already present, now add in features specific to the added method in the derived class Swig_features_get(Swig_cparse_features(), Swig_symbol_qualifiedscopename(0), name, decl, cn); Swig_symbol_setscope(oldscope); appendChild(n, cn); } } else { Delete(mi.item); } } Delete(methods); } } /* ----------------------------------------------------------------------------- * Swig_interface_feature_enable() * * Turn on interface feature support * ----------------------------------------------------------------------------- */ void Swig_interface_feature_enable() { interface_feature_enabled = true; } swig-4.4.0/Source/Modules/javascript.cxx0000664000175000017500000030354015075443613020127 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * javascript.cxx * * Javascript language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" /** * Enables extra debugging information in typemaps. */ static bool js_template_enable_debug = false; #define ERR_MSG_ONLY_ONE_ENGINE_PLEASE "Only one engine can be specified at a time." // keywords used for state variables #define NAME "name" #define NAME_MANGLED "name_mangled" #define INDEX "idx" #define TYPE "type" #define TYPE_MANGLED "type_mangled" #define WRAPPER_NAME "wrapper" #define IS_IMMUTABLE "is_immutable" #define IS_STATIC "is_static" #define IS_ABSTRACT "is_abstract" #define IS_WRAPPED "is_wrapped" #define GETTER "getter" #define SETTER "setter" #define PARENT "parent" #define PARENT_MANGLED "parent_mangled" #define CTOR "ctor" #define CTOR_WRAPPERS "ctor_wrappers" #define CTOR_DISPATCHERS "ctor_dispatchers" #define DTOR "dtor" #define ARGCOUNT "wrap:argc" #define ARGREQUIRED "wrap:argmin" #define HAS_TEMPLATES "has_templates" #define FORCE_CPP "force_cpp" #define RESET true // keys for global state variables #define CREATE_NAMESPACES "create_namespaces" #define REGISTER_NAMESPACES "register_namespaces" #define INITIALIZER "initializer" // keys for class scoped state variables #define MEMBER_VARIABLES "member_variables" #define MEMBER_FUNCTIONS "member_functions" #define STATIC_FUNCTIONS "static_functions" #define STATIC_VARIABLES "static_variables" /** * A convenience class to manage state variables for emitters. * The implementation delegates to SWIG Hash DOHs and provides * named sub-hashes for class, variable, and function states. */ class JSEmitterState { public: JSEmitterState(); ~JSEmitterState(); DOH *globals(); DOH *globals(const char *key, DOH *initial = 0); DOH *clazz(bool reset = false); DOH *clazz(const char *key, DOH *initial = 0); DOH *function(bool reset = false); DOH *function(const char *key, DOH *initial = 0); DOH *variable(bool reset = false); DOH *variable(const char *key, DOH *initial = 0); static int IsSet(DOH *val); private: DOH *getState(const char *key, bool reset = false); Hash *globalHash; }; /** * A convenience class that wraps a code snippet used as template * for code generation. */ class Template { public: Template(const String *code); Template(const String *code, const String *templateName); Template(const Template & other); ~Template(); String *str(); Template & replace(const String *pattern, const String *repl); Template & print(DOH *doh); Template & pretty_print(DOH *doh); void operator=(const Template & t); Template & trim(); private: String *code; String *templateName; }; /** * JSEmitter represents an abstraction of javascript code generators * for different javascript engines. **/ class JSEmitter { protected: typedef JSEmitterState State; enum MarshallingMode { Setter, Getter, Ctor, Function }; public: enum JSEngine { JavascriptCore, V8, NodeJS, NAPI }; JSEmitter(JSEngine engine); virtual ~ JSEmitter(); /** * Opens output files and temporary output DOHs. */ virtual int initialize(Node *n); /** * Writes all collected code into the output file(s). */ virtual int dump(Node *n) = 0; /** * Cleans up all open output DOHs. */ virtual int close() = 0; /** * Switches the context for code generation. * * Classes, global variables and global functions may need to * be registered in certain static tables. * This method should be used to switch output DOHs correspondingly. */ virtual int switchNamespace(Node *); /** * Invoked at the beginning of the classHandler. */ virtual int enterClass(Node *); /** * Invoked at the end of the classHandler. */ virtual int exitClass(Node *) { return SWIG_OK; } /** * Invoked at the beginning of the variableHandler. */ virtual int enterVariable(Node *); /** * Invoked at the end of the variableHandler. */ virtual int exitVariable(Node *) { return SWIG_OK; } /** * Invoked at the beginning of the functionHandler. */ virtual int enterFunction(Node *); /** * Invoked at the end of the functionHandler. */ virtual int exitFunction(Node *) { return SWIG_OK; } /** * Invoked by functionWrapper callback after call to Language::functionWrapper. */ virtual int emitWrapperFunction(Node *n); /** * Invoked by nativeWrapper callback */ virtual int emitNativeFunction(Node *n); /** * Invoked from constantWrapper after call to Language::constantWrapper. **/ virtual int emitConstant(Node *n); /** * Registers a given code snippet for a given key name. * * This method is called by the fragmentDirective handler * of the JAVASCRIPT language module. **/ int registerTemplate(const String *name, const String *code); /** * Retrieve the code template registered for a given name. */ Template getTemplate(const String *name); State & getState(); protected: /** * Helper function for detecting if the constructor Node does not use a name that matches * the expected name for a constructor. Occurs when %rename is used for just a constructor * or %template instantiates a templated constructor with a different name to the class. */ bool isRenamedConstructor(Node *n); /** * Generates code for a constructor function. */ virtual int emitCtor(Node *n); /** * Generates code for a destructor function. */ virtual int emitDtor(Node *n); /** * Generates code for a function. */ virtual int emitFunction(Node *n, bool is_member, bool is_static); virtual int emitFunctionDispatcher(Node *n, bool /*is_member */ ); /** * Generates code for a getter function. */ virtual int emitGetter(Node *n, bool is_member, bool is_static); /** * Generates code for a setter function. */ virtual int emitSetter(Node *n, bool is_member, bool is_static); virtual void marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static) = 0; virtual String *emitInputTypemap(Node *n, Parm *params, Wrapper *wrapper, String *arg); virtual String *emitCheckTypemap(Node *n, Parm *params, Wrapper *wrapper, String *arg); virtual void marshalOutput(Node *n, ParmList *params, Wrapper *wrapper, String *actioncode, const String *cresult = 0, bool emitReturnVariable = true); virtual void emitCleanupCode(Node *n, Wrapper *wrapper, ParmList *params); /** * Helper function to retrieve the first parent class node. */ Node *getBaseClass(Node *n); virtual int createNamespace(String *scope); virtual Hash *createNamespaceEntry(const char *name, const char *parent, const char *parent_mangled); virtual int emitNamespaces() = 0; virtual const char *getFunctionTemplate(bool); virtual const char *getFunctionDispatcherTemplate(bool); virtual const char *getOverloadedFunctionTemplate(bool); virtual const char *getSetterTemplate(bool); virtual const char *getGetterTemplate(bool); protected: JSEngine engine; Hash *templates; State state; // contains context specific data (DOHs) // to allow generation of namespace related code // which are switched on namespace change Hash *namespaces; Hash *current_namespace; String *defaultResultName; File *f_wrappers; }; /* factory methods for concrete JSEmitters: */ JSEmitter *swig_javascript_create_JSCEmitter(); JSEmitter *swig_javascript_create_V8Emitter(); JSEmitter *swig_javascript_create_NodeJSEmitter(); JSEmitter *swig_javascript_create_NAPIEmitter(); /********************************************************************** * JAVASCRIPT: SWIG module implementation **********************************************************************/ class JAVASCRIPT:public Language { public: JAVASCRIPT():emitter(NULL) { } ~JAVASCRIPT() { delete emitter; } virtual int functionHandler(Node *n); virtual int globalfunctionHandler(Node *n); virtual int variableHandler(Node *n); virtual int globalvariableHandler(Node *n); virtual int staticmemberfunctionHandler(Node *n); virtual int classHandler(Node *n); virtual int functionWrapper(Node *n); virtual int constantWrapper(Node *n); virtual int nativeWrapper(Node *n); virtual void main(int argc, char *argv[]); virtual int top(Node *n); /** * Registers all %fragments assigned to section "templates". **/ virtual int fragmentDirective(Node *n); public: virtual String *getNSpace() const; private: JSEmitter *emitter; }; /* --------------------------------------------------------------------- * functionWrapper() * * Low level code generator for functions * --------------------------------------------------------------------- */ int JAVASCRIPT::functionWrapper(Node *n) { // note: the default implementation only prints a message // Language::functionWrapper(n); emitter->emitWrapperFunction(n); return SWIG_OK; } /* --------------------------------------------------------------------- * functionHandler() * * Function handler for generating wrappers for functions * --------------------------------------------------------------------- */ int JAVASCRIPT::functionHandler(Node *n) { if (GetFlag(n, "isextension") == 1) { SetFlag(n, "ismember"); } emitter->enterFunction(n); Language::functionHandler(n); emitter->exitFunction(n); return SWIG_OK; } /* --------------------------------------------------------------------- * globalfunctionHandler() * * Function handler for generating wrappers for functions * --------------------------------------------------------------------- */ int JAVASCRIPT::globalfunctionHandler(Node *n) { emitter->switchNamespace(n); Language::globalfunctionHandler(n); return SWIG_OK; } /* --------------------------------------------------------------------- * staticmemberfunctionHandler() * * Function handler for generating wrappers for static member functions * --------------------------------------------------------------------- */ int JAVASCRIPT::staticmemberfunctionHandler(Node *n) { /* * Note: storage=static is removed by Language::staticmemberfunctionHandler. * So, don't rely on that after here. Instead use the state variable which is * set by JSEmitter::enterFunction(). */ Language::staticmemberfunctionHandler(n); return SWIG_OK; } /* --------------------------------------------------------------------- * variableHandler() * * Function handler for generating wrappers for variables * --------------------------------------------------------------------- */ int JAVASCRIPT::variableHandler(Node *n) { emitter->enterVariable(n); Language::variableHandler(n); emitter->exitVariable(n); return SWIG_OK; } /* --------------------------------------------------------------------- * globalvariableHandler() * * Function handler for generating wrappers for global variables * --------------------------------------------------------------------- */ int JAVASCRIPT::globalvariableHandler(Node *n) { emitter->switchNamespace(n); Language::globalvariableHandler(n); return SWIG_OK; } /* --------------------------------------------------------------------- * constantHandler() * * Function handler for generating wrappers for constants * --------------------------------------------------------------------- */ int JAVASCRIPT::constantWrapper(Node *n) { emitter->switchNamespace(n); // Note: callbacks trigger this wrapper handler // TODO: handle callback declarations if (Equal(Getattr(n, "kind"), "function")) { return SWIG_OK; } // TODO: the emitter for constants must be implemented in a cleaner way // currently we treat it like a read-only variable // however, there is a remaining bug with function pointer constants // which could be fixed with a cleaner approach emitter->emitConstant(n); return SWIG_OK; } /* --------------------------------------------------------------------- * nativeWrapper() * * Function wrapper for generating placeholders for native functions * --------------------------------------------------------------------- */ int JAVASCRIPT::nativeWrapper(Node *n) { emitter->emitNativeFunction(n); return SWIG_OK; } /* --------------------------------------------------------------------- * classHandler() * * Function handler for generating wrappers for class * --------------------------------------------------------------------- */ int JAVASCRIPT::classHandler(Node *n) { emitter->switchNamespace(n); emitter->enterClass(n); Language::classHandler(n); emitter->exitClass(n); return SWIG_OK; } int JAVASCRIPT::fragmentDirective(Node *n) { // catch all fragment directives that have "templates" as location // and register them at the emitter. String *section = Getattr(n, "section"); if (Equal(section, "templates") && !ImportMode) { emitter->registerTemplate(Getattr(n, "value"), Getattr(n, "code")); } else { return Language::fragmentDirective(n); } return SWIG_OK; } String *JAVASCRIPT::getNSpace() const { return Language::getNSpace(); } /* --------------------------------------------------------------------- * top() * * Function handler for processing top node of the parse tree * Wrapper code generation essentially starts from here * --------------------------------------------------------------------- */ int JAVASCRIPT::top(Node *n) { emitter->initialize(n); Language::top(n); emitter->dump(n); emitter->close(); return SWIG_OK; } static const char *usage = (char *)"\ Javascript Options (available with -javascript)\n\ -jsc - creates a JavascriptCore extension \n\ -v8 - creates a v8 extension \n\ -node - creates a node.js extension \n\ -napi - creates a NAPI extension \n\ -debug-codetemplates - generates information about the origin of code templates\n"; /* --------------------------------------------------------------------- * main() * * Entry point for the JAVASCRIPT module * --------------------------------------------------------------------- */ void JAVASCRIPT::main(int argc, char *argv[]) { // Set javascript subdirectory in SWIG library SWIG_library_directory("javascript"); int engine = -1; for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-v8") == 0) { if (engine != -1) { Printf(stderr, ERR_MSG_ONLY_ONE_ENGINE_PLEASE); Exit(EXIT_FAILURE); } Swig_mark_arg(i); engine = JSEmitter::V8; } else if (strcmp(argv[i], "-jsc") == 0) { if (engine != -1) { Printf(stderr, ERR_MSG_ONLY_ONE_ENGINE_PLEASE); Exit(EXIT_FAILURE); } Swig_mark_arg(i); engine = JSEmitter::JavascriptCore; } else if (strcmp(argv[i], "-node") == 0) { if (engine != -1) { Printf(stderr, ERR_MSG_ONLY_ONE_ENGINE_PLEASE); Exit(EXIT_FAILURE); } Swig_mark_arg(i); engine = JSEmitter::NodeJS; } else if (strcmp(argv[i], "-napi") == 0) { if (engine != -1) { Printf(stderr, ERR_MSG_ONLY_ONE_ENGINE_PLEASE); Exit(EXIT_FAILURE); } Swig_mark_arg(i); engine = JSEmitter::NAPI; } else if (strcmp(argv[i], "-debug-codetemplates") == 0) { Swig_mark_arg(i); js_template_enable_debug = true; } else if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); return; } } } switch (engine) { case JSEmitter::NodeJS: case JSEmitter::V8: { emitter = swig_javascript_create_V8Emitter(); Preprocessor_define("SWIG_JAVASCRIPT_V8 1", 0); SWIG_library_directory("javascript/v8"); // V8 API is C++, so output must be C++ compatible even when wrapping C code if (!cparse_cplusplus) { Swig_cparse_cplusplusout(1); } if (engine == JSEmitter::NodeJS) { Preprocessor_define("BUILDING_NODE_EXTENSION 1", 0); } break; } case JSEmitter::JavascriptCore: { emitter = swig_javascript_create_JSCEmitter(); Preprocessor_define("SWIG_JAVASCRIPT_JSC 1", 0); SWIG_library_directory("javascript/jsc"); break; } case JSEmitter::NAPI: { emitter = swig_javascript_create_NAPIEmitter(); Preprocessor_define("SWIG_JAVASCRIPT_NAPI 1", 0); SWIG_library_directory("javascript/napi"); Preprocessor_define("BUILDING_NODE_EXTENSION 1", 0); if (!cparse_cplusplus) { Swig_cparse_cplusplusout(1); } break; } default: { Printf(stderr, "SWIG Javascript: Unknown engine. Please specify one of '-jsc', '-v8', '-node' or '-napi'.\n"); Exit(EXIT_FAILURE); break; } } // Add a symbol to the parser for conditional compilation Preprocessor_define("SWIGJAVASCRIPT 1", 0); // Set configuration file SWIG_config_file("javascript.swg"); allow_overloading(); } /* ----------------------------------------------------------------------------- * swig_javascript() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_javascript() { return new JAVASCRIPT(); } extern "C" Language *swig_javascript(void) { return new_swig_javascript(); } /********************************************************************** * Emitter implementations **********************************************************************/ /* ----------------------------------------------------------------------------- * JSEmitter() * ----------------------------------------------------------------------------- */ JSEmitter::JSEmitter(JSEmitter::JSEngine engine) : engine(engine), templates(NewHash()), namespaces(NULL), current_namespace(NULL), defaultResultName(NewString("result")), f_wrappers(NULL) { } /* ----------------------------------------------------------------------------- * ~JSEmitter() * ----------------------------------------------------------------------------- */ JSEmitter::~JSEmitter() { Delete(templates); } /* ----------------------------------------------------------------------------- * JSEmitter::RegisterTemplate() : Registers a code template * * Note: this is used only by JAVASCRIPT::fragmentDirective(). * ----------------------------------------------------------------------------- */ int JSEmitter::registerTemplate(const String *name, const String *code) { if (!State::IsSet(state.globals(HAS_TEMPLATES))) { SetFlag(state.globals(), HAS_TEMPLATES); } return Setattr(templates, name, code); } /* ----------------------------------------------------------------------------- * JSEmitter::getTemplate() : Provides a registered code template * ----------------------------------------------------------------------------- */ Template JSEmitter::getTemplate(const String *name) { String *templ = Getattr(templates, name); if (!templ) { Printf(stderr, "Could not find template %s\n.", name); Exit(EXIT_FAILURE); } Template t(templ, name); return t; } JSEmitterState & JSEmitter::getState() { return state; } int JSEmitter::initialize(Node * /*n */ ) { if (namespaces != NULL) { Delete(namespaces); } namespaces = NewHash(); Hash *global_namespace = createNamespaceEntry("exports", 0, 0); Setattr(namespaces, "::", global_namespace); current_namespace = global_namespace; f_wrappers = NewString(""); return SWIG_OK; } /* ----------------------------------------------------------------------------- * JSEmitter::getBaseClass() : the node of the base class or NULL * * Note: the first base class is provided. Multiple inheritance is not * supported. * ----------------------------------------------------------------------------- */ Node *JSEmitter::getBaseClass(Node *n) { // retrieve the first base class that is not %ignored List *baselist = Getattr(n, "bases"); if (baselist) { Iterator base = First(baselist); while (base.item && GetFlag(base.item, "feature:ignore")) { base = Next(base); } return base.item; } return NULL; } /* ----------------------------------------------------------------------------- * JSEmitter::emitWrapperFunction() : dispatches emitter functions. * * This allows having small sized, dedicated emitting functions. * All state dependent branching is done here. * ----------------------------------------------------------------------------- */ int JSEmitter::emitWrapperFunction(Node *n) { int ret = SWIG_OK; String *kind = Getattr(n, "kind"); if (kind) { if (Equal(kind, "function") // HACK: sneaky.ctest revealed that typedef'd (global) functions must be // detected via the 'view' attribute. || (Equal(kind, "variable") && Equal(Getattr(n, "view"), "globalfunctionHandler")) ) { bool is_member = GetFlag(n, "ismember") != 0 || GetFlag(n, "feature:extend") != 0; bool is_static = GetFlag(state.function(), IS_STATIC) != 0; ret = emitFunction(n, is_member, is_static); } else if (Cmp(kind, "variable") == 0) { bool is_static = GetFlag(state.variable(), IS_STATIC) != 0; // HACK: smartpointeraccessed static variables are not treated as statics if (GetFlag(n, "allocate:smartpointeraccess")) { is_static = false; } bool is_member = GetFlag(n, "ismember") != 0; bool is_setter = GetFlag(n, "memberset") != 0 || GetFlag(n, "varset") != 0; bool is_getter = GetFlag(n, "memberget") != 0 || GetFlag(n, "varget") != 0; if (is_setter) { ret = emitSetter(n, is_member, is_static); } else if (is_getter) { ret = emitGetter(n, is_member, is_static); } } else { Printf(stderr, "Warning: unsupported wrapper function type\n"); ret = SWIG_ERROR; } } else { String *view = Getattr(n, "view"); if (Cmp(view, "constructorHandler") == 0) { ret = emitCtor(n); } else if (Cmp(view, "destructorHandler") == 0) { ret = emitDtor(n); } else { Printf(stderr, "Warning: unsupported wrapper function type"); ret = SWIG_ERROR; } } return ret; } int JSEmitter::emitNativeFunction(Node *n) { String *wrapname = Getattr(n, "wrap:name"); enterFunction(n); state.function(WRAPPER_NAME, wrapname); exitFunction(n); return SWIG_OK; } int JSEmitter::enterClass(Node *n) { state.clazz(RESET); state.clazz(NAME, Getattr(n, "sym:name")); state.clazz("nspace", current_namespace); // Creating a mangled name using the current namespace and the symbol name String *mangled_name = NewString(""); Printf(mangled_name, "%s_%s", Getattr(current_namespace, NAME_MANGLED), Getattr(n, "sym:name")); state.clazz(NAME_MANGLED, SwigType_manglestr(mangled_name)); Delete(mangled_name); state.clazz(TYPE, NewString(Getattr(n, "classtype"))); String *type = SwigType_manglestr(Getattr(n, "classtypeobj")); String *classtype_mangled = NewString(""); Printf(classtype_mangled, "p%s", type); state.clazz(TYPE_MANGLED, classtype_mangled); Delete(type); String *ctor_wrapper = NewString("_wrap_new_veto_"); Append(ctor_wrapper, state.clazz(NAME)); state.clazz(CTOR, ctor_wrapper); state.clazz(CTOR_DISPATCHERS, NewString("")); state.clazz(DTOR, NewString("0")); // HACK: assume that a class is abstract // this is resolved by emitCtor (which is only called for non abstract classes) SetFlag(state.clazz(), IS_ABSTRACT); return SWIG_OK; } int JSEmitter::enterFunction(Node *n) { state.function(RESET); state.function(NAME, Getattr(n, "sym:name")); if (Equal(Getattr(n, "storage"), "static")) { SetFlag(state.function(), IS_STATIC); } return SWIG_OK; } int JSEmitter::enterVariable(Node *n) { // reset the state information for variables. state.variable(RESET); // Retrieve a pure symbol name. Using 'sym:name' as a basis, as it considers %renamings. if (Equal(Getattr(n, "view"), "memberconstantHandler")) { // Note: this is kind of hacky/experimental // For constants/enums 'sym:name' contains e.g., 'Foo_Hello' instead of 'Hello' state.variable(NAME, Getattr(n, "memberconstantHandler:sym:name")); } else { state.variable(NAME, Swig_scopename_last(Getattr(n, "sym:name"))); } if (Equal(Getattr(n, "storage"), "static")) { SetFlag(state.variable(), IS_STATIC); } if (Language::instance()->is_immutable(n)) { SetFlag(state.variable(), IS_IMMUTABLE); } // FIXME: test "arrays_global" does not compile with that as it is not allowed to assign to char[] if (Equal(Getattr(n, "type"), "a().char")) { SetFlag(state.variable(), IS_IMMUTABLE); } return SWIG_OK; } const char *JSEmitter::getFunctionTemplate(bool) { return "js_function"; } const char *JSEmitter::getFunctionDispatcherTemplate(bool) { return "js_function_dispatcher"; } const char *JSEmitter::getOverloadedFunctionTemplate(bool) { return "js_overloaded_function"; } const char *JSEmitter::getGetterTemplate(bool) { return "js_getter"; } const char *JSEmitter::getSetterTemplate(bool) { return "js_setter"; } bool JSEmitter::isRenamedConstructor(Node *n) { Node *cls = parentNode(n); if (!Equal(nodeType(cls), "class")) { cls = parentNode(cls); assert(Equal(nodeType(cls), "class")); } return !Equal(Getattr(n, "constructorHandler:sym:name"), Getattr(cls, "sym:name")); } int JSEmitter::emitCtor(Node *n) { // Constructor renaming does not work in JavaScript. // This allows us to slip past the unit tests which are broken for all JavaScript backends. // TODO: fix by correcting the bad constructor name - this is the approach used in the Java module. if (isRenamedConstructor(n)) return SWIG_ERROR; Wrapper *wrapper = NewWrapper(); bool is_overloaded = GetFlag(n, "sym:overloaded") != 0; Template t_ctor(getTemplate("js_ctor")); // prepare the function wrapper name String *iname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); String *wrap_name = Swig_name_wrapper(iname); if (is_overloaded) { t_ctor = getTemplate("js_overloaded_ctor"); Append(wrap_name, Getattr(n, "sym:overname")); } Setattr(n, "wrap:name", wrap_name); // note: we can remove the is_abstract flag now, as this // is called for non-abstract classes only. Setattr(state.clazz(), IS_ABSTRACT, 0); ParmList *params = Getattr(n, "parms"); emit_parameter_variables(params, wrapper); emit_attach_parmmaps(params, wrapper); Printf(wrapper->locals, "%sresult;", SwigType_str(Getattr(n, "type"), 0)); marshalInputArgs(n, params, wrapper, Ctor, true, false); String *action = emit_action(n); Printv(wrapper->code, action, "\n", 0); emitCleanupCode(n, wrapper, params); bool isvoid = !Cmp(returntype, "void"); Replaceall(wrapper->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(wrapper->code, "$symname", iname); t_ctor.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wrap_name) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jslocals", wrapper->locals) .replace("$jscode", wrapper->code) .replace("$jsargcount", Getattr(n, ARGCOUNT)) .replace("$jsparent", state.clazz(PARENT_MANGLED)) .replace("$jsargrequired", Getattr(n, ARGREQUIRED)) .pretty_print(f_wrappers); Template t_ctor_case(getTemplate("js_ctor_dispatch_case")); t_ctor_case.replace("$jswrapper", wrap_name) .replace("$jsargcount", Getattr(n, ARGCOUNT)) .replace("$jsargrequired", Getattr(n, ARGREQUIRED)); Append(state.clazz(CTOR_DISPATCHERS), t_ctor_case.str()); DelWrapper(wrapper); // create a dispatching ctor if (is_overloaded) { if (!Getattr(n, "sym:nextSibling")) { String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); Template t_mainctor(getTemplate("js_ctor_dispatcher")); t_mainctor.replace("$jswrapper", wrap_name) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsdispatchcases", state.clazz(CTOR_DISPATCHERS)) .replace("$jsparent", state.clazz(PARENT_MANGLED)) .pretty_print(f_wrappers); state.clazz(CTOR, wrap_name); } } else { state.clazz(CTOR, wrap_name); } return SWIG_OK; } int JSEmitter::emitDtor(Node *n) { String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); SwigType *type = state.clazz(TYPE); String *p_classtype = SwigType_add_pointer(state.clazz(TYPE)); String *ctype = SwigType_lstr(p_classtype, ""); String *jsfree = NewString(""); // (Taken from JSCore implementation.) /* The if (Extend) block was taken from the Ruby implementation. * The problem is that in the case of an %extend to create a destructor for a struct to coordinate automatic memory cleanup with the Javascript collector, * the SWIG function was not being generated. More specifically: struct MyData { %extend { ~MyData() { FreeData($self); } } }; %newobject CreateData; struct MyData* CreateData(void); %delobject FreeData; void FreeData(struct MyData* the_data); where the use case is something like: var my_data = example.CreateData(); my_data = null; This function was not being generated: SWIGINTERN void delete_MyData(struct MyData *self){ FreeData(self); } I don't understand fully why it wasn't being generated. It just seems to happen in the Lua generator. There is a comment about staticmemberfunctionHandler having an inconsistency and I tracked down dome of the SWIGINTERN void delete_* code to that function in the Language base class. The Ruby implementation seems to have an explicit check for if(Extend) and explicitly generates the code, so that's what I'm doing here. The Ruby implementation does other stuff which I omit. */ if (Extend) { String *wrap = Getattr(n, "wrap:code"); if (wrap) { Printv(f_wrappers, wrap, NIL); } } // HACK: this is only for the v8 emitter. maybe set an attribute wrap:action of node // TODO: generate dtors more similar to other wrappers // EW: I think this is wrong. delete should only be used when new was used to create. If malloc was used, free needs to be used. if (SwigType_isarray(type)) { Printf(jsfree, "delete [] (%s)", ctype); } else { Printf(jsfree, "delete (%s)", ctype); } String *destructor_action = Getattr(n, "wrap:action"); // Adapted from the JSCore implementation. /* The next challenge is to generate the correct finalize function for JavaScriptCore to call. Originally, it would use this fragment from javascriptcode.swg %fragment ("JS_destructordefn", "templates") %{ void _wrap_${classname_mangled}_finalize(JSObjectRef thisObject) { SWIG_PRV_DATA* t = (SWIG_PRV_DATA*)JSObjectGetPrivate(thisObject); if(t && t->swigCMemOwn) free ((${type}*)t->swigCObject); free(t); } %} But for the above example case of %extend to define a destructor on a struct, we need to override the system to not call free ((${type}*)t->swigCObject); and substitute it with what the user has provided. To solve this, I created a variation fragment called JS_destructoroverridedefn: SWIG_PRV_DATA* t = (SWIG_PRV_DATA*)JSObjectGetPrivate(thisObject); if(t && t->swigCMemOwn) { ${type}* arg1 = (${type}*)t->swigCObject; ${destructor_action} } free(t); Based on what I saw in the Lua and Ruby modules, I use Getattr(n, "wrap:action") to decide if the user has a preferred destructor action. Based on that, I decide which fragment to use. And in the case of the custom action, I substitute that action in. I noticed that destructor_action has the form delete_MyData(arg1); The explicit arg1 is a little funny, so I structured the fragment to create a temporary variable called arg1 to make the generation easier. This might suggest this solution misunderstands a more complex case. Also, there is a problem where destructor_action is always true for me, even when not requesting %extend as above. So this code doesn't actually quite work as I expect. The end result is that the code still works because destructor_action calls free like the original template. The one caveat is the string in destructor_action casts to char* which is weird. I think there is a deeper underlying SWIG issue because I don't think it should be char*. However, it doesn't really matter for free. Maybe the fix for the destructor_action always true problem is that this is supposed to be embedded in the if(Extend) block above. But I don't fully understand the conditions of any of these things, and since it works for the moment, I don't want to break more stuff. */ if (destructor_action) { Template t_dtor = getTemplate("js_dtoroverride"); state.clazz(DTOR, wrap_name); t_dtor.replace("${classname_mangled}", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wrap_name) .replace("$jsfree", jsfree) .replace("$jstype", ctype); t_dtor.replace("${destructor_action}", destructor_action); Wrapper_pretty_print(t_dtor.str(), f_wrappers); } else { Template t_dtor = getTemplate("js_dtor"); state.clazz(DTOR, wrap_name); t_dtor.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wrap_name) .replace("$jsfree", jsfree) .replace("$jstype", ctype) .pretty_print(f_wrappers); } Delete(p_classtype); Delete(ctype); Delete(jsfree); return SWIG_OK; } int JSEmitter::emitGetter(Node *n, bool is_member, bool is_static) { Wrapper *wrapper = NewWrapper(); Template t_getter(getTemplate(getGetterTemplate(is_member))); // prepare wrapper name String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); Setattr(n, "wrap:name", wrap_name); state.variable(GETTER, wrap_name); // prepare local variables ParmList *params = Getattr(n, "parms"); emit_parameter_variables(params, wrapper); emit_attach_parmmaps(params, wrapper); // prepare code part String *action = emit_action(n); marshalInputArgs(n, params, wrapper, Getter, is_member, is_static); marshalOutput(n, params, wrapper, action); emitCleanupCode(n, wrapper, params); t_getter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wrap_name) .replace("$jslocals", wrapper->locals) .replace("$jscode", wrapper->code) .pretty_print(f_wrappers); DelWrapper(wrapper); return SWIG_OK; } int JSEmitter::emitSetter(Node *n, bool is_member, bool is_static) { // skip variables that are immutable if (State::IsSet(state.variable(IS_IMMUTABLE))) { return SWIG_OK; } Wrapper *wrapper = NewWrapper(); Template t_setter(getTemplate(getSetterTemplate(is_member))); // prepare wrapper name String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); Setattr(n, "wrap:name", wrap_name); state.variable(SETTER, wrap_name); // prepare local variables ParmList *params = Getattr(n, "parms"); emit_parameter_variables(params, wrapper); emit_attach_parmmaps(params, wrapper); // prepare code part String *action = emit_action(n); marshalInputArgs(n, params, wrapper, Setter, is_member, is_static); Append(wrapper->code, action); emitCleanupCode(n, wrapper, params); t_setter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wrap_name) .replace("$jslocals", wrapper->locals) .replace("$jscode", wrapper->code) .pretty_print(f_wrappers); DelWrapper(wrapper); return SWIG_OK; } /* ----------------------------------------------------------------------------- * JSEmitter::emitConstant() : triggers code generation for constants * ----------------------------------------------------------------------------- */ int JSEmitter::emitConstant(Node *n) { // HACK: somehow it happened under Mac OS X that before everything started // a lot of SWIG internal constants were emitted // This didn't happen on other platforms yet... // we ignore those premature definitions if (!State::IsSet(state.globals(HAS_TEMPLATES))) { return SWIG_ERROR; } Wrapper *wrapper = NewWrapper(); SwigType *type = Getattr(n, "type"); String *iname = Getattr(n, "sym:name"); String *wname = Swig_name_get(Getattr(current_namespace, NAME_MANGLED), iname); String *value = Getattr(n, "value"); // HACK: forcing usage of cppvalue for v8 (which turned out to fix typedef_struct.i, et. al) if (State::IsSet(state.globals(FORCE_CPP)) && Getattr(n, "cppvalue") != NULL) { value = Getattr(n, "cppvalue"); } bool is_member = GetFlag(n, "ismember"); Template t_getter(getTemplate(getGetterTemplate(is_member))); // call the variable methods as a constants are // registered in same way enterVariable(n); state.variable(GETTER, wname); // TODO: why do we need this? Setattr(n, "wrap:name", wname); // special treatment of member pointers if (SwigType_type(type) == T_MPOINTER) { // TODO: this could go into a code-template String *mpointer_wname = NewString(""); Printf(mpointer_wname, "_wrapConstant_%s", iname); Setattr(n, "memberpointer:constant:wrap:name", mpointer_wname); String *str = SwigType_str(type, mpointer_wname); Printf(f_wrappers, "static %s = %s;\n", str, value); Delete(str); value = mpointer_wname; } marshalOutput(n, 0, wrapper, NewString(""), value, false); t_getter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wname) .replace("$jslocals", wrapper->locals) .replace("$jscode", wrapper->code) .pretty_print(f_wrappers); exitVariable(n); // This is the counterpart to the "constant" test in // exitVariable, it prevents double setting of // symbols that are both constants and variables SetFlag(n, "constant"); DelWrapper(wrapper); return SWIG_OK; } int JSEmitter::emitFunction(Node *n, bool is_member, bool is_static) { Wrapper *wrapper = NewWrapper(); Template t_function(getTemplate(getFunctionTemplate(is_member))); bool is_overloaded = GetFlag(n, "sym:overloaded") != 0; // prepare the function wrapper name String *iname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); String *wrap_name = Swig_name_wrapper(iname); if (is_overloaded) { t_function = getTemplate(getOverloadedFunctionTemplate(is_member)); Append(wrap_name, Getattr(n, "sym:overname")); } Setattr(n, "wrap:name", wrap_name); state.function(WRAPPER_NAME, wrap_name); // prepare local variables ParmList *params = Getattr(n, "parms"); emit_parameter_variables(params, wrapper); emit_attach_parmmaps(params, wrapper); marshalInputArgs(n, params, wrapper, Function, is_member, is_static); String *action = emit_action(n); marshalOutput(n, params, wrapper, action); emitCleanupCode(n, wrapper, params); bool isvoid = !Cmp(returntype, "void"); Replaceall(wrapper->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(wrapper->code, "$symname", iname); t_function.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", wrap_name) .replace("$jslocals", wrapper->locals) .replace("$jscode", wrapper->code) .replace("$jsargcount", Getattr(n, ARGCOUNT)) .replace("$jsargrequired", Getattr(n, ARGREQUIRED)) .pretty_print(f_wrappers); DelWrapper(wrapper); return SWIG_OK; } int JSEmitter::emitFunctionDispatcher(Node *n, bool is_member) { Wrapper *wrapper = NewWrapper(); // Generate call list, go to first node Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up do { String *siblname = Getattr(sibl, "wrap:name"); if (siblname) { // handle function overloading Template t_dispatch_case = getTemplate("js_function_dispatch_case"); t_dispatch_case.replace("$jswrapper", siblname) .replace("$jsargcount", Getattr(sibl, ARGCOUNT)) .replace("$jsargrequired", Getattr(sibl, ARGREQUIRED)); Append(wrapper->code, t_dispatch_case.str()); } } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Template t_function(getTemplate(getFunctionDispatcherTemplate(is_member))); // Note: this dispatcher function gets called after the last overloaded function has been created. // At this time, n.wrap:name contains the name of the last wrapper function. // To get a valid function name for the dispatcher function we take the last wrapper name and // subtract the extension "sym:overname", String *wrap_name = NewString(Getattr(n, "wrap:name")); String *overname = Getattr(n, "sym:overname"); Node *methodclass = Swig_methodclass(n); String *class_name = Getattr(methodclass, "sym:name"); int l1 = Len(wrap_name); int l2 = Len(overname); Delslice(wrap_name, l1 - l2, l1); String *new_string = NewStringf("%s_%s", class_name, wrap_name); String *final_wrap_name = Swig_name_wrapper(new_string); Setattr(n, "wrap:name", final_wrap_name); state.function(WRAPPER_NAME, final_wrap_name); t_function.replace("$jslocals", wrapper->locals) .replace("$jscode", wrapper->code); // call this here, to replace all variables t_function.replace("$jswrapper", final_wrap_name) .replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .pretty_print(f_wrappers); // Delete the state variable DelWrapper(wrapper); return SWIG_OK; } String *JSEmitter::emitInputTypemap(Node *n, Parm *p, Wrapper *wrapper, String *arg) { String *code = NewString(""); // Get input typemap for current param String *tm = Getattr(p, "tmap:in"); String *tm_def = Getattr(p, "tmap:default"); SwigType *type = Getattr(p, "type"); int argmin = -1; int argidx = -1; bool is_optional = false; if (Getattr(n, ARGREQUIRED)) { argmin = GetInt(n, ARGREQUIRED); } if (Getattr(p, INDEX)) { argidx = GetInt(p, INDEX); } if (tm_def != NULL) { is_optional = true; } if (argmin >= 0 && argidx >= 0 && argidx >= argmin) { is_optional = true; } if (is_optional && Getattr(p, INDEX) == NULL) { Printf(stderr, "Argument %s in %s cannot be a default argument\n", Getattr(p, NAME), state.function(NAME)); return SWIG_ERROR; } if (is_optional) { Template t_check_default(getTemplate("js_check_arg")); t_check_default.replace("$jsarg", Getattr(p, INDEX)).pretty_print(code); Printf(code, "{\n"); } if (tm != NULL) { Replaceall(tm, "$input", arg); Setattr(p, "emit:input", arg); // do replacements for built-in variables if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Replaceall(tm, "$symname", Getattr(n, "sym:name")); if (!checkAttribute(p, "tmap:in:noblock", "1")) { Printf(code, "{\n"); } Printf(code, "%s", tm); if (!checkAttribute(p, "tmap:in:noblock", "1")) { Printf(code, "\n}\n"); } } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(type, 0)); return NULL; } if (is_optional) { Printf(code, "}\n"); } // numinputs=0 typemaps are emitted by the legacy code in // emit_attach_parmmaps() in emit.cxx, check the comment there // All generators work around this if (!checkAttribute(p, "tmap:in:numinputs", "0")) { Append(wrapper->code, code); } return code; } String *JSEmitter::emitCheckTypemap(Node *, Parm *p, Wrapper *wrapper, String *arg) { String *tm = Getattr(p, "tmap:check"); if (tm != NULL) { Replaceall(tm, "$input", arg); Printf(wrapper->code, "%s\n", tm); } return tm; } void JSEmitter::marshalOutput(Node *n, ParmList *params, Wrapper *wrapper, String *actioncode, const String *cresult, bool emitReturnVariable) { SwigType *type = Getattr(n, "type"); String *tm; Parm *p; // adds a declaration for the result variable if (emitReturnVariable) emit_return_variable(n, type, wrapper); // if not given, use default result identifier ('result') for output typemap if (cresult == 0) cresult = defaultResultName; tm = Swig_typemap_lookup_out("out", n, cresult, wrapper, actioncode); bool should_own = GetFlag(n, "feature:new") != 0; if (tm) { Replaceall(tm, "$objecttype", Swig_scopename_last(SwigType_str(SwigType_strip_qualifiers(type), 0))); if (should_own) { Replaceall(tm, "$owner", "SWIG_POINTER_OWN"); } else { Replaceall(tm, "$owner", "0"); } Append(wrapper->code, tm); if (Len(tm) > 0) { Printf(wrapper->code, "\n"); } } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(type, 0), Getattr(n, "name")); } if (params) { for (p = params; p;) { if ((tm = Getattr(p, "tmap:argout"))) { Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(wrapper->code, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } } Replaceall(wrapper->code, "$result", "jsresult"); } void JSEmitter::emitCleanupCode(Node *n, Wrapper *wrapper, ParmList *params) { Parm *p; String *tm; for (p = params; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { //addThrows(n, "tmap:freearg", p); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(wrapper->code, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } if (GetFlag(n, "feature:new")) { tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0); if (tm != NIL) { //addThrows(throws_hash, "newfree", n); Printv(wrapper->code, tm, "\n", NIL); } } /* See if there is any return cleanup code */ if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(wrapper->code, "%s\n", tm); Delete(tm); } } int JSEmitter::switchNamespace(Node *n) { // HACK: somehow this gets called for member functions. // We can safely ignore them, as members are not associated to a namespace (only their class) if (GetFlag(n, "ismember")) { return SWIG_OK; } // if nspace is deactivated, everything goes into the global scope if (!GetFlag(n, "feature:nspace")) { current_namespace = Getattr(namespaces, "::"); return SWIG_OK; } // EXPERIMENTAL: we want to use Language::getNSpace() here // However, it is not working yet. // For namespace functions Language::getNSpace() does not give a valid result #if 0 JAVASCRIPT *lang = static_cast(Language::instance()); String *_nspace = lang->getNSpace(); if (!Equal(nspace, _nspace)) { Printf(stdout, "##### Custom vs Language::getNSpace(): %s | %s\n", nspace, _nspace); } #endif String *nspace = Getattr(n, "sym:nspace"); if (nspace == NULL) { // It seems that only classes have 'sym:nspace' set. // We try to get the namespace from the qualified name (i.e., everything before the last '::') nspace = Swig_scopename_prefix(Getattr(n, "name")); } // If there is not even a scopename prefix then it must be global scope if (nspace == NULL) { current_namespace = Getattr(namespaces, "::"); return SWIG_OK; } String *scope = NewString(nspace); // replace "." with "::" that we can use Swig_scopename_last Replaceall(scope, ".", "::"); // if the scope is not yet registered // create (parent) namespaces recursively if (!Getattr(namespaces, scope)) { createNamespace(scope); } current_namespace = Getattr(namespaces, scope); return SWIG_OK; } int JSEmitter::createNamespace(String *scope) { String *parent_scope = Swig_scopename_prefix(scope); Hash *parent_namespace; if (parent_scope == 0) { parent_namespace = Getattr(namespaces, "::"); } else if (!Getattr(namespaces, parent_scope)) { createNamespace(parent_scope); parent_namespace = Getattr(namespaces, parent_scope); } else { parent_namespace = Getattr(namespaces, parent_scope); } assert(parent_namespace != 0); Hash *new_namespace = createNamespaceEntry(Char(scope), Char(Getattr(parent_namespace, "name")), Char(Getattr(parent_namespace, "name_mangled"))); Setattr(namespaces, scope, new_namespace); Delete(parent_scope); return SWIG_OK; } Hash *JSEmitter::createNamespaceEntry(const char *_name, const char *parent, const char *parent_mangled) { Hash *entry = NewHash(); String *name = NewString(_name); Setattr(entry, NAME, Swig_scopename_last(name)); Setattr(entry, NAME_MANGLED, Swig_name_mangle_string(name)); Setattr(entry, PARENT, NewString(parent)); Setattr(entry, PARENT_MANGLED, NewString(parent_mangled)); Delete(name); return entry; } /********************************************************************** * JavascriptCore: JSEmitter implementation for JavascriptCore engine **********************************************************************/ class JSCEmitter:public JSEmitter { public: JSCEmitter(); virtual ~ JSCEmitter(); virtual int initialize(Node *n); virtual int dump(Node *n); virtual int close(); protected: virtual int enterVariable(Node *n); virtual int exitVariable(Node *n); virtual int enterFunction(Node *n); virtual int exitFunction(Node *n); virtual int enterClass(Node *n); virtual int exitClass(Node *n); virtual void marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static); virtual Hash *createNamespaceEntry(const char *name, const char *parent, const char *parent_mangled); virtual int emitNamespaces(); private: String *NULL_STR; String *VETO_SET; // output file and major code parts File *f_wrap_cpp; File *f_runtime; File *f_header; File *f_init; }; JSCEmitter::JSCEmitter() : JSEmitter(JSEmitter::JavascriptCore), NULL_STR(NewString("NULL")), VETO_SET(NewString("JS_veto_set_variable")), f_wrap_cpp(NULL), f_runtime(NULL), f_header(NULL), f_init(NULL) { } JSCEmitter::~JSCEmitter() { Delete(NULL_STR); Delete(VETO_SET); } /* --------------------------------------------------------------------- * marshalInputArgs() * * Process all of the arguments passed into the argv array * and convert them into C/C++ function arguments using the * supplied typemaps. * --------------------------------------------------------------------- */ void JSCEmitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static) { Parm *p; String *tm = NULL; // determine an offset index, as members have an extra 'this' argument // except: static members and ctors. int startIdx = 0; if (is_member && !is_static && mode != Ctor) { startIdx = 1; } // store number of arguments for argument checks int num_args = emit_num_arguments(parms) - startIdx; String *argcount = NewString(""); Printf(argcount, "%d", num_args); Setattr(n, ARGCOUNT, argcount); int num_required = emit_num_required(parms) - startIdx; SetInt(n, ARGREQUIRED, num_required); // process arguments int i = 0; for (p = parms; p;) { String *arg = NewString(""); String *type = Getattr(p, "type"); // ignore varargs if (SwigType_isvarargs(type)) break; switch (mode) { case Getter: case Function: if (is_member && !is_static && i == 0) { Printv(arg, "thisObject", 0); i++; } else { Printf(arg, "argv[%d]", i - startIdx); SetInt(p, INDEX, i - startIdx); i += GetInt(p, "tmap:in:numinputs"); } break; case Setter: if (is_member && !is_static && i == 0) { Printv(arg, "thisObject", 0); i++; } else { Printv(arg, "value", 0); i++; } break; case Ctor: Printf(arg, "argv[%d]", i); SetInt(p, INDEX, i); i += GetInt(p, "tmap:in:numinputs"); break; default: Printf(stderr, "Illegal MarshallingMode."); Exit(EXIT_FAILURE); } tm = emitInputTypemap(n, p, wrapper, arg); Delete(arg); if (tm) { p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } for (p = parms; p;) { tm = emitCheckTypemap(n, p, wrapper, Getattr(p, "emit:input")); if (tm) { p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } } int JSCEmitter::initialize(Node *n) { JSEmitter::initialize(n); /* Get the output file name */ String *outfile = Getattr(n, "outfile"); /* Initialize I/O */ f_wrap_cpp = NewFile(outfile, "w", SWIG_output_files()); if (!f_wrap_cpp) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } /* Initialization of members */ f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); state.globals(CREATE_NAMESPACES, NewString("")); state.globals(REGISTER_NAMESPACES, NewString("")); state.globals(INITIALIZER, NewString("")); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("begin", f_wrap_cpp); Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_banner(f_wrap_cpp); Swig_obligatory_macros(f_runtime, "JAVASCRIPT"); return SWIG_OK; } int JSCEmitter::dump(Node *n) { /* Get the module name */ String *module = Getattr(n, "name"); Template initializer_define(getTemplate("js_initializer_define")); initializer_define.replace("$jsname", module).pretty_print(f_header); SwigType_emit_type_table(f_runtime, f_wrappers); Printv(f_wrap_cpp, f_runtime, "\n", 0); Printv(f_wrap_cpp, f_header, "\n", 0); Printv(f_wrap_cpp, f_wrappers, "\n", 0); emitNamespaces(); // compose the initializer function using a template Template initializer(getTemplate("js_initializer")); initializer.replace("$jsname", module) .replace("$jsregisterclasses", state.globals(INITIALIZER)) .replace("$jscreatenamespaces", state.globals(CREATE_NAMESPACES)) .replace("$jsregisternamespaces", state.globals(REGISTER_NAMESPACES)) .pretty_print(f_init); Printv(f_wrap_cpp, f_init, 0); return SWIG_OK; } int JSCEmitter::close() { Delete(f_runtime); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(namespaces); Delete(f_wrap_cpp); return SWIG_OK; } int JSCEmitter::enterFunction(Node *n) { JSEmitter::enterFunction(n); return SWIG_OK; } int JSCEmitter::exitFunction(Node *n) { Template t_function = getTemplate("jsc_function_declaration"); bool is_member = GetFlag(n, "ismember") != 0 || GetFlag(n, "feature:extend") != 0; bool is_overloaded = GetFlag(n, "sym:overloaded") != 0; // handle overloaded functions if (is_overloaded) { if (!Getattr(n, "sym:nextSibling")) { //state.function(WRAPPER_NAME, Swig_name_wrapper(Getattr(n, "name"))); // create dispatcher emitFunctionDispatcher(n, is_member); } else { //don't register wrappers of overloaded functions in function tables return SWIG_OK; } } t_function.replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)); if (is_member) { if (GetFlag(state.function(), IS_STATIC)) { t_function.pretty_print(state.clazz(STATIC_FUNCTIONS)); } else { t_function.pretty_print(state.clazz(MEMBER_FUNCTIONS)); } } else { t_function.pretty_print(Getattr(current_namespace, "functions")); } return SWIG_OK; } int JSCEmitter::enterVariable(Node *n) { JSEmitter::enterVariable(n); state.variable(GETTER, NULL_STR); state.variable(SETTER, VETO_SET); return SWIG_OK; } int JSCEmitter::exitVariable(Node *n) { Template t_variable(getTemplate("jsc_variable_declaration")); t_variable.replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER)); if (GetFlag(n, "ismember")) { if (GetFlag(state.variable(), IS_STATIC) || Equal(Getattr(n, "nodeType"), "enumitem")) { t_variable.pretty_print(state.clazz(STATIC_VARIABLES)); } else { t_variable.pretty_print(state.clazz(MEMBER_VARIABLES)); } } else { t_variable.pretty_print(Getattr(current_namespace, "values")); } return SWIG_OK; } int JSCEmitter::enterClass(Node *n) { JSEmitter::enterClass(n); state.clazz(MEMBER_VARIABLES, NewString("")); state.clazz(MEMBER_FUNCTIONS, NewString("")); state.clazz(STATIC_VARIABLES, NewString("")); state.clazz(STATIC_FUNCTIONS, NewString("")); Template t_class_decl = getTemplate("jsc_class_declaration"); t_class_decl.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .pretty_print(f_wrappers); return SWIG_OK; } int JSCEmitter::exitClass(Node *n) { Template t_class_tables(getTemplate("jsc_class_tables")); t_class_tables.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsclassvariables", state.clazz(MEMBER_VARIABLES)) .replace("$jsclassfunctions", state.clazz(MEMBER_FUNCTIONS)) .replace("$jsstaticclassfunctions", state.clazz(STATIC_FUNCTIONS)) .replace("$jsstaticclassvariables", state.clazz(STATIC_VARIABLES)) .pretty_print(f_wrappers); /* adds the ctor wrappers at this position */ // Note: this is necessary to avoid extra forward declarations. //Append(f_wrappers, state.clazz(CTOR_WRAPPERS)); // for abstract classes add a vetoing ctor if (GetFlag(state.clazz(), IS_ABSTRACT)) { Template t_veto_ctor(getTemplate("js_veto_ctor")); t_veto_ctor.replace("$jswrapper", state.clazz(CTOR)) .replace("$jsname", state.clazz(NAME)) .pretty_print(f_wrappers); } /* adds a class template statement to initializer function */ Template t_classtemplate(getTemplate("jsc_class_definition")); /* prepare registration of base class */ String *jsclass_inheritance = NewString(""); Node *base_class = getBaseClass(n); if (base_class != NULL) { Template t_inherit(getTemplate("jsc_class_inherit")); t_inherit.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsbaseclassmangled", SwigType_manglestr(Getattr(base_class, "name"))) .pretty_print(jsclass_inheritance); } else { Template t_inherit(getTemplate("jsc_class_noinherit")); t_inherit.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .pretty_print(jsclass_inheritance); } t_classtemplate.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsclass_inheritance", jsclass_inheritance) .replace("$jsctor", state.clazz(CTOR)) .replace("$jsdtor", state.clazz(DTOR)) .pretty_print(state.globals(INITIALIZER)); Delete(jsclass_inheritance); /* Note: this makes sure that there is a swig_type added for this class */ SwigType_remember_clientdata(state.clazz(TYPE_MANGLED), NewString("0")); /* adds a class registration statement to initializer function */ Template t_registerclass(getTemplate("jsc_class_registration")); t_registerclass.replace("$jsname", state.clazz(NAME)) .replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsnspace", Getattr(state.clazz("nspace"), NAME_MANGLED)) .pretty_print(state.globals(INITIALIZER)); return SWIG_OK; } Hash *JSCEmitter::createNamespaceEntry(const char *name, const char *parent, const char *parent_mangled) { Hash *entry = JSEmitter::createNamespaceEntry(name, parent, parent_mangled); Setattr(entry, "functions", NewString("")); Setattr(entry, "values", NewString("")); return entry; } int JSCEmitter::emitNamespaces() { Iterator it; for (it = First(namespaces); it.item; it = Next(it)) { Hash *entry = it.item; String *name = Getattr(entry, NAME); String *name_mangled = Getattr(entry, NAME_MANGLED); String *parent_mangled = Getattr(entry, PARENT_MANGLED); String *functions = Getattr(entry, "functions"); String *variables = Getattr(entry, "values"); // skip the global namespace which is given by the application Template namespace_definition(getTemplate("jsc_nspace_declaration")); namespace_definition.replace("$jsglobalvariables", variables) .replace("$jsglobalfunctions", functions) .replace("$jsnspace", name_mangled) .replace("$jsmangledname", name_mangled) .pretty_print(f_wrap_cpp); Template t_createNamespace(getTemplate("jsc_nspace_definition")); t_createNamespace.replace("$jsmangledname", name_mangled); Append(state.globals(CREATE_NAMESPACES), t_createNamespace.str()); // Don't register 'exports' as namespace. It is return to the application. if (!Equal("exports", name)) { Template t_registerNamespace(getTemplate("jsc_nspace_registration")); t_registerNamespace.replace("$jsmangledname", name_mangled) .replace("$jsname", name) .replace("$jsparent", parent_mangled); Append(state.globals(REGISTER_NAMESPACES), t_registerNamespace.str()); } } return SWIG_OK; } JSEmitter *swig_javascript_create_JSCEmitter() { return new JSCEmitter(); } /********************************************************************** * V8: JSEmitter implementation for V8 engine **********************************************************************/ class V8Emitter:public JSEmitter { public: V8Emitter(); virtual ~ V8Emitter(); virtual int initialize(Node *n); virtual int dump(Node *n); virtual int close(); virtual int enterClass(Node *n); virtual int exitClass(Node *n); virtual int enterVariable(Node *n); virtual int exitVariable(Node *n); virtual int exitFunction(Node *n); protected: virtual void marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static); virtual int emitNamespaces(); protected: /* built-in parts */ String *f_runtime; String *f_header; String *f_init; String *f_post_init; /* part for class templates */ String *f_class_templates; /* parts for initilizer */ String *f_init_namespaces; String *f_init_class_templates; String *f_init_wrappers; String *f_init_inheritance; String *f_init_class_instances; String *f_init_static_wrappers; String *f_init_register_classes; String *f_init_register_namespaces; // the output cpp file File *f_wrap_cpp; String *NULL_STR; String *VETO_SET; String *moduleName; }; V8Emitter::V8Emitter() : JSEmitter(JSEmitter::V8), NULL_STR(NewString("0")), VETO_SET(NewString("JS_veto_set_variable")) { } V8Emitter::~V8Emitter() { Delete(NULL_STR); Delete(VETO_SET); } int V8Emitter::initialize(Node *n) { JSEmitter::initialize(n); moduleName = Getattr(n, "name"); // Get the output file name String *outfile = Getattr(n, "outfile"); f_wrap_cpp = NewFile(outfile, "w", SWIG_output_files()); if (!f_wrap_cpp) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_header = NewString(""); f_class_templates = NewString(""); f_init = NewString(""); f_post_init = NewString(""); f_init_namespaces = NewString(""); f_init_class_templates = NewString(""); f_init_wrappers = NewString(""); f_init_inheritance = NewString(""); f_init_class_instances = NewString(""); f_init_static_wrappers = NewString(""); f_init_register_classes = NewString(""); f_init_register_namespaces = NewString(""); // note: this is necessary for built-in generation of SWIG runtime code Swig_register_filebyname("begin", f_wrap_cpp); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("init", f_init); Swig_register_filebyname("post-init", f_post_init); state.globals(FORCE_CPP, NewString("1")); Swig_banner(f_wrap_cpp); Swig_obligatory_macros(f_runtime, "JAVASCRIPT"); return SWIG_OK; } int V8Emitter::dump(Node *n) { /* Get the module name */ String *module = Getattr(n, "name"); Template initializer_define(getTemplate("js_initializer_define")); initializer_define.replace("$jsname", module).pretty_print(f_header); SwigType_emit_type_table(f_runtime, f_wrappers); Printv(f_wrap_cpp, f_runtime, "\n", 0); Printv(f_wrap_cpp, f_header, "\n", 0); Printv(f_wrap_cpp, f_class_templates, "\n", 0); Printv(f_wrap_cpp, f_wrappers, "\n", 0); emitNamespaces(); // compose the initializer function using a template // filled with sub-parts Template initializer(getTemplate("js_initializer")); initializer.replace("$jsname", moduleName) .replace("$jsv8nspaces", f_init_namespaces) .replace("$jsv8classtemplates", f_init_class_templates) .replace("$jsv8wrappers", f_init_wrappers) .replace("$jsv8inheritance", f_init_inheritance) .replace("$jsv8classinstances", f_init_class_instances) .replace("$jsv8staticwrappers", f_init_static_wrappers) .replace("$jsv8registerclasses", f_init_register_classes) .replace("$jsv8registernspaces", f_init_register_namespaces); Printv(f_init, initializer.str(), 0); Printv(f_wrap_cpp, f_init, 0); Printv(f_wrap_cpp, f_post_init, 0); return SWIG_OK; } int V8Emitter::close() { Delete(f_runtime); Delete(f_header); Delete(f_class_templates); Delete(f_init_namespaces); Delete(f_init_class_templates); Delete(f_init_wrappers); Delete(f_init_inheritance); Delete(f_init_class_instances); Delete(f_init_static_wrappers); Delete(f_init_register_classes); Delete(f_init_register_namespaces); Delete(f_init); Delete(f_post_init); Delete(f_wrap_cpp); return SWIG_OK; } int V8Emitter::enterClass(Node *n) { JSEmitter::enterClass(n); // emit declaration of a v8 class template Template t_decl_class(getTemplate("jsv8_declare_class_template")); t_decl_class.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .trim() .pretty_print(f_class_templates); return SWIG_OK; } int V8Emitter::exitClass(Node *n) { if (GetFlag(state.clazz(), IS_ABSTRACT)) { Template t_veto_ctor(getTemplate("js_veto_ctor")); t_veto_ctor.replace("$jswrapper", state.clazz(CTOR)) .replace("$jsname", state.clazz(NAME)) .pretty_print(f_wrappers); } /* Note: this makes sure that there is a swig_type added for this class */ String *clientData = NewString(""); Printf(clientData, "&%s_clientData", state.clazz(NAME_MANGLED)); /* Note: this makes sure that there is a swig_type added for this class */ SwigType_remember_clientdata(state.clazz(TYPE_MANGLED), NewString("0")); // emit definition of v8 class template Template t_def_class = getTemplate("jsv8_define_class_template"); t_def_class.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsdtor", state.clazz(DTOR)) .trim() .pretty_print(f_init_class_templates); Template t_class_instance = getTemplate("jsv8_create_class_instance"); t_class_instance.replace("$jsname", state.clazz(NAME)) .replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsctor", state.clazz(CTOR)) .trim() .pretty_print(f_init_class_instances); // emit inheritance setup Node *baseClass = getBaseClass(n); if (baseClass) { String *base_name = Getattr(baseClass, "name"); Template t_inherit = getTemplate("jsv8_inherit"); String *base_name_mangled = SwigType_manglestr(base_name); t_inherit.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsbaseclass", base_name_mangled) .trim() .pretty_print(f_init_inheritance); Delete(base_name_mangled); } // emit registration of class template Template t_register = getTemplate("jsv8_register_class"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsparent", Getattr(state.clazz("nspace"), NAME_MANGLED)) .trim() .pretty_print(f_init_register_classes); return SWIG_OK; } int V8Emitter::enterVariable(Node *n) { JSEmitter::enterVariable(n); state.variable(GETTER, NULL_STR); state.variable(SETTER, VETO_SET); return SWIG_OK; } int V8Emitter::exitVariable(Node *n) { if (GetFlag(n, "ismember")) { if (GetFlag(state.variable(), IS_STATIC) || Equal(Getattr(n, "nodeType"), "enumitem")) { Template t_register = getTemplate("jsv8_register_static_variable"); t_register.replace("$jsparent", state.clazz(NAME_MANGLED)) .replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER)) .trim() .pretty_print(f_init_static_wrappers); } else { Template t_register = getTemplate("jsv8_register_member_variable"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER)) .trim() .pretty_print(f_init_wrappers); } } else { // Note: a global variable is treated like a static variable // with the parent being a nspace object (instead of class object) Template t_register = getTemplate("jsv8_register_static_variable"); t_register.replace("$jsparent", Getattr(current_namespace, NAME_MANGLED)) .replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER)) .trim() .pretty_print(f_init_wrappers); } return SWIG_OK; } int V8Emitter::exitFunction(Node *n) { bool is_member = GetFlag(n, "ismember") != 0 || GetFlag(n, "feature:extend") != 0; // create a dispatcher for overloaded functions bool is_overloaded = GetFlag(n, "sym:overloaded") != 0; if (is_overloaded) { if (!Getattr(n, "sym:nextSibling")) { //state.function(WRAPPER_NAME, Swig_name_wrapper(Getattr(n, "name"))); emitFunctionDispatcher(n, is_member); } else { //don't register wrappers of overloaded functions in function tables return SWIG_OK; } } // register the function at the specific context if (is_member) { if (GetFlag(state.function(), IS_STATIC)) { Template t_register = getTemplate("jsv8_register_static_function"); t_register.replace("$jsparent", state.clazz(NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .trim() .pretty_print(f_init_static_wrappers); } else { Template t_register = getTemplate("jsv8_register_member_function"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .trim() .pretty_print(f_init_wrappers); } } else { // Note: a global function is treated like a static function // with the parent being a nspace object instead of class object Template t_register = getTemplate("jsv8_register_static_function"); t_register.replace("$jsparent", Getattr(current_namespace, NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .trim() .pretty_print(f_init_static_wrappers); } return SWIG_OK; } void V8Emitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static) { Parm *p; String *tm = NULL; int startIdx = 0; if (is_member && !is_static && mode != Ctor) { startIdx = 1; } // store number of arguments for argument checks int num_args = emit_num_arguments(parms) - startIdx; String *argcount = NewString(""); Printf(argcount, "%d", num_args); Setattr(n, ARGCOUNT, argcount); int num_required = emit_num_required(parms) - startIdx; SetInt(n, ARGREQUIRED, num_required); int i = 0; for (p = parms; p;) { String *arg = NewString(""); String *type = Getattr(p, "type"); // ignore varargs if (SwigType_isvarargs(type)) break; switch (mode) { case Getter: if (is_member && !is_static && i == 0) { Printv(arg, "info.Holder()", 0); i++; } else { Printf(arg, "args[%d]", i - startIdx); SetInt(p, INDEX, i - startIdx); i += GetInt(p, "tmap:in:numinputs"); } break; case Function: if (is_member && !is_static && i == 0) { Printv(arg, "args.Holder()", 0); i++; } else { Printf(arg, "args[%d]", i - startIdx); SetInt(p, INDEX, i - startIdx); i += GetInt(p, "tmap:in:numinputs"); } break; case Setter: if (is_member && !is_static && i == 0) { Printv(arg, "info.Holder()", 0); i++; } else { Printv(arg, "value", 0); i++; } break; case Ctor: Printf(arg, "args[%d]", i); SetInt(p, INDEX, i); i += GetInt(p, "tmap:in:numinputs"); break; default: Printf(stderr, "Illegal MarshallingMode."); Exit(EXIT_FAILURE); } tm = emitInputTypemap(n, p, wrapper, arg); Delete(arg); if (tm) { p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } for (p = parms; p;) { tm = emitCheckTypemap(n, p, wrapper, Getattr(p, "emit:input")); if (tm) { p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } } int V8Emitter::emitNamespaces() { Iterator it; for (it = First(namespaces); it.item; it = Next(it)) { Hash *entry = it.item; String *name = Getattr(entry, NAME); String *name_mangled = Getattr(entry, NAME_MANGLED); String *parent = Getattr(entry, PARENT); String *parent_mangled = Getattr(entry, PARENT_MANGLED); bool do_create = true; bool do_register = true; if (Equal(parent, "")) { do_register = false; } // Note: 'exports' is by convention the name of the object where // globals are stored into if (Equal(name, "exports")) { do_create = false; } if (do_create) { // create namespace object and register it to the parent scope Template t_create_ns = getTemplate("jsv8_create_namespace"); t_create_ns.replace("$jsmangledname", name_mangled) .trim() .pretty_print(f_init_namespaces); } if (do_register) { Template t_register_ns = getTemplate("jsv8_register_namespace"); t_register_ns.replace("$jsmangledname", name_mangled) .replace("$jsname", name) .replace("$jsparent", parent_mangled) .trim(); // prepend in order to achieve reversed order of registration statements String *tmp_register_stmt = NewString(""); t_register_ns.pretty_print(tmp_register_stmt); Insert(f_init_register_namespaces, 0, tmp_register_stmt); Delete(tmp_register_stmt); } } return SWIG_OK; } /********************************************************************** * NAPI: JSEmitter implementation for N-API **********************************************************************/ class NAPIEmitter:public JSEmitter { public: NAPIEmitter(); virtual ~NAPIEmitter(); virtual int initialize(Node *n); virtual int dump(Node *n); virtual int close(); virtual int enterClass(Node *n); virtual int exitClass(Node *n); virtual int enterVariable(Node *n); virtual int exitVariable(Node *n); virtual int exitFunction(Node *n); protected: virtual void marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static); virtual int emitNamespaces(); virtual int emitCtor(Node *); virtual int emitDtor(Node *); virtual int emitClassMethodDeclaration(Node *); virtual const char *getFunctionTemplate(bool is_member); virtual const char *getFunctionDispatcherTemplate(bool is_member); virtual const char *getOverloadedFunctionTemplate(bool is_member); virtual const char *getSetterTemplate(bool is_member); virtual const char *getGetterTemplate(bool is_member); protected: /* built-in parts */ String *f_runtime; String *f_header; String *f_init; String *f_post_init; /* class declarations */ String *f_class_declarations; /* parts for initilizer */ String *f_init_namespaces; String *f_init_wrappers; String *f_init_inheritance; String *f_init_static_wrappers; String *f_init_register_classes; String *f_init_register_namespaces; // the output cpp file File *f_wrap_cpp; String *NULL_STR; String *VETO_SET; String *moduleName; // the current index in the class table size_t class_idx; }; NAPIEmitter::NAPIEmitter() : JSEmitter(JSEmitter::NAPI), NULL_STR(NewString("0")), VETO_SET(NewString("JS_veto_set_variable")), class_idx(0) { } NAPIEmitter::~NAPIEmitter() { Delete(NULL_STR); Delete(VETO_SET); } int NAPIEmitter::initialize(Node *n) { JSEmitter::initialize(n); moduleName = Getattr(n, "name"); // Get the output file name String *outfile = Getattr(n, "outfile"); f_wrap_cpp = NewFile(outfile, "w", SWIG_output_files()); if (!f_wrap_cpp) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_header = NewString(""); f_init = NewString(""); f_post_init = NewString(""); f_class_declarations = NewString(""); f_init_namespaces = NewString(""); f_init_wrappers = NewString(""); f_init_inheritance = NewString(""); f_init_static_wrappers = NewString(""); f_init_register_classes = NewString(""); f_init_register_namespaces = NewString(""); // note: this is necessary for built-in generation of SWIG runtime code Swig_register_filebyname("begin", f_wrap_cpp); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("init", f_init); Swig_register_filebyname("post-init", f_post_init); state.globals(FORCE_CPP, NewString("1")); Swig_banner(f_wrap_cpp); Swig_obligatory_macros(f_runtime, "JAVASCRIPT"); return SWIG_OK; } int NAPIEmitter::dump(Node *n) { /* Get the module name */ String *module = Getattr(n, "name"); Template initializer_define(getTemplate("js_initializer_define")); initializer_define.replace("$jsname", module).pretty_print(f_header); SwigType_emit_type_table(f_runtime, f_wrappers); Printv(f_wrap_cpp, f_runtime, "\n", 0); Printv(f_wrap_cpp, f_header, "\n", 0); Printv(f_wrap_cpp, f_class_declarations, "\n", 0); Printv(f_wrap_cpp, f_wrappers, "\n", 0); emitNamespaces(); String *inheritance = NewStringEmpty(); if (Len(f_init_inheritance) > 0) { Template t_inheritance(getTemplate("js_init_inheritance")); t_inheritance.pretty_print(inheritance); } // compose the initializer function using a template // filled with sub-parts Template initializer(getTemplate("js_initializer")); initializer.replace("$jsname", moduleName) .replace("$jsnapinspaces", f_init_namespaces) .replace("$jsnapipreinheritance", inheritance) .replace("$jsnapiinitinheritance", f_init_inheritance) .replace("$jsnapiregisterclasses", f_init_register_classes) .replace("$jsnapiregisternspaces", f_init_register_namespaces); Printv(f_init, initializer.str(), 0); Printv(f_wrap_cpp, f_init, 0); Printv(f_wrap_cpp, f_post_init, 0); Delete(inheritance); return SWIG_OK; } int NAPIEmitter::close() { Delete(f_runtime); Delete(f_header); Delete(f_class_declarations); Delete(f_init_namespaces); Delete(f_init_wrappers); Delete(f_init_inheritance); Delete(f_init_static_wrappers); Delete(f_init_register_classes); Delete(f_init_register_namespaces); Delete(f_init); Delete(f_post_init); Delete(f_wrap_cpp); return SWIG_OK; } const char *NAPIEmitter::getFunctionTemplate(bool is_member) { return is_member ? "js_function" : "js_global_function"; } const char *NAPIEmitter::getFunctionDispatcherTemplate(bool is_member) { return is_member ? "js_function_dispatcher" : "js_global_function_dispatcher"; } const char *NAPIEmitter::getOverloadedFunctionTemplate(bool is_member) { return is_member ? "js_overloaded_function" : "js_global_overloaded_function"; } const char *NAPIEmitter::getGetterTemplate(bool is_member) { return is_member ? "js_getter" : "js_global_getter"; } const char *NAPIEmitter::getSetterTemplate(bool is_member) { return is_member ? "js_setter" : "js_global_setter"; } int NAPIEmitter::enterClass(Node *n) { JSEmitter::enterClass(n); // emit registration of class template String *idx = NewString(""); Printf(idx, "%d", class_idx++); Template t_register = getTemplate("jsnapi_registerclass"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsparent", Getattr(state.clazz("nspace"), NAME_MANGLED)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsclassidx", idx) .trim() .pretty_print(f_init_register_classes); Delete(idx); // emit inheritance String *baseMangled; Node *baseClass = getBaseClass(n); SetFlag(n, IS_WRAPPED); if (baseClass && GetFlag(baseClass, IS_WRAPPED)) { String *jsName = NewString(""); String *nspace = Getattr(baseClass, "sym:nspace"); if (Len(nspace) == 0) nspace = Getattr(current_namespace, NAME_MANGLED); Printf(jsName, "%s_%s", nspace, Getattr(baseClass, "sym:name")); baseMangled = SwigType_manglestr(jsName); Delete(jsName); f_init_wrappers = Copy(Getattr(baseClass, MEMBER_FUNCTIONS)); f_init_static_wrappers = Copy(Getattr(baseClass, STATIC_FUNCTIONS)); } else { baseMangled = NewString("SWIG_NAPI_ObjectWrap"); f_init_wrappers = NewString(""); f_init_static_wrappers = NewString(""); } state.clazz(PARENT_MANGLED, baseMangled); Template t_setup_inheritance(getTemplate("jsnapi_setup_inheritance")); t_setup_inheritance.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", state.clazz(CTOR)) .replace("$jsname", state.clazz(NAME)) .replace("$jsparent", baseMangled) .pretty_print(f_init_inheritance); // emit declaration of a NAPI class template Template t_decl_class(getTemplate("jsnapi_class_prologue_template")); t_decl_class.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsparent", baseMangled) .trim() .pretty_print(f_class_declarations); Delete(baseMangled); return SWIG_OK; } int NAPIEmitter::exitClass(Node *n) { if (GetFlag(state.clazz(), IS_ABSTRACT)) { Template t_veto_ctor(getTemplate("js_veto_ctor")); t_veto_ctor.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", state.clazz(CTOR)) .replace("$jsname", state.clazz(NAME)) .replace("$jsparent", state.clazz(PARENT_MANGLED)) .pretty_print(f_wrappers); } /* Note: this makes sure that there is a swig_type added for this class */ String *clientData = NewString(""); Printf(clientData, "&%s_clientData", state.clazz(NAME_MANGLED)); /* Note: this makes sure that there is a swig_type added for this class */ SwigType_remember_clientdata(state.clazz(TYPE_MANGLED), NewString("0")); // emit definition of NAPI class template Template t_def_class = getTemplate("jsnapi_class_epilogue_template"); t_def_class.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsdtor", state.clazz(DTOR)) .trim() .pretty_print(f_class_declarations); Template t_class_instance = getTemplate("jsnapi_declare_class_instance"); t_class_instance.replace("$jsname", state.clazz(NAME)) .replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .trim() .pretty_print(f_class_declarations); Template t_class_template = getTemplate("jsnapi_getclass"); t_class_template.replace("$jsname", state.clazz(NAME)) .replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsnapiwrappers", f_init_wrappers) .replace("$jsnapistaticwrappers", f_init_static_wrappers) .replace("$jsparent", state.clazz(PARENT_MANGLED)) .trim() .pretty_print(f_class_declarations); /* Save these to be reused in the child classes */ Setattr(n, MEMBER_FUNCTIONS, f_init_wrappers); Setattr(n, STATIC_FUNCTIONS, f_init_static_wrappers); return SWIG_OK; } int NAPIEmitter::enterVariable(Node *n) { // Somehow, this is not always reset // (some constructs like smart pointers reuse Nodes) UnsetFlag(n, "constant"); JSEmitter::enterVariable(n); state.variable(GETTER, VETO_SET); state.variable(SETTER, VETO_SET); return SWIG_OK; } int NAPIEmitter::exitVariable(Node *n) { // Due to special handling of C++ "static const" member variables // (refer to the comment in lang.cxx:Language::staticmembervariableHandler) // a static const member variable may get transformed into a constant // and be emitted by emitConstant which will result calling exitVariable twice if (GetFlag(n, "constant")) { return SWIG_OK; } if (GetFlag(n, "ismember")) { String *modifier = NewStringEmpty(); if (GetFlag(state.variable(), IS_STATIC) || Equal(Getattr(n, "nodeType"), "enumitem")) { Template t_register = getTemplate("jsnapi_register_static_variable"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER) != VETO_SET ? state.variable(SETTER) : "JS_veto_set_static_variable") .trim() .pretty_print(f_init_static_wrappers); Append(modifier, "static"); } else { Template t_register = getTemplate("jsnapi_register_member_variable"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER)) .trim() .pretty_print(f_init_wrappers); } // emit declaration of a class member function Template t_getter = getTemplate("jsnapi_class_method_declaration"); t_getter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsdtor", state.clazz(DTOR)) .replace("$jswrapper", state.variable(GETTER)) .replace("$jsstatic", modifier) .trim() .pretty_print(f_class_declarations); if (state.variable(SETTER) != VETO_SET) { Template t_setter = getTemplate("jsnapi_class_setter_declaration"); t_setter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsdtor", state.clazz(DTOR)) .replace("$jswrapper", state.variable(SETTER)) .replace("$jsstatic", modifier) .trim() .pretty_print(f_class_declarations); } Delete(modifier); } else { Template t_register = getTemplate("jsnapi_register_global_variable"); t_register.replace("$jsparent", Getattr(current_namespace, NAME_MANGLED)) .replace("$jsname", state.variable(NAME)) .replace("$jsgetter", state.variable(GETTER)) .replace("$jssetter", state.variable(SETTER)) .trim() .pretty_print(f_init_register_namespaces); } return SWIG_OK; } int NAPIEmitter::emitClassMethodDeclaration(Node *) { // emit declaration of a class member function Template t_def_class = getTemplate("jsnapi_class_method_declaration"); t_def_class.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.clazz(NAME)) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsdtor", state.clazz(DTOR)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .replace("$jsstatic", GetFlag(state.function(), IS_STATIC) ? "static" : "") .trim() .pretty_print(f_class_declarations); return SWIG_OK; } int NAPIEmitter::exitFunction(Node *n) { bool is_member = GetFlag(n, "ismember") != 0 || GetFlag(n, "feature:extend") != 0; // create a dispatcher for overloaded functions bool is_overloaded = GetFlag(n, "sym:overloaded") != 0; if (is_overloaded) { emitClassMethodDeclaration(n); if (!Getattr(n, "sym:nextSibling")) { emitFunctionDispatcher(n, is_member); } else { return SWIG_OK; } } // register the function at the specific context if (is_member) { if (GetFlag(state.function(), IS_STATIC)) { Template t_register = getTemplate("jsnapi_register_static_function"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .trim() .pretty_print(f_init_static_wrappers); } else { Template t_register = getTemplate("jsnapi_register_member_function"); t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .trim() .pretty_print(f_init_wrappers); } emitClassMethodDeclaration(n); } else { // Note: a global function is treated like a static function // with the parent being a nspace object instead of class object Template t_register = getTemplate("jsnapi_register_global_function"); t_register.replace("$jsparent", Getattr(current_namespace, NAME_MANGLED)) .replace("$jsname", state.function(NAME)) .replace("$jswrapper", state.function(WRAPPER_NAME)) .trim() .pretty_print(f_init_register_namespaces); } return SWIG_OK; } void NAPIEmitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static) { Parm *p; String *tm; int startIdx = 0; if (is_member && !is_static && mode != Ctor) { startIdx = 1; } // store number of arguments for argument checks int num_args = emit_num_arguments(parms) - startIdx; String *argcount = NewString(""); Printf(argcount, "%d", num_args); Setattr(n, ARGCOUNT, argcount); int num_required = emit_num_required(parms) - startIdx; SetInt(n, ARGREQUIRED, num_required); int i = 0; for (p = parms; p;) { String *arg = NewString(""); String *type = Getattr(p, "type"); // ignore varargs if (SwigType_isvarargs(type)) break; switch (mode) { case Getter: if (is_member && !is_static && i == 0) { Printv(arg, "info.This()", 0); i++; } else { Printf(arg, "info[%d]", i - startIdx); SetInt(p, INDEX, i - startIdx); i += GetInt(p, "tmap:in:numinputs"); } break; case Function: if (is_member && !is_static && i == 0) { Printv(arg, "info.This()", 0); i++; } else { Printf(arg, "info[%d]", i - startIdx); SetInt(p, INDEX, i - startIdx); i += GetInt(p, "tmap:in:numinputs"); } break; case Setter: if (is_member && !is_static && i == 0) { Printv(arg, "info.This()", 0); i++; } else { Printv(arg, "value", 0); i++; } break; case Ctor: Printf(arg, "info[%d]", i); SetInt(p, INDEX, i - startIdx); i += GetInt(p, "tmap:in:numinputs"); break; default: Printf(stderr, "Illegal MarshallingMode."); Exit(EXIT_FAILURE); } tm = emitInputTypemap(n, p, wrapper, arg); Delete(arg); if (tm) { p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } for (p = parms; p;) { tm = emitCheckTypemap(n, p, wrapper, Getattr(p, "emit:input")); if (tm) { p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } } int NAPIEmitter::emitNamespaces() { Iterator it; for (it = First(namespaces); it.item; it = Next(it)) { Hash *entry = it.item; String *name = Getattr(entry, NAME); String *name_mangled = Getattr(entry, NAME_MANGLED); String *parent = Getattr(entry, PARENT); String *parent_mangled = Getattr(entry, PARENT_MANGLED); bool do_create = true; bool do_register = true; if (Equal(parent, "")) { do_register = false; } // Note: 'exports' is by convention the name of the object where // globals are stored into if (Equal(name, "exports")) { do_create = false; } if (do_create) { // create namespace object and register it to the parent scope Template t_create_ns = getTemplate("jsnapi_create_namespace"); t_create_ns.replace("$jsmangledname", name_mangled) .trim() .pretty_print(f_init_namespaces); } if (do_register) { Template t_register_ns = getTemplate("jsnapi_register_namespace"); t_register_ns.replace("$jsmangledname", name_mangled) .replace("$jsname", name) .replace("$jsparent", parent_mangled) .trim(); // prepend in order to achieve reversed order of registration statements String *tmp_register_stmt = NewString(""); t_register_ns.pretty_print(tmp_register_stmt); Insert(f_init_register_namespaces, 0, tmp_register_stmt); Delete(tmp_register_stmt); } } return SWIG_OK; } int NAPIEmitter::emitCtor(Node *n) { int r = JSEmitter::emitCtor(n); if (r != SWIG_OK) return r; Template t_getter = getTemplate("jsnapi_class_method_declaration"); t_getter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jswrapper", Getattr(n, "wrap:name")) .replace("$jsmangledtype", state.clazz(TYPE_MANGLED)) .replace("$jsstatic", "") .trim() .pretty_print(f_class_declarations); return SWIG_OK; } int NAPIEmitter::emitDtor(Node *n) { // NAPI destructors must have a class declaration Template t_getter = getTemplate("jsnapi_class_dtor_declaration"); t_getter.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .trim() .pretty_print(f_class_declarations); return JSEmitter::emitDtor(n); } JSEmitter *swig_javascript_create_V8Emitter() { return new V8Emitter(); } JSEmitter *swig_javascript_create_NAPIEmitter() { return new NAPIEmitter(); } /********************************************************************** * Helper implementations **********************************************************************/ JSEmitterState::JSEmitterState() : globalHash(NewHash()) { // initialize sub-hashes Setattr(globalHash, "class", NewHash()); Setattr(globalHash, "function", NewHash()); Setattr(globalHash, "variable", NewHash()); } JSEmitterState::~JSEmitterState() { Delete(globalHash); } DOH *JSEmitterState::getState(const char *key, bool new_key) { if (new_key) { Hash *hash = NewHash(); Setattr(globalHash, key, hash); } return Getattr(globalHash, key); } DOH *JSEmitterState::globals() { return globalHash; } DOH *JSEmitterState::globals(const char *key, DOH *initial) { if (initial != 0) { Setattr(globalHash, key, initial); } return Getattr(globalHash, key); } DOH *JSEmitterState::clazz(bool new_key) { return getState("class", new_key); } DOH *JSEmitterState::clazz(const char *key, DOH *initial) { DOH *c = clazz(); if (initial != 0) { Setattr(c, key, initial); } return Getattr(c, key); } DOH *JSEmitterState::function(bool new_key) { return getState("function", new_key); } DOH *JSEmitterState::function(const char *key, DOH *initial) { DOH *f = function(); if (initial != 0) { Setattr(f, key, initial); } return Getattr(f, key); } DOH *JSEmitterState::variable(bool new_key) { return getState("variable", new_key); } DOH *JSEmitterState::variable(const char *key, DOH *initial) { DOH *v = variable(); if (initial != 0) { Setattr(v, key, initial); } return Getattr(v, key); } /*static*/ int JSEmitterState::IsSet(DOH *val) { if (!val) { return 0; } else { const char *cval = Char(val); if (!cval) return 0; return (strcmp(cval, "0") != 0) ? 1 : 0; } } /* ----------------------------------------------------------------------------- * Template::Template() : creates a Template class for given template code * ----------------------------------------------------------------------------- */ Template::Template(const String *code_) { if (!code_) { Printf(stdout, "Template code was null. Illegal input for template."); Exit(EXIT_FAILURE); } code = NewString(code_); templateName = NewString(""); } Template::Template(const String *code_, const String *templateName_) { if (!code_) { Printf(stdout, "Template code was null. Illegal input for template."); Exit(EXIT_FAILURE); } code = NewString(code_); templateName = NewString(templateName_); } /* ----------------------------------------------------------------------------- * Template::~Template() : cleans up of Template. * ----------------------------------------------------------------------------- */ Template::~Template() { Delete(code); Delete(templateName); } /* ----------------------------------------------------------------------------- * String* Template::str() : retrieves the current content of the template. * ----------------------------------------------------------------------------- */ String *Template::str() { if (js_template_enable_debug) { String *pre_code = NewString(""); String *post_code = NewString(""); String *debug_code = NewString(""); Printf(pre_code, "/* begin fragment(\"%s\") */", templateName); Printf(post_code, "/* end fragment(\"%s\") */", templateName); Printf(debug_code, "%s\n%s\n%s\n", pre_code, code, post_code); Delete(code); Delete(pre_code); Delete(post_code); code = debug_code; } return code; } Template & Template::trim() { const char *str = Char(code); if (str == 0) return *this; int length = Len(code); if (length == 0) return *this; int idx; for (idx = 0; idx < length; ++idx) { if (str[idx] != ' ' && str[idx] != '\t' && str[idx] != '\r' && str[idx] != '\n') break; } int start_pos = idx; for (idx = length - 1; idx >= start_pos; --idx) { if (str[idx] != ' ' && str[idx] != '\t' && str[idx] != '\r' && str[idx] != '\n') break; } int end_pos = idx; int new_length = end_pos - start_pos + 1; char *newstr = new char[new_length + 1]; memcpy(newstr, str + start_pos, new_length); newstr[new_length] = 0; Delete(code); code = NewString(newstr); delete[]newstr; return *this; } /* ----------------------------------------------------------------------------- * Template& Template::replace(const String* pattern, const String* repl) : * * replaces all occurrences of a given pattern with a given replacement. * * - pattern: the pattern to be replaced * - repl: the replacement string * - returns a reference to the Template to allow chaining of methods. * ----------------------------------------------------------------------------- */ Template & Template::replace(const String *pattern, const String *repl) { Replaceall(code, pattern, repl); return *this; } Template & Template::print(DOH *doh) { Printv(doh, str(), 0); return *this; } Template & Template::pretty_print(DOH *doh) { Wrapper_pretty_print(str(), doh); return *this; } Template::Template(const Template & t) { code = NewString(t.code); templateName = NewString(t.templateName); } void Template::operator=(const Template & t) { Delete(code); Delete(templateName); code = NewString(t.code); templateName = NewString(t.templateName); } swig-4.4.0/Source/Modules/directors.cxx0000664000175000017500000002150515075443613017755 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * directors.cxx * * Director support functions. * Not all of these may be necessary, and some may duplicate existing functionality * in SWIG. --MR * ----------------------------------------------------------------------------- */ #include "swigmod.h" /* ----------------------------------------------------------------------------- * Swig_csuperclass_call() * * Generates a fully qualified method call, including the full parameter list. * e.g. "base::method(i, j)" * ----------------------------------------------------------------------------- */ String *Swig_csuperclass_call(String *base, String *method, ParmList *l) { String *call = NewString(""); int arg_idx = 0; Parm *p; if (base) { Printf(call, "%s::", base); } Printf(call, "%s(", method); for (p = l; p; p = nextSibling(p)) { String *pname = Getattr(p, "name"); if (!pname && Cmp(Getattr(p, "type"), "void")) { pname = NewString(""); Printf(pname, "arg%d", arg_idx++); } if (p != l) Printf(call, ", "); Printv(call, pname, NIL); } Printf(call, ")"); return call; } /* ----------------------------------------------------------------------------- * Swig_class_declaration() * * Generate the start of a class/struct declaration. * e.g. "class myclass" * ----------------------------------------------------------------------------- */ String *Swig_class_declaration(Node *n, String *name) { if (!name) { name = Getattr(n, "sym:name"); } String *result = NewString(""); String *kind = Getattr(n, "kind"); Printf(result, "%s %s", kind, name); return result; } /* ----------------------------------------------------------------------------- * Swig_class_name() * ----------------------------------------------------------------------------- */ String *Swig_class_name(Node *n) { String *name; name = Copy(Getattr(n, "sym:name")); return name; } /* ----------------------------------------------------------------------------- * Swig_director_declaration() * * Generate the full director class declaration, complete with base classes. * e.g. "class SwigDirector_myclass : public myclass, public Swig::Director {" * ----------------------------------------------------------------------------- */ String *Swig_director_declaration(Node *n) { String *classname = Swig_class_name(n); String *directorname = Language::instance()->directorClassName(n); String *base = Getattr(n, "classtype"); String *declaration = Swig_class_declaration(n, directorname); Printf(declaration, " : public %s, public Swig::Director {\n", base); Delete(classname); Delete(directorname); return declaration; } /* ----------------------------------------------------------------------------- * Swig_method_call() * ----------------------------------------------------------------------------- */ String *Swig_method_call(const_String_or_char_ptr name, ParmList *parms) { String *func; int comma = 0; Parm *p = parms; SwigType *pt; String *nname; func = NewString(""); nname = SwigType_namestr(name); Printf(func, "%s(", nname); while (p) { String *pname; pt = Getattr(p, "type"); if ((SwigType_type(pt) != T_VOID)) { if (comma) Printf(func, ","); pname = Getattr(p, "name"); Printf(func, "%s", pname); comma = 1; } p = nextSibling(p); } Printf(func, ")"); return func; } /* ----------------------------------------------------------------------------- * Swig_method_decl() * * Return a stringified version of a C/C++ declaration. * ----------------------------------------------------------------------------- */ String *Swig_method_decl(SwigType *return_base_type, SwigType *decl, const_String_or_char_ptr id, List *args, int default_args) { String *result = NewString(""); bool conversion_operator = Strstr(id, "operator ") != 0 && !return_base_type; Parm *parm = args; int arg_idx = 0; while (parm) { String *type = Getattr(parm, "type"); String *name = Getattr(parm, "name"); if (!name && Cmp(type, "void")) { name = NewString(""); Printf(name, "arg%d", arg_idx++); Setattr(parm, "name", name); } parm = nextSibling(parm); } String *rettype = Copy(decl); String *quals = SwigType_pop_function_qualifiers(rettype); String *qualifiers = 0; if (quals) qualifiers = SwigType_str(quals, 0); String *popped_decl = SwigType_pop_function(rettype); if (return_base_type) Append(rettype, return_base_type); if (!conversion_operator) { SwigType *rettype_stripped = SwigType_strip_qualifiers(rettype); String *rtype = SwigType_str(rettype, 0); Append(result, rtype); if ((SwigType_issimple(rettype_stripped) && return_base_type) || SwigType_isqualifier(rettype)) Append(result, " "); Delete(rtype); Delete(rettype_stripped); } if (id) Append(result, id); String *args_string = default_args ? ParmList_str_defaultargs(args) : ParmList_str(args); Printv(result, "(", args_string, ")", NIL); if (qualifiers) Printv(result, " ", qualifiers, NIL); Delete(args_string); Delete(popped_decl); Delete(qualifiers); Delete(quals); Delete(rettype); return result; } /* ----------------------------------------------------------------------------- * Swig_director_emit_dynamic_cast() * * In order to call protected virtual director methods from the target language, we need * to add an extra dynamic_cast to call the public C++ wrapper in the director class. * Also for non-static protected members when the allprotected option is on. * ----------------------------------------------------------------------------- */ void Swig_director_emit_dynamic_cast(Node *n, Wrapper *f) { // TODO: why is the storage element removed in staticmemberfunctionHandler ?? if ((!is_public(n) && (is_member_director(n) || GetFlag(n, "explicitcall"))) || (is_non_virtual_protected_access(n) && !(Swig_storage_isstatic_custom(n, "staticmemberfunctionHandler:storage") || Swig_storage_isstatic(n)) && !Equal(nodeType(n), "constructor"))) { Node *parent = Getattr(n, "parentNode"); String *dirname; String *dirdecl; dirname = Language::instance()->directorClassName(parent); dirdecl = NewStringf("%s *darg = 0", dirname); Wrapper_add_local(f, "darg", dirdecl); Printf(f->code, "darg = dynamic_cast<%s *>(arg1);\n", dirname); Delete(dirname); Delete(dirdecl); } } /* ----------------------------------------------------------------------------- * Swig_director_parms_fixup() * * For each parameter in the C++ member function, copy the parameter name * to its "lname"; this ensures that Swig_typemap_attach_parms() will do * the right thing when it sees strings like "$1" in "directorin" typemaps. * ----------------------------------------------------------------------------- */ void Swig_director_parms_fixup(ParmList *parms) { Parm *p; int i; for (i = 0, p = parms; p; p = nextSibling(p), ++i) { String *arg = Getattr(p, "name"); String *lname = 0; if (!arg && !Equal(Getattr(p, "type"), "void")) { lname = NewStringf("arg%d", i); Setattr(p, "name", lname); } else lname = Copy(arg); Setattr(p, "lname", lname); Delete(lname); } } /* ----------------------------------------------------------------------------- * Swig_director_can_unwrap() * * Determine whether a function's return type can be returned as an existing * target language object instead of creating a new target language object. * Must be a director class and only for return by pointer or reference only * (not by value or by pointer to pointer etc). * ----------------------------------------------------------------------------- */ bool Swig_director_can_unwrap(Node *n) { // FIXME: this will not try to unwrap directors returned as non-director // base class pointers! bool unwrap = false; String *type = Getattr(n, "type"); SwigType *t = SwigType_typedef_resolve_all(type); SwigType *t1 = SwigType_strip_qualifiers(t); SwigType *prefix = SwigType_prefix(t1); if (Strcmp(prefix, "p.") == 0 || Strcmp(prefix, "r.") == 0) { Node *parent = Swig_methodclass(n); Node *module = Getattr(parent, "module"); Node *target = Swig_directormap(module, t1); if (target) unwrap = true; } Delete(prefix); Delete(t1); Delete(t); return unwrap; } swig-4.4.0/Source/Modules/xml.cxx0000664000175000017500000001751015075443613016560 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * xml.cxx * * An Xml parse tree generator. * ----------------------------------------------------------------------------- */ #include "swigmod.h" static const char *usage = "\ XML Options (available with -xml)\n\ -xmllite - More lightweight version of XML\n"; static File *out = 0; static int xmllite = 0; class XML:public Language { public: int indent_level; long id; XML() :indent_level(0) , id(0) { } ~XML() { } virtual void main(int argc, char *argv[]) { for (int iX = 0; iX < argc; iX++) { if (strcmp(argv[iX], "-help") == 0) { fputs(usage, stdout); } if (strcmp(argv[iX], "-xmllite") == 0) { Swig_mark_arg(iX); xmllite = 1; } } // Add a symbol to the parser for conditional compilation Preprocessor_define("SWIGXML 1", 0); } /* Top of the parse tree */ virtual int top(Node *n) { if (out == 0) { String *outfile = Getattr(n, "outfile"); String *ext = Swig_file_extension(outfile); // If there's an extension, ext will include the ".". Delslice(outfile, Len(outfile) - Len(ext), DOH_END); Delete(ext); Append(outfile, ".xml"); out = NewFile(outfile, "w", SWIG_output_files()); if (!out) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } } Printf(out, " \n"); Xml_print_tree(n); return SWIG_OK; } void print_indent(int l) { int i; for (i = 0; i < indent_level; i++) { Printf(out, " "); } if (l) { Printf(out, " "); } } void Xml_print_tree(DOH *obj) { while (obj) { Xml_print_node(obj); obj = nextSibling(obj); } } void Xml_print_attributes(Node *obj) { String *k; indent_level += 4; print_indent(0); Printf(out, "\n", ++id, obj); indent_level += 4; Iterator ki; ki = First(obj); while (ki.key) { k = ki.key; if ((Cmp(k, "nodeType") == 0) || (Cmp(k, "firstChild") == 0) || (Cmp(k, "lastChild") == 0) || (Cmp(k, "parentNode") == 0) || (Cmp(k, "nextSibling") == 0) || (Cmp(k, "previousSibling") == 0) || (*(Char(k)) == '$')) { /* Do nothing */ } else if (Cmp(k, "module") == 0) { Xml_print_module(Getattr(obj, k)); } else if (Cmp(k, "baselist") == 0) { Xml_print_baselist(Getattr(obj, k)); } else if (!xmllite && Cmp(k, "typescope") == 0) { Xml_print_typescope(Getattr(obj, k)); } else if (!xmllite && Cmp(k, "typetab") == 0) { Xml_print_typetab(Getattr(obj, k)); } else if (Cmp(k, "kwargs") == 0) { Xml_print_kwargs(Getattr(obj, k)); } else if (Cmp(k, "parms") == 0 || Cmp(k, "pattern") == 0) { Xml_print_parmlist(Getattr(obj, k)); } else if (Cmp(k, "catchlist") == 0 || Cmp(k, "templateparms") == 0) { Xml_print_parmlist(Getattr(obj, k), Char(k)); } else { DOH *o; print_indent(0); if (DohIsString(Getattr(obj, k))) { String *ck = NewString(k); o = Str(Getattr(obj, k)); Replaceall(ck, ":", "_"); Replaceall(ck, "<", "<"); /* Do first to avoid aliasing errors. */ Replaceall(o, "&", "&"); Replaceall(o, "<", "<"); Replaceall(o, "\"", """); Replaceall(o, "\\", "\\\\"); Replaceall(o, "\n", " "); Printf(out, "\n", ck, o, ++id, o); Delete(o); Delete(ck); } else { o = Getattr(obj, k); String *ck = NewString(k); Replaceall(ck, ":", "_"); Printf(out, "\n", ck, o, ++id, o); Delete(ck); } } ki = Next(ki); } indent_level -= 4; print_indent(0); Printf(out, "\n"); indent_level -= 4; } void Xml_print_node(Node *obj) { Node *cobj; print_indent(0); Printf(out, "<%s id=\"%ld\" addr=\"%p\">\n", nodeType(obj), ++id, obj); Xml_print_attributes(obj); cobj = firstChild(obj); if (cobj) { indent_level += 4; Printf(out, "\n"); Xml_print_tree(cobj); indent_level -= 4; } else { print_indent(1); Printf(out, "\n"); } print_indent(0); Printf(out, "\n", nodeType(obj)); } void Xml_print_parmlist(ParmList *p, const char* markup = "parmlist") { print_indent(0); Printf(out, "<%s id=\"%ld\" addr=\"%p\">\n", markup, ++id, p); indent_level += 4; while (p) { print_indent(0); Printf(out, "\n", ++id); Xml_print_attributes(p); print_indent(0); Printf(out, "\n"); p = nextSibling(p); } indent_level -= 4; print_indent(0); Printf(out, "\n", markup); } void Xml_print_baselist(List *p) { print_indent(0); Printf(out, "\n", ++id, p); indent_level += 4; Iterator s; for (s = First(p); s.item; s = Next(s)) { print_indent(0); String *item_name = Xml_escape_string(s.item); Printf(out, "\n", item_name, ++id, s.item); Delete(item_name); } indent_level -= 4; print_indent(0); Printf(out, "\n"); } String *Xml_escape_string(String *str) { String *escaped_str = 0; if (str) { escaped_str = NewString(str); Replaceall(escaped_str, "&", "&"); Replaceall(escaped_str, "<", "<"); Replaceall(escaped_str, "\"", """); Replaceall(escaped_str, "\\", "\\\\"); Replaceall(escaped_str, "\n", " "); } return escaped_str; } void Xml_print_module(Node *p) { print_indent(0); Printf(out, "\n", Getattr(p, "name"), ++id, p); } void Xml_print_kwargs(Hash *p) { Xml_print_hash(p, "kwargs"); } void Xml_print_typescope(Hash *p) { Xml_print_hash(p, "typescope"); } void Xml_print_typetab(Hash *p) { Xml_print_hash(p, "typetab"); } void Xml_print_hash(Hash *p, const char *markup) { print_indent(0); Printf(out, "<%s id=\"%ld\" addr=\"%p\">\n", markup, ++id, p); Xml_print_attributes(p); indent_level += 4; Iterator n = First(p); while (n.key) { print_indent(0); Printf(out, "<%ssitem id=\"%ld\" addr=\"%p\">\n", markup, ++id, n.item); Xml_print_attributes(n.item); print_indent(0); Printf(out, "\n", markup); n = Next(n); } indent_level -= 4; print_indent(0); Printf(out, "\n", markup); } NestedClassSupport nestedClassesSupport() const { return NCS_Full; } }; /* ----------------------------------------------------------------------------- * Swig_print_xml * * Dump an XML version of the parse tree. This is different from using the -xml * language module normally as it allows the real language module to process the * tree first, possibly stuffing in new attributes, so the XML that is output ends * up being a post-processing version of the tree. * ----------------------------------------------------------------------------- */ void Swig_print_xml(Node *obj, String *filename) { XML xml; xmllite = 1; out = NewFile(filename, "w", SWIG_output_files()); if (!out) { FileErrorDisplay(filename); Exit(EXIT_FAILURE); } Printf(out, " \n"); xml.Xml_print_tree(obj); } static Language *new_swig_xml() { return new XML(); } extern "C" Language *swig_xml(void) { return new_swig_xml(); } swig-4.4.0/Source/Modules/d.cxx0000664000175000017500000051513415075443613016210 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * d.cxx * * D language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include // Hash type used for storing information about director callbacks for a class. typedef DOH UpcallData; class D : public Language { static const char *usage; const String *empty_string; const String *public_string; const String *private_string; const String *protected_string; /* * Files and file sections containing C/C++ code. */ File *f_begin; File *f_runtime; File *f_runtime_h; File *f_header; File *f_wrappers; File *f_init; File *f_directors; File *f_directors_h; List *filenames_list; /* * Command line-set modes of operation. */ // Whether a single proxy D module is generated or classes and enums are // written to their own files. bool split_proxy_dmodule; /* * State variables which indicate what is being wrapped at the moment. * This is probably not the most elegant way of handling state, but it has * proven to work in the C# and Java modules. */ // Indicates if wrapping a native function. bool native_function_flag; // Indicates if wrapping a static functions or member variables bool static_flag; // Indicates if wrapping a nonstatic member variable bool variable_wrapper_flag; // Indicates if wrapping a member variable/enum/const. bool wrapping_member_flag; // Indicates if wrapping a global variable. bool global_variable_flag; // Name of a variable being wrapped. String *variable_name; /* * Variables temporarily holding the generated C++ code. */ // C++ code for the generated wrapper functions for casts up the C++ // for inheritance hierarchies. String *upcasts_code; // Function pointer typedefs for handling director callbacks on the C++ side. String *director_callback_typedefs; // Variables for storing the function pointers to the director callbacks on // the C++ side. String *director_callback_pointers; /* * Names of generated D entities. */ // The name of the D module containing the interface to the C wrapper. String *im_dmodule_name; // The fully qualified name of the wrap D module (package name included). String *im_dmodule_fq_name; // The name of the proxy module which exposes the (SWIG) module contents as a // D module. String *proxy_dmodule_name; // The fully qualified name of the proxy D module. String *proxy_dmodule_fq_name; // Optional: Package the D modules are placed in (set via the -package // command line option). String *package; // The directory the generated D module files are written to. Is constructed // from the package path if a target package is set, points to the general // output directory otherwise. String *dmodule_directory; // The name of the library which contains the C wrapper (used when generating // the dynamic library loader). Can be overridden via the -wrapperlibrary // command line flag. String *wrap_library_name; /* * Variables temporarily holding the generated D code. */ // Import statements written to the intermediary D module header set via // %pragma(d) imdmoduleimports. String *im_dmodule_imports; // The code for the intermediary D module body. String *im_dmodule_code; // Import statements for all proxy modules (the main proxy module and, if in // split proxy module mode, the proxy class modules) from // %pragma(d) globalproxyimports. String *global_proxy_imports; // The D code for the main proxy modules. nspace_proxy_dmodules is a hash from // the namespace name as key to an {"imports", "code"}. If the nspace feature // is not active, only proxy_dmodule_imports and proxy_dmodule_code are used, // which contain the code for the root proxy module. // // These variables should not be accessed directly but rather via the // proxy{Imports, Code}Buffer)() helper functions which return the right // buffer for a given namespace. If not in split proxy mode, they contain the // whole proxy code. String *proxy_dmodule_imports; String *proxy_dmodule_code; Hash *nspace_proxy_dmodules; // The D code generated for the currently processed enum. String *proxy_enum_code; /* * D data for the current proxy class. * * These strings are mainly used to temporarily accumulate code from the * various member handling functions while a single class is processed and are * no longer relevant once that class has been finished, i.e. after * classHandler() has returned. */ // The unqualified name of the current proxy class. String *proxy_class_name; // The name of the current proxy class, qualified with the name of the // namespace it is in, if any. String *proxy_class_qname; // The import directives for the current proxy class. They are written to the // same D module the proxy class is written to. String *proxy_class_imports; // Code for enumerations nested in the current proxy class. Is emitted earlier // than the rest of the body to work around forward referencing-issues. String *proxy_class_enums_code; // The generated D code making up the body of the current proxy class. String *proxy_class_body_code; // D code which is emitted right after the proxy class. String *proxy_class_epilogue_code; // The full code for the current proxy class, including the epilogue. String* proxy_class_code; // Code generated at the begin of every D file String *common_begin_code; // Contains a D call to the function wrapping C++ the destructor of the // current class (if there is a public C++ destructor). String *destructor_call; // D code for the director callbacks generated for the current class. String *director_dcallbacks_code; /* * Code for dynamically loading the wrapper library on the D side. */ // D code which is inserted into the im D module if dynamic linking is used. String *wrapper_loader_code; // The D code to bind a function pointer to a library symbol. String *wrapper_loader_bind_command; // The cumulated binding commands binding all the functions declared in the // intermediary D module to the C/C++ library symbols. String *wrapper_loader_bind_code; /* * Director data. */ List *dmethods_seq; Hash *dmethods_table; int n_dmethods; int first_class_dmethod; int curr_class_dmethod; /* * SWIG types data. */ // Collects information about encountered types SWIG does not know about (e.g. // incomplete types). This is used later to generate type wrapper proxy // classes for the unknown types. Hash *unknown_types; public: /* --------------------------------------------------------------------------- * D::D() * --------------------------------------------------------------------------- */ D():empty_string(NewString("")), public_string(NewString("public")), private_string(NewString("private")), protected_string(NewString("protected")), f_begin(NULL), f_runtime(NULL), f_runtime_h(NULL), f_header(NULL), f_wrappers(NULL), f_init(NULL), f_directors(NULL), f_directors_h(NULL), filenames_list(NULL), split_proxy_dmodule(false), native_function_flag(false), static_flag(false), variable_wrapper_flag(false), wrapping_member_flag(false), global_variable_flag(false), variable_name(NULL), upcasts_code(NULL), director_callback_typedefs(NULL), director_callback_pointers(NULL), im_dmodule_name(NULL), im_dmodule_fq_name(NULL), proxy_dmodule_name(NULL), proxy_dmodule_fq_name(NULL), package(NULL), dmodule_directory(NULL), wrap_library_name(NULL), im_dmodule_imports(NULL), im_dmodule_code(NULL), global_proxy_imports(NULL), proxy_dmodule_imports(NULL), proxy_dmodule_code(NULL), nspace_proxy_dmodules(NULL), proxy_enum_code(NULL), proxy_class_name(NULL), proxy_class_qname(NULL), proxy_class_imports(NULL), proxy_class_enums_code(NULL), proxy_class_body_code(NULL), proxy_class_epilogue_code(NULL), proxy_class_code(NULL), common_begin_code(NULL), destructor_call(NULL), director_dcallbacks_code(NULL), wrapper_loader_code(NULL), wrapper_loader_bind_command(NULL), wrapper_loader_bind_code(NULL), dmethods_seq(NULL), dmethods_table(NULL), n_dmethods(0), first_class_dmethod(0), curr_class_dmethod(0), unknown_types(NULL) { // For now, multiple inheritance with directors is not possible. It should be // easy to implement though. director_multiple_inheritance = 0; directorLanguage(); // Not used: Delete(none_comparison); none_comparison = NewString(""); } /* --------------------------------------------------------------------------- * D::main() * --------------------------------------------------------------------------- */ virtual void main(int argc, char *argv[]) { SWIG_library_directory("d"); // Look for certain command line options for (int i = 1; i < argc; i++) { if (argv[i]) { if ((strcmp(argv[i], "-d2") == 0)) { /* Keep for backward compatible only */ Swig_mark_arg(i); } else if (strcmp(argv[i], "-wrapperlibrary") == 0) { if (argv[i + 1]) { wrap_library_name = NewString(""); Printf(wrap_library_name, argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-package") == 0) { if (argv[i + 1]) { package = NewString(""); Printf(package, argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if ((strcmp(argv[i], "-splitproxy") == 0)) { Swig_mark_arg(i); split_proxy_dmodule = true; } else if (strcmp(argv[i], "-help") == 0) { Printf(stdout, "%s\n", usage); } } } // Add a symbol to the parser for conditional compilation Preprocessor_define("SWIGD 1", 0); // Also make the target D version available as preprocessor symbol for // use in our library files. Preprocessor_define("SWIG_D_VERSION 2", 0); SWIG_config_file("d.swg"); allow_overloading(); } /* --------------------------------------------------------------------------- * D::top() * --------------------------------------------------------------------------- */ virtual int top(Node *n) { // Get any options set in the module directive Node *module = Getattr(n, "module"); Node *optionsnode = Getattr(module, "options"); if (optionsnode) { if (Getattr(optionsnode, "imdmodulename")) { im_dmodule_name = Copy(Getattr(optionsnode, "imdmodulename")); } if (Getattr(optionsnode, "directors")) { // Check if directors are enabled for this module. Note: This is a // "master switch", if it is not set, not director code will be emitted // at all. %feature("director") statements are also required to enable // directors for individual classes or methods. // // Use the »directors« attributte of the %module directive to enable // director generation (e.g. »%module(directors="1") modulename«). allow_directors(); } if (Getattr(optionsnode, "dirprot")) { allow_dirprot(); } allow_allprotected(GetFlag(optionsnode, "allprotected")); common_begin_code = Getattr(optionsnode, "dbegin"); if (common_begin_code) Printf(common_begin_code, "\n"); } /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = Getattr(n, "outfile_h"); if (!outfile) { Printf(stderr, "Unable to determine outfile\n"); Exit(EXIT_FAILURE); } f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } if (Swig_directors_enabled()) { if (!outfile_h) { Printf(stderr, "Unable to determine outfile_h\n"); Exit(EXIT_FAILURE); } f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); unknown_types = NewHash(); filenames_list = NewList(); // Make the package name and the resulting module output path. if (package) { // Append a dot so we can prepend the package variable directly to the // module names in the rest of the code. Printv(package, ".", NIL); } else { // Write the generated D modules to the »root« package by default. package = NewString(""); } dmodule_directory = Copy(SWIG_output_directory()); if (Len(package) > 0) { String *package_directory = Copy(package); Replaceall(package_directory, ".", SWIG_FILE_DELIMITER); Printv(dmodule_directory, package_directory, NIL); Delete(package_directory); } // Make the wrap and proxy D module names. // The wrap module name can be set in the module directive. if (!im_dmodule_name) { im_dmodule_name = NewStringf("%s_im", Getattr(n, "name")); } im_dmodule_fq_name = NewStringf("%s%s", package, im_dmodule_name); proxy_dmodule_name = Copy(Getattr(n, "name")); proxy_dmodule_fq_name = NewStringf("%s%s", package, proxy_dmodule_name); im_dmodule_code = NewString(""); proxy_class_imports = NewString(""); proxy_class_enums_code = NewString(""); proxy_class_body_code = NewString(""); proxy_class_epilogue_code = NewString(""); proxy_class_code = NewString(""); destructor_call = NewString(""); proxy_dmodule_code = NewString(""); proxy_dmodule_imports = NewString(""); nspace_proxy_dmodules = NewHash(); im_dmodule_imports = NewString(""); upcasts_code = NewString(""); global_proxy_imports = NewString(""); wrapper_loader_code = NewString(""); wrapper_loader_bind_command = NewString(""); wrapper_loader_bind_code = NewString(""); dmethods_seq = NewList(); dmethods_table = NewHash(); n_dmethods = 0; // By default, expect the dynamically loaded wrapper library to be named // [lib]_wrap[.so/.dll]. if (!wrap_library_name) wrap_library_name = NewStringf("%s_wrap", Getattr(n, "name")); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "D"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); /* Emit initial director header and director code: */ Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", proxy_dmodule_name); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", proxy_dmodule_name); Printf(f_directors, "\n\n"); Printf(f_directors, "/* ---------------------------------------------------\n"); Printf(f_directors, " * C++ director class methods\n"); Printf(f_directors, " * --------------------------------------------------- */\n\n"); if (outfile_h) { String *filename = Swig_file_filename(outfile_h); Printf(f_directors, "#include \"%s\"\n\n", filename); Delete(filename); } } Printf(f_runtime, "\n"); Swig_name_register("wrapper", "D_%f"); Printf(f_wrappers, "\n#ifdef __cplusplus\n"); Printf(f_wrappers, "extern \"C\" {\n"); Printf(f_wrappers, "#endif\n\n"); // Emit all the wrapper code. Language::top(n); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (before %header section). Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } // Generate the wrap D module. // TODO: Add support for »static« linking. { String *filen = NewStringf("%s%s.d", dmodule_directory, im_dmodule_name); File *im_d_file = NewFile(filen, "w", SWIG_output_files()); if (!im_d_file) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; // Start writing out the intermediary class file. emitBanner(im_d_file); Printf(im_d_file, "module %s;\n", im_dmodule_fq_name); Printv(im_d_file, im_dmodule_imports, "\n", NIL); Replaceall(wrapper_loader_code, "$wraplibrary", wrap_library_name); Replaceall(wrapper_loader_code, "$wrapperloaderbindcode", wrapper_loader_bind_code); Replaceall(wrapper_loader_code, "$module", proxy_dmodule_name); Printf(im_d_file, "%s\n", wrapper_loader_code); // Add the wrapper function declarations. replaceModuleVariables(im_dmodule_code); Printv(im_d_file, im_dmodule_code, NIL); Delete(im_d_file); } // Generate the main D proxy module. { String *filen = NewStringf("%s%s.d", dmodule_directory, proxy_dmodule_name); File *proxy_d_file = NewFile(filen, "w", SWIG_output_files()); if (!proxy_d_file) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; emitBanner(proxy_d_file); Printf(proxy_d_file, "module %s;\n", proxy_dmodule_fq_name); Printf(proxy_d_file, "\nstatic import %s;\n", im_dmodule_fq_name); Printv(proxy_d_file, global_proxy_imports, NIL); Printv(proxy_d_file, proxy_dmodule_imports, NIL); Printv(proxy_d_file, "\n", NIL); // Write a D type wrapper class for each SWIG type to the proxy module code. for (Iterator swig_type = First(unknown_types); swig_type.key; swig_type = Next(swig_type)) { writeTypeWrapperClass(swig_type.key, swig_type.item); } // Add the proxy functions (and classes, if they are not written to a separate file). replaceModuleVariables(proxy_dmodule_code); Printv(proxy_d_file, proxy_dmodule_code, NIL); Delete(proxy_d_file); } // Generate the additional proxy modules for nspace support. for (Iterator it = First(nspace_proxy_dmodules); it.key; it = Next(it)) { String *module_name = createLastNamespaceName(it.key); String *filename = NewStringf("%s%s.d", outputDirectory(it.key), module_name); File *file = NewFile(filename, "w", SWIG_output_files()); if (!file) { FileErrorDisplay(filename); Exit(EXIT_FAILURE); } Delete(filename); emitBanner(file); Printf(file, "module %s%s.%s;\n", package, it.key, module_name); Printf(file, "\nstatic import %s;\n", im_dmodule_fq_name); Printv(file, global_proxy_imports, NIL); Printv(file, Getattr(it.item, "imports"), NIL); Printv(file, "\n", NIL); String *code = Getattr(it.item, "code"); replaceModuleVariables(code); Printv(file, code, NIL); Delete(file); Delete(module_name); } if (upcasts_code) Printv(f_wrappers, upcasts_code, NIL); Printf(f_wrappers, "#ifdef __cplusplus\n"); Printf(f_wrappers, "}\n"); Printf(f_wrappers, "#endif\n"); // Check for overwriting file problems on filesystems that are case insensitive Iterator it1; Iterator it2; for (it1 = First(filenames_list); it1.item; it1 = Next(it1)) { String *item1_lower = Swig_string_lower(it1.item); for (it2 = Next(it1); it2.item; it2 = Next(it2)) { String *item2_lower = Swig_string_lower(it2.item); if (it1.item && it2.item) { if (Strcmp(item1_lower, item2_lower) == 0) { Swig_warning(WARN_LANG_PORTABILITY_FILENAME, input_file, line_number, "Portability warning: File %s will be overwritten by %s on case insensitive filesystems such as " "Windows' FAT32 and NTFS unless the class/module name is renamed\n", it1.item, it2.item); } } Delete(item2_lower); } Delete(item1_lower); } Delete(unknown_types); unknown_types = NULL; Delete(filenames_list); filenames_list = NULL; Delete(im_dmodule_name); im_dmodule_name = NULL; Delete(im_dmodule_fq_name); im_dmodule_fq_name = NULL; Delete(im_dmodule_code); im_dmodule_code = NULL; Delete(proxy_class_imports); proxy_class_imports = NULL; Delete(proxy_class_enums_code); proxy_class_enums_code = NULL; Delete(proxy_class_body_code); proxy_class_body_code = NULL; Delete(proxy_class_epilogue_code); proxy_class_epilogue_code = NULL; Delete(proxy_class_code); proxy_class_code = NULL; Delete(destructor_call); destructor_call = NULL; Delete(proxy_dmodule_name); proxy_dmodule_name = NULL; Delete(proxy_dmodule_fq_name); proxy_dmodule_fq_name = NULL; Delete(proxy_dmodule_code); proxy_dmodule_code = NULL; Delete(proxy_dmodule_imports); proxy_dmodule_imports = NULL; Delete(nspace_proxy_dmodules); nspace_proxy_dmodules = NULL; Delete(im_dmodule_imports); im_dmodule_imports = NULL; Delete(upcasts_code); upcasts_code = NULL; Delete(global_proxy_imports); global_proxy_imports = NULL; Delete(wrapper_loader_code); wrapper_loader_code = NULL; Delete(wrapper_loader_bind_code); wrapper_loader_bind_code = NULL; Delete(wrapper_loader_bind_command); wrapper_loader_bind_command = NULL; Delete(dmethods_seq); dmethods_seq = NULL; Delete(dmethods_table); dmethods_table = NULL; Delete(package); package = NULL; Delete(dmodule_directory); dmodule_directory = NULL; n_dmethods = 0; // Merge all the generated C/C++ code and close the output files. Dump(f_runtime, f_begin); Dump(f_header, f_begin); if (Swig_directors_enabled()) { Dump(f_directors, f_begin); Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); Delete(f_runtime_h); f_runtime_h = NULL; Delete(f_directors); f_directors = NULL; Delete(f_directors_h); f_directors_h = NULL; } Dump(f_wrappers, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::insertDirective() * --------------------------------------------------------------------------- */ virtual int insertDirective(Node *n) { int ret = SWIG_OK; String *code = Getattr(n, "code"); String *section = Getattr(n, "section"); replaceModuleVariables(code); if (!ImportMode && (Cmp(section, "proxycode") == 0)) { if (proxy_class_body_code) { Swig_typemap_replace_embedded_typemap(code, n); Printv(proxy_class_body_code, code, NIL); } } else { ret = Language::insertDirective(n); } return ret; } /* --------------------------------------------------------------------------- * D::pragmaDirective() * * Valid Pragmas: * imdmodulecode - text (D code) is copied verbatim to the wrap module * imdmoduleimports - import statements for the im D module * * proxydmodulecode - text (D code) is copied verbatim to the proxy module * (the main proxy module if in split proxy mode). * globalproxyimports - import statements inserted into _all_ proxy modules. * * wrapperloadercode - D code for loading the wrapper library (is copied to * the im D module). * wrapperloaderbindcommand - D code for binding a symbol from the wrapper * library to the declaration in the im D module. * --------------------------------------------------------------------------- */ virtual int pragmaDirective(Node *n) { if (!ImportMode) { String *lang = Getattr(n, "lang"); String *code = Getattr(n, "name"); String *value = Getattr(n, "value"); if (Strcmp(lang, "d") == 0) { String *strvalue = NewString(value); Replaceall(strvalue, "\\\"", "\""); if (Strcmp(code, "imdmodulecode") == 0) { Printf(im_dmodule_code, "%s\n", strvalue); } else if (Strcmp(code, "imdmoduleimports") == 0) { replaceImportTypeMacros(strvalue); Chop(strvalue); Printf(im_dmodule_imports, "%s\n", strvalue); } else if (Strcmp(code, "proxydmodulecode") == 0) { Printf(proxyCodeBuffer(0), "%s\n", strvalue); } else if (Strcmp(code, "globalproxyimports") == 0) { replaceImportTypeMacros(strvalue); Chop(strvalue); Printf(global_proxy_imports, "%s\n", strvalue); } else if (Strcmp(code, "wrapperloadercode") == 0) { Delete(wrapper_loader_code); wrapper_loader_code = Copy(strvalue); } else if (Strcmp(code, "wrapperloaderbindcommand") == 0) { Delete(wrapper_loader_bind_command); wrapper_loader_bind_command = Copy(strvalue); } else { Swig_error(input_file, line_number, "Unrecognized pragma.\n"); } Delete(strvalue); } } return Language::pragmaDirective(n); } /* --------------------------------------------------------------------------- * D::enumDeclaration() * * Wraps C/C++ enums as D enums. * --------------------------------------------------------------------------- */ virtual int enumDeclaration(Node *n) { if (ImportMode) return SWIG_OK; if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; proxy_enum_code = NewString(""); String *symname = Getattr(n, "sym:name"); String *typemap_lookup_type = Getattr(n, "name"); // Emit the enum declaration. if (typemap_lookup_type) { // Enum base (underlying enum type) Node *attributes = NewHash(); const String *pure_baseclass = lookupCodeTypemap(n, "dbase", typemap_lookup_type, WARN_NONE, attributes); bool purebase_replace = GetFlag(attributes, "tmap:dbase:replace") ? true : false; Delete(attributes); const String *baseclass = NULL; if (!purebase_replace) { String *underlying_enum_type = Getattr(n, "enumbase"); if (underlying_enum_type) { baseclass = lookupCodeTypemap(n, "dtype", underlying_enum_type, WARN_D_TYPEMAP_DTYPE_UNDEF); } } const String *wanted_base = baseclass ? baseclass : pure_baseclass; if (purebase_replace) { wanted_base = pure_baseclass; } else if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) { Swig_warning(WARN_D_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, enum base %s ignored. Multiple enum bases is not supported in D enums. " "Perhaps you need the 'replace' attribute in the dbase typemap?\n", typemap_lookup_type, pure_baseclass); } const String *enummodifiers = lookupCodeTypemap(n, "dclassmodifiers", typemap_lookup_type, WARN_D_TYPEMAP_CLASSMOD_UNDEF); Printv(proxy_enum_code, "\n", enummodifiers, " ", symname, *Char(wanted_base) ? " : " : "", wanted_base, " {\n", NIL); } else { // Handle anonymous enums. Printv(proxy_enum_code, "\nenum {\n", NIL); } // Emit each enum item. Language::enumDeclaration(n); if (GetFlag(n, "nonempty")) { // Finish the enum. if (typemap_lookup_type) { Printv(proxy_enum_code, lookupCodeTypemap(n, "dcode", typemap_lookup_type, WARN_NONE), // Extra D code "\n}\n", NIL); } else { // Handle anonymous enums. Printv(proxy_enum_code, "\n}\n", NIL); } Replaceall(proxy_enum_code, "$dclassname", symname); } else { // D enum declarations must have at least one member to be legal, so emit // an alias to int instead (their ctype/imtype is always int). Delete(proxy_enum_code); proxy_enum_code = NewStringf("\nalias int %s;\n", symname); } const String* imports = lookupCodeTypemap(n, "dimports", typemap_lookup_type, WARN_NONE); String* imports_trimmed; if (Len(imports) > 0) { imports_trimmed = Copy(imports); Chop(imports_trimmed); replaceImportTypeMacros(imports_trimmed); Printv(imports_trimmed, "\n", NIL); } else { imports_trimmed = NewString(""); } if (is_wrapping_class()) { // Enums defined within the C++ class are written into the proxy // class. Printv(proxy_class_imports, imports_trimmed, NIL); Printv(proxy_class_enums_code, proxy_enum_code, NIL); } else { // Write non-anonymous enums to their own file if in split proxy module // mode. if (split_proxy_dmodule && typemap_lookup_type) { assertClassNameValidity(proxy_class_name); String *nspace = Getattr(n, "sym:nspace"); String *output_directory = outputDirectory(nspace); String *filename = NewStringf("%s%s.d", output_directory, symname); Delete(output_directory); File *class_file = NewFile(filename, "w", SWIG_output_files()); if (!class_file) { FileErrorDisplay(filename); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filename)); Delete(filename); emitBanner(class_file); if (nspace) { Printf(class_file, "module %s%s.%s;\n", package, nspace, symname); } else { Printf(class_file, "module %s%s;\n", package, symname); } Printv(class_file, imports_trimmed, NIL); Printv(class_file, proxy_enum_code, NIL); Delete(class_file); } else { String *nspace = Getattr(n, "sym:nspace"); Printv(proxyImportsBuffer(nspace), imports, NIL); Printv(proxyCodeBuffer(nspace), proxy_enum_code, NIL); } } Delete(imports_trimmed); Delete(proxy_enum_code); proxy_enum_code = NULL; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::enumvalueDeclaration() * --------------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; Swig_require("enumvalueDeclaration", n, "*name", "?value", NIL); String *value = Getattr(n, "value"); String *name = Getattr(n, "name"); Node *parent = parentNode(n); String *tmpValue; // Strange hack from parent method. // RESEARCH: What is this doing? if (value) tmpValue = NewString(value); else tmpValue = NewString(name); // Note that this is used in enumValue() amongst other places Setattr(n, "value", tmpValue); // Deal with enum values that are not int { String *numval = Getattr(n, "enumnumval"); if (numval) Setattr(n, "enumvalue", numval); } // Emit the enum item. { if (!GetFlag(n, "firstenumitem")) Printf(proxy_enum_code, ",\n"); Printf(proxy_enum_code, " %s", Getattr(n, "sym:name")); // Check for the %dconstvalue feature String *value = Getattr(n, "feature:d:constvalue"); // Note that in D, enum values must be compile-time constants. Thus, // %dmanifestconst(0) (getting the enum values at runtime) is not supported. value = value ? value : Getattr(n, "enumvalue"); if (value) { Printf(proxy_enum_code, " = %s", value); } // Keep track that the currently processed enum has at least one value. SetFlag(parent, "nonempty"); } Delete(tmpValue); Swig_restore(n); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::memberfunctionHandler() * --------------------------------------------------------------------------- */ virtual int memberfunctionHandler(Node *n) { Language::memberfunctionHandler(n); String *overloaded_name = getOverloadedName(n); String *intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, overloaded_name); Setattr(n, "imfuncname", intermediary_function_name); String *proxy_func_name = Getattr(n, "sym:name"); Setattr(n, "proxyfuncname", proxy_func_name); if (split_proxy_dmodule && Len(Getattr(n, "parms")) == 0 && Strncmp(proxy_func_name, package, Len(proxy_func_name)) == 0) { // If we are in split proxy mode and the function is named like the // target package, the D compiler is unable to resolve the ambiguity // between the package name and an argument-less function call. // TODO: This might occur with nspace as well, augment the check. Swig_warning(WARN_D_NAME_COLLISION, input_file, line_number, "%s::%s might collide with the package name, consider using %%rename to resolve the ambiguity.\n", proxy_class_name, proxy_func_name); } writeProxyClassFunction(n); Delete(overloaded_name); // For each function, look if we have to alias in the parent class function // for the overload resolution process to work as expected from C++ // (http://www.digitalmars.com/d/2.0/function.html#function-inheritance). // For multiple overloads, only emit the alias directive once (for the // last method, »sym:nextSibling« is null then). // Smart pointer classes do not mirror the inheritance hierarchy of the // underlying types, so aliasing the base class methods in is not required // for them. // DMD BUG: We have to emit the alias after the last function because // taking a delegate in the overload checking code fails otherwise // (http://d.puremagic.com/issues/show_bug.cgi?id=4860). if (!Getattr(n, "sym:nextSibling") && !is_smart_pointer() && !areAllOverloadsOverridden(n)) { String *name = Getattr(n, "sym:name"); Printf(proxy_class_body_code, "\nalias $dbaseclass.%s %s;\n", name, name); } return SWIG_OK; } /* --------------------------------------------------------------------------- * D::staticmemberfunctionHandler() * --------------------------------------------------------------------------- */ virtual int staticmemberfunctionHandler(Node *n) { static_flag = true; Language::staticmemberfunctionHandler(n); String *overloaded_name = getOverloadedName(n); String *intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, overloaded_name); Setattr(n, "proxyfuncname", Getattr(n, "sym:name")); Setattr(n, "imfuncname", intermediary_function_name); writeProxyClassFunction(n); Delete(overloaded_name); static_flag = false; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::globalvariableHandler() * --------------------------------------------------------------------------- */ virtual int globalvariableHandler(Node *n) { variable_name = Getattr(n, "sym:name"); global_variable_flag = true; int ret = Language::globalvariableHandler(n); global_variable_flag = false; return ret; } /* --------------------------------------------------------------------------- * D::membervariableHandler() * --------------------------------------------------------------------------- */ virtual int membervariableHandler(Node *n) { variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; variable_wrapper_flag = true; Language::membervariableHandler(n); wrapping_member_flag = false; variable_wrapper_flag = false; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::staticmembervariableHandler() * --------------------------------------------------------------------------- */ virtual int staticmembervariableHandler(Node *n) { if (GetFlag(n, "feature:d:manifestconst") != 1) { Delattr(n, "value"); } variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; static_flag = true; Language::staticmembervariableHandler(n); wrapping_member_flag = false; static_flag = false; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::memberconstantHandler() * --------------------------------------------------------------------------- */ virtual int memberconstantHandler(Node *n) { variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; Language::memberconstantHandler(n); wrapping_member_flag = false; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::constructorHandler() * --------------------------------------------------------------------------- */ virtual int constructorHandler(Node *n) { Language::constructorHandler(n); // Wrappers not wanted for some methods where the parameters cannot be overloadedprocess in D. if (Getattr(n, "overload:ignore")) { return SWIG_OK; } ParmList *l = Getattr(n, "parms"); String *tm; String *proxy_constructor_code = NewString(""); int i; // Holds code for the constructor helper method generated only when the din // typemap has code in the pre or post attributes. String *helper_code = NewString(""); String *helper_args = NewString(""); String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); NewString(""); String *overloaded_name = getOverloadedName(n); String *mangled_overname = Swig_name_construct(getNSpace(), overloaded_name); String *imcall = NewString(""); const String *methodmods = Getattr(n, "feature:d:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); // Typemaps were attached earlier to the node, get the return type of the // call to the C++ constructor wrapper. const String *wrapper_return_type = lookupDTypemap(n, "imtype", true); String *imtypeout = Getattr(n, "tmap:imtype:out"); if (imtypeout) { // The type in the imtype typemap's out attribute overrides the type in // the typemap itself. wrapper_return_type = imtypeout; } Printf(proxy_constructor_code, "\n%s this(", methodmods); Printf(helper_code, "static private %s SwigConstruct%s(", wrapper_return_type, proxy_class_name); Printv(imcall, im_dmodule_fq_name, ".", mangled_overname, "(", NIL); /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("in", l, NULL); Swig_typemap_attach_parms("dtype", l, NULL); Swig_typemap_attach_parms("din", l, NULL); emit_mark_varargs(l); int gencomma = 0; /* Output each parameter */ Parm *p = l; for (i = 0; p; i++) { if (checkAttribute(p, "varargs:ignore", "1")) { // Skip ignored varargs. p = nextSibling(p); continue; } if (checkAttribute(p, "tmap:in:numinputs", "0")) { // Skip ignored parameters. p = Getattr(p, "tmap:in:next"); continue; } SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); // Get the D parameter type. if ((tm = lookupDTypemap(p, "dtype", true))) { const String *inattributes = Getattr(p, "tmap:dtype:inattributes"); Printf(param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, false); String *parmtype = 0; // Get the D code to convert the parameter value to the type used in the // intermediary D module. if ((tm = lookupDTypemap(p, "din", true))) { Replaceall(tm, "$dinput", arg); String *pre = Getattr(p, "tmap:din:pre"); if (pre) { replaceClassname(pre, pt); Replaceall(pre, "$dinput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:din:post"); if (post) { replaceClassname(post, pt); Replaceall(post, "$dinput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:din:terminator"); if (terminator) { replaceClassname(terminator, pt); Replaceall(terminator, "$dinput", arg); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } parmtype = Getattr(p, "tmap:din:parmtype"); if (parmtype) Replaceall(parmtype, "$dinput", arg); Printv(imcall, tm, NIL); } else { Swig_warning(WARN_D_TYPEMAP_DIN_UNDEF, input_file, line_number, "No din typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to proxy function */ if (gencomma) { Printf(proxy_constructor_code, ", "); Printf(helper_code, ", "); Printf(helper_args, ", "); } Printf(proxy_constructor_code, "%s %s", param_type, arg); Printf(helper_code, "%s %s", param_type, arg); Printf(helper_args, "%s", parmtype ? parmtype : arg); ++gencomma; Delete(parmtype); Delete(arg); Delete(param_type); p = Getattr(p, "tmap:in:next"); } Printf(imcall, ")"); Printf(proxy_constructor_code, ")"); Printf(helper_code, ")"); // Insert the dconstructor typemap (replacing $directorconnect as needed). Hash *attributes = NewHash(); String *typemap_lookup_type = Getattr(getCurrentClass(), "classtypeobj"); String *construct_tm = Copy(lookupCodeTypemap(n, "dconstructor", typemap_lookup_type, WARN_D_TYPEMAP_DCONSTRUCTOR_UNDEF, attributes)); if (construct_tm) { const bool use_director = (parentNode(n) && Swig_directorclass(n)); if (!use_director) { Replaceall(construct_tm, "$directorconnect", ""); } else { String *connect_attr = Getattr(attributes, "tmap:dconstructor:directorconnect"); if (connect_attr) { Replaceall(construct_tm, "$directorconnect", connect_attr); } else { Swig_warning(WARN_D_NO_DIRECTORCONNECT_ATTR, input_file, line_number, "\"directorconnect\" attribute missing in %s \"dconstructor\" typemap.\n", Getattr(n, "name")); Replaceall(construct_tm, "$directorconnect", ""); } } Printv(proxy_constructor_code, " ", construct_tm, NIL); } replaceExcode(n, proxy_constructor_code, "dconstructor", attributes); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code || is_post_code || is_terminator_code) { Printf(helper_code, " {\n"); if (is_pre_code) { Printv(helper_code, pre_code, "\n", NIL); } if (is_post_code) { Printf(helper_code, " try {\n"); Printv(helper_code, " return ", imcall, ";\n", NIL); Printv(helper_code, " } finally {\n", post_code, "\n }", NIL); } else { Printv(helper_code, " return ", imcall, ";", NIL); } if (is_terminator_code) { Printv(helper_code, "\n", terminator_code, NIL); } Printf(helper_code, "\n}\n"); String *helper_name = NewStringf("%s.SwigConstruct%s(%s)", proxy_class_name, proxy_class_name, helper_args); Replaceall(proxy_constructor_code, "$imcall", helper_name); Delete(helper_name); } else { Replaceall(proxy_constructor_code, "$imcall", imcall); } Printv(proxy_class_body_code, proxy_constructor_code, "\n", NIL); Delete(helper_args); Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(construct_tm); Delete(attributes); Delete(overloaded_name); Delete(imcall); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::destructorHandler() * --------------------------------------------------------------------------- */ virtual int destructorHandler(Node *n) { Language::destructorHandler(n); String *symname = Getattr(n, "sym:name"); Printv(destructor_call, im_dmodule_fq_name, ".", Swig_name_destroy(getNSpace(),symname), "(cast(void*)swigCPtr)", NIL); const String *methodmods = Getattr(n, "feature:d:methodmodifiers"); if (methodmods) Setattr(getCurrentClass(), "destructmethodmodifiers", methodmods); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::classHandler() * --------------------------------------------------------------------------- */ virtual int classHandler(Node *n) { String *nspace = getNSpace(); File *class_file = NULL; proxy_class_name = Copy(Getattr(n, "sym:name")); if (nspace) { proxy_class_qname = NewStringf("%s.%s", nspace, proxy_class_name); } else { proxy_class_qname = Copy(proxy_class_name); } if (!addSymbol(proxy_class_name, n, nspace)) { return SWIG_ERROR; } assertClassNameValidity(proxy_class_name); if (split_proxy_dmodule) { String *output_directory = outputDirectory(nspace); String *filename = NewStringf("%s%s.d", output_directory, proxy_class_name); class_file = NewFile(filename, "w", SWIG_output_files()); Delete(output_directory); if (!class_file) { FileErrorDisplay(filename); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filename)); Delete(filename); emitBanner(class_file); if (nspace) { Printf(class_file, "module %s%s.%s;\n", package, nspace, proxy_class_name); } else { Printf(class_file, "module %s%s;\n", package, proxy_class_name); } Printf(class_file, "\nstatic import %s;\n", im_dmodule_fq_name); } Clear(proxy_class_enums_code); Clear(proxy_class_body_code); Clear(proxy_class_epilogue_code); Clear(proxy_class_code); Clear(destructor_call); // Traverse the tree for this class, using the *Handler()s to generate code // to the proxy_class_* variables. Language::classHandler(n); // This function write super methods in proxy_class_body_code writeDirectorSuperFunctions(n); writeProxyClassAndUpcasts(n); writeDirectorConnectWrapper(n); Replaceall(proxy_class_code, "$dclassname", proxy_class_name); String *dclazzname = Swig_name_member(getNSpace(), proxy_class_name, ""); Replaceall(proxy_class_code, "$dclazzname", dclazzname); Delete(dclazzname); if (split_proxy_dmodule) { Printv(class_file, global_proxy_imports, NIL); Printv(class_file, proxy_class_imports, NIL); replaceModuleVariables(proxy_class_code); Printv(class_file, proxy_class_code, NIL); Delete(class_file); } else { Printv(proxyImportsBuffer(getNSpace()), proxy_class_imports, NIL); Printv(proxyCodeBuffer(getNSpace()), proxy_class_code, NIL); } Clear(proxy_class_imports); Delete(proxy_class_qname); proxy_class_qname = NULL; Delete(proxy_class_name); proxy_class_name = NULL; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::constantWrapper() * * Used for wrapping constants declared by #define or %constant and also for * (primitive) static member constants initialised inline. * * If the %dmanifestconst feature is used, the C/C++ constant value is used to * initialize a D »const«. If not, a »getter« method is generated which * retrieves the value via a call to the C wrapper. However, if there is a * %dconstvalue specified, it overrides all other settings. * --------------------------------------------------------------------------- */ virtual int constantWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); if (!addSymbol(symname, n)) return SWIG_ERROR; // The %dmanifestconst feature determines if a D manifest constant // (const/enum) or a getter function is created. if (GetFlag(n, "feature:d:manifestconst") != 1) { // Default constant handling will work with any type of C constant. It // generates a getter function (which is the same as a read only property // in D) which retrieves the value via by calling the C wrapper. // Note that this is only called for global constants, static member // constants are already handled in staticmemberfunctionHandler(). Swig_save("constantWrapper", n, "tmap:ctype:out", "tmap:imtype:out", "tmap:dtype:out", "tmap:out:null", "tmap:imtype:outattributes", "tmap:dtype:outattributes", NIL); SetFlag(n, "feature:immutable"); int result = globalvariableHandler(n); Swig_restore(n); return result; } String *constants_code = NewString(""); SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); // Attach the non-standard typemaps to the parameter list. Swig_typemap_attach_parms("dtype", l, NULL); // Get D return type. String *return_type = getOutDtype(n); if (!return_type) { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(t, 0)); return_type = NewString(""); } const String *itemname = wrapping_member_flag ? variable_name : symname; String *attributes = Getattr(n, "feature:d:methodmodifiers"); if (attributes) { attributes = Copy(attributes); } else { attributes = Copy(is_public(n) ? public_string : protected_string); } Printf(constants_code, "\n%s enum %s %s = ", attributes, return_type, itemname); Delete(attributes); // Retrieve the override value set via %dconstvalue, if any. String *override_value = Getattr(n, "feature:d:constvalue"); if (override_value) { Printf(constants_code, "%s;\n", override_value); } else { // Just take the value from the C definition and hope it compiles in D. if (Getattr(n, "wrappedasconstant")) { Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); } else { Printf(constants_code, "%s;\n", Getattr(n, "value")); } } // Emit the generated code to appropriate place. if (wrapping_member_flag) { Printv(proxy_class_body_code, constants_code, NIL); } else { Printv(proxyCodeBuffer(getNSpace()), constants_code, NIL); } // Cleanup. Delete(return_type); Delete(constants_code); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::functionWrapper() * * Generates the C wrapper code for a function and the corresponding * declaration in the wrap D module. * --------------------------------------------------------------------------- */ virtual int functionWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *c_return_type = NewString(""); String *im_return_type = NewString(""); String *cleanup = NewString(""); String *outarg = NewString(""); int num_arguments = 0; bool is_void_return; String *overloaded_name = getOverloadedName(n); if (!Getattr(n, "sym:overloaded")) { if (!addSymbol(Getattr(n, "sym:name"), n)) return SWIG_ERROR; } // A new wrapper function object Wrapper *f = NewWrapper(); // Make a wrapper name for this function String *wname = Swig_name_wrapper(overloaded_name); /* Attach the non-standard typemaps to the parameter list. */ Swig_typemap_attach_parms("ctype", l, f); Swig_typemap_attach_parms("imtype", l, f); /* Get return types */ if ((tm = lookupDTypemap(n, "ctype"))) { String *ctypeout = Getattr(n, "tmap:ctype:out"); if (ctypeout) { // The type in the ctype typemap's out attribute overrides the type in // the typemap itself. tm = ctypeout; } Printf(c_return_type, "%s", tm); } else { Swig_warning(WARN_D_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(returntype, 0)); } if ((tm = lookupDTypemap(n, "imtype"))) { String *imtypeout = Getattr(n, "tmap:imtype:out"); if (imtypeout) { // The type in the imtype typemap's out attribute overrides the type in // the typemap itself. tm = imtypeout; } Printf(im_return_type, "%s", tm); } else { Swig_warning(WARN_D_TYPEMAP_IMTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s\n", SwigType_str(returntype, 0)); } is_void_return = (Cmp(c_return_type, "void") == 0); if (!is_void_return) Wrapper_add_localv(f, "jresult", c_return_type, "jresult", NIL); Printv(f->def, " SWIGEXPORT ", c_return_type, " ", wname, "(", NIL); // Emit all of the local variables for holding arguments. emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); // Parameter overloading Setattr(n, "wrap:parms", l); Setattr(n, "wrap:name", wname); // Wrappers not wanted for some methods where the parameters cannot be overloaded in D if (Getattr(n, "sym:overloaded")) { // Emit warnings for the few cases that can't be overloaded in D and give up on generating wrapper Swig_overload_check(n); if (Getattr(n, "overload:ignore")) { DelWrapper(f); return SWIG_OK; } } // Collect the parameter list for the intermediary D module declaration of // the generated wrapper function. String *im_dmodule_parameters = NewString("("); /* Get number of required and total arguments */ num_arguments = emit_num_arguments(l); int gencomma = 0; // Now walk the function parameter list and generate code to get arguments for (i = 0, p = l; i < num_arguments; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); String *im_param_type = NewString(""); String *c_param_type = NewString(""); String *arg = NewString(""); Printf(arg, "j%s", ln); /* Get the ctype types of the parameter */ if ((tm = lookupDTypemap(p, "ctype", true))) { Printv(c_param_type, tm, NIL); } else { Swig_warning(WARN_D_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(pt, 0)); } /* Get the intermediary class parameter types of the parameter */ if ((tm = lookupDTypemap(p, "imtype", true))) { const String *inattributes = Getattr(p, "tmap:imtype:inattributes"); Printf(im_param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_D_TYPEMAP_IMTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to intermediary class method */ if (gencomma) Printf(im_dmodule_parameters, ", "); Printf(im_dmodule_parameters, "%s %s", im_param_type, arg); // Add parameter to C function Printv(f->def, gencomma ? ", " : "", c_param_type, " ", arg, NIL); gencomma = 1; // Get typemap for this argument if ((tm = Getattr(p, "tmap:in"))) { canThrow(n, "in", p); Replaceall(tm, "$input", arg); Setattr(p, "emit:input", arg); Printf(f->code, "%s\n", tm); p = Getattr(p, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); p = nextSibling(p); } Delete(im_param_type); Delete(c_param_type); Delete(arg); } /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { canThrow(n, "check", p); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { canThrow(n, "freearg", p); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { canThrow(n, "argout", p); Replaceall(tm, "$result", "jresult"); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } // Look for usage of throws typemap and the canthrow flag ParmList *throw_parm_list = NULL; if ((throw_parm_list = Getattr(n, "catchlist"))) { Swig_typemap_attach_parms("throws", throw_parm_list, f); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { canThrow(n, "throws", p); } } } String *null_attribute = 0; // Now write code to make the function call if (!native_function_flag) { Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { canThrow(n, "out", n); Replaceall(tm, "$result", "jresult"); if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "1"); else Replaceall(tm, "$owner", "0"); Printf(f->code, "%s", tm); null_attribute = Getattr(n, "tmap:out:null"); if (Len(tm)) Printf(f->code, "\n"); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), Getattr(n, "name")); } emit_return_variable(n, returntype, f); } /* Output argument output code */ Printv(f->code, outarg, NIL); /* Output cleanup code */ Printv(f->code, cleanup, NIL); /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { canThrow(n, "newfree", n); Printf(f->code, "%s\n", tm); } } /* See if there is any return cleanup code */ if (!native_function_flag) { if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { canThrow(n, "ret", n); Printf(f->code, "%s\n", tm); } } // Complete D im parameter list and emit the declaration/binding code. Printv(im_dmodule_parameters, ")", NIL); writeImDModuleFunction(overloaded_name, im_return_type, im_dmodule_parameters, wname); Delete(im_dmodule_parameters); // Finish C function header. Printf(f->def, ") {"); if (!is_void_return) Printv(f->code, " return jresult;\n", NIL); Printf(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); Replaceall(f->code, "$isvoid", is_void_return ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", symname); /* Contract macro modification */ if (Replaceall(f->code, "SWIG_contract_assert(", "SWIG_contract_assert($null, ") > 0) { Setattr(n, "d:canthrow", "1"); } if (!null_attribute) Replaceall(f->code, "$null", "0"); else Replaceall(f->code, "$null", null_attribute); /* Dump the function out */ if (!native_function_flag) { Wrapper_print(f, f_wrappers); // Handle %exception which sets the canthrow attribute. if (Getattr(n, "feature:except:canthrow")) { Setattr(n, "d:canthrow", "1"); } // A very simple check (it is not foolproof) to assist typemap writers // with setting the correct features when the want to throw D exceptions // from C++ code. It checks for the common methods which set // a pending D exception and issues a warning if one of them has been found // in the typemap, but the »canthrow« attribute/feature is not set. if (!Getattr(n, "d:canthrow")) { if (Strstr(f->code, "SWIG_exception")) { Swig_warning(WARN_D_CANTHROW_MISSING, input_file, line_number, "C code contains a call to SWIG_exception and D code does not handle pending exceptions via the canthrow attribute.\n"); } else if (Strstr(f->code, "SWIG_DSetPendingException")) { Swig_warning(WARN_D_CANTHROW_MISSING, input_file, line_number, "C code contains a call to a SWIG_DSetPendingException method and D code does not handle pending exceptions via the canthrow attribute.\n"); } } } // If we are not processing an enum or constant, and we were not generating // a wrapper function which will be accessed via a proxy class, write a // function to the proxy D module. if (!is_wrapping_class()) { writeProxyDModuleFunction(n); } // If we are processing a public member variable, write the property-style // member function to the proxy class. if (wrapping_member_flag) { Setattr(n, "proxyfuncname", variable_name); Setattr(n, "imfuncname", symname); writeProxyClassFunction(n); } Delete(c_return_type); Delete(im_return_type); Delete(cleanup); Delete(outarg); Delete(overloaded_name); DelWrapper(f); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::nativeWrapper() * --------------------------------------------------------------------------- */ virtual int nativeWrapper(Node *n) { String *wrapname = Getattr(n, "wrap:name"); if (!addSymbol(wrapname, n)) return SWIG_ERROR; if (Getattr(n, "type")) { Swig_save("nativeWrapper", n, "name", NIL); Setattr(n, "name", wrapname); native_function_flag = true; functionWrapper(n); Swig_restore(n); native_function_flag = false; } else { Swig_error(input_file, line_number, "No return type for %%native method %s.\n", Getattr(n, "wrap:name")); } return SWIG_OK; } /* --------------------------------------------------------------------------- * D::classDirector() * --------------------------------------------------------------------------- */ virtual int classDirector(Node *n) { String *nspace = Getattr(n, "sym:nspace"); proxy_class_name = NewString(Getattr(n, "sym:name")); if (nspace) { proxy_class_qname = NewStringf("%s.%s", nspace, proxy_class_name); } else { proxy_class_qname = Copy(proxy_class_name); } int success = Language::classDirector(n); Delete(proxy_class_qname); proxy_class_qname = NULL; Delete(proxy_class_name); proxy_class_name = NULL; return success; } /* --------------------------------------------------------------------------- * D::classDirectorInit() * --------------------------------------------------------------------------- */ virtual int classDirectorInit(Node *n) { Delete(director_ctor_code); director_ctor_code = NewString("$director_new"); // Write C++ director class declaration, for example: // class SwigDirector_myclass : public myclass, public Swig::Director { String *classname = Swig_class_name(n); String *directorname = directorClassName(n); String *declaration = Swig_class_declaration(n, directorname); const String *base = Getattr(n, "classtype"); Printf(f_directors_h, "%s : public %s, public Swig::Director {\n", declaration, base); Printf(f_directors_h, "\npublic:\n"); Delete(declaration); Delete(directorname); Delete(classname); // Stash for later. Setattr(n, "director:ctor", NewString("Swig::Director()")); // Keep track of the director methods for this class. first_class_dmethod = curr_class_dmethod = n_dmethods; director_callback_typedefs = NewString(""); director_callback_pointers = NewString(""); director_dcallbacks_code = NewString(""); return Language::classDirectorInit(n); } /* --------------------------------------------------------------------------- * D::classDirectorMethod() * * Emit a virtual director method to pass a method call on to the * underlying D object. * --------------------------------------------------------------------------- */ virtual int classDirectorMethod(Node *n, Node *parent, String *super) { String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); String *overloaded_name = 0; String *storage = Getattr(n, "storage"); String *value = Getattr(n, "value"); String *decl = Getattr(n, "decl"); String *declaration = NewString(""); String *tm; Parm *p; int i; Wrapper *w = NewWrapper(); ParmList *l = Getattr(n, "parms"); bool is_void = !(Cmp(returntype, "void")); String *qualified_return = 0; bool pure_virtual = (!(Cmp(storage, "virtual")) && !(Cmp(value, "0"))); int status = SWIG_OK; bool output_director = true; String *dirclassname = directorClassName(parent); String *qualified_name = NewStringf("%s::%s", dirclassname, name); SwigType *c_ret_type = NULL; String *dcallback_call_args = NewString(""); String *callback_typedef_parms = NewString(""); String *delegate_parms = NewString(""); String *proxy_method_param_list = NewString(""); String *proxy_callback_return_type = NewString(""); String *callback_def = NewString(""); String *callback_code = NewString(""); String *imcall_args = NewString(""); bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; // Kludge Alert: functionWrapper sets sym:overload properly, but it // isn't at this point, so we have to manufacture it ourselves. At least // we're consistent with the sym:overload name in functionWrapper. (?? when // does the overloaded method name get set?) if (!ignored_method) overloaded_name = getOverloadedName(n); qualified_return = SwigType_rcaststr(returntype, "c_result"); if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { String *base_typename = SwigType_base(returntype); String *resolved_typename = SwigType_typedef_resolve_all(base_typename); Symtab *symtab = Getattr(n, "sym:symtab"); Node *typenode = Swig_symbol_clookup(resolved_typename, symtab); if (SwigType_ispointer(returntype) || (typenode && Getattr(typenode, "abstracts"))) { /* initialize pointers to something sane. Same for abstract classes when a reference is returned. */ Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } else { /* If returning a reference, initialize the pointer to a sane default - if a D exception occurs, then the pointer returns something other than a NULL-initialized reference. */ SwigType *noref_type = SwigType_del_reference(Copy(returntype)); String *noref_ltype = SwigType_lstr(noref_type, 0); String *return_ltype = SwigType_lstr(returntype, 0); Wrapper_add_localv(w, "result_default", "static", noref_ltype, "result_default", NIL); Wrapper_add_localv(w, "c_result", return_ltype, "c_result", NIL); Printf(w->code, "result_default = SwigValueInit< %s >();\n", noref_ltype); Printf(w->code, "c_result = &result_default;\n"); Delete(return_ltype); Delete(noref_ltype); Delete(noref_type); } Delete(base_typename); Delete(resolved_typename); } } else { SwigType *vt; vt = cplus_value_type(returntype); if (!vt) { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), NIL); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(vt, "c_result"), NIL); Delete(vt); } } } /* Create the intermediate class wrapper */ tm = lookupDTypemap(n, "imtype"); if (tm) { String *imtypeout = Getattr(n, "tmap:imtype:out"); if (imtypeout) { // The type in the imtype typemap's out attribute overrides the type // in the typemap. tm = imtypeout; } Printf(callback_def, "\nprivate extern(C) %s swigDirectorCallback_%s_%s(void* dObject", tm, classname, overloaded_name); Printv(proxy_callback_return_type, tm, NIL); } else { Swig_warning(WARN_D_TYPEMAP_IMTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s\n", SwigType_str(returntype, 0)); } if ((c_ret_type = Swig_typemap_lookup("ctype", n, "", 0))) { if (!is_void && !ignored_method) { String *jretval_decl = NewStringf("%s jresult", c_ret_type); Wrapper_add_localv(w, "jresult", jretval_decl, "= 0", NIL); Delete(jretval_decl); } } else { Swig_warning(WARN_D_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Swig_director_parms_fixup(l); // Attach the standard typemaps. Swig_typemap_attach_parms("out", l, 0); Swig_typemap_attach_parms("ctype", l, 0); Swig_typemap_attach_parms("imtype", l, 0); Swig_typemap_attach_parms("dtype", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("ddirectorin", l, 0); Swig_typemap_attach_parms("directorargout", l, w); // Preamble code. if (!ignored_method) Printf(w->code, "if (!swig_callback_%s) {\n", overloaded_name); if (!pure_virtual) { String *super_call = Swig_method_call(super, l); if (is_void) { Printf(w->code, "%s;\n", super_call); if (!ignored_method) Printf(w->code, "return;\n"); } else { Printf(w->code, "return %s;\n", super_call); } Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"%s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); if (!is_void) Printf(w->code, "return %s;", qualified_return); else if (!ignored_method) Printf(w->code, "return;\n"); } if (!ignored_method) Printf(w->code, "} else {\n"); // Go through argument list. for (i = 0, p = l; p; ++i) { /* Is this superfluous? */ while (checkAttribute(p, "tmap:directorin:numinputs", "0")) { p = Getattr(p, "tmap:directorin:next"); } SwigType *pt = Getattr(p, "type"); String *ln = makeParameterName(n, p, i, false); String *c_param_type = NULL; String *c_decl = NewString(""); String *arg = NewString(""); Printf(arg, "j%s", ln); // Add each parameter to the D callback invocation arguments. Printf(dcallback_call_args, ", %s", arg); /* Get parameter's intermediary C type */ if ((c_param_type = lookupDTypemap(p, "ctype", true))) { String *ctypeout = Getattr(p, "tmap:ctype:out"); if (ctypeout) { // The type in the ctype typemap's out attribute overrides the type // in the typemap itself. c_param_type = ctypeout; } // ctype default assignment const String *ctypedef = Getattr(p, "tmap:ctype:default"); String *ctypeassign; if (ctypedef) { ctypeassign = Copy(ctypedef); } else if (SwigType_ispointer(pt) || SwigType_isreference(pt)) { ctypeassign = NewString("= 0"); } else { ctypeassign = NewString(""); } /* Add to local variables */ Printf(c_decl, "%s %s", c_param_type, arg); if (!ignored_method) Wrapper_add_localv(w, arg, c_decl, ctypeassign, NIL); Delete(ctypeassign); /* Add input marshalling code */ if ((tm = Getattr(p, "tmap:directorin"))) { Setattr(p, "emit:directorinput", arg); Replaceall(tm, "$input", arg); Replaceall(tm, "$owner", "0"); if (Len(tm)) if (!ignored_method) Printf(w->code, "%s\n", tm); // Add parameter type to the C typedef for the D callback function. Printf(callback_typedef_parms, ", %s", c_param_type); /* Add parameter to the intermediate class code if generating the * intermediate's upcall code */ if ((tm = lookupDTypemap(p, "imtype", true))) { String *imtypeout = Getattr(p, "tmap:imtype:out"); if (imtypeout) { // The type in the imtype typemap's out attribute overrides the // type in the typemap itself. tm = imtypeout; } const String *im_directorinattributes = Getattr(p, "tmap:imtype:directorinattributes"); // TODO: Is this copy really needed? String *din = Copy(lookupDTypemap(p, "ddirectorin", true)); if (din) { Replaceall(din, "$winput", ln); Printf(delegate_parms, ", "); if (i > 0) { Printf(proxy_method_param_list, ", "); Printf(imcall_args, ", "); } Printf(delegate_parms, "%s%s %s", im_directorinattributes ? im_directorinattributes : empty_string, tm, ln); if (Cmp(din, ln)) { Printv(imcall_args, din, NIL); } else { Printv(imcall_args, ln, NIL); } Delete(din); // Get the parameter type in the proxy D class (used later when // generating the overload checking code for the directorConnect // function). if ((tm = lookupDTypemap(p, "dtype", true))) { Printf(proxy_method_param_list, "%s", tm); } else { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(pt, 0)); } } else { Swig_warning(WARN_D_TYPEMAP_DDIRECTORIN_UNDEF, input_file, line_number, "No ddirectorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } } else { Swig_warning(WARN_D_TYPEMAP_IMTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } p = Getattr(p, "tmap:directorin:next"); } else { Swig_warning(WARN_D_TYPEMAP_DDIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap defined for argument %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); p = nextSibling(p); output_director = false; } } else { Swig_warning(WARN_D_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; p = nextSibling(p); } Delete(arg); Delete(c_decl); Delete(c_param_type); Delete(ln); } /* header declaration, start wrapper definition */ String *target; SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Add any exception specifications to the methods in the director class if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = NULL; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } Printf(w->def, "%s", SwigType_str(Getattr(p, "type"), 0)); Printf(declaration, "%s", SwigType_str(Getattr(p, "type"), 0)); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); // Finish the callback function declaraction. Printf(callback_def, "%s)", delegate_parms); Printf(callback_def, " {\n"); /* Emit the intermediate class's upcall to the actual class */ String *upcall = NewStringf("(cast(%s)dObject).%s(%s)", classname, symname, imcall_args); if (!is_void) { if ((tm = lookupDTypemap(n, "ddirectorout"))) { Replaceall(tm, "$dcall", upcall); Printf(callback_code, " return %s;\n", tm); } } else { Printf(callback_code, " %s;\n", upcall); } Printf(callback_code, "}\n"); Delete(upcall); if (!ignored_method) { if (!is_void) Printf(w->code, "jresult = (%s) ", c_ret_type); Printf(w->code, "swig_callback_%s(d_object%s);\n", overloaded_name, dcallback_call_args); if (!is_void) { String *jresult_str = NewString("jresult"); String *result_str = NewString("c_result"); /* Copy jresult into c_result... */ if ((tm = Swig_typemap_lookup("directorout", n, result_str, w))) { Replaceall(tm, "$input", jresult_str); Replaceall(tm, "$result", result_str); Printf(w->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s used in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Delete(jresult_str); Delete(result_str); } /* Marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout"))) { canThrow(n, "directorargout", p); Replaceall(tm, "$result", "jresult"); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } /* Terminate wrapper code */ Printf(w->code, "}\n"); if (!is_void) Printf(w->code, "return %s;", qualified_return); } Printf(w->code, "}"); // We expose virtual protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK && output_director) { if (!is_void) { Replaceall(w->code, "$null", qualified_return); } else { Replaceall(w->code, "$null", ""); } Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!ignored_method) Printv(director_dcallbacks_code, callback_def, callback_code, NIL); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } if (!ignored_method) { // Register the upcall method so that the callback registering code can // be written later. // We cannot directly use n here because its »type« attribute does not // the full return type any longer after Language::functionHandler has // returned. String *dp_return_type = getOutDtype(n); if (!dp_return_type) { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(returntype, 0)); dp_return_type = NewString(""); } String *member_name = Swig_name_member(getNSpace(), classname, overloaded_name); String *imclass_dmethod = NewStringf("SwigDirector_%s", member_name); UpcallData *udata = addUpcallMethod(imclass_dmethod, symname, decl, overloaded_name, dp_return_type, proxy_method_param_list); // Write the global callback function pointer on the C code. String *methid = Getattr(udata, "class_methodidx"); Printf(director_callback_typedefs, " typedef %s (* SWIG_Callback%s_t)", c_ret_type, methid); Printf(director_callback_typedefs, "(void *dobj%s);\n", callback_typedef_parms); Printf(director_callback_pointers, " SWIG_Callback%s_t swig_callback_%s;\n", methid, overloaded_name); // Write the type alias for the callback to the intermediary D module. String *proxy_callback_type = NewString(""); String *dirClassName = directorClassName(parent); Printf(proxy_callback_type, "%s_Callback%s", dirClassName, methid); Printf(im_dmodule_code, "alias extern(C) %s function(void*%s) %s;\n", proxy_callback_return_type, delegate_parms, proxy_callback_type); Delete(imclass_dmethod); Delete(member_name); Delete(dp_return_type); Delete(proxy_callback_type); Delete(dirClassName); } Delete(qualified_return); Delete(c_ret_type); Delete(declaration); Delete(callback_typedef_parms); Delete(delegate_parms); Delete(proxy_method_param_list); Delete(callback_def); Delete(callback_code); DelWrapper(w); return status; } /* --------------------------------------------------------------------------- * D::classDirectorConstructor() * --------------------------------------------------------------------------- */ virtual int classDirectorConstructor(Node *n) { Node *parent = parentNode(n); String *decl = Getattr(n, "decl");; String *supername = Swig_class_name(parent); String *dirclassname = directorClassName(parent); String *sub = NewString(""); Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms; int argidx = 0; /* Assign arguments to superclass's parameters, if not already done */ for (p = superparms; p; p = nextSibling(p)) { String *pname = Getattr(p, "name"); if (!pname) { pname = NewStringf("arg%d", argidx++); Setattr(p, "name", pname); } } // TODO: Is this copy needed? parms = CopyParmList(superparms); if (!Getattr(n, "defaultargs")) { /* constructor */ { String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, dirclassname, parms, 0); String *call = Swig_csuperclass_call(0, basetype, superparms); String *classtype = SwigType_namestr(Getattr(n, "name")); Printf(f_directors, "%s::%s : %s, %s {\n", dirclassname, target, call, Getattr(parent, "director:ctor")); Printf(f_directors, " swig_init_callbacks();\n"); Printf(f_directors, "}\n\n"); Delete(classtype); Delete(target); Delete(call); } /* constructor header */ { String *target = Swig_method_decl(0, decl, dirclassname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(supername); Delete(parms); Delete(dirclassname); return Language::classDirectorConstructor(n); } /* --------------------------------------------------------------------------- * D::classDirectorDefaultConstructor() * --------------------------------------------------------------------------- */ virtual int classDirectorDefaultConstructor(Node *n) { String *dirclassname = directorClassName(n); String *classtype = SwigType_namestr(Getattr(n, "name")); Wrapper *w = NewWrapper(); Printf(w->def, "%s::%s() : %s {", dirclassname, dirclassname, Getattr(n, "director:ctor")); Printf(w->code, "}\n"); Wrapper_print(w, f_directors); Printf(f_directors_h, " %s();\n", dirclassname); DelWrapper(w); Delete(classtype); Delete(dirclassname); return Language::classDirectorDefaultConstructor(n); } /* --------------------------------------------------------------------------- * D::classDirectorDestructor() * --------------------------------------------------------------------------- */ virtual int classDirectorDestructor(Node *n) { Node *current_class = getCurrentClass(); String *dirclassname = directorClassName(current_class); Wrapper *w = NewWrapper(); if (Getattr(n, "noexcept")) { Printf(f_directors_h, " virtual ~%s() noexcept;\n", dirclassname); Printf(w->def, "%s::~%s() noexcept {\n", dirclassname, dirclassname); } else if (Getattr(n, "throw")) { Printf(f_directors_h, " virtual ~%s() throw();\n", dirclassname); Printf(w->def, "%s::~%s() throw() {\n", dirclassname, dirclassname); } else { Printf(f_directors_h, " virtual ~%s();\n", dirclassname); Printf(w->def, "%s::~%s() {\n", dirclassname, dirclassname); } Printv(w->code, "}\n", NIL); Wrapper_print(w, f_directors); DelWrapper(w); Delete(dirclassname); return SWIG_OK; } /* --------------------------------------------------------------------------- * D::classDirectorEnd() * --------------------------------------------------------------------------- */ virtual int classDirectorEnd(Node *n) { int i; String *director_classname = directorClassName(n); Wrapper *w = NewWrapper(); if (Len(director_callback_typedefs) > 0) { Printf(f_directors_h, "\n%s", director_callback_typedefs); } Printf(f_directors_h, " void swig_connect_director(void* dobj"); Printf(w->def, "void %s::swig_connect_director(void* dobj", director_classname); Printf(w->code, "d_object = dobj;"); for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *methid = Getattr(udata, "class_methodidx"); String *overname = Getattr(udata, "overname"); Printf(f_directors_h, ", SWIG_Callback%s_t callback%s", methid, overname); Printf(w->def, ", SWIG_Callback%s_t callback_%s", methid, overname); Printf(w->code, "swig_callback_%s = callback_%s;\n", overname, overname); } Printf(f_directors_h, ");\n"); Printf(w->def, ") {"); Printf(f_directors_h, "\nprivate:\n"); Printf(f_directors_h, " void swig_init_callbacks();\n"); Printf(f_directors_h, " void *d_object;\n"); if (Len(director_callback_pointers) > 0) { Printf(f_directors_h, "%s", director_callback_pointers); } Printf(f_directors_h, "};\n\n"); Printf(w->code, "}\n\n"); Printf(w->code, "void %s::swig_init_callbacks() {\n", director_classname); for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *overname = Getattr(udata, "overname"); Printf(w->code, "swig_callback_%s = 0;\n", overname); } Printf(w->code, "}"); Wrapper_print(w, f_directors); DelWrapper(w); return Language::classDirectorEnd(n); } /* --------------------------------------------------------------------------- * D::classDirectorDisown() * --------------------------------------------------------------------------- */ virtual int classDirectorDisown(Node *n) { (void) n; return SWIG_OK; } /* --------------------------------------------------------------------------- * D::replaceSpecialVariables() * --------------------------------------------------------------------------- */ virtual void replaceSpecialVariables(String *method, String *tm, Parm *parm) { (void)method; SwigType *type = Getattr(parm, "type"); // Just assume that this goes to the proxy class, we cannot know. replaceClassname(tm, type); } protected: /* --------------------------------------------------------------------------- * D::extraDirectorProtectedCPPMethodsRequired() * --------------------------------------------------------------------------- */ virtual bool extraDirectorProtectedCPPMethodsRequired() const { return false; } private: /* --------------------------------------------------------------------------- * D::writeImDModuleFunction() * * Writes a function declaration for the given (C) wrapper function to the * intermediary D module. * * d_name - The name the function in the intermediary D module will get. * return type - The return type of the function in the C wrapper. * parameters - The parameter list of the C wrapper function. * wrapper_function_name - The name of the exported function in the C wrapper * (usually d_name prefixed by »D_«). * --------------------------------------------------------------------------- */ void writeImDModuleFunction(const_String_or_char_ptr d_name, const_String_or_char_ptr return_type, const_String_or_char_ptr parameters, const_String_or_char_ptr wrapper_function_name) { // TODO: Add support for static linking here. Printf(im_dmodule_code, "SwigExternC!(%s function%s) %s;\n", return_type, parameters, d_name); Printv(wrapper_loader_bind_code, wrapper_loader_bind_command, NIL); Replaceall(wrapper_loader_bind_code, "$function", d_name); Replaceall(wrapper_loader_bind_code, "$symbol", wrapper_function_name); } /* --------------------------------------------------------------------------- * D::writeProxyClassFunction() * * Creates a D proxy function for a C++ function in the wrapped class. Used * for both static and non-static C++ class functions. * * The Node must contain two extra attributes. * - "proxyfuncname": The name of the D proxy function. * - "imfuncname": The corresponding function in the intermediary D module. * --------------------------------------------------------------------------- */ void writeProxyClassFunction(Node *n, bool super = false) { SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *intermediary_function_name = Getattr(n, "imfuncname"); String *proxy_function_name = Getattr(n, "proxyfuncname"); String *tm; Parm *p; int i; String *imcall = NewString(""); String *function_code = NewString(""); bool setter_flag = false; String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); // Wrappers not wanted for some methods where the parameters cannot be // overloaded in D. if (Getattr(n, "overload:ignore")) return; // Don't generate proxy method for additional explicitcall method used in // directors. if (GetFlag(n, "explicitcall")) return; // RESEARCH: What is this good for? if (l) { if (SwigType_type(Getattr(l, "type")) == T_VOID) { l = nextSibling(l); } } /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("in", l, NULL); Swig_typemap_attach_parms("dtype", l, NULL); Swig_typemap_attach_parms("din", l, NULL); // Get return types. String *return_type = getOutDtype(n); if (!return_type) { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(t, 0)); return_type = NewString(""); } if (wrapping_member_flag) { // Check if this is a setter method for a public member. const String *setter_name = Swig_name_set(getNSpace(), Swig_name_member(0, proxy_class_name, variable_name)); if (Cmp(Getattr(n, "sym:name"), setter_name) == 0) { setter_flag = true; } } // We move the modifiers after the parameter list // as we need the D parametes to catch D override of functions String *param_function_code = NewString(""); if (super) { Printf(imcall, "super.$funcname("); } else { // Write the wrapper function call up to the parameter list. Printv(imcall, im_dmodule_fq_name, ".$imfuncname(", NIL); if (!static_flag) { Printf(imcall, "cast(void*)swigCPtr"); } } String *proxy_param_types = NewString(""); // Write the parameter list for the proxy function declaration and the // wrapper function call. emit_mark_varargs(l); int gencomma = !static_flag && !super; for (i = 0, p = l; p; i++) { // Ignored varargs. if (checkAttribute(p, "varargs:ignore", "1")) { Setattr(p, "d:type", NewString("")); p = nextSibling(p); continue; } // Ignored parameters. if (checkAttribute(p, "tmap:in:numinputs", "0")) { Setattr(p, "d:type", NewString("")); p = Getattr(p, "tmap:in:next"); continue; } // Ignore the 'this' argument for variable wrappers. if (!(variable_wrapper_flag && i == 0)) { String *param_name = makeParameterName(n, p, i, setter_flag); SwigType *pt = Getattr(p, "type"); // Write the wrapper function call argument. { if (gencomma) { Printf(imcall, ", "); } if ((tm = lookupDTypemap(p, "din", true))) { Replaceall(tm, "$dinput", param_name); String *pre = Getattr(p, "tmap:din:pre"); if (pre) { replaceClassname(pre, pt); Replaceall(pre, "$dinput", param_name); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:din:post"); if (post) { replaceClassname(post, pt); Replaceall(post, "$dinput", param_name); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:din:terminator"); if (terminator) { replaceClassname(terminator, pt); Replaceall(terminator, "$dinput", param_name); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_D_TYPEMAP_DIN_UNDEF, input_file, line_number, "No din typemap defined for %s\n", SwigType_str(pt, 0)); } } // Write the D proxy function parameter. { String *proxy_type = NewString(""); if ((tm = lookupDTypemap(p, "dtype", true))) { const String *inattributes = Getattr(p, "tmap:dtype:inattributes"); Printf(proxy_type, "%s%s", inattributes ? inattributes : empty_string, tm); { int ln = Len(package); /* If proxy_type uses the package name, it must be larger */ if (Len(proxy_type) > ln && Strncmp(package, proxy_type, ln) == 0) { Setattr(p, "d:type", NewString(Char(proxy_type) + ln)); } else { Setattr(p, "d:type", Copy(proxy_type)); } } } else { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma >= 2) { Printf(param_function_code, ", "); Printf(proxy_param_types, ", "); } gencomma = 2; Printf(param_function_code, "%s %s", proxy_type, param_name); Append(proxy_param_types, proxy_type); Delete(proxy_type); } Delete(param_name); } p = Getattr(p, "tmap:in:next"); } // Complete the function declaration up to the parameter list. // Write function modifiers. { const String *mods_override = Getattr(n, "feature:d:methodmodifiers"); bool isPrivate = false; if (mods_override) { isPrivate = Strcmp(mods_override, private_string) == 0; } else { mods_override = is_public(n) ? public_string : protected_string; } String *modifiers = Copy(mods_override); Setattr(n, "dmodify", Copy(mods_override)); /* private function are never override */ if (super || (!isPrivate && isDOverride(n, l))) { Printf(modifiers, " override"); } Chop(modifiers); if (static_flag) { Printf(modifiers, " static"); } Printf(function_code, "%s ", modifiers); Delete(modifiers); } Printf(function_code, "%s %s(", return_type, proxy_function_name); // Add Body the parameter list part after the modifiers Append(function_code, param_function_code); Printf(imcall, ")"); Printf(function_code, ") "); if (wrapping_member_flag) { Printf(function_code, "@property "); } if (wrapMemberFunctionAsDConst(n)) { Printf(function_code, "const "); } if (super) { Replaceall(imcall, "$funcname", proxy_function_name); if (!Cmp(return_type, "void")) { tm = NewString("{\n $imcall;\n}"); } else { tm = NewString("{\n return $imcall;\n}"); } Replaceall(tm, "$imcall", imcall); } else { // Lookup the code used to convert the wrapper return value to the proxy // function return type. if ((tm = lookupDTypemap(n, "dout"))) { replaceExcode(n, tm, "dout", n); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code || is_post_code || is_terminator_code) { if (is_post_code) { Insert(tm, 0, "\n try "); Printv(tm, " finally {\n", post_code, "\n }", NIL); } else { Insert(tm, 0, "\n "); } if (is_pre_code) { Insert(tm, 0, pre_code); Insert(tm, 0, "\n"); } if (is_terminator_code) { Printv(tm, "\n", terminator_code, NIL); } Insert(tm, 0, "{"); Printv(tm, "}", NIL); } if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); replaceClassname(tm, t); // For director methods: generate code to selectively make a normal // polymorphic call or an explicit method call. Needed to prevent infinite // recursion when calling director methods. Node *explicit_n = Getattr(n, "explicitcallnode"); if (explicit_n && Swig_directorclass(getCurrentClass())) { String *ex_overloaded_name = getOverloadedName(explicit_n); String *ex_intermediary_function_name = Swig_name_member(getNSpace(), proxy_class_name, ex_overloaded_name); String *ex_imcall = Copy(imcall); Replaceall(ex_imcall, "$imfuncname", ex_intermediary_function_name); Replaceall(imcall, "$imfuncname", intermediary_function_name); String *override_use_const = NewString(""); if (wrapMemberFunctionAsDConst(n)) { Printf(override_use_const, "Const"); } String *excode = NewString(""); if (!Cmp(return_type, "void")) Printf(excode, "if (swigIsMethodOverridden%s!(%s delegate(%s), %s function(%s), %s)()) %s; else %s", override_use_const, return_type, proxy_param_types, return_type, proxy_param_types, proxy_function_name, ex_imcall, imcall); else Printf(excode, "((swigIsMethodOverridden%s!(%s delegate(%s), %s function(%s), %s)()) ? %s : %s)", override_use_const, return_type, proxy_param_types, return_type, proxy_param_types, proxy_function_name, ex_imcall, imcall); Clear(imcall); Printv(imcall, excode, NIL); Delete(ex_overloaded_name); Delete(excode); } else { Replaceall(imcall, "$imfuncname", intermediary_function_name); } Replaceall(tm, "$imfuncname", intermediary_function_name); Replaceall(tm, "$imcall", imcall); } else { Swig_warning(WARN_D_TYPEMAP_DOUT_UNDEF, input_file, line_number, "No dout typemap defined for %s\n", SwigType_str(t, 0)); } } Delete(proxy_param_types); // The whole function body is now in stored tm (if there was a matching type // map, of course), so simply append it to the code buffer. The braces are // included in the typemap. Printv(function_code, tm, NIL); // Write function code buffer to the class code. Printv(proxy_class_body_code, "\n", function_code, "\n", NIL); Delete(tm); Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(function_code); Delete(return_type); Delete(imcall); } /* --------------------------------------------------------------------------- * D::writeProxyDModuleFunction() * --------------------------------------------------------------------------- */ void writeProxyDModuleFunction(Node *n) { SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *imcall = NewString(""); String *function_code = NewString(""); int num_arguments = 0; String *overloaded_name = getOverloadedName(n); String *func_name = NULL; String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); // RESEARCH: What is this good for? if (l) { if (SwigType_type(Getattr(l, "type")) == T_VOID) { l = nextSibling(l); } } /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("dtype", l, NULL); Swig_typemap_attach_parms("din", l, NULL); /* Get return types */ String *return_type = getOutDtype(n); if (!return_type) { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(t, 0)); return_type = NewString(""); } /* Change function name for global variables */ if (global_variable_flag) { // RESEARCH: Is the Copy() needed here? func_name = Copy(variable_name); } else { func_name = Copy(Getattr(n, "sym:name")); } /* Start generating the function */ const String *outattributes = Getattr(n, "tmap:dtype:outattributes"); if (outattributes) Printf(function_code, " %s\n", outattributes); const String *methodmods = Getattr(n, "feature:d:methodmodifiers"); // TODO: Check if is_public(n) could possibly make any sense here // (private global functions would be useless anyway?). methodmods = methodmods ? methodmods : empty_string; Printf(function_code, "\n%s%s %s(", methodmods, return_type, func_name); Printv(imcall, im_dmodule_fq_name, ".", overloaded_name, "(", NIL); /* Get number of required and total arguments */ num_arguments = emit_num_arguments(l); int gencomma = 0; /* Output each parameter */ for (i = 0, p = l; i < num_arguments; i++) { /* Ignored parameters */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); // Get the D parameter type. if ((tm = lookupDTypemap(p, "dtype", true))) { const String *inattributes = Getattr(p, "tmap:dtype:inattributes"); Printf(param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_D_TYPEMAP_DTYPE_UNDEF, input_file, line_number, "No dtype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); const bool generating_setter = global_variable_flag || wrapping_member_flag; String *arg = makeParameterName(n, p, i, generating_setter); // Get the D code to convert the parameter value to the type used in the // wrapper D module. if ((tm = lookupDTypemap(p, "din", true))) { Replaceall(tm, "$dinput", arg); String *pre = Getattr(p, "tmap:din:pre"); if (pre) { replaceClassname(pre, pt); Replaceall(pre, "$dinput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:din:post"); if (post) { replaceClassname(post, pt); Replaceall(post, "$dinput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:din:terminator"); if (terminator) { replaceClassname(terminator, pt); Replaceall(terminator, "$dinput", arg); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_D_TYPEMAP_DIN_UNDEF, input_file, line_number, "No din typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to module class function */ if (gencomma >= 2) Printf(function_code, ", "); gencomma = 2; Printf(function_code, "%s %s", param_type, arg); p = Getattr(p, "tmap:in:next"); Delete(arg); Delete(param_type); } Printf(imcall, ")"); Printf(function_code, ") "); if (global_variable_flag) { Printf(function_code, "@property "); } // Lookup the code used to convert the wrapper return value to the proxy // function return type. if ((tm = lookupDTypemap(n, "dout"))) { replaceExcode(n, tm, "dout", n); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code || is_post_code || is_terminator_code) { if (is_post_code) { Insert(tm, 0, "\n try "); Printv(tm, " finally {\n", post_code, "\n }", NIL); } else { Insert(tm, 0, "\n "); } if (is_pre_code) { Insert(tm, 0, pre_code); Insert(tm, 0, "\n"); } if (is_terminator_code) { Printv(tm, "\n", terminator_code, NIL); } Insert(tm, 0, " {"); Printf(tm, "\n}"); } if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); replaceClassname(tm, t); Replaceall(tm, "$imfuncname", overloaded_name); Replaceall(tm, "$imcall", imcall); } else { Swig_warning(WARN_D_TYPEMAP_DOUT_UNDEF, input_file, line_number, "No dout typemap defined for %s\n", SwigType_str(t, 0)); } // The whole function code is now stored in tm (if there was a matching // type map, of course), so simply append it to the code buffer. Printf(function_code, "%s\n", tm ? (const String *) tm : empty_string); Printv(proxyCodeBuffer(getNSpace()), function_code, NIL); Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(function_code); Delete(return_type); Delete(imcall); Delete(func_name); } /* --------------------------------------------------------------------------- * D::writeProxyClassAndUpcasts() * * Collects all the code fragments generated by the handler function while * traversing the tree from the proxy_class_* variables and writes the * class definition (including any epilogue code) to proxy_class_code. * * Also writes the upcast function to the wrapper layer when processing a * derived class. * * Inputs: * n – The class node currently processed. * --------------------------------------------------------------------------- */ void writeProxyClassAndUpcasts(Node *n) { SwigType *typemap_lookup_type = Getattr(n, "classtypeobj"); /* * Handle inheriting from D and C++ classes. */ String *c_classname = Getattr(n, "name"); String *c_baseclassname = NULL; Node *basenode = NULL; String *baseclass = NULL; SwigType *bsmart = 0; // Inheritance from pure D classes. Node *attributes = NewHash(); const String *pure_baseclass = lookupCodeTypemap(n, "dbase", typemap_lookup_type, WARN_NONE, attributes); bool purebase_replace = GetFlag(attributes, "tmap:dbase:replace") ? true : false; bool purebase_notderived = GetFlag(attributes, "tmap:dbase:notderived") ? true : false; Delete(attributes); // C++ inheritance. if (!purebase_replace) { List *baselist = Getattr(n, "bases"); if (baselist) { Iterator base = First(baselist); while (base.item) { if (!GetFlag(base.item, "feature:ignore")) { SwigType *baseclassname = Getattr(base.item, "name"); if (!c_baseclassname) { basenode = base.item; String *name = createProxyName(baseclassname); if (name) { c_baseclassname = baseclassname; baseclass = name; bsmart = Getattr(base.item, "smart"); } } else { /* Warn about multiple inheritance for additional base class(es) */ String *proxyclassname = Getattr(n, "classtypeobj"); Swig_warning(WARN_D_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Base %s of class %s ignored: multiple inheritance is not supported in D.\n", SwigType_namestr(baseclassname), SwigType_namestr(proxyclassname)); } } base = Next(base); } } } bool derived = baseclass != NULL; if (derived && purebase_notderived) { pure_baseclass = empty_string; } const String *wanted_base = baseclass ? baseclass : pure_baseclass; if (purebase_replace) { wanted_base = pure_baseclass; derived = false; basenode = NULL; baseclass = NULL; if (purebase_notderived) { Swig_error(Getfile(n), Getline(n), "The dbase typemap for proxy %s must contain just one of the 'replace' or 'notderived' attributes.\n", typemap_lookup_type); } } else if (baseclass && Len(pure_baseclass) > 0) { Swig_warning(WARN_D_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, base class %s ignored. Multiple inheritance is not supported in D. " "Perhaps you need one of the 'replace' or 'notderived' attributes in the dbase typemap?\n", typemap_lookup_type, pure_baseclass); } // Add code to do C++ casting to base class (only for classes in an inheritance hierarchy) if (derived) { writeClassUpcast(n, bsmart, proxy_class_name, c_classname, c_baseclassname); } /* * Write needed imports. */ // If this class is derived from a C++ class, we need to have the D class // generated for it in scope. if (derived) { requireDType(Getattr(basenode, "sym:nspace"), Getattr(basenode, "sym:name")); } // Write any custom import statements to the proxy module header. const String *imports = lookupCodeTypemap(n, "dimports", typemap_lookup_type, WARN_NONE); if (Len(imports) > 0) { String* imports_trimmed = Copy(imports); Chop(imports_trimmed); replaceImportTypeMacros(imports_trimmed); Printv(proxy_class_imports, imports_trimmed, "\n", NIL); Delete(imports_trimmed); } /* * Write the proxy class header. */ // Class modifiers. const String *modifiers = lookupCodeTypemap(n, "dclassmodifiers", typemap_lookup_type, WARN_D_TYPEMAP_CLASSMOD_UNDEF); // User-defined interfaces. const String *interfaces = lookupCodeTypemap(n, derived ? "dinterfaces_derived" : "dinterfaces", typemap_lookup_type, WARN_NONE); Printv(proxy_class_code, "\n", modifiers, " $dclassname", (*Char(wanted_base) || *Char(interfaces)) ? " : " : "", wanted_base, (*Char(wanted_base) && *Char(interfaces)) ? ", " : "", interfaces, " {", NIL); /* * Write the proxy class body. */ String* body = NewString(""); // Default class body. const String *dbody; if (derived) { dbody = lookupCodeTypemap(n, "dbody_derived", typemap_lookup_type, WARN_D_TYPEMAP_DBODY_UNDEF); } else { dbody = lookupCodeTypemap(n, "dbody", typemap_lookup_type, WARN_D_TYPEMAP_DBODY_UNDEF); } Printv(body, dbody, NIL); // Destructor and dispose(). // If the C++ destructor is accessible (public), it is wrapped by the // dispose() method which is also called by the emitted D constructor. If it // is not accessible, no D destructor is written and the generated dispose() // method throws an exception. // This enables C++ classes with protected or private destructors to be used // in D as it would be used in C++ (GC finalization is a no-op then because // of the empty D destructor) while preventing usage in »scope« variables. // The method name for the dispose() method is specified in a typemap // attribute called »methodname«. const String *tm = NULL; const String *dispose_methodname; const String *dispose_methodmodifiers; const String *dispose_parameters; attributes = NewHash(); if (derived) { tm = lookupCodeTypemap(n, "ddispose_derived", typemap_lookup_type, WARN_NONE, attributes); dispose_methodname = Getattr(attributes, "tmap:ddispose_derived:methodname"); dispose_methodmodifiers = Getattr(attributes, "tmap:ddispose_derived:methodmodifiers"); dispose_parameters = Getattr(attributes, "tmap:ddispose_derived:parameters"); } else { tm = lookupCodeTypemap(n, "ddispose", typemap_lookup_type, WARN_NONE, attributes); dispose_methodname = Getattr(attributes, "tmap:ddispose:methodname"); dispose_methodmodifiers = Getattr(attributes, "tmap:ddispose:methodmodifiers"); dispose_parameters = Getattr(attributes, "tmap:ddispose:parameters"); } if (tm && *Char(tm)) { if (!dispose_methodname) { Swig_error(Getfile(n), Getline(n), "No methodname attribute defined in the ddispose%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name); } if (!dispose_methodmodifiers) { Swig_error(Getfile(n), Getline(n), "No methodmodifiers attribute defined in ddispose%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name); } if (!dispose_parameters) dispose_parameters = empty_string; } if (tm) { // Write the destructor if the C++ one is accessible. if (*Char(destructor_call)) { Printv(body, lookupCodeTypemap(n, "ddestructor", typemap_lookup_type, WARN_NONE), NIL); } // Write the dispose() method. String *dispose_code = NewString(""); Printv(dispose_code, tm, NIL); if (*Char(destructor_call)) { Replaceall(dispose_code, "$imcall", destructor_call); } else { Replaceall(dispose_code, "$imcall", "throw new object.Exception(\"C++ destructor does not have public access\")"); } if (*Char(dispose_code)) { Printv(body, "\n", NIL); const String *methodmods = Getattr(n, "destructmethodmodifiers"); if (methodmods) Printv(body, methodmods, NIL); else Printv(body, dispose_methodmodifiers, (derived ? " override" : ""), NIL); Printv(body, " void ", dispose_methodname, "(", dispose_parameters, ") ", dispose_code, "\n", NIL); } } if (Swig_directorclass(n)) { // If directors are enabled for the current class, generate the // director connect helper function which is called from the constructor // and write it to the class body. writeDirectorConnectProxy(n); } // Write all constants and enumerations first to prevent forward reference // errors. Printv(body, proxy_class_enums_code, NIL); // Write the code generated in other methods to the class body. Printv(body, proxy_class_body_code, NIL); // Append extra user D code to the class body. Printv(body, lookupCodeTypemap(n, "dcode", typemap_lookup_type, WARN_NONE), "\n", NIL); // Write the class body and the curly bracket closing the class definition // to the proxy module. indentCode(body); Replaceall(body, "$dbaseclass", baseclass); Printv(proxy_class_code, body, "\n}\n", NIL); Delete(body); // Write the epilogue code if there is any. Printv(proxy_class_code, proxy_class_epilogue_code, NIL); } /* --------------------------------------------------------------------------- * D::writeClassUpcast() * --------------------------------------------------------------------------- */ void writeClassUpcast(Node *n, SwigType *bsmart, const String *d_class_name, SwigType *c_classname, SwigType *c_baseclassname) { SwigType *smart = Getattr(n, "smart"); String *upcast_name = Swig_name_member(getNSpace(), d_class_name, (smart != 0 ? "SmartPtrUpcast" : "Upcast")); String *upcast_wrapper_name = Swig_name_wrapper(upcast_name); writeImDModuleFunction(upcast_name, "void*", "(void* objectRef)", upcast_wrapper_name); String *classname = SwigType_namestr(c_classname); String *baseclassname = SwigType_namestr(c_baseclassname); if (smart) { if (bsmart) { String *smartnamestr = SwigType_namestr(smart); String *bsmartnamestr = SwigType_namestr(bsmart); Printv(upcasts_code, "SWIGEXPORT ", bsmartnamestr, " * ", upcast_wrapper_name, "(", smartnamestr, " *objectRef) {\n", " return objectRef ? new ", bsmartnamestr, "(*objectRef) : 0;\n" "}\n", "\n", NIL); Delete(bsmartnamestr); Delete(smartnamestr); } } else { Printv(upcasts_code, "SWIGEXPORT ", baseclassname, " * ", upcast_wrapper_name, "(", classname, " *objectRef) {\n", " return (", baseclassname, " *)objectRef;\n" "}\n", "\n", NIL); } Replaceall(upcasts_code, "$cclass", classname); Replaceall(upcasts_code, "$cbaseclass", baseclassname); Delete(baseclassname); Delete(classname); Delete(upcast_wrapper_name); Delete(upcast_name); } /* --------------------------------------------------------------------------- * D::writeTypeWrapperClass() * --------------------------------------------------------------------------- */ void writeTypeWrapperClass(String *classname, SwigType *type) { Node *n = NewHash(); Setfile(n, input_file); Setline(n, line_number); assertClassNameValidity(classname); String* imports_target; String* code_target; File *class_file = NULL; if (split_proxy_dmodule) { String *filename = NewStringf("%s%s.d", dmodule_directory, classname); class_file = NewFile(filename, "w", SWIG_output_files()); if (!class_file) { FileErrorDisplay(filename); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filename)); Delete(filename); emitBanner(class_file); Printf(class_file, "module %s%s;\n", package, classname); Printf(class_file, "\nstatic import %s;\n", im_dmodule_fq_name); imports_target = NewString(""); code_target = NewString(""); } else { imports_target = proxyImportsBuffer(0); code_target = proxyCodeBuffer(0); } // Import statements. const String *imports = lookupCodeTypemap(n, "dimports", type, WARN_NONE); if (Len(imports) > 0) { String *imports_trimmed = Copy(imports); Chop(imports_trimmed); replaceImportTypeMacros(imports_trimmed); Printv(imports_target, imports_trimmed, "\n", NIL); Delete(imports_trimmed); } // Pure D baseclass and interfaces (no C++ inheritance possible. const String *pure_baseclass = lookupCodeTypemap(n, "dbase", type, WARN_NONE); const String *pure_interfaces = lookupCodeTypemap(n, "dinterfaces", type, WARN_NONE); // Emit the class. Printv(code_target, "\n", lookupCodeTypemap(n, "dclassmodifiers", type, WARN_D_TYPEMAP_CLASSMOD_UNDEF), " $dclassname", (*Char(pure_baseclass) || *Char(pure_interfaces)) ? " : " : "", pure_baseclass, ((*Char(pure_baseclass)) && *Char(pure_interfaces)) ? ", " : "", pure_interfaces, " {", NIL); String* body = NewString(""); Printv(body, lookupCodeTypemap(n, "dbody", type, WARN_D_TYPEMAP_DBODY_UNDEF), lookupCodeTypemap(n, "dcode", type, WARN_NONE), NIL); indentCode(body); Printv(code_target, body, "\n}\n", NIL); Delete(body); Replaceall(code_target, "$dclassname", classname); if (split_proxy_dmodule) { Printv(class_file, imports_target, NIL); Delete(imports_target); replaceModuleVariables(code_target); Printv(class_file, code_target, NIL); Delete(code_target); Delete(class_file); } Delete(n); } /* --------------------------------------------------------------------------- * D::writeDirectorConnectProxy(Node *classNode) * * Writes the helper method which registers the director callbacks by calling * the director connect function from the D side to the proxy class. * --------------------------------------------------------------------------- */ void writeDirectorConnectProxy(Node* classNode) { String *dirClassName = directorClassName(classNode); String *connect_name = Swig_name_member(getNSpace(), proxy_class_name, "director_connect"); Printf(proxy_class_body_code, "\nprivate void swigDirectorConnect() {\n"); int i; for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *method = Getattr(udata, "method"); String *overloaded_name = Getattr(udata, "overname"); String *return_type = Getattr(udata, "return_type"); String *param_list = Getattr(udata, "param_list"); String *methid = Getattr(udata, "class_methodidx"); Printf(proxy_class_body_code, " %s.%s_Callback%s callback%s;\n", im_dmodule_fq_name, dirClassName, methid, methid); Printf(proxy_class_body_code, " if (swigIsMethodOverridden!(%s delegate(%s), %s function(%s), %s)()) {\n", return_type, param_list, return_type, param_list, method); Printf(proxy_class_body_code, " callback%s = &swigDirectorCallback_%s_%s;\n", methid, proxy_class_name, overloaded_name); Printf(proxy_class_body_code, " }\n\n"); } Printf(proxy_class_body_code, " %s.%s(cast(void*)swigCPtr, cast(void*)this", im_dmodule_fq_name, connect_name); for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *methid = Getattr(udata, "class_methodidx"); Printf(proxy_class_body_code, ", callback%s", methid); } Printf(proxy_class_body_code, ");\n"); Printf(proxy_class_body_code, "}\n"); // Helper function to determine if a method has been overridden in a // subclass of the wrapped class. If not, we just pass null to the // director_connect_function since the method from the C++ class should // be called as usual (see above). // Only emit it if the proxy class has at least one method. if (first_class_dmethod < curr_class_dmethod) { Printf(proxy_class_body_code, "\n"); Printf(proxy_class_body_code, "private bool swigIsMethodOverridden(DelegateType, FunctionType, alias fn)() {\n"); Printf(proxy_class_body_code, " DelegateType dg = &fn;\n"); Printf(proxy_class_body_code, " return dg.funcptr != SwigNonVirtualAddressOf!(FunctionType, fn);\n"); Printf(proxy_class_body_code, "}\n"); Printf(proxy_class_body_code, "private bool swigIsMethodOverriddenConst(DelegateType, FunctionType, alias fn)() inout {\n"); Printf(proxy_class_body_code, " DelegateType dg = &fn;\n"); Printf(proxy_class_body_code, " return dg.funcptr != SwigNonVirtualAddressOf!(FunctionType, fn);\n"); Printf(proxy_class_body_code, "}\n"); Printf(proxy_class_body_code, "\n"); Printf(proxy_class_body_code, "private static Function SwigNonVirtualAddressOf(Function, alias fn)() {\n"); Printf(proxy_class_body_code, " return cast(Function) &fn;\n"); Printf(proxy_class_body_code, "}\n"); } if (Len(director_dcallbacks_code) > 0) { Printv(proxy_class_epilogue_code, director_dcallbacks_code, NIL); } Delete(director_callback_typedefs); director_callback_typedefs = NULL; Delete(director_callback_pointers); director_callback_pointers = NULL; Delete(director_dcallbacks_code); director_dcallbacks_code = NULL; Delete(dirClassName); Delete(connect_name); } /* --------------------------------------------------------------------------- * D::writeDirectorSuperFunctions() * * Writes super methods for protected methods in virtual table * which are not implemented in class. * So the director connect function can call them. * As function outside the class, can call module protected methods * but not protected of clases outside this module! * --------------------------------------------------------------------------- */ // TODO WORK void writeDirectorSuperFunctions(Node *n) { if (!Swig_directorclass(n)) return; Node *vtable = Getattr(n, "vtable"); int len = Len(vtable); for (int i = 0; i < len; i++) { Node *item = Getitem(vtable, i); if (GetFlag(item, "director")) { Node *method = Getattr(item, "methodNode"); Node *p = parentNode(method); if (p && p != n && is_protected(method)) { const String *nodeType = nodeType(method); if (Strcmp(nodeType, "cdecl") == 0) { Setattr(method, "proxyfuncname", Getattr(method, "sym:name")); writeProxyClassFunction(method, true); } } } } } /* --------------------------------------------------------------------------- * D::writeDirectorConnectWrapper() * * Writes the director connect function and the corresponding declaration to * the C++ wrapper respectively the D wrapper. * --------------------------------------------------------------------------- */ void writeDirectorConnectWrapper(Node *n) { if (!Swig_directorclass(n)) return; // Output the director connect method. String *norm_name = SwigType_namestr(Getattr(n, "name")); String *connect_name = Swig_name_member(getNSpace(), proxy_class_name, "director_connect"); String *dirClassName = directorClassName(n); Wrapper *code_wrap; Printv(wrapper_loader_bind_code, wrapper_loader_bind_command, NIL); Replaceall(wrapper_loader_bind_code, "$function", connect_name); Replaceall(wrapper_loader_bind_code, "$symbol", Swig_name_wrapper(connect_name)); Printf(im_dmodule_code, "extern(C) void function(void* cObject, void* dObject"); code_wrap = NewWrapper(); Printf(code_wrap->def, "SWIGEXPORT void D_%s(void *objarg, void *dobj", connect_name); Printf(code_wrap->code, " %s *obj = (%s *)objarg;\n", norm_name, norm_name); Printf(code_wrap->code, " %s *director = static_cast<%s *>(obj);\n", dirClassName, dirClassName); Printf(code_wrap->code, " director->swig_connect_director(dobj"); for (int i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *methid = Getattr(udata, "class_methodidx"); Printf(code_wrap->def, ", %s::SWIG_Callback%s_t callback%s", dirClassName, methid, methid); Printf(code_wrap->code, ", callback%s", methid); Printf(im_dmodule_code, ", %s_Callback%s callback%s", dirClassName, methid, methid); } Printf(code_wrap->def, ") {\n"); Printf(code_wrap->code, ");\n"); Printf(im_dmodule_code, ") %s;\n", connect_name); Printf(code_wrap->code, "}\n"); Wrapper_print(code_wrap, f_wrappers); DelWrapper(code_wrap); Delete(connect_name); Delete(dirClassName); } /* --------------------------------------------------------------------------- * D::requireDType() * * If the given type is not already in scope in the current module, adds an * import statement for it. The name is considered relative to the global root * package if one is set. * * This is only used for dependencies created in generated code, user- * (i.e. typemap-) specified import statements are handled separately. * --------------------------------------------------------------------------- */ void requireDType(const String *nspace, const String *symname) { String *dmodule = createModuleName(nspace, symname); if (!inProxyModule(dmodule)) { String *import = createImportStatement(dmodule); Append(import, "\n"); if (is_wrapping_class()) { addImportStatement(proxy_class_imports, import); } else { addImportStatement(proxyImportsBuffer(getNSpace()), import); } Delete(import); } Delete(dmodule); } /* --------------------------------------------------------------------------- * D::addImportStatement() * * Adds the given import statement to the given list of import statements if * there is no statement importing that module present yet. * --------------------------------------------------------------------------- */ void addImportStatement(String *target, const String *import) const { char *position = Strstr(target, import); if (position) { // If the import statement has been found in the target string, we have to // check if the previous import was static, which would lead to problems // if this import is not. // Thus, we check if the seven characters in front of the occurrence are // »static «. If the import string passed is also static, the checks fail // even if the found statement is also static because the last seven // characters would be part of the previous import statement then. if (position - Char(target) < 7) { return; } if (strncmp(position - 7, "static ", 7)) { return; } } Printv(target, import, NIL); } /* --------------------------------------------------------------------------- * D::createImportStatement() * * Creates a string containing an import statement for the given module. * --------------------------------------------------------------------------- */ String *createImportStatement(const String *dmodule_name, bool static_import = true) const { if (static_import) { return NewStringf("static import %s%s;", package, dmodule_name); } else { return NewStringf("import %s%s;", package, dmodule_name); } } /* --------------------------------------------------------------------------- * D::inProxyModule() * * Determines if the specified proxy type is declared in the currently * processed proxy D module. * * This function is used to determine if fully qualified type names have to * be used (package, module and type name). If the split proxy mode is not * used, this solely depends on whether the type is in the current namespace. * --------------------------------------------------------------------------- */ bool inProxyModule(const String *type_name) const { if (!split_proxy_dmodule) { String *nspace = createOuterNamespaceNames(type_name); // Check if strings are either both null (no namespace) or are both // non-null and have the same contents. Cannot use Strcmp for this // directly because of its strange way of handling the case where only // one argument is 0 ("<"). bool result = !nspace && !getNSpace(); if (nspace && getNSpace()) result = (Strcmp(nspace, getNSpace()) == 0); Delete(nspace); return result; } if (!is_wrapping_class()) { return false; } return (Strcmp(proxy_class_qname, type_name) == 0); } /* --------------------------------------------------------------------------- * D::addUpcallMethod() * * Adds new director upcall signature. * --------------------------------------------------------------------------- */ UpcallData *addUpcallMethod(String *imclass_method, String *class_method, String *decl, String *overloaded_name, String *return_type, String *param_list) { String *key = NewStringf("%s|%s", imclass_method, decl); ++curr_class_dmethod; String *class_methodidx = NewStringf("%d", n_dmethods - first_class_dmethod); n_dmethods++; Hash *new_udata = NewHash(); Append(dmethods_seq, new_udata); Setattr(dmethods_table, key, new_udata); Setattr(new_udata, "method", Copy(class_method)); Setattr(new_udata, "class_methodidx", class_methodidx); Setattr(new_udata, "decl", Copy(decl)); Setattr(new_udata, "overname", Copy(overloaded_name)); Setattr(new_udata, "return_type", Copy(return_type)); Setattr(new_udata, "param_list", Copy(param_list)); Delete(key); return new_udata; } /* --------------------------------------------------------------------------- * D::assertClassNameValidity() * --------------------------------------------------------------------------- */ void assertClassNameValidity(const String* class_name) const { // TODO: With nspace support, there could arise problems also when not in // split proxy mode, warnings for these should be added. if (split_proxy_dmodule) { if (Cmp(class_name, im_dmodule_name) == 0) { Swig_error(input_file, line_number, "Class name cannot be equal to intermediary D module name: %s\n", class_name); Exit(EXIT_FAILURE); } String *nspace = getNSpace(); if (nspace) { // Check the root package/outermost namespace (a class A in module // A.B leads to problems if another module A.C is also imported) if (Len(package) > 0) { String *dotless_package = NewStringWithSize(package, Len(package) - 1); if (Cmp(class_name, dotless_package) == 0) { Swig_error(input_file, line_number, "Class name cannot be the same as the root package it is in: %s\n", class_name); Exit(EXIT_FAILURE); } Delete(dotless_package); } else { String *outer = createFirstNamespaceName(nspace); if (Cmp(class_name, outer) == 0) { Swig_error(input_file, line_number, "Class name cannot be the same as the outermost namespace it is in: %s\n", class_name); Exit(EXIT_FAILURE); } Delete(outer); } // … and the innermost one (because of the conflict with the main proxy // module named like the namespace). String *inner = createLastNamespaceName(nspace); if (Cmp(class_name, inner) == 0) { Swig_error(input_file, line_number, "Class name cannot be the same as the innermost namespace it is in: %s\n", class_name); Exit(EXIT_FAILURE); } Delete(inner); } else { if (Cmp(class_name, proxy_dmodule_name) == 0) { Swig_error(input_file, line_number, "Class name cannot be equal to proxy D module name: %s\n", class_name); Exit(EXIT_FAILURE); } } } } /* --------------------------------------------------------------------------- * D::getPrimitiveDptype() * * Returns the D proxy type for the passed type if it is a primitive type in * both C and D. * --------------------------------------------------------------------------- */ String *getPrimitiveDptype(Node *node, SwigType *type) { SwigType *stripped_type = SwigType_typedef_resolve_all(type); // A reference can only be the »outermost element« of a type. bool mutable_ref = false; if (SwigType_isreference(stripped_type)) { SwigType_del_reference(stripped_type); if (SwigType_isconst(stripped_type)) { SwigType_del_qualifier(stripped_type); } else { mutable_ref = true; } } // Strip all the pointers from the type. int indirection_count = 0; while (SwigType_ispointer(stripped_type)) { ++indirection_count; SwigType_del_pointer(stripped_type); } // Now that we got rid of the pointers, see if we are dealing with a // primitive type. String *dtype = 0; if (SwigType_isfunction(stripped_type) && indirection_count > 0) { // type was a function pointer, split it up. SwigType_add_pointer(stripped_type); --indirection_count; SwigType *return_type = Copy(stripped_type); SwigType *params_type = SwigType_functionpointer_decompose(return_type); String *return_dtype = getPrimitiveDptype(node, return_type); Delete(return_type); if (!return_dtype) { return 0; } List *parms = SwigType_parmlist(params_type); List *param_dtypes = NewList(); for (Iterator it = First(parms); it.item; it = Next(it)) { String *current_dtype = getPrimitiveDptype(node, it.item); if (Cmp(current_dtype, "void") == 0) { // void somefunc(void) is legal syntax in C, but not in D, so simply // skip the void parameter. Delete(current_dtype); continue; } if (!current_dtype) { Delete(return_dtype); Delete(param_dtypes); return 0; } Append(param_dtypes, current_dtype); } String *param_list = NewString(""); { bool gen_comma = false; for (Iterator it = First(param_dtypes); it.item; it = Next(it)) { if (gen_comma) { Append(param_list, ", "); } Append(param_list, it.item); Delete(it.item); gen_comma = true; } } dtype = NewStringf("%s.SwigExternC!(%s function(%s))", im_dmodule_fq_name, return_dtype, param_list); Delete(param_list); Delete(param_dtypes); Delete(return_dtype); } else { Hash *attributes = NewHash(); const String *tm = lookupCodeTypemap(node, "dtype", stripped_type, WARN_NONE, attributes); if(!GetFlag(attributes, "tmap:dtype:cprimitive")) { dtype = 0; } else { dtype = Copy(tm); // We need to call replaceClassname here with the stripped type to avoid // $dclassname in the enum typemaps being replaced later with the full // type. replaceClassname(dtype, stripped_type); } Delete(attributes); } Delete(stripped_type); if (!dtype) { // The type passed is no primitive type. return 0; } // The type is ultimately a primitive type, now append the right number of // indirection levels (pointers). for (int i = 0; i < indirection_count; ++i) { Append(dtype, "*"); } // Add a level of indirection for a mutable reference since it is wrapped // as a pointer. if (mutable_ref) { Append(dtype, "*"); } return dtype; } /* --------------------------------------------------------------------------- * D::lookupCodeTypemap() * * Looks up a D code fragment for generating the wrapper class for the given * type. * * n - for input only and must contain info for Getfile(n) and Getline(n) to work * tmap_method - typemap method name * type - typemap type to lookup * warning - warning number to issue if no typemaps found * typemap_attributes - the typemap attributes are attached to this node and will * also be used for temporary storage if non null * return is never NULL, unlike Swig_typemap_lookup() * --------------------------------------------------------------------------- */ const String *lookupCodeTypemap(Node *n, const_String_or_char_ptr tmap_method, SwigType *type, int warning, Node *typemap_attributes = 0) const { Node *node = !typemap_attributes ? NewHash() : typemap_attributes; Setattr(node, "type", type); Setfile(node, Getfile(n)); Setline(node, Getline(n)); const String *tm = Swig_typemap_lookup(tmap_method, node, "", 0); if (!tm) { tm = empty_string; if (warning != WARN_NONE) { Swig_warning(warning, Getfile(n), Getline(n), "No %s typemap defined for %s\n", tmap_method, SwigType_str(type, 0)); } } if (!typemap_attributes) { Delete(node); } return tm; } /* --------------------------------------------------------------------------- * D::lookupDTypemap() * * Looks up a D typemap for the given node, replacing D-specific special * variables as needed. * * The method parameter specifies the typemap method to use. If attached is * true, the value is just fetched from the tmap: node attribute, * Swig_typemap_lookup is used otherwise. * --------------------------------------------------------------------------- */ String *lookupDTypemap(Node *n, const_String_or_char_ptr method, bool attached = false) { String *result = 0; if (attached) { String *attr_name = NewStringf("tmap:%s", method); result = Copy(Getattr(n, attr_name)); Delete(attr_name); } else { // FIXME: As a workaround for a bug so far only surfacing in the // smart_pointer_const_overload test case, remove the nativepointer // typemap attribute since it seems to be already there from a dout // typemap of a different type in that test. String *np_key = NewStringf("tmap:%s:nativepointer", method); Delattr(n, np_key); Delete(np_key); result = Swig_typemap_lookup(method, n, "", 0); } if (!result) { return 0; } // Check if the passed node actually has type information attached. This // is not the case e.g. in constructorWrapper. SwigType *type = Getattr(n, "type"); if (type) { String *np_key = NewStringf("tmap:%s:nativepointer", method); String *np_value = Getattr(n, np_key); Delete(np_key); String *dtype; if (np_value && (dtype = getPrimitiveDptype(n, type))) { // If the typemap in question has a »nativepointer« attribute and we // are dealing with a primitive type, use it instead. result = Copy(np_value); Replaceall(result, "$dtype", dtype); } replaceClassname(result, type); } return result; } /* --------------------------------------------------------------------------- * D::replaceClassname() * * Replaces the special variable $dclassname with the proxy class name for * classes/structs/unions SWIG knows about. Also substitutes the enumeration * name for non-anonymous enums. Otherwise, $classname is replaced with a * $descriptor(type)-like name. * * $*dclassname and $&classname work like with descriptors (see manual section * 10.4.3), they remove a prointer from respectively add a pointer to the type. * * Inputs: * tm - String to perform the substitution at (will usually come from a * typemap. * pt - The type to substitute for the variables. * Outputs: * tm - String with the variables substituted. * Return: * substitution_performed - flag indicating if a substitution was performed * --------------------------------------------------------------------------- */ bool replaceClassname(String *tm, SwigType *pt) { bool substitution_performed = false; SwigType *type = Copy(SwigType_typedef_resolve_all(pt)); SwigType *strippedtype = SwigType_strip_qualifiers(type); if (Strstr(tm, "$dclassname")) { SwigType *classnametype = Copy(strippedtype); replaceClassnameVariable(tm, "$dclassname", classnametype); substitution_performed = true; Delete(classnametype); } if (Strstr(tm, "$*dclassname")) { SwigType *classnametype = Copy(strippedtype); Delete(SwigType_pop(classnametype)); replaceClassnameVariable(tm, "$*dclassname", classnametype); substitution_performed = true; Delete(classnametype); } if (Strstr(tm, "$&dclassname")) { SwigType *classnametype = Copy(strippedtype); SwigType_add_pointer(classnametype); replaceClassnameVariable(tm, "$&dclassname", classnametype); substitution_performed = true; Delete(classnametype); } Delete(strippedtype); Delete(type); return substitution_performed; } /* --------------------------------------------------------------------------- * D::replaceClassnameVariable() * * See D::replaceClassname(). * --------------------------------------------------------------------------- */ void replaceClassnameVariable(String *target, const char *variable, SwigType *type) { // TODO: Fix const-correctness of methods called in here and make type const. // We make use of the fact that this function is called at least once for // every type encountered which is written to a separate file, which allows // us to handle imports here. // When working in split proxy module mode, each generated proxy class/enum // is written to a separate module. This requires us to add a corresponding // import when a type is used in another generated module. If we are not // working in split proxy module mode, this is not relevant and the // generated module name is discarded. String *type_name; if (SwigType_isenum(type)) { // RESEARCH: Make sure that we really cannot get here for anonymous enums. Node *n = enumLookup(type); if (n) { String *enum_name = Getattr(n, "sym:name"); Node *p = parentNode(n); if (p && !Strcmp(nodeType(p), "class")) { // This is a nested enum. String *parent_name = Getattr(p, "sym:name"); String *nspace = Getattr(p, "sym:nspace"); // An enum nested in a class is not written to a separate module (this // would not even be possible in D), so just import the parent. requireDType(nspace, parent_name); String *module = createModuleName(nspace, parent_name); if (inProxyModule(module)) { type_name = NewStringf("%s.%s", parent_name, enum_name); } else { type_name = NewStringf("%s%s.%s.%s", package, module, parent_name, enum_name); } } else { // A non-nested enum is written to a separate module, import it. String *nspace = Getattr(n, "sym:nspace"); requireDType(nspace, enum_name); String *module = createModuleName(nspace, enum_name); if (inProxyModule(module)) { type_name = Copy(enum_name); } else { type_name = NewStringf("%s%s.%s", package, module, enum_name); } } } else { type_name = NewStringf("int"); } } else { Node *n = classLookup(type); if (n) { String *class_name = Getattr(n, "sym:name"); String *nspace = Getattr(n, "sym:nspace"); requireDType(nspace, class_name); String *module = createModuleName(nspace, class_name); if (inProxyModule(module)) { type_name = Copy(class_name); } else { type_name = NewStringf("%s%s.%s", package, module, class_name); } Delete(module); } else { // SWIG does not know anything about the type (after resolving typedefs). // Just mangle the type name string like $descriptor(type) would do. String *descriptor = NewStringf("SWIGTYPE%s", SwigType_manglestr(type)); requireDType(NULL, descriptor); String *module = createModuleName(NULL, descriptor); if (inProxyModule(module)) { type_name = Copy(descriptor); } else { type_name = NewStringf("%s%s.%s", package, module, descriptor); } Delete(module); // Add to hash table so that a type wrapper class can be created later. Setattr(unknown_types, descriptor, type); Delete(descriptor); } } Replaceall(target, variable, type_name); Delete(type_name); } /* --------------------------------------------------------------------------- * D::createModuleName() * * Returns a string holding the name of the module to import to bring the * given type in scope. * --------------------------------------------------------------------------- */ String *createModuleName(const String *nspace, const String *type_name) const { String *module; if (nspace) { module = NewStringf("%s.", nspace); if (split_proxy_dmodule) { Printv(module, type_name, NIL); } else { String *inner = createLastNamespaceName(nspace); Printv(module, inner, NIL); Delete(inner); } } else { if (split_proxy_dmodule) { module = Copy(type_name); } else { module = Copy(proxy_dmodule_name); } } return module; } /* --------------------------------------------------------------------------- * D::replaceModuleVariables() * * Replaces the $imdmodule and $module variables with their values in the * target string. * --------------------------------------------------------------------------- */ void replaceModuleVariables(String *target) const { Replaceall(target, "$imdmodule", im_dmodule_fq_name); Replaceall(target, "$module", proxy_dmodule_name); } /* --------------------------------------------------------------------------- * D::replaceExcode() * * If a C++ method can throw a exception, additional code is added to the * proxy method to check if an exception is pending so that it can be * rethrown on the D side. * * This method replaces the $excode variable with the exception handling code * in the excode typemap attribute if it »canthrow« an exception. * --------------------------------------------------------------------------- */ void replaceExcode(Node *n, String *code, const String *typemap, Node *parameter) const { String *excode_attribute = NewStringf("tmap:%s:excode", typemap); String *excode = Getattr(parameter, excode_attribute); if (Getattr(n, "d:canthrow")) { int count = Replaceall(code, "$excode", excode); if (count < 1 || !excode) { Swig_warning(WARN_D_EXCODE_MISSING, input_file, line_number, "D exception may not be thrown – no $excode or excode attribute in '%s' typemap.\n", typemap); } } else { Replaceall(code, "$excode", ""); } Delete(excode_attribute); } /* --------------------------------------------------------------------------- * D::replaceImportTypeMacros() * * Replaces the $importtype(SomeDClass) macro with an import statement if it * is required to get SomeDClass in scope for the currently generated proxy * D module. * --------------------------------------------------------------------------- */ void replaceImportTypeMacros(String *target) const { // Code from replace_embedded_typemap. char *start = 0; while ((start = Strstr(target, "$importtype("))) { char *end = 0; char *param_start = 0; char *param_end = 0; int level = 0; char *c = start; while (*c) { if (*c == '(') { if (level == 0) { param_start = c + 1; } level++; } if (*c == ')') { level--; if (level == 0) { param_end = c; end = c + 1; break; } } c++; } if (end) { String *current_macro = NewStringWithSize(start, (int)(end - start)); String *current_param = NewStringWithSize(param_start, (int)(param_end - param_start)); if (inProxyModule(current_param)) { Replace(target, current_macro, "", DOH_REPLACE_ANY); } else { String *import = createImportStatement(current_param, false); Replace(target, current_macro, import, DOH_REPLACE_ANY); Delete(import); } Delete(current_param); Delete(current_macro); } else { String *current_macro = NewStringWithSize(start, (int)(c - start)); Swig_error(Getfile(target), Getline(target), "Syntax error in: %s\n", current_macro); Replace(target, current_macro, "", DOH_REPLACE_ANY); Delete(current_macro); } } } /* --------------------------------------------------------------------------- * D::getOverloadedName() * --------------------------------------------------------------------------- */ String *getOverloadedName(Node *n) const { // A void* parameter is used for all wrapped classes in the wrapper code. // Thus, the wrapper function names for overloaded functions are postfixed // with a counter string to make them unique. String *overloaded_name = Copy(Getattr(n, "sym:name")); if (Getattr(n, "sym:overloaded")) { Append(overloaded_name, Getattr(n, "sym:overname")); } return overloaded_name; } /* --------------------------------------------------------------------------- * D::createProxyName() * * Returns the D class name if a type corresponds to something wrapped with a * proxy class, NULL otherwise. * --------------------------------------------------------------------------- */ String *createProxyName(SwigType *t) { String *proxyname = NULL; Node *n = classLookup(t); if (n) { String *nspace = Getattr(n, "sym:nspace"); String *symname = Getattr(n, "sym:name"); String *module = createModuleName(nspace, symname); if (inProxyModule(module)) { proxyname = Copy(symname); } else { proxyname = NewStringf("%s%s.%s", package, module, symname); } } return proxyname; } String *makeParameterName(Node *n, Parm *p, int arg_num, bool setter) const { String *arg = Language::makeParameterName(n, p, arg_num, setter); if (split_proxy_dmodule && Strncmp(arg, package, Len(arg)) == 0) { // If we are in split proxy mode and the argument is named like the target // package, we append an underscore to its name to avoid clashes. Append(arg, "_"); } return arg; } /* --------------------------------------------------------------------------- * D::canThrow() * * Determines whether the code in the typemap can throw a D exception. * If so, note it for later when excodeSubstitute() is called. * --------------------------------------------------------------------------- */ void canThrow(Node *n, const String *typemap, Node *parameter) const { String *canthrow_attribute = NewStringf("tmap:%s:canthrow", typemap); String *canthrow = Getattr(parameter, canthrow_attribute); if (canthrow) Setattr(n, "d:canthrow", "1"); Delete(canthrow_attribute); } /* --------------------------------------------------------------------------- * D::wrapMemberFunctionAsDConst() * * Determines whether the member function represented by the passed node is * wrapped as D »const« or not. * --------------------------------------------------------------------------- */ bool wrapMemberFunctionAsDConst(Node *n) const { if (static_flag) return false; // Never emit »const« for static member functions. return GetFlag(n, "memberget") || SwigType_isconst(Getattr(n, "decl")); } /* --------------------------------------------------------------------------- * D::areAllOverloadsOverridden() * * Determines whether the class the passed function node belongs to overrides * all the overlaods for the passed function node defined somewhere up the * inheritance hierarchy. * --------------------------------------------------------------------------- */ bool areAllOverloadsOverridden(Node *n) const { List *base_list = Getattr(parentNode(n), "bases"); if (!base_list) { // If the class which contains n is not derived from any other class, // there cannot be any not-overridden overloads. return true; } // In case of multiple base classes, skip to the one which has not been // ignored. // RESEARCH: Also emit a warning in case of multiple inheritance here? Iterator it = First(base_list); while (it.item && GetFlag(it.item, "feature:ignore")) { it = Next(it); } Node *base_class = it.item; if (!base_class) { // If all base classes have been ignored, there cannot be one either. return true; } // We try to find at least a single overload which exists in the base class // so we can progress up the inheritance hierarchy even if there have been // new overloads introduced after the topmost class. Node *base_function = NULL; String *symname = Getattr(n, "sym:name"); if (symname) { for (Node *tmp = firstChild(base_class); tmp; tmp = nextSibling(tmp)) { String *child_symname = Getattr(tmp, "sym:name"); if (child_symname && (Strcmp(child_symname, symname) == 0)) { base_function = tmp; break; } } } if (!base_function) { // If there is no overload which also exists in the super class, there // cannot be any base class overloads not overridden. return true; } size_t base_overload_count = 0; for (Node *tmp = firstSibling(base_function); tmp; tmp = Getattr(tmp, "sym:nextSibling")) { if (is_protected(base_function) && !(Swig_director_mode() && Swig_director_protected_mode() && Swig_all_protected_mode())) { // If the base class function is »protected« and were are not in // director mode, it is not emitted to the base class and thus we do // not count it. Otherwise, we would run into issues if the visibility // of some functions was changed from protected to public in a child // class with the using directive. continue; } ++base_overload_count; } return ((base_overload_count <= overridingOverloadCount(n)) && areAllOverloadsOverridden(base_function)); } /* --------------------------------------------------------------------------- * D::checkClassBaseOver() * * Search a class for a method that can override the current method. * --------------------------------------------------------------------------- */ bool checkClassBaseOver(Node *b, const String *name, ParmList *l, const int llen, const String *bname = NULL) { if (bname == NULL) { bname = Getattr(b, "name"); } for(Node *e = firstChild(b); e; e = nextSibling(e)) { /* We look for D only fuctions! */ const String *ename = Getattr(e, "name"); const String *etype = nodeType(e); if (Strcmp(etype, "extend") == 0) { /* extend of class do not have a name attribute, * it is the same class. */ if (checkClassBaseOver(e, name, l, llen, bname)) { return true; } } else if (Strcmp(etype, "cdecl") == 0 || Strcmp(etype, "using") == 0) { /* 'using' is marked in markDOverride() same as 'cdecl' */ if (Strcmp(name, ename) == 0) { if (GetFlag(e, "d:override_property")) { return true; } /* Do we need a full compare of ParmList? How about in typemaps? */ ParmList *el = Getattr(e, "d:override_parms"); const int ellen = ParmList_len(el); if (GetFlag(e, "d:can_override") && ellen == llen) { bool eq = true; String *detd = NewString(""); if (llen > 0) { for(ParmList *le = el, *ln = l; eq && le && ln; le = nextSibling(le), ln = nextSibling(ln)) { const String *ntd = Getattr(ln, "d:type"); const String *etd = Getattr(le, "d:type"); Printf(detd, "%s.%s", etd, etd); /* Parameter types are equal, or the type is 'class.class' */ eq = etd && ntd && (Strcmp(ntd, etd) == 0 || Strcmp(ntd, detd) == 0); } } Delete(detd); if (eq) { return true; } } } } } return false; } /* --------------------------------------------------------------------------- * D::checkBaseOver() * * Traverse all base classes and look for a method the current method override. * --------------------------------------------------------------------------- */ bool checkBaseOver(Node *c, const String *name, ParmList *l, const int llen) { // * Member template functions? if (!c) { return false; } List *bases = Getattr(c, "bases"); if (!bases) { return false; } for (int i = 0; i < Len(bases); i++) { Node *b = Getitem(bases, i); if (checkClassBaseOver(b, name, l, llen)) { return true; } if (checkBaseOver(b, name, l, llen)) { return true; } } return false; } /* --------------------------------------------------------------------------- * D::markDOverride() * * Mark current method for methods in derived classes. * --------------------------------------------------------------------------- */ void markDOverride(Node *n, const String *name, ParmList *l, Node *p, const String *pname, const String *ptype) { if (!pname && Strcmp(ptype, "extend") == 0) { Node *pp = parentNode(p); if (pp) { pname = Getattr(pp, "name"); } } for(Node *e = firstChild(p); e; e = nextSibling(e)) { /* Am I in my class? */ if (n == e) { SetFlag(n, "d:can_override"); if (wrapping_member_flag) { SetFlag(n, "d:override_property"); } else { Setattr(n, "d:override_parms", CopyParmList(l)); } return; } } /* * I am a 'using' method. * Our method is using a method definition from a different class. * We need to mark the original 'using' method in our class, * as this node is not accessibale in derived classes. */ for(Node *e = firstChild(p); e; e = nextSibling(e)) { const String *ename = Getattr(e, "name"); const String *ntype = nodeType(e); if (ename && Strcmp(ename, name) == 0 && ntype && Strcmp(ntype, "using") == 0) { SetFlag(e, "d:can_override"); Setattr(e, "d:override_parms", CopyParmList(l)); return; } } } /* --------------------------------------------------------------------------- * D::isDOverride() * * Override should be used for override D existing method. * Class D methods, non static and non private. * Check if current method override, * anf mark it for derived classes use. * --------------------------------------------------------------------------- */ bool isDOverride(Node *n, ParmList *l) { if (is_smart_pointer()) { /* Smart pointer classes do not mirror the inheritance hierarchy of the * underlying pointer type, so no override required. */ return false; } if (static_flag) { /* Static are never override */ return false; } Node *p = parentNode(n); if (!p) { return false; } const String *name = Getattr(n, "name"); const int llen = ParmList_len(l); const String *ptype = nodeType(p); const String *pname = Getattr(p, "name"); markDOverride(n, name, l, p, pname, ptype); if (Strcmp(ptype, "extend") == 0) { /* The 'bases' are in the class we extend */ p = parentNode(p); } return checkBaseOver(p, name, l, llen); } /* --------------------------------------------------------------------------- * D::overridingOverloadCount() * * Given a member function node, this function counts how many of the * overloads of the function (including itself) override a function in the * base class. * --------------------------------------------------------------------------- */ size_t overridingOverloadCount(Node *n) const { size_t result = 0; Node *tmp = firstSibling(n); do { // KLUDGE: We also have to count the function if the access attribute is // not present, since this means that it has been promoted into another // protection level in the base class with the C++ »using« directive, and // is thus taken into account when counting the base class overloads, even // if it is not marked as »override« by the SWIG parser. if (Getattr(n, "override") || !Getattr(n, "access")) { ++result; } } while((tmp = Getattr(tmp, "sym:nextSibling"))); return result; } /* --------------------------------------------------------------------------- * D::firstSibling() * * Returns the first sibling of the passed node. * --------------------------------------------------------------------------- */ Node *firstSibling(Node *n) const { Node *result = n; while (Node *tmp = Getattr(result, "sym:previousSibling")) { result = tmp; } return result; } /* --------------------------------------------------------------------------- * D::indentCode() * * Helper function to indent a code (string) by one level. * --------------------------------------------------------------------------- */ void indentCode(String* code) const { Replaceall(code, "\n", "\n "); Replaceall(code, " \n", "\n"); Chop(code); } /* --------------------------------------------------------------------------- * D::emitBanner() * --------------------------------------------------------------------------- */ void emitBanner(File *f) const { Printf(f, "/* ----------------------------------------------------------------------------\n"); Swig_banner_target_lang(f, " *"); Printf(f, " * ----------------------------------------------------------------------------- */\n\n"); Printv(f, common_begin_code, NIL); } /* --------------------------------------------------------------------------- * D::getOutDtype() * * Returns the return (out) D type and check the Dtype out typemap. * --------------------------------------------------------------------------- */ String *getOutDtype(Node *n) { String *result = lookupDTypemap(n, "dtype"); if (result) { String *dtypeout = Copy(Getattr(n, "tmap:dtype:out")); if (dtypeout) { /* The type in the out attribute of the typemap overrides the type * in the dtype typemap. */ Delete(result); result = dtypeout; replaceClassname(result, Getattr(n, "type")); } } return result; } /* --------------------------------------------------------------------------- * D::outputDirectory() * * Returns the directory to write the D modules for the given namespace to and * and creates the subdirectory if it doesn't exist. * --------------------------------------------------------------------------- */ String *outputDirectory(String *nspace) { String *output_directory = Copy(dmodule_directory); if (nspace) { String *nspace_subdirectory = Copy(nspace); Replaceall(nspace_subdirectory, ".", SWIG_FILE_DELIMITER); String *newdir_error = Swig_new_subdirectory(output_directory, nspace_subdirectory); if (newdir_error) { Printf(stderr, "%s\n", newdir_error); Delete(newdir_error); Exit(EXIT_FAILURE); } Printv(output_directory, nspace_subdirectory, SWIG_FILE_DELIMITER, 0); Delete(nspace_subdirectory); } return output_directory; } /* --------------------------------------------------------------------------- * D::proxyCodeBuffer() * * Returns the buffer to write proxy code for the given namespace to. * --------------------------------------------------------------------------- */ String *proxyCodeBuffer(String *nspace) { if (!nspace) { return proxy_dmodule_code; } Hash *hash = Getattr(nspace_proxy_dmodules, nspace); if (!hash) { hash = NewHash(); Setattr(hash, "code", NewString("")); Setattr(hash, "imports", NewString("")); Setattr(nspace_proxy_dmodules, nspace, hash); } return Getattr(hash, "code"); } /* --------------------------------------------------------------------------- * D::proxyCodeBuffer() * * Returns the buffer to write imports for the proxy code for the given * namespace to. * --------------------------------------------------------------------------- */ String *proxyImportsBuffer(String *nspace) { if (!nspace) { return proxy_dmodule_imports; } Hash *hash = Getattr(nspace_proxy_dmodules, nspace); if (!hash) { hash = NewHash(); Setattr(hash, "code", NewString("")); Setattr(hash, "imports", NewString("")); Setattr(nspace_proxy_dmodules, nspace, hash); } return Getattr(hash, "imports"); } /* --------------------------------------------------------------------------- * D::createFirstNamespaceName() * * Returns a new string containing the name of the outermost namespace, e.g. * »A« for the argument »A.B.C«. * --------------------------------------------------------------------------- */ String *createFirstNamespaceName(const String *nspace) const { char *tmp = Char(nspace); char *c = tmp; char *co = 0; if (!strchr(c, '.')) return 0; co = c + Len(nspace); while (*c && (c != co)) { if (*c == '.') { break; } c++; } if (!*c || (c == tmp)) { return NULL; } return NewStringWithSize(tmp, (int)(c - tmp)); } /* --------------------------------------------------------------------------- * D::createLastNamespaceName() * * Returns a new string containing the name of the innermost namespace, e.g. * »C« for the argument »A.B.C«. * --------------------------------------------------------------------------- */ String *createLastNamespaceName(const String *nspace) const { if (!nspace) return NULL; char *c = Char(nspace); char *cc = c; if (!strchr(c, '.')) return NewString(nspace); while (*c) { if (*c == '.') { cc = c; } ++c; } return NewString(cc + 1); } /* --------------------------------------------------------------------------- * D::createOuterNamespaceNames() * * Returns a new string containing the name of the outer namespace, e.g. * »A.B« for the argument »A.B.C«. * --------------------------------------------------------------------------- */ String *createOuterNamespaceNames(const String *nspace) const { if (!nspace) return NULL; char *tmp = Char(nspace); char *c = tmp; char *cc = c; if (!strchr(c, '.')) return NULL; while (*c) { if (*c == '.') { cc = c; } ++c; } if (cc == tmp) { return NULL; } return NewStringWithSize(tmp, (int)(cc - tmp)); } }; static Language *new_swig_d() { return new D(); } /* ----------------------------------------------------------------------------- * swig_d() - Instantiate module * ----------------------------------------------------------------------------- */ extern "C" Language *swig_d(void) { return new_swig_d(); } /* ----------------------------------------------------------------------------- * Usage information displayed at the command line. * ----------------------------------------------------------------------------- */ const char *D::usage = "\ D Options (available with -d)\n\ -d2 - Generate code for D2/Phobos (The default, left for backward compatibility)\n\ -package - Write generated D modules into package \n\ -splitproxy - Write each D type to a dedicated file instead of\n\ generating a single proxy D module.\n\ -wrapperlibrary - Set the name of the wrapper library to \n\ \n"; swig-4.4.0/Source/Modules/utils.cxx0000664000175000017500000002031215075443613017112 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * utils.cxx * * Various utility functions. * ----------------------------------------------------------------------------- */ #include "swigmod.h" int is_public(Node *n) { String *access = Getattr(n, "access"); return !access || !Cmp(access, "public"); } int is_private(Node *n) { String *access = Getattr(n, "access"); return access && !Cmp(access, "private"); } int is_protected(Node *n) { String *access = Getattr(n, "access"); return access && !Cmp(access, "protected"); } static int is_member_director_helper(Node *parentnode, Node *member) { int parent_nodirector = GetFlag(parentnode, "feature:nodirector"); if (parent_nodirector) return 0; int parent_director = Swig_director_mode() && GetFlag(parentnode, "feature:director"); int cdecl_director = parent_director || GetFlag(member, "feature:director"); int cdecl_nodirector = GetFlag(member, "feature:nodirector"); return cdecl_director && !cdecl_nodirector && !GetFlag(member, "feature:extend"); } int is_member_director(Node *parentnode, Node *member) { if (parentnode && checkAttribute(member, "storage", "virtual")) { return is_member_director_helper(parentnode, member); } else { return 0; } } int is_member_director(Node *member) { return is_member_director(Getattr(member, "parentNode"), member); } // Identifies the additional protected members that are generated when the allprotected option is used. // This does not include protected virtual methods as they are turned on with the dirprot option. int is_non_virtual_protected_access(Node *n) { int result = 0; if (Swig_director_mode() && Swig_director_protected_mode() && Swig_all_protected_mode() && is_protected(n) && !checkAttribute(n, "storage", "virtual")) { Node *parentNode = Getattr(n, "parentNode"); // When vtable is empty, the director class does not get emitted, so a check for an empty vtable should be done. // However, vtable is set in Language and so is not yet set when methods in Typepass call clean_overloaded() // which calls is_non_virtual_protected_access. So commented out below. // Moving the director vtable creation into Typepass should solve this problem. if (is_member_director_helper(parentNode, n) /* && Getattr(parentNode, "vtable")*/) result = 1; } return result; } /* ----------------------------------------------------------------------------- * clean_overloaded() * * Clean overloaded list. Removes templates, ignored, and errors. * ----------------------------------------------------------------------------- */ void clean_overloaded(Node *n) { Node *nn = Getattr(n, "sym:overloaded"); Node *first = 0; while (nn) { String *ntype = nodeType(nn); if ((GetFlag(nn, "feature:ignore")) || (Getattr(nn, "error")) || (Strcmp(ntype, "template") == 0) || ((Strcmp(ntype, "cdecl") == 0) && is_protected(nn) && !is_member_director(nn) && !is_non_virtual_protected_access(n))) { /* Remove from overloaded list */ Node *ps = Getattr(nn, "sym:previousSibling"); Node *ns = Getattr(nn, "sym:nextSibling"); if (ps) { Setattr(ps, "sym:nextSibling", ns); } if (ns) { Setattr(ns, "sym:previousSibling", ps); } Delattr(nn, "sym:previousSibling"); Delattr(nn, "sym:nextSibling"); Delattr(nn, "sym:overloaded"); Delattr(nn, "sym:overname"); nn = ns; continue; } else { if (!first) first = nn; Setattr(nn, "sym:overloaded", first); } nn = Getattr(nn, "sym:nextSibling"); } if (!first || (first && !Getattr(first, "sym:nextSibling"))) { if (Getattr(n, "sym:overloaded")) Delattr(n, "sym:overloaded"); } Swig_symbol_fix_overname(Getattr(n, "sym:overloaded")); } /* ----------------------------------------------------------------------------- * Swig_set_max_hash_expand() * * Controls how many Hash objects are displayed when displaying nested Hash objects. * Makes DohSetMaxHashExpand an externally callable function (for debugger). * ----------------------------------------------------------------------------- */ void Swig_set_max_hash_expand(int count) { SetMaxHashExpand(count); } extern "C" { /* ----------------------------------------------------------------------------- * Swig_get_max_hash_expand() * * Returns how many Hash objects are displayed when displaying nested Hash objects. * Makes DohGetMaxHashExpand an externally callable function (for debugger). * ----------------------------------------------------------------------------- */ int Swig_get_max_hash_expand() { return GetMaxHashExpand(); } /* ----------------------------------------------------------------------------- * Swig_to_doh_string() * * DOH version of Swig_to_string() * ----------------------------------------------------------------------------- */ static String *Swig_to_doh_string(DOH *object, int count) { int old_count = Swig_get_max_hash_expand(); if (count >= 0) Swig_set_max_hash_expand(count); String *debug_string = object ? NewStringf("%s", object) : NewString("NULL"); Swig_set_max_hash_expand(old_count); return debug_string; } /* ----------------------------------------------------------------------------- * Swig_to_doh_string_with_location() * * DOH version of Swig_to_string_with_location() * ----------------------------------------------------------------------------- */ static String *Swig_to_doh_string_with_location(DOH *object, int count) { int old_count = Swig_get_max_hash_expand(); if (count >= 0) Swig_set_max_hash_expand(count); String *debug_string = Swig_stringify_with_location(object); Swig_set_max_hash_expand(old_count); return debug_string; } /* ----------------------------------------------------------------------------- * Swig_to_string() * * Swig debug - return C string representation of any DOH type. * Nested Hash types expand count is value of Swig_get_max_hash_expand when count<0 * Note: leaks memory. * ----------------------------------------------------------------------------- */ const char *Swig_to_string(DOH *object, int count) { return Char(Swig_to_doh_string(object, count)); } /* ----------------------------------------------------------------------------- * Swig_to_string_with_location() * * Swig debug - return C string representation of any DOH type, within [] brackets * for Hash and List types, prefixed by line and file information. * Nested Hash types expand count is value of Swig_get_max_hash_expand when count<0 * Note: leaks memory. * ----------------------------------------------------------------------------- */ const char *Swig_to_string_with_location(DOH *object, int count) { return Char(Swig_to_doh_string_with_location(object, count)); } /* ----------------------------------------------------------------------------- * Swig_print() * * Swig debug - display string representation of any DOH type. * Nested Hash types expand count is value of Swig_get_max_hash_expand when count<0 * ----------------------------------------------------------------------------- */ void Swig_print(DOH *object, int count) { String *output = Swig_to_doh_string(object, count); Printf(stdout, "%s\n", output); Delete(output); } /* ----------------------------------------------------------------------------- * Swig_to_string_with_location() * * Swig debug - display string representation of any DOH type, within [] brackets * for Hash and List types, prefixed by line and file information. * Nested Hash types expand count is value of Swig_get_max_hash_expand when count<0 * ----------------------------------------------------------------------------- */ void Swig_print_with_location(DOH *object, int count) { String *output = Swig_to_doh_string_with_location(object, count); Printf(stdout, "%s\n", output); Delete(output); } } // extern "C" swig-4.4.0/Source/Modules/lua.cxx0000664000175000017500000025130515075443613016543 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * lua.cxx * * Lua language module for SWIG. * ----------------------------------------------------------------------------- */ /* NEW LANGUAGE NOTE: * ver001 this is simply a copy of tcl8.cxx, which has been renamed * ver002 all non essential code commented out, program now does virtually nothing it prints to stderr the list of functions to wrap, but does not create the XXX_wrap.c file * ver003 added back top(), still prints the list of fns to stderr but now creates a rather empty XXX_wrap.c with some basic boilerplate code * ver004 very basic version of functionWrapper() also uncommented usage_string() to keep compiler happy this will start producing proper looking code soon (I hope) produced the wrapper code, but without any type conversion (in or out) generates a few warning because of no wrappering does not generate SWIG_init() reason for this is that lua.swg is empty we will need to add code into this to make it work * ver005/6 massive rework, basing work on the pike module instead of tcl (pike module it only 1/3 of the size)(though not as complete) * ver007 added simple type checking * ver008 INPUT, OUTPUT, INOUT typemaps handled (though not all types yet) * ver009 class support: ok for basic types, but methods still TDB (code is VERY messed up & needs to be cleaned) * ver010 Added support for embedded Lua. Try swig -lua -help for more information */ #include "swigmod.h" #include "cparse.h" /**** Diagnostics: With the #define REPORT(), you can change the amount of diagnostics given This helps me search the parse tree & figure out what is going on inside SWIG (because it's not clear or documented) */ #define REPORT(T,D) // no info: //#define REPORT(T,D) {Printf(stdout,T"\n");} // only title //#define REPORT(T,D) {Printf(stdout,T" %p\n",n);} // title & pointer //#define REPORT(T,D) {Printf(stdout,T"\n");display_mapping(D);} // the works //#define REPORT(T,D) {Printf(stdout,T"\n");if(D)Swig_print_node(D);} // the works void display_mapping(DOH *d) { if (d == 0 || !DohIsMapping(d)) return; for (Iterator it = First(d); it.item; it = Next(it)) { if (DohIsString(it.item)) Printf(stdout, " %s = %s\n", it.key, it.item); else if (DohIsMapping(it.item)) Printf(stdout, " %s = \n", it.key); else if (DohIsSequence(it.item)) Printf(stdout, " %s = \n", it.key); else Printf(stdout, " %s = \n", it.key); } } extern "C" { static int compareByLen(const DOH *f, const DOH *s) { return Len(s) - Len(f); } } /* NEW LANGUAGE NOTE:*********************************************** most of the default options are handled by SWIG you can add new ones here (though for now I have not bothered) NEW LANGUAGE NOTE:END ************************************************/ static const char *usage = "\ Lua Options (available with -lua)\n\ -elua - Generates LTR compatible wrappers for smaller devices running elua\n\ -eluac - LTR compatible wrappers in \"crass compress\" mode for elua\n\ -elua-emulate - Emulates behaviour of eLua. Useful only for testing.\n\ Incompatible with -elua/-eluac options.\n\ -nomoduleglobal - Do not register the module name as a global variable \n\ but return the module table from calls to require.\n\ -no-old-metatable-bindings\n\ - Disable support for old-style bindings name generation, some\n\ old-style members scheme etc.\n\ -squash-bases - Squashes symbols from all inheritance tree of a given class\n\ into itself. Emulates pre-SWIG3.0 inheritance. Insignificantly\n\ speeds things up, but increases memory consumption.\n\ \n"; static int nomoduleglobal = 0; static int elua_ltr = 0; static int eluac_ltr = 0; static int elua_emulate = 0; static int squash_bases = 0; /* The new metatable bindings were introduced in SWIG 3.0.0. * old_metatable_bindings in v2: * 1. static methods will be put into the scope their respective class * belongs to as well as into the class scope itself. (only for classes without %nspace given) * 2. The layout in elua mode is somewhat different */ static int old_metatable_bindings = 1; static int old_compatible_names = 1; // This flag can temporarily disable backward compatible names generation if old_metatable_bindings is enabled /* NEW LANGUAGE NOTE:*********************************************** To add a new language, you need to derive your class from Language and the overload various virtual functions (more on this as I figure it out) NEW LANGUAGE NOTE:END ************************************************/ class LUA:public Language { private: File *f_begin; File *f_runtime; File *f_header; File *f_wrappers; File *f_init; File *f_initbeforefunc; String *s_luacode; // luacode to be called during init String *module; //name of the module // Parameters for current class. NIL if not parsing class int have_constructor; int have_destructor; String *destructor_action; // This variable holds the name of the current class in Lua. Usually it is // the same as C++ class name, but rename directives can change it. String *proxy_class_name; // This is a so called fully qualified symname - the above proxy class name // prepended with class namespace. If class Lua name is the same as class C++ name, // then it is basically C++ fully qualified name with colons replaced with dots. String *full_proxy_class_name; // All static methods and/or variables are treated as if they were in the // special C++ namespace $(classname).SwigStatic. This is internal mechanism only // and is not visible to user in any manner. This variable holds the name // of such pseudo-namespace a.k.a the result of above expression evaluation String *class_static_nspace; // This variable holds the name of generated C function that acts as a constructor // for the currently parsed class. String *constructor_name; // Many wrappers forward calls to each other, for example staticmembervariableHandler // forwards calls to variableHandler, which, in turn, makes to call to functionWrapper. // In order to access information about whether it is a static member of class or just // a plain old variable, the current array is kept and used as a 'log' of the call stack. enum TState { NO_CPP, VARIABLE, GLOBAL_FUNC, GLOBAL_VAR, MEMBER_FUNC, CONSTRUCTOR, DESTRUCTOR, MEMBER_VAR, STATIC_FUNC, STATIC_VAR, STATIC_CONST, // enums and things like static const int x = 5; ENUM_CONST, // This is only needed for backward compatibility in C mode STATES_COUNT }; bool current[STATES_COUNT]; public: /* --------------------------------------------------------------------- * LUA() * * Initialize member data * --------------------------------------------------------------------- */ LUA(): f_begin(0), f_runtime(0), f_header(0), f_wrappers(0), f_init(0), f_initbeforefunc(0), s_luacode(0), module(0), have_constructor(0), have_destructor(0), destructor_action(0), proxy_class_name(0), full_proxy_class_name(0), class_static_nspace(0), constructor_name(0) { for (int i = 0; i < STATES_COUNT; i++) current[i] = false; } /* NEW LANGUAGE NOTE:*********************************************** This is called to initialise the system & read any command line args most of this is boilerplate code, except the command line args which depends upon what args your code supports NEW LANGUAGE NOTE:END *********************************************** */ /* --------------------------------------------------------------------- * main() * * Parse command line options and initializes variables. * --------------------------------------------------------------------- */ virtual void main(int argc, char *argv[]) { /* Set location of SWIG library */ SWIG_library_directory("lua"); /* Look for certain command line options */ for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-help") == 0) { // usage flags fputs(usage, stdout); } else if (strcmp(argv[i], "-nomoduleglobal") == 0) { nomoduleglobal = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-elua") == 0) { elua_ltr = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-eluac") == 0) { eluac_ltr = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-no-old-metatable-bindings") == 0) { Swig_mark_arg(i); old_metatable_bindings = 0; } else if (strcmp(argv[i], "-squash-bases") == 0) { Swig_mark_arg(i); squash_bases = 1; } else if (strcmp(argv[i], "-elua-emulate") == 0) { Swig_mark_arg(i); elua_emulate = 1; } } } if (elua_emulate && (eluac_ltr || elua_ltr )) { Printf(stderr, "Cannot have -elua-emulate with either -eluac or -elua\n"); Swig_arg_error(); } // Set elua_ltr if elua_emulate is requested if(elua_emulate) elua_ltr = 1; /* NEW LANGUAGE NOTE:*********************************************** This is the boilerplate code, setting a few #defines and which lib directory to use the SWIG_library_directory() is also boilerplate code but it always seems to be the first line of code NEW LANGUAGE NOTE:END *********************************************** */ /* Add a symbol to the parser for conditional compilation */ Preprocessor_define("SWIGLUA 1", 0); /* Set language-specific configuration file */ SWIG_config_file("lua.swg"); /* Enable overloaded methods support */ allow_overloading(); } /* NEW LANGUAGE NOTE:*********************************************** After calling main, SWIG parses the code to wrap (I believe) then calls top() in this is more boilerplate code to set everything up and a call to Language::top() which begins the code generations by calling the member fns after all that is more boilerplate code to close all down (overall there is virtually nothing here that needs to be edited just use as is) NEW LANGUAGE NOTE:END *********************************************** */ /* --------------------------------------------------------------------- * top() * --------------------------------------------------------------------- */ virtual int top(Node *n) { /* Get the module name */ module = Getattr(n, "name"); /* Get the output file name */ String *outfile = Getattr(n, "outfile"); /* Open the output file */ f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_initbeforefunc = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("initbeforefunc", f_initbeforefunc); s_luacode = NewString(""); Swig_register_filebyname("luacode", s_luacode); current[NO_CPP] = true; /* Standard stuff for the SWIG runtime section */ Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "LUA"); emitLuaFlavor(f_runtime); if (nomoduleglobal) { Printf(f_runtime, "#define SWIG_LUA_NO_MODULE_GLOBAL\n"); } else { Printf(f_runtime, "#define SWIG_LUA_MODULE_GLOBAL\n"); } if (squash_bases) Printf(f_runtime, "#define SWIG_LUA_SQUASH_BASES\n"); // if (NoInclude) { // Printf(f_runtime, "#define SWIG_NOINCLUDE\n"); // } Printf(f_runtime, "\n"); //String *init_name = NewStringf("%(title)s_Init", module); //Printf(f_header, "#define SWIG_init %s\n", init_name); //Printf(f_header, "#define SWIG_name \"%s\"\n", module); /* SWIG_import is a special function name for importing within Lua5.1 */ //Printf(f_header, "#define SWIG_import luaopen_%s\n\n", module); Printf(f_header, "#define SWIG_name \"%s\"\n", module); Printf(f_header, "#define SWIG_init luaopen_%s\n", module); Printf(f_header, "#define SWIG_init_user luaopen_%s_user\n\n", module); Printf(f_header, "#define SWIG_LUACODE luaopen_%s_luacode\n", module); Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); /* %init code inclusion, effectively in the SWIG_init function */ Printf(f_init, "void SWIG_init_user(lua_State* L)\n{\n"); Language::top(n); Printf(f_init, "/* exec Lua code if applicable */\nSWIG_Lua_dostring(L,SWIG_LUACODE);\n"); Printf(f_init, "}\n"); // Done. Close up the module & write to the wrappers closeNamespaces(f_wrappers); Printf(f_wrappers, "#ifdef __cplusplus\n}\n#endif\n"); SwigType_emit_type_table(f_runtime, f_wrappers); /* NEW LANGUAGE NOTE:*********************************************** this basically combines several of the strings together and then writes it all to a file NEW LANGUAGE NOTE:END *********************************************** */ Dump(f_runtime, f_begin); Dump(f_header, f_begin); Dump(f_wrappers, f_begin); Dump(f_initbeforefunc, f_begin); /* for the Lua code it needs to be properly escaped to be added into the C/C++ code */ escapeCode(s_luacode); Printf(f_begin, "const char* SWIG_LUACODE=\n \"%s\";\n\n", s_luacode); Wrapper_pretty_print(f_init, f_begin); /* Close all of the files */ Delete(s_luacode); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_initbeforefunc); Delete(f_runtime); Delete(f_begin); /* Done */ return SWIG_OK; } /* ------------------------------------------------------------ * importDirective() * ------------------------------------------------------------ */ virtual int importDirective(Node *n) { return Language::importDirective(n); } /* ------------------------------------------------------------ * cDeclaration() * It copies sym:name to lua:name to preserve its original value * ------------------------------------------------------------ */ virtual int cDeclaration(Node *n) { // class 'Language' is messing with symname in a really heavy way. // Although documentation states that sym:name is a name in // the target language space, it is not true. sym:name and // its derivatives are used in various places, including // behind-the-scene C code generation. The best way is not to // touch it at all. // But we need to know what was the name of function/variable // etc that user desired, that's why we store correct symname // as lua:name String *symname = Getattr(n, "sym:name"); if (symname) Setattr(n, "lua:name", symname); return Language::cDeclaration(n); } virtual int constructorDeclaration(Node *n) { Setattr(n, "lua:name", Getattr(n, "sym:name")); return Language::constructorDeclaration(n); } virtual int destructorDeclaration(Node *n) { Setattr(n, "lua:name", Getattr(n, "sym:name")); return Language::destructorDeclaration(n); } /* NEW LANGUAGE NOTE:*********************************************** This is it! you get this one right, and most of your work is done but it's going to take some file to get it working right quite a bit of this is generally boilerplate code (or stuff I don't understand) that which matters will have extra added comments NEW LANGUAGE NOTE:END *********************************************** */ /* --------------------------------------------------------------------- * functionWrapper() * * Create a function declaration and register it with the interpreter. * --------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- * registerMethod() * * Determines wrap name of a method, its scope etc and calls * registerMethod overload with correct arguments * Overloaded variant adds method to the "methods" array of specified lua scope/class * ---------------------------------------------------------------------- */ void registerMethod(Node *n, bool overwrite = false, String *overwriteLuaScope = 0) { String *symname = Getattr(n, "sym:name"); assert(symname); if (Getattr(n, "sym:nextSibling")) return; // Lua scope. It is not symbol NSpace, it is the actual key to retrieve getCArraysHash. String *luaScope = luaCurrentSymbolNSpace(); if (overwrite) luaScope = overwriteLuaScope; String *wrapname = 0; String *mrename; if (current[NO_CPP] || !getCurrentClass()) { mrename = symname; } else { assert(!current[NO_CPP]); if (current[STATIC_FUNC] || current[MEMBER_FUNC]) { mrename = Swig_name_member(getNSpace(), getClassPrefix(), symname); } else { mrename = symname; } } wrapname = Swig_name_wrapper(mrename); registerMethod(n, wrapname, luaScope); } /* ----------------------------------------------------------------------- * registerMethod() * * Add method to the "methods" C array of given namespace/class * ---------------------------------------------------------------------- */ void registerMethod(Node *n, String* wname, String *luaScope) { assert(n); Hash *nspaceHash = getCArraysHash(luaScope); String *s_ns_methods_tab = Getattr(nspaceHash, "methods"); String *lua_name = Getattr(n, "lua:name"); if (elua_ltr || eluac_ltr) Printv(s_ns_methods_tab, tab4, "{LSTRKEY(\"", lua_name, "\")", ", LFUNCVAL(", wname, ")", "},\n", NIL); else Printv(s_ns_methods_tab, tab4, "{ \"", lua_name, "\", ", wname, "},\n", NIL); // Add to the metatable if method starts with '__' const char * tn = Char(lua_name); if (tn[0]=='_' && tn[1] == '_' && !eluac_ltr) { String *metatable_tab = Getattr(nspaceHash, "metatable"); assert(metatable_tab); if (elua_ltr) Printv(metatable_tab, tab4, "{LSTRKEY(\"", lua_name, "\")", ", LFUNCVAL(", wname, ")", "},\n", NIL); else Printv(metatable_tab, tab4, "{ \"", lua_name, "\", ", wname, "},\n", NIL); } } virtual int functionWrapper(Node *n) { REPORT("functionWrapper", n); String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); String *lua_name = Getattr(n, "lua:name"); assert(lua_name); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); Parm *p; String *tm; int i; //Printf(stdout,"functionWrapper %s %s %d\n",name,iname,current); String *overname = 0; if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } else { if (!luaAddSymbol(lua_name, n)) { return SWIG_ERROR; } } /* NEW LANGUAGE NOTE:*********************************************** the wrapper object holds all the wrapper code we need to add a couple of local variables NEW LANGUAGE NOTE:END *********************************************** */ Wrapper *f = NewWrapper(); Wrapper_add_local(f, "SWIG_arg", "int SWIG_arg = 0"); String *wname = Swig_name_wrapper(iname); if (overname) { Append(wname, overname); } if (current[CONSTRUCTOR]) { if (constructor_name != 0) Delete(constructor_name); constructor_name = Copy(wname); } /* NEW LANGUAGE NOTE:*********************************************** the format of a lua fn is: static int wrap_XXX(lua_State* L){...} this line adds this into the wrapper code NEW LANGUAGE NOTE:END *********************************************** */ Printv(f->def, "static int ", wname, "(lua_State* L) {", NIL); // SWIG_fail in lua leads to a call to lua_error() which calls longjmp() // which means the destructors of any live function-local C++ objects won't // get run. To avoid this happening, we wrap almost everything in the // function in a block, and end that right before lua_error() at which // point those destructors will get called. if (CPlusPlus) Append(f->def, "\n{"); /* NEW LANGUAGE NOTE:*********************************************** this prints the list of args, eg for a C fn int gcd(int x,int y); it will print int arg1; int arg2; NEW LANGUAGE NOTE:END *********************************************** */ /* Write code to extract function parameters. */ emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); Setattr(n, "wrap:parms", l); /* Get number of required and total arguments */ int num_arguments = emit_num_arguments(l); int num_required = emit_num_required(l); int varargs = emit_isvarargs(l); // Check if we have to ignore arguments that are passed by LUA. // Needed for unary minus, where lua passes two arguments and // we have to ignore the second. int args_to_ignore = 0; if (Getattr(n, "lua:ignore_args")) { args_to_ignore = GetInt(n, "lua:ignore_args"); } /* NEW LANGUAGE NOTE:*********************************************** from here on in, it gets rather hairy this is the code to convert from the scripting language to C/C++ some of the stuff will refer to the typemaps code written in your swig file (lua.swg), and some is done in the code here I suppose you could do all the conversion in C, but it would be a nightmare to do NEW LANGUAGE NOTE:END *********************************************** */ /* Generate code for argument marshalling */ // String *description = NewString(""); /* NEW LANGUAGE NOTE:*********************************************** argument_check is a new feature I added to check types of arguments: eg for int gcd(int,int) I want to check that arg1 & arg2 really are integers NEW LANGUAGE NOTE:END *********************************************** */ String *argument_check = NewString(""); String *argument_parse = NewString(""); String *checkfn = NULL; char source[64]; Printf(argument_check, "SWIG_check_num_args(\"%s\",%d,%d)\n", Swig_name_str(n), num_required + args_to_ignore, num_arguments + args_to_ignore); for (i = 0, p = l; i < num_arguments; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); /* Look for an input typemap */ sprintf(source, "%d", i + 1); if ((tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } /* NEW LANGUAGE NOTE:*********************************************** look for a 'checkfn' typemap this an additional parameter added to the in typemap if found the type will be tested for this will result in code either in the argument_check or argument_parse string NEW LANGUAGE NOTE:END *********************************************** */ if ((checkfn = Getattr(p, "tmap:in:checkfn"))) { if (i < num_required) { Printf(argument_check, "if(!%s(L,%s))", checkfn, source); } else { Printf(argument_check, "if(lua_gettop(L)>=%s && !%s(L,%s))", source, checkfn, source); } Printf(argument_check, " SWIG_fail_arg(\"%s\",%s,\"%s\");\n", Swig_name_str(n), source, SwigType_str(pt, 0)); } /* NEW LANGUAGE NOTE:*********************************************** lua states the number of arguments passed to a function using the fn lua_gettop() we can use this to deal with default arguments NEW LANGUAGE NOTE:END *********************************************** */ if (i < num_required) { Printf(argument_parse, "%s\n", tm); } else { Printf(argument_parse, "if(lua_gettop(L)>=%s){%s}\n", source, tm); } p = Getattr(p, "tmap:in:next"); continue; } else { /* NEW LANGUAGE NOTE:*********************************************** // why is this code not called when I don't have a typemap? // instead of giving a warning, no code is generated NEW LANGUAGE NOTE:END *********************************************** */ Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); break; } } // add all argcheck code Printv(f->code, argument_check, argument_parse, NIL); /* Check for trailing varargs */ if (varargs) { if (p && (tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", "varargs"); Printv(f->code, tm, "\n", NIL); } } /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ String *cleanup = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ String *outarg = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { // // managing the number of returning variables // if (numoutputs=Getattr(p,"tmap:argout:numoutputs")){ // int i=GetInt(p,"tmap:argout:numoutputs"); // printf("got argout:numoutputs of %d\n",i); // returnval+=GetInt(p,"tmap:argout:numoutputs"); // } // else returnval++; Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } // Remember C name of the wrapping function Setattr(n, "wrap:name", wname); /* Emit the function call */ String *actioncode = emit_action(n); /* NEW LANGUAGE NOTE:*********************************************** FIXME: returns 1 if there is a void return type this is because there is a typemap for void NEW LANGUAGE NOTE:END *********************************************** */ // Return value if necessary if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { // managing the number of returning variables // if (numoutputs=Getattr(tm,"numoutputs")){ // int i=GetInt(tm,"numoutputs"); // printf("return numoutputs %d\n",i); // returnval+=GetInt(tm,"numoutputs"); // } // else returnval++; if (GetFlag(n, "feature:new")) { Replaceall(tm, "$owner", "1"); } else { Replaceall(tm, "$owner", "0"); } Printf(f->code, "%s\n", tm); // returnval++; } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), name); } emit_return_variable(n, returntype, f); /* Output argument output code */ Printv(f->code, outarg, NIL); /* Output cleanup code */ Printv(f->code, cleanup, NIL); /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } } /* See if there is any return cleanup code */ if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } /* Close the function */ Printv(f->code, "return SWIG_arg;\n", NIL); // add the failure cleanup code: Printv(f->code, "\nfail: SWIGUNUSED;\n", "$cleanup", NIL); if (CPlusPlus) Append(f->code, "}\n"); Printv(f->code, "lua_error(L);\n", NIL); // lua_error() calls longjmp() but we need a dummy return to avoid compiler // warnings. Printv(f->code, "return 0;\n", NIL); Printf(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", iname); Replaceall(f->code, "$result", Swig_cresult_name()); /* Dump the function out */ /* in Lua we will not emit the destructor as a wrapper function, Lua will automatically call the destructor when the object is free'd However: you cannot just skip this function as it will not emit any custom destructor (using %extend), as you need to call emit_action() Therefore we go though the whole function, but do not write the code into the wrapper */ if (!current[DESTRUCTOR]) { Wrapper_print(f, f_wrappers); } /* NEW LANGUAGE NOTE:*********************************************** register the function in SWIG different language mappings seem to use different ideas NEW LANGUAGE NOTE:END *********************************************** */ /* Now register the function with the interpreter. */ int result = SWIG_OK; if (Getattr(n, "sym:overloaded")) { if (!Getattr(n, "sym:nextSibling")) { result = dispatchFunction(n); } } Delete(argument_check); Delete(argument_parse); Delete(cleanup); Delete(outarg); // Delete(description); DelWrapper(f); return result; } /* ------------------------------------------------------------ * dispatchFunction() * * Emit overloading dispatch function * ------------------------------------------------------------ */ /* NEW LANGUAGE NOTE:*********************************************** This is an extra function used for overloading of functions it checks the args & then calls the relevant fn most of the real work in again typemaps: look for %typecheck(SWIG_TYPECHECK_*) in the .swg file NEW LANGUAGE NOTE:END *********************************************** */ int dispatchFunction(Node *n) { //REPORT("dispatchFunction", n); /* Last node in overloaded chain */ int maxargs; bool check_emitted = false; String *tmp = NewString(""); String *dispatch = Swig_overload_dispatch(n, "return %s(L);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *f = NewWrapper(); String *symname = Getattr(n, "sym:name"); String *lua_name = Getattr(n, "lua:name"); assert(lua_name); String *wname = Swig_name_wrapper(symname); //Printf(stdout,"Swig_overload_dispatch %s %s '%s' %d\n",symname,wname,dispatch,maxargs); if (!luaAddSymbol(lua_name, n)) { DelWrapper(f); Delete(dispatch); Delete(tmp); return SWIG_ERROR; } Printv(f->def, "static int ", wname, "(lua_State* L) {", NIL); Wrapper_add_local(f, "argc", "int argc"); if (maxargs > 0 && check_emitted) { Printf(tmp, "int argv[%d]={1", maxargs + 1); for (int i = 1; i <= maxargs; i++) { Printf(tmp, ",%d", i + 1); } Printf(tmp, "}"); Wrapper_add_local(f, "argv", tmp); } Printf(f->code, "argc = lua_gettop(L);\n"); Replaceall(dispatch, "$args", "self,args"); Printv(f->code, dispatch, "\n", NIL); Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up String *protoTypes = NewString(""); do { String *fulldecl = Swig_name_decl(sibl); Printf(protoTypes, "\n\" %s\\n\"", fulldecl); Delete(fulldecl); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Printf(f->code, "SWIG_Lua_pusherrstring(L,\"Wrong arguments for overloaded function '%s'\\n\"\n" "\" Possible C/C++ prototypes are:\\n\"%s);\n", symname, protoTypes); Delete(protoTypes); Printf(f->code, "lua_error(L);return 0;\n"); Printv(f->code, "}\n", NIL); Wrapper_print(f, f_wrappers); // Remember C name of the wrapping function Setattr(n, "wrap:name", wname); if (current[CONSTRUCTOR]) { if (constructor_name != 0) Delete(constructor_name); constructor_name = Copy(wname); } DelWrapper(f); Delete(dispatch); Delete(tmp); return SWIG_OK; } /* ------------------------------------------------------------ * Add variable to "attributes" C arrays of given namespace or class. * Input is node. Based on the state of "current" array it determines * the name of the getter function, setter function etc and calls * registerVariable overload with necessary params. * Lua scope could be overwritten. (Used only for backward compatibility) * ------------------------------------------------------------ */ void registerVariable(Node *n, bool overwrite = false, String *overwriteLuaScope = 0) { int assignable = !is_immutable(n); String *symname = Getattr(n, "sym:name"); assert(symname); // Lua scope. It is not symbol NSpace, it is the actual key to retrieve getCArraysHash. String *luaScope = luaCurrentSymbolNSpace(); if (overwrite) luaScope = overwriteLuaScope; // Getter and setter String *getName = 0; String *setName = 0; String *mrename = 0; if (current[NO_CPP] || !getCurrentClass()) { // Global variable getName = Swig_name_get(getNSpace(), symname); if (assignable) setName = Swig_name_set(getNSpace(), symname); } else { assert(!current[NO_CPP]); if (current[STATIC_VAR] ) { mrename = Swig_name_member(getNSpace(), getClassPrefix(), symname); getName = Swig_name_get(0, mrename); if (assignable) setName = Swig_name_set(0, mrename); } else if (current[MEMBER_VAR]) { mrename = Swig_name_member(0, getClassPrefix(), symname); getName = Swig_name_get(getNSpace(), mrename); if (assignable) setName = Swig_name_set(getNSpace(), mrename); } else { assert(false); } } getName = Swig_name_wrapper(getName); if (setName) setName = Swig_name_wrapper(setName); registerVariable(luaScope, n, getName, setName); } /* ------------------------------------------------------------ * registerVariable() * * Add variable to the "attributes" (or "get"/"set" in * case of elua_ltr) C arrays of given namespace or class * ------------------------------------------------------------ */ void registerVariable(String *lua_nspace_or_class_name, Node *n, String *getName, String *setName) { String *unassignable = NewString("SWIG_Lua_set_immutable"); if (setName == 0 || GetFlag(n, "feature:immutable")) { setName = unassignable; } Hash *nspaceHash = getCArraysHash(lua_nspace_or_class_name); String *s_ns_methods_tab = Getattr(nspaceHash, "methods"); String *s_ns_var_tab = Getattr(nspaceHash, "attributes"); String *lua_name = Getattr(n, "lua:name"); if (elua_ltr) { String *s_ns_dot_get = Getattr(nspaceHash, "get"); String *s_ns_dot_set = Getattr(nspaceHash, "set"); Printf(s_ns_dot_get, "%s{LSTRKEY(\"%s\"), LFUNCVAL(%s)},\n", tab4, lua_name, getName); Printf(s_ns_dot_set, "%s{LSTRKEY(\"%s\"), LFUNCVAL(%s)},\n", tab4, lua_name, setName); } else if (eluac_ltr) { Printv(s_ns_methods_tab, tab4, "{LSTRKEY(\"", lua_name, "_get", "\")", ", LFUNCVAL(", getName, ")", "},\n", NIL); Printv(s_ns_methods_tab, tab4, "{LSTRKEY(\"", lua_name, "_set", "\")", ", LFUNCVAL(", setName, ")", "},\n", NIL); } else { Printf(s_ns_var_tab, "%s{ \"%s\", %s, %s },\n", tab4, lua_name, getName, setName); } } /* ------------------------------------------------------------ * variableWrapper() * ------------------------------------------------------------ */ virtual int variableWrapper(Node *n) { /* NEW LANGUAGE NOTE:*********************************************** Language::variableWrapper(n) will generate two wrapper fns Foo_get & Foo_set by calling functionWrapper() so we will just add these into the variable lists ideally we should not have registered these as functions, only WRT this variable will look into this later. NEW LANGUAGE NOTE:END *********************************************** */ // REPORT("variableWrapper", n); String *lua_name = Getattr(n, "lua:name"); assert(lua_name); (void)lua_name; current[VARIABLE] = true; // let SWIG generate the wrappers int result = Language::variableWrapper(n); // It is impossible to use registerVariable, because sym:name of the Node is currently // in an undefined state - the callees of this function may have modified it. // registerVariable should be used from respective callees.* current[VARIABLE] = false; return result; } /* ------------------------------------------------------------ * Add constant to appropriate C array. constantRecord is an array record. * Actually, in current implementation it is resolved consttab typemap * ------------------------------------------------------------ */ void registerConstant(String *nspace, String *constantRecord) { Hash *nspaceHash = getCArraysHash(nspace); String *s_const_tab = 0; if (eluac_ltr || elua_ltr) // In elua everything goes to "methods" tab s_const_tab = Getattr(nspaceHash, "methods"); else s_const_tab = Getattr(nspaceHash, "constants"); assert(s_const_tab); Printf(s_const_tab, " %s,\n", constantRecord); if ((eluac_ltr || elua_ltr) && old_metatable_bindings) { s_const_tab = Getattr(nspaceHash, "constants"); assert(s_const_tab); Printf(s_const_tab, " %s,\n", constantRecord); } } /* ------------------------------------------------------------ * constantWrapper() * ------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { REPORT("constantWrapper", n); String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); String *lua_name = Getattr(n, "lua:name"); if (lua_name == 0) lua_name = iname; String *nsname = Copy(iname); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *tm; String *lua_name_v2 = 0; String *tm_v2 = 0; String *iname_v2 = 0; Node *n_v2 = 0; if (!luaAddSymbol(lua_name, n)) return SWIG_ERROR; Swig_save("lua_constantMember", n, "sym:name", NIL); Setattr(n, "sym:name", lua_name); /* Special hook for member pointer */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(iname); Printf(f_wrappers, "static %s = %s;\n", SwigType_str(type, wname), value); value = Char(wname); } if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) { Replaceall(tm, "$value", value); Replaceall(tm, "$nsname", nsname); registerConstant(luaCurrentSymbolNSpace(), tm); } else if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) { Replaceall(tm, "$value", value); Replaceall(tm, "$nsname", nsname); Printf(f_init, "%s\n", tm); } else { Delete(nsname); nsname = 0; Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); Swig_restore(n); return SWIG_NOWRAP; } bool make_v2_compatible = old_metatable_bindings && getCurrentClass() && old_compatible_names; if (make_v2_compatible) { // Don't do anything for enums in C mode - they are already // wrapped correctly if (CPlusPlus || !current[ENUM_CONST]) { lua_name_v2 = Swig_name_member(0, proxy_class_name, lua_name); iname_v2 = Swig_name_member(0, proxy_class_name, iname); n_v2 = Copy(n); if (!luaAddSymbol(iname_v2, n, getNSpace())) { Swig_restore(n); return SWIG_ERROR; } Setattr(n_v2, "sym:name", lua_name_v2); tm_v2 = Swig_typemap_lookup("consttab", n_v2, name, 0); if (tm_v2) { Replaceall(tm_v2, "$value", value); Replaceall(tm_v2, "$nsname", nsname); registerConstant(getNSpace(), tm_v2); } else { tm_v2 = Swig_typemap_lookup("constcode", n_v2, name, 0); if (!tm_v2) { // This can't be. assert(false); Swig_restore(n); return SWIG_ERROR; } Replaceall(tm_v2, "$value", value); Replaceall(tm_v2, "$nsname", nsname); Printf(f_init, "%s\n", tm_v2); } Delete(n_v2); } } Swig_restore(n); Delete(nsname); return SWIG_OK; } /* ------------------------------------------------------------ * nativeWrapper() * ------------------------------------------------------------ */ virtual int nativeWrapper(Node *n) { // REPORT("nativeWrapper", n); String *symname = Getattr(n, "sym:name"); String *wrapname = Getattr(n, "wrap:name"); if (!luaAddSymbol(wrapname, n)) return SWIG_ERROR; Hash *nspaceHash = getCArraysHash(getNSpace()); String *s_ns_methods_tab = Getattr(nspaceHash, "methods"); Printv(s_ns_methods_tab, tab4, "{ \"", symname, "\",", wrapname, "},\n", NIL); // return Language::nativeWrapper(n); // this does nothing... return SWIG_OK; } /* ------------------------------------------------------------ * enumDeclaration() * ------------------------------------------------------------ */ virtual int enumDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; current[STATIC_CONST] = true; current[ENUM_CONST] = true; // There is some slightly specific behaviour with enums. Basically, // their NSpace may be tracked separately. The code below tries to work around // this issue to some degree. // The idea is the same as in classHandler - to drop old names generation if // enum is in class in namespace. const int old_compatible_names_saved = old_compatible_names; if (getNSpace() || ( Getattr(n, "sym:nspace") != 0 && Len(Getattr(n, "sym:nspace")) > 0 ) ) { old_compatible_names = 0; } int result = Language::enumDeclaration(n); current[STATIC_CONST] = false; current[ENUM_CONST] = false; old_compatible_names = old_compatible_names_saved; return result; } /* ------------------------------------------------------------ * enumvalueDeclaration() * ------------------------------------------------------------ */ virtual int enumvalueDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; Swig_require("enumvalueDeclaration", n, "*name", "?value", "*sym:name", NIL); String *symname = Getattr(n, "sym:name"); String *value = Getattr(n, "value"); String *name = Getattr(n, "name"); String *tmpValue; Node *parent = parentNode(n); if (value) tmpValue = NewString(value); else tmpValue = NewString(name); Setattr(n, "value", tmpValue); Setattr(n, "name", tmpValue); /* for wrapping of enums in a namespace when emit_action is used */ if (GetFlag(parent, "scopedenum")) { symname = Swig_name_member(0, Getattr(parent, "sym:name"), symname); Setattr(n, "sym:name", symname); Delete(symname); } int result = constantWrapper(n); Delete(tmpValue); Swig_restore(n); return result; } /* ------------------------------------------------------------ * classDeclaration() * ------------------------------------------------------------ */ virtual int classDeclaration(Node *n) { return Language::classDeclaration(n); } /* ------------------------------------------------------------ * Helper function that adds record to appropriate C arrays * ------------------------------------------------------------ */ void registerClass(String *scope, String *wrap_class) { assert(wrap_class); Hash *nspaceHash = getCArraysHash(scope); String *ns_classes = Getattr(nspaceHash, "classes"); Printv(ns_classes, "&", wrap_class, ",\n", NIL); if (elua_ltr || eluac_ltr) { String *ns_methods = Getattr(nspaceHash, "methods"); Hash *class_hash = getCArraysHash(class_static_nspace); assert(class_hash); String *cls_methods = Getattr(class_hash, "methods:name"); assert(cls_methods); Printv(ns_methods, tab4, "{LSTRKEY(\"", proxy_class_name, "\")", ", LROVAL(", cls_methods, ")", "},\n", NIL); } } /* ------------------------------------------------------------ * classHandler() * ------------------------------------------------------------ */ virtual int classHandler(Node *n) { //REPORT("classHandler", n); String *mangled_full_proxy_class_name = 0; String *destructor_name = 0; String *nspace = getNSpace(); constructor_name = 0; have_constructor = 0; have_destructor = 0; destructor_action = 0; assert(class_static_nspace == 0); assert(full_proxy_class_name == 0); assert(proxy_class_name == 0); current[NO_CPP] = false; proxy_class_name = Getattr(n, "sym:name"); // We have to enforce nspace here, because technically we are already // inside class parsing (getCurrentClass != 0), but we should register // class in its parent namespace if (!luaAddSymbol(proxy_class_name, n, nspace)) return SWIG_ERROR; if (nspace == 0) full_proxy_class_name = NewStringf("%s", proxy_class_name); else full_proxy_class_name = NewStringf("%s.%s", nspace, proxy_class_name); assert(full_proxy_class_name); mangled_full_proxy_class_name = Swig_name_mangle_string(full_proxy_class_name); SwigType *t = Copy(Getattr(n, "name")); SwigType *fr_t = SwigType_typedef_resolve_all(t); /* Create fully resolved type */ SwigType *t_tmp = 0; t_tmp = SwigType_typedef_qualified(fr_t); // Temporary variable Delete(fr_t); fr_t = SwigType_strip_qualifiers(t_tmp); String *mangled_fr_t = 0; mangled_fr_t = SwigType_manglestr(fr_t); // not sure exactly how this works, // but tcl has a static hashtable of all classes emitted and then only emits code for them once. // this fixes issues in test suites: template_default2 & template_specialization // * if i understand correctly, this is a bug. // * consider effect on template_specialization_defarg static Hash *emitted = NewHash(); if (GetFlag(emitted, mangled_fr_t)) { full_proxy_class_name = 0; proxy_class_name = 0; return SWIG_NOWRAP; } SetFlag(emitted, mangled_fr_t); // We treat class T as both 'class' and 'namespace'. All static members, attributes // and constants are considered part of namespace T, all members - part of the 'class' // Now, here is a trick. Static methods, attributes and non-static methods and attributes // are described with same structures - swig_lua_attribute/swig_lua_method. Instead of calling // getCArraysHash(class name) to initialize things for static methods/attributes and then // manually doing same initialization for non-static methods, we call getCArraysHash 2 times: // 1) With name "class name" + "." + "SwigStatic" to initialize static things // 2) With "class name" to initialize non-static things Hash *instance_cls = getCArraysHash(full_proxy_class_name, false); assert(instance_cls); String *s_attr_tab_name = Getattr(instance_cls, "attributes:name"); String *s_methods_tab_name = Getattr(instance_cls, "methods:name"); SetFlag(instance_cls, "lua:no_namespaces"); SetFlag(instance_cls, "lua:no_classes"); SetFlag(instance_cls, "lua:class_instance"); /* There is no use for "constants", "classes" and "namespaces" arrays. * All constants are considered part of static part of class. */ class_static_nspace = NewStringf("%s%sSwigStatic", full_proxy_class_name, NSPACE_SEPARATOR); Hash *static_cls = getCArraysHash(class_static_nspace, false); assert(static_cls); SetFlag(static_cls, "lua:no_namespaces"); SetFlag(static_cls, "lua:class_static"); // Notifying instance_cls and static_cls hashes about each other Setattr(instance_cls, "lua:class_instance:static_hash", static_cls); Setattr(static_cls, "lua:class_static:instance_hash", instance_cls); const int old_compatible_names_saved = old_compatible_names; // If class has %nspace enabled, then generation of backward compatible names // should be disabled if (getNSpace()) { old_compatible_names = 0; } /* There is no use for "classes" and "namespaces" arrays. Subclasses are not supported * by SWIG and namespaces couldn't be nested inside classes (C++ Standard) */ // Generate normal wrappers Language::classHandler(n); old_compatible_names = old_compatible_names_saved; SwigType_add_pointer(t); // Catch all: eg. a class with only static functions and/or variables will not have 'remembered' String *wrap_class_name = Swig_name_wrapper(NewStringf("class_%s", mangled_full_proxy_class_name)); String *wrap_class = NewStringf("&%s", wrap_class_name); SwigType_remember_clientdata(t, wrap_class); String *rt = Copy(getClassType()); SwigType_add_pointer(rt); // Adding class to appropriate namespace registerClass(nspace, wrap_class_name); Hash *nspaceHash = getCArraysHash(nspace); // Register the class structure with the type checker // Printf(f_init,"SWIG_TypeClientData(SWIGTYPE%s, (void *) &_wrap_class_%s);\n", SwigType_manglestr(t), mangled_full_proxy_class_name); // emit a function to be called to delete the object if (have_destructor) { destructor_name = NewStringf("swig_delete_%s", mangled_full_proxy_class_name); Printv(f_wrappers, "static void ", destructor_name, "(void *obj) {\n", NIL); if (destructor_action) { Printv(f_wrappers, SwigType_str(rt, "arg1"), " = (", SwigType_str(rt, 0), ") obj;\n", NIL); Printv(f_wrappers, destructor_action, "\n", NIL); } else { if (CPlusPlus) { Printv(f_wrappers, " delete (", SwigType_str(rt, 0), ") obj;\n", NIL); } else { Printv(f_wrappers, " free((char *) obj);\n", NIL); } } Printf(f_wrappers, "}\n"); } // Wrap constructor wrapper into one more proxy function. It will be used as class namespace __call method, thus // allowing both // Module.ClassName.StaticMethod to access static method/variable/constant // Module.ClassName() to create new object if (have_constructor) { assert(constructor_name); String *constructor_proxy_name = NewStringf("_proxy_%s", constructor_name); Printv(f_wrappers, "static int ", constructor_proxy_name, "(lua_State *L) {\n", NIL); Printv(f_wrappers, tab4, "assert(lua_istable(L,1));\n", tab4, "lua_pushcfunction(L,", constructor_name, ");\n", tab4, "assert(!lua_isnil(L,-1));\n", tab4, "lua_replace(L,1); /* replace our table with real constructor */\n", tab4, "lua_call(L,lua_gettop(L)-1,1);\n", tab4, "return 1;\n}\n", NIL); Delete(constructor_name); constructor_name = constructor_proxy_name; if (elua_ltr) { String *static_cls_metatable_tab = Getattr(static_cls, "metatable"); assert(static_cls_metatable_tab); Printf(static_cls_metatable_tab, " {LSTRKEY(\"__call\"), LFUNCVAL(%s)},\n", constructor_name); } else if (eluac_ltr) { String *ns_methods_tab = Getattr(nspaceHash, "methods"); assert(ns_methods_tab); Printv(ns_methods_tab, tab4, "{LSTRKEY(\"", "new_", proxy_class_name, "\")", ", LFUNCVAL(", constructor_name, ")", "},\n", NIL); } } if (have_destructor) { if (eluac_ltr) { String *ns_methods_tab = Getattr(nspaceHash, "methods"); assert(ns_methods_tab); Printv(ns_methods_tab, tab4, "{LSTRKEY(\"", "free_", mangled_full_proxy_class_name, "\")", ", LFUNCVAL(", destructor_name, ")", "},\n", NIL); } } closeCArraysHash(full_proxy_class_name, f_wrappers); closeCArraysHash(class_static_nspace, f_wrappers); // Handle inheritance // note: with the idea of class hierarchies spread over multiple modules // cf test-suite: imports.i // it is not possible to just add the pointers to the base classes to the code // (as sometimes these classes are not present) // therefore we instead hold the name of the base class and a null pointer // at runtime: we can query the swig type manager & see if the class exists // if so, we can get the pointer to the base class & replace the null pointer // if the type does not exist, then we cannot... String *base_class = NewString(""); String *base_class_names = NewString(""); List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator b; b = First(baselist); while (b.item) { String *bname = Getattr(b.item, "name"); if ((!bname) || GetFlag(b.item, "feature:ignore") || (!Getattr(b.item, "module"))) { b = Next(b); continue; } // stores a null pointer & the name Printf(base_class, "0,"); Printf(base_class_names, "\"%s *\",", SwigType_namestr(bname)); b = Next(b); } } // First, print class static part printCArraysDefinition(class_static_nspace, proxy_class_name, f_wrappers); assert(mangled_full_proxy_class_name); assert(base_class); assert(base_class_names); assert(proxy_class_name); assert(full_proxy_class_name); // Then print class instance part Printv(f_wrappers, "static swig_lua_class *swig_", mangled_full_proxy_class_name, "_bases[] = {", base_class, "0};\n", NIL); Delete(base_class); Printv(f_wrappers, "static const char *swig_", mangled_full_proxy_class_name, "_base_names[] = {", base_class_names, "0};\n", NIL); Delete(base_class_names); Printv(f_wrappers, "static swig_lua_class _wrap_class_", mangled_full_proxy_class_name, " = { \"", proxy_class_name, "\", \"", full_proxy_class_name, "\", &SWIGTYPE", SwigType_manglestr(t), ",", NIL); if (have_constructor) { Printv(f_wrappers, constructor_name, NIL); Delete(constructor_name); constructor_name = 0; } else { Printf(f_wrappers, "0"); } if (have_destructor) { Printv(f_wrappers, ", ", destructor_name, NIL); } else { Printf(f_wrappers, ",0"); } Printf(f_wrappers, ", %s, %s, &%s", s_methods_tab_name, s_attr_tab_name, Getattr(static_cls, "cname")); if (!eluac_ltr) { Printf(f_wrappers, ", %s", Getattr(instance_cls,"metatable:name")); } else Printf(f_wrappers, ", 0"); Printf(f_wrappers, ", swig_%s_bases, swig_%s_base_names };\n\n", mangled_full_proxy_class_name, mangled_full_proxy_class_name); current[NO_CPP] = true; Delete(class_static_nspace); class_static_nspace = 0; Delete(mangled_full_proxy_class_name); mangled_full_proxy_class_name = 0; Delete(destructor_name); destructor_name = 0; Delete(full_proxy_class_name); full_proxy_class_name = 0; proxy_class_name = 0; return SWIG_OK; } /* ------------------------------------------------------------ * memberfunctionHandler() * ------------------------------------------------------------ */ virtual int memberfunctionHandler(Node *n) { String *symname = GetChar(n, "sym:name"); //Printf(stdout,"memberfunctionHandler %s %s\n",name,iname); // Special case unary minus: LUA passes two parameters for the // wrapper function while we want only one. Tell our // functionWrapper to ignore a parameter. if (Cmp(symname, "__unm") == 0) { //Printf(stdout, "unary minus: ignore one argument\n"); SetInt(n, "lua:ignore_args", 1); } current[MEMBER_FUNC] = true; Language::memberfunctionHandler(n); registerMethod(n); current[MEMBER_FUNC] = false; return SWIG_OK; } /* ------------------------------------------------------------ * membervariableHandler() * ------------------------------------------------------------ */ virtual int membervariableHandler(Node *n) { // REPORT("membervariableHandler",n); current[MEMBER_VAR] = true; Language::membervariableHandler(n); registerVariable(n); current[MEMBER_VAR] = false; return SWIG_OK; } /* ------------------------------------------------------------ * constructorHandler() * * Method for adding C++ member constructor * ------------------------------------------------------------ */ virtual int constructorHandler(Node *n) { // REPORT("constructorHandler", n); current[CONSTRUCTOR] = true; Language::constructorHandler(n); current[CONSTRUCTOR] = false; //constructor_name = NewString(Getattr(n, "sym:name")); have_constructor = 1; return SWIG_OK; } /* ------------------------------------------------------------ * destructorHandler() * ------------------------------------------------------------ */ virtual int destructorHandler(Node *n) { REPORT("destructorHandler", n); current[DESTRUCTOR] = true; Language::destructorHandler(n); current[DESTRUCTOR] = false; have_destructor = 1; destructor_action = Getattr(n, "wrap:action"); return SWIG_OK; } /* ---------------------------------------------------------------------- * globalfunctionHandler() * * It can be called: * 1. Usual C/C++ global function. * 2. During class parsing for functions declared/defined as friend * 3. During class parsing from staticmemberfunctionHandler * ---------------------------------------------------------------------- */ virtual int globalfunctionHandler(Node *n) { bool oldVal = current[NO_CPP]; if (!current[STATIC_FUNC]) // If static function, don't switch to NO_CPP current[NO_CPP] = true; const int result = Language::globalfunctionHandler(n); if (!current[STATIC_FUNC]) // Register only if not called from static function handler registerMethod(n); current[NO_CPP] = oldVal; return result; } /* ---------------------------------------------------------------------- * globalvariableHandler() * * Sets "current" array correctly * ---------------------------------------------------------------------- */ virtual int globalvariableHandler(Node *n) { bool oldVal = current[NO_CPP]; current[GLOBAL_VAR] = true; current[NO_CPP] = true; const int result = Language::globalvariableHandler(n); registerVariable(n); current[GLOBAL_VAR] = false; current[NO_CPP] = oldVal; return result; } /* ----------------------------------------------------------------------- * staticmemberfunctionHandler() * * Wrap a static C++ function * ---------------------------------------------------------------------- */ virtual int staticmemberfunctionHandler(Node *n) { REPORT("staticmemberfunctionHandler", n); current[STATIC_FUNC] = true; const int result = Language::staticmemberfunctionHandler(n); registerMethod(n); if (old_metatable_bindings && result == SWIG_OK && old_compatible_names) { Swig_require("lua_staticmemberfunctionHandler", n, "*lua:name", NIL); String *lua_name = Getattr(n, "lua:name"); // Although this function uses Swig_name_member, it actually generates the Lua name, // not the C++ name. This is because an earlier version used such a scheme for static function // name generation and we have to maintain backward compatibility. String *compat_name = Swig_name_member(0, proxy_class_name, lua_name); Setattr(n, "lua:name", compat_name); registerMethod(n, true, getNSpace()); Delete(compat_name); Swig_restore(n); } current[STATIC_FUNC] = false;; return result; } /* ------------------------------------------------------------ * memberconstantHandler() * * Create a C++ constant * ------------------------------------------------------------ */ virtual int memberconstantHandler(Node *n) { REPORT("memberconstantHandler", n); int result = Language::memberconstantHandler(n); return result; } /* --------------------------------------------------------------------- * staticmembervariableHandler() * --------------------------------------------------------------------- */ virtual int staticmembervariableHandler(Node *n) { REPORT("staticmembervariableHandler", n); current[STATIC_VAR] = true; //String *symname = Getattr(n, "sym:name"); int result = Language::staticmembervariableHandler(n); if (!GetFlag(n, "wrappedasconstant")) { registerVariable(n); } if (result == SWIG_OK) { // This will add static member variable to the class namespace with name ClassName_VarName if (old_metatable_bindings && old_compatible_names) { Swig_save("lua_staticmembervariableHandler", n, "lua:name", NIL); String *lua_name = Getattr(n, "lua:name"); // Although this function uses Swig_name_member, it actually generates the Lua name, // not the C++ name. This is because an earlier version used such a scheme for static function // name generation and we have to maintain backward compatibility. String *v2_name = Swig_name_member(NIL, proxy_class_name, lua_name); if (!GetFlag(n, "wrappedasconstant")) { Setattr(n, "lua:name", v2_name); // Registering static var in the class parent nspace registerVariable(n, true, getNSpace()); } // If static member variable was wrapped as a constant, then // constant wrapper has already performed all actions necessary for old_metatable_bindings Delete(v2_name); Swig_restore(n); } } current[STATIC_VAR] = false; return result; } /* --------------------------------------------------------------------- * external runtime generation * --------------------------------------------------------------------- */ /* This is to support the usage SWIG -external-runtime The code consists of two functions: String *runtimeCode() // returns a large string with all the runtimes in String *defaultExternalRuntimeFilename() // returns the default filename I am writing a generic solution, even though SWIG-Lua only has one file right now... */ String *runtimeCode() { String *s = NewString(""); const char *filenames[] = { "luarun.swg", 0 }; // must be 0 terminated emitLuaFlavor(s); String *sfile = 0; for (int i = 0; filenames[i] != 0; i++) { sfile = Swig_include_sys(filenames[i]); if (!sfile) { Printf(stderr, "*** Unable to open '%s'\n", filenames[i]); } else { Append(s, sfile); Delete(sfile); } } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigluarun.h"); } /* --------------------------------------------------------------------- * helpers * --------------------------------------------------------------------- */ void emitLuaFlavor(String *s) { if (elua_emulate) { Printf(s, "/*This is only emulation!*/\n"); Printf(s, "#define SWIG_LUA_TARGET SWIG_LUA_FLAVOR_ELUA\n"); Printf(s, "#define SWIG_LUA_ELUA_EMULATE\n"); } else if (elua_ltr) Printf(s, "#define SWIG_LUA_TARGET SWIG_LUA_FLAVOR_ELUA\n"); else if (eluac_ltr) Printf(s, "#define SWIG_LUA_TARGET SWIG_LUA_FLAVOR_ELUAC\n"); else Printf(s, "#define SWIG_LUA_TARGET SWIG_LUA_FLAVOR_LUA\n"); } /* ----------------------------------------------------------------------------- * escapeCode() * * This is to convert the string of Lua code into a proper string, which can then be * emitted into the C/C++ code. * Basically it is a lot of search & replacing of odd sequences * ---------------------------------------------------------------------------- */ void escapeCode(String *str) { //Printf(f_runtime,"/* original luacode:[[[\n%s\n]]]\n*/\n",str); Chop(str); // trim Replace(str, "\\", "\\\\", DOH_REPLACE_ANY); // \ to \\ (this must be done first) Replace(str, "\"", "\\\"", DOH_REPLACE_ANY); // " to \" Replace(str, "\n", "\\n\"\n \"", DOH_REPLACE_ANY); // \n to \n"\n" (ie quoting every line) //Printf(f_runtime,"/* hacked luacode:[[[\n%s\n]]]\n*/\n",str); } /* ----------------------------------------------------------------------------- * rawGetCArraysHash(String *name) * * A small helper to hide implementation of how CArrays hashes are stored * ---------------------------------------------------------------------------- */ Hash *rawGetCArraysHash(const_String_or_char_ptr name) { Hash *scope = symbolScopeLookup( name ? name : "" ); if(!scope) return 0; Hash *carrays_hash = Getattr(scope, "lua:cdata"); return carrays_hash; } /* ----------------------------------------------------------------------------- * getCArraysHash() * * Each namespace can be described with a hash that stores C arrays * where members of the namespace should be added. All these hashes are stored * inside the symbols table, in pseudo-symbol for every namespace. * nspace could be NULL (NSPACE_TODO), that means functions and variables and classes * that are not in any namespace (this is default for SWIG unless %nspace feature is used). * You can later set some attributes that will affect behaviour of functions that use this hash: * "lua:no_namespaces" will disable "namespaces" array. * "lua:no_classes" will disable "classes" array. * For every component ("attributes", "methods", etc) there are subcomponents: * XXX:name - name of the C array that stores data for component * XXX:decl - statement with forward declaration of this array; * Namespace could be automatically registered to its parent if 'reg' == true. This can only be * done during the first call (a.k.a when nspace is created). * ---------------------------------------------------------------------------- */ Hash *getCArraysHash(String *nspace, bool reg = true) { Hash *scope = symbolScopeLookup(nspace ? nspace : ""); if(!scope) { symbolAddScope( nspace ? nspace : "" ); scope = symbolScopeLookup(nspace ? nspace : ""); assert(scope); } Hash *carrays_hash = Getattr(scope, "lua:cdata"); if (carrays_hash != 0) return carrays_hash; carrays_hash = NewHash(); String *mangled_name = 0; if (nspace == 0 || Len(nspace) == 0) mangled_name = NewString("SwigModule"); else mangled_name = Swig_name_mangle_string(nspace); String *cname = NewStringf("swig_%s", mangled_name); Setattr(carrays_hash, "cname", cname); String *attr_tab = NewString(""); String *attr_tab_name = NewStringf("swig_%s_attributes", mangled_name); String *attr_tab_decl = NewString(""); Printv(attr_tab, "static swig_lua_attribute ", NIL); Printv(attr_tab, attr_tab_name, "[]", NIL); Printv(attr_tab_decl, attr_tab, ";\n", NIL); Printv(attr_tab, " = {\n", NIL); Setattr(carrays_hash, "attributes", attr_tab); Setattr(carrays_hash, "attributes:name", attr_tab_name); Setattr(carrays_hash, "attributes:decl", attr_tab_decl); String *methods_tab = NewString(""); String *methods_tab_name = NewStringf("swig_%s_methods", mangled_name); String *methods_tab_decl = NewString(""); if (elua_ltr || eluac_ltr) // In this case methods array also acts as namespace rotable Printf(methods_tab, "const LUA_REG_TYPE "); else Printf(methods_tab, "static swig_lua_method "); Printv(methods_tab, methods_tab_name, "[]", NIL); Printv(methods_tab_decl, methods_tab, ";\n", NIL); Printv(methods_tab, "= {\n", NIL); Setattr(carrays_hash, "methods", methods_tab); Setattr(carrays_hash, "methods:name", methods_tab_name); Setattr(carrays_hash, "methods:decl", methods_tab_decl); String *const_tab = NewString(""); String *const_tab_name = NewStringf("swig_%s_constants", mangled_name); String *const_tab_decl = NewString(""); if (elua_ltr || eluac_ltr) // In this case const array holds rotable with namespace constants Printf(const_tab, "const LUA_REG_TYPE "); else Printf(const_tab, "static swig_lua_const_info "); Printv(const_tab, const_tab_name, "[]", NIL); Printv(const_tab_decl, const_tab, ";", NIL); Printv(const_tab, "= {\n", NIL); Setattr(carrays_hash, "constants", const_tab); Setattr(carrays_hash, "constants:name", const_tab_name); Setattr(carrays_hash, "constants:decl", const_tab_decl); String *classes_tab = NewString(""); String *classes_tab_name = NewStringf("swig_%s_classes", mangled_name); String *classes_tab_decl = NewString(""); Printf(classes_tab, "static swig_lua_class* "); Printv(classes_tab, classes_tab_name, "[]", NIL); Printv(classes_tab_decl, classes_tab, ";", NIL); Printv(classes_tab, "= {\n", NIL); Setattr(carrays_hash, "classes", classes_tab); Setattr(carrays_hash, "classes:name", classes_tab_name); Setattr(carrays_hash, "classes:decl", classes_tab_decl); String *namespaces_tab = NewString(""); String *namespaces_tab_name = NewStringf("swig_%s_namespaces", mangled_name); String *namespaces_tab_decl = NewString(""); Printf(namespaces_tab, "static swig_lua_namespace* "); Printv(namespaces_tab, namespaces_tab_name, "[]", NIL); Printv(namespaces_tab_decl, namespaces_tab, ";", NIL); Printv(namespaces_tab, " = {\n", NIL); Setattr(carrays_hash, "namespaces", namespaces_tab); Setattr(carrays_hash, "namespaces:name", namespaces_tab_name); Setattr(carrays_hash, "namespaces:decl", namespaces_tab_decl); if (elua_ltr) { String *get_tab = NewString(""); String *get_tab_name = NewStringf("swig_%s_get", mangled_name); String *get_tab_decl = NewString(""); Printv(get_tab, "const LUA_REG_TYPE ", get_tab_name, "[]", NIL); Printv(get_tab_decl, get_tab, ";", NIL); Printv(get_tab, " = {\n", NIL); Setattr(carrays_hash, "get", get_tab); Setattr(carrays_hash, "get:name", get_tab_name); Setattr(carrays_hash, "get:decl", get_tab_decl); String *set_tab = NewString(""); String *set_tab_name = NewStringf("swig_%s_set", mangled_name); String *set_tab_decl = NewString(""); Printv(set_tab, "const LUA_REG_TYPE ", set_tab_name, "[]", NIL); Printv(set_tab_decl, set_tab, ";", NIL); Printv(set_tab, " = {\n", NIL); Setattr(carrays_hash, "set", set_tab); Setattr(carrays_hash, "set:name", set_tab_name); Setattr(carrays_hash, "set:decl", set_tab_decl); } if (!eluac_ltr) { String *metatable_tab = NewString(""); String *metatable_tab_name = NewStringf("swig_%s_meta", mangled_name); String *metatable_tab_decl = NewString(""); if (elua_ltr) // In this case const array holds rotable with namespace constants Printf(metatable_tab, "const LUA_REG_TYPE "); else Printf(metatable_tab, "static swig_lua_method "); Printv(metatable_tab, metatable_tab_name, "[]", NIL); Printv(metatable_tab_decl, metatable_tab, ";", NIL); Printv(metatable_tab, " = {\n", NIL); Setattr(carrays_hash, "metatable", metatable_tab); Setattr(carrays_hash, "metatable:name", metatable_tab_name); Setattr(carrays_hash, "metatable:decl", metatable_tab_decl); } Setattr(scope, "lua:cdata", carrays_hash); assert(rawGetCArraysHash(nspace)); if (reg && nspace != 0 && Len(nspace) != 0 && GetFlag(carrays_hash, "lua:no_reg") == 0) { // Split names into components List *components = Split(nspace, '.', -1); String *parent_path = NewString(""); int len = Len(components); String *name = Copy(Getitem(components, len - 1)); for (int i = 0; i < len - 1; i++) { if (i > 0) Printv(parent_path, NSPACE_SEPARATOR, NIL); String *item = Getitem(components, i); Printv(parent_path, item, NIL); } Hash *parent = getCArraysHash(parent_path, true); String *namespaces_tab = Getattr(parent, "namespaces"); Printv(namespaces_tab, "&", cname, ",\n", NIL); if (elua_ltr || eluac_ltr) { String *methods_tab = Getattr(parent, "methods"); Printv(methods_tab, tab4, "{LSTRKEY(\"", name, "\")", ", LROVAL(", methods_tab_name, ")", "},\n", NIL); } Setattr(carrays_hash, "name", name); Delete(components); Delete(parent_path); } else if (!reg) // This namespace shouldn't be registered. Lets remember it. SetFlag(carrays_hash, "lua:no_reg"); Delete(mangled_name); mangled_name = 0; return carrays_hash; } /* ----------------------------------------------------------------------------- * closeCArraysHash() * * Functions add end markers {0,0,...,0} to all arrays, prints them to * output and marks hash as closed (lua:closed). Consequent attempts to * close the same hash will result in an error. * closeCArraysHash DOES NOT print structure that describes namespace, it only * prints array. You can use printCArraysDefinition to print structure. * if "lua:no_namespaces" is set, then array for "namespaces" won't be printed * if "lua:no_classes" is set, then array for "classes" won't be printed * ----------------------------------------------------------------------------- */ void closeCArraysHash(String *nspace, File *output) { Hash *carrays_hash = rawGetCArraysHash(nspace); assert(carrays_hash); assert(GetFlag(carrays_hash, "lua:closed") == 0); SetFlag(carrays_hash, "lua:closed"); // Do arrays describe class instance part or class static part const int is_instance = GetFlag(carrays_hash, "lua:class_instance"); String *attr_tab = Getattr(carrays_hash, "attributes"); Printf(attr_tab, " {0,0,0}\n};\n"); Printv(output, attr_tab, NIL); String *const_tab = Getattr(carrays_hash, "constants"); String *const_tab_name = Getattr(carrays_hash, "constants:name"); if (elua_ltr || eluac_ltr) Printv(const_tab, tab4, "{LNILKEY, LNILVAL}\n", "};\n", NIL); else Printf(const_tab, " {0,0,0,0,0,0}\n};\n"); // For the sake of compiling with -Wall -Werror we print constants // only when necessary int need_constants = 0; if ( (elua_ltr || eluac_ltr) && (old_metatable_bindings) ) need_constants = 1; else if (!is_instance) // static part need constants tab need_constants = 1; if (need_constants) Printv(output, const_tab, NIL); if (elua_ltr) { // Put forward declaration of metatable array Printv(output, "extern ", Getattr(carrays_hash, "metatable:decl"), "\n", NIL); } String *methods_tab = Getattr(carrays_hash, "methods"); String *metatable_tab_name = Getattr(carrays_hash, "metatable:name"); if (elua_ltr || eluac_ltr) { if (old_metatable_bindings) Printv(methods_tab, tab4, "{LSTRKEY(\"const\"), LROVAL(", const_tab_name, ")},\n", NIL); if (elua_ltr) { Printv(methods_tab, tab4, "{LSTRKEY(\"__metatable\"), LROVAL(", metatable_tab_name, ")},\n", NIL); } Printv(methods_tab, tab4, "{LSTRKEY(\"__disown\"), LFUNCVAL(SWIG_Lua_class_disown)},\n", NIL); Printv(methods_tab, tab4, "{LNILKEY, LNILVAL}\n};\n", NIL); } else Printf(methods_tab, " {0,0}\n};\n"); Printv(output, methods_tab, NIL); if (!GetFlag(carrays_hash, "lua:no_classes")) { String *classes_tab = Getattr(carrays_hash, "classes"); Printf(classes_tab, " 0\n};\n"); Printv(output, classes_tab, NIL); } if (!GetFlag(carrays_hash, "lua:no_namespaces")) { String *namespaces_tab = Getattr(carrays_hash, "namespaces"); Printf(namespaces_tab, " 0\n};\n"); Printv(output, namespaces_tab, NIL); } if (elua_ltr) { String *get_tab = Getattr(carrays_hash, "get"); String *set_tab = Getattr(carrays_hash, "set"); Printv(get_tab, tab4, "{LNILKEY, LNILVAL}\n};\n", NIL); Printv(set_tab, tab4, "{LNILKEY, LNILVAL}\n};\n", NIL); Printv(output, get_tab, NIL); Printv(output, set_tab, NIL); } // Heuristic whether we need to print metatable or not. // For the sake of compiling with -Wall -Werror we don't print // metatable for static part. int need_metatable = 0; if (eluac_ltr) need_metatable = 0; else if(!is_instance) need_metatable = 0; else need_metatable = 1; if (need_metatable) { String *metatable_tab = Getattr(carrays_hash, "metatable"); assert(metatable_tab); if (elua_ltr) { String *get_tab_name = Getattr(carrays_hash, "get:name"); String *set_tab_name = Getattr(carrays_hash, "set:name"); if (GetFlag(carrays_hash, "lua:class_instance")) { Printv(metatable_tab, tab4, "{LSTRKEY(\"__index\"), LFUNCVAL(SWIG_Lua_class_get)},\n", NIL); Printv(metatable_tab, tab4, "{LSTRKEY(\"__newindex\"), LFUNCVAL(SWIG_Lua_class_set)},\n", NIL); } else { Printv(metatable_tab, tab4, "{LSTRKEY(\"__index\"), LFUNCVAL(SWIG_Lua_namespace_get)},\n", NIL); Printv(metatable_tab, tab4, "{LSTRKEY(\"__newindex\"), LFUNCVAL(SWIG_Lua_namespace_set)},\n", NIL); } Printv(metatable_tab, tab4, "{LSTRKEY(\"__gc\"), LFUNCVAL(SWIG_Lua_class_destruct)},\n", NIL); Printv(metatable_tab, tab4, "{LSTRKEY(\".get\"), LROVAL(", get_tab_name, ")},\n", NIL); Printv(metatable_tab, tab4, "{LSTRKEY(\".set\"), LROVAL(", set_tab_name, ")},\n", NIL); Printv(metatable_tab, tab4, "{LSTRKEY(\".fn\"), LROVAL(", Getattr(carrays_hash, "methods:name"), ")},\n", NIL); if (GetFlag(carrays_hash, "lua:class_instance")) { String *static_cls = Getattr(carrays_hash, "lua:class_instance:static_hash"); assert(static_cls); // static_cls is swig_lua_namespace. This structure can't be used with eLua(LTR) // Instead a structure describing its methods is used String *static_cls_cname = Getattr(static_cls, "methods:name"); assert(static_cls_cname); Printv(metatable_tab, tab4, "{LSTRKEY(\".static\"), LROVAL(", static_cls_cname, ")},\n", NIL); // Put forward declaration of this array Printv(output, "extern ", Getattr(static_cls, "methods:decl"), "\n", NIL); } else if (GetFlag(carrays_hash, "lua:class_static")) { Hash *instance_cls = Getattr(carrays_hash, "lua:class_static:instance_hash"); assert(instance_cls); String *instance_cls_metatable_name = Getattr(instance_cls, "metatable:name"); assert(instance_cls_metatable_name); Printv(metatable_tab, tab4, "{LSTRKEY(\".instance\"), LROVAL(", instance_cls_metatable_name, ")},\n", NIL); } Printv(metatable_tab, tab4, "{LNILKEY, LNILVAL}\n};\n", NIL); } else { Printf(metatable_tab, " {0,0}\n};\n"); } Printv(output, metatable_tab, NIL); } Printv(output, "\n", NIL); } /* ----------------------------------------------------------------------------- * closeNamespaces() * * Recursively close all non-closed namespaces. Prints data to dataOutput. * ----------------------------------------------------------------------------- */ void closeNamespaces(File *dataOutput) { // Special handling for empty module. if (symbolScopeLookup("") == 0 || rawGetCArraysHash("") == 0) { // Module is empty. Create hash for global scope in order to have swig_SwigModule // variable in resulting file getCArraysHash(0); } // Because we can't directly access 'symtabs', instead we access // top-level scope and look on all scope pseudo-symbols in it. Hash *top_scope = symbolScopeLookup(""); assert(top_scope); Iterator ki = First(top_scope); List *to_close = NewList(); while (ki.key) { assert(ki.item); if (Getattr(ki.item, "sym:scope")) { // We have a pseudo symbol. Lets get actual scope for this pseudo symbol Hash *carrays_hash = rawGetCArraysHash(ki.key); assert(carrays_hash); if (GetFlag(carrays_hash, "lua:closed") == 0) Append(to_close, ki.key); } ki = Next(ki); } SortList(to_close, &compareByLen); int len = Len(to_close); for (int i = 0; i < len; i++) { String *key = Getitem(to_close, i); closeCArraysHash(key, dataOutput); Hash *carrays_hash = rawGetCArraysHash(key); String *name = 0; // name - name of the namespace as it should be visible in Lua if (Len(key) == 0) // This is global module name = module; else name = Getattr(carrays_hash, "name"); assert(name); printCArraysDefinition(key, name, dataOutput); } Delete(to_close); } /* ----------------------------------------------------------------------------- * printCArraysDefinition() * * This function prints to output a definition of namespace in form * swig_lua_namespace $cname = { attr_array, methods_array, ... , namespaces_array }; * You can call this function as many times as is necessary. * 'name' is a user-visible name that this namespace will have in Lua. It shouldn't * be a fully qualified name, just its own name. * ----------------------------------------------------------------------------- */ void printCArraysDefinition(String *nspace, String *name, File *output) { Hash *carrays_hash = getCArraysHash(nspace, false); String *cname = Getattr(carrays_hash, "cname"); // cname - name of the C structure that describes namespace assert(cname); Printv(output, "static swig_lua_namespace ", cname, " = ", NIL); String *null_string = NewString("0"); String *attr_tab_name = Getattr(carrays_hash, "attributes:name"); String *methods_tab_name = Getattr(carrays_hash, "methods:name"); String *const_tab_name = Getattr(carrays_hash, "constants:name"); String *classes_tab_name = Getattr(carrays_hash, "classes:name"); String *namespaces_tab_name = Getattr(carrays_hash, "namespaces:name"); bool has_classes = GetFlag(carrays_hash, "lua:no_classes") == 0; bool has_namespaces = GetFlag(carrays_hash, "lua:no_namespaces") == 0; Printv(output, "{\n", tab4, "\"", name, "\",\n", tab4, methods_tab_name, ",\n", tab4, attr_tab_name, ",\n", tab4, const_tab_name, ",\n", tab4, (has_classes) ? classes_tab_name : null_string, ",\n", tab4, (has_namespaces) ? namespaces_tab_name : null_string, "\n};\n", NIL); Delete(null_string); } /* ----------------------------------------------------------------------------- * luaCurrentSymbolNSpace() * * This function determines actual namespace/scope where any symbol at the * current moment should be placed. It looks at the 'current' array * and depending on where are we - static class member/function, * instance class member/function or just global functions decides * where symbol should be put. * The namespace/scope doesn't depend from symbol, only from 'current' * ----------------------------------------------------------------------------- */ String *luaCurrentSymbolNSpace() { String *scope = 0; // If outside class, than NSpace is used. // If inside class, but current[NO_CPP], then this is friend function. It belongs to NSpace if (!getCurrentClass() || current[NO_CPP]) { scope = getNSpace(); } else if (current[ENUM_CONST] && !CPlusPlus ) { // Enums in C mode go to NSpace scope = getNSpace(); } else { // If inside class, then either class static namespace or class fully qualified name is used assert(!current[NO_CPP]); if (current[STATIC_FUNC] || current[STATIC_VAR] || current[STATIC_CONST]) { scope = class_static_nspace; } else if (current[MEMBER_VAR] || current[CONSTRUCTOR] || current[DESTRUCTOR] || current[MEMBER_FUNC]) { scope = full_proxy_class_name; } else { // Friend functions are handled this way scope = class_static_nspace; } assert(scope); } return scope; } /* ----------------------------------------------------------------------------- * luaAddSymbol() * * Our implementation of addSymbol. Determines scope correctly, then * forwards to Language::addSymbol * ----------------------------------------------------------------------------- */ int luaAddSymbol(const String *s, const Node *n) { String *scope = luaCurrentSymbolNSpace(); return luaAddSymbol(s, n, scope); } /* ----------------------------------------------------------------------------- * luaAddSymbol() * * Overload. Enforces given scope. Actually, it simply forwards call to Language::addSymbol * ----------------------------------------------------------------------------- */ int luaAddSymbol(const String *s, const Node *n, const_String_or_char_ptr scope) { int result = Language::addSymbol(s, n, scope); if (!result) Printf(stderr, "addSymbol(%s to scope %s) failed\n", s, scope); return result; } }; /* ----------------------------------------------------------------------------- * swig_lua() - Instantiate module * ----------------------------------------------------------------------------- */ extern "C" Language *swig_lua(void) { return new LUA(); } swig-4.4.0/Source/Modules/ruby.cxx0000664000175000017500000032740515075443613016750 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * ruby.cxx * * Ruby language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include #include #include /* for INT_MAX */ #define SWIG_PROTECTED_TARGET_METHODS 1 class RClass { private: String *temp; public: String *name; /* class name (renamed) */ String *cname; /* original C class/struct name */ String *mname; /* Mangled name */ /** * The C variable name used in the SWIG-generated wrapper code to refer to * this class; usually it is of the form "SwigClassXXX.klass", where SwigClassXXX * is a swig_class struct instance and klass is a member of that struct. */ String *vname; /** * The C variable name used in the SWIG-generated wrapper code to refer to * the module that implements this class's methods (when we're trying to * support C++ multiple inheritance). Usually it is of the form * "SwigClassClassName.mImpl", where SwigClassXXX is a swig_class struct instance * and mImpl is a member of that struct. */ String *mImpl; String *type; String *prefix; String *init; int constructor_defined; int destructor_defined; RClass() { temp = NewString(""); name = NewString(""); cname = NewString(""); mname = NewString(""); vname = NewString(""); mImpl = NewString(""); type = NewString(""); prefix = NewString(""); init = NewString(""); constructor_defined = 0; destructor_defined = 0; } ~RClass() { Delete(name); Delete(cname); Delete(vname); Delete(mImpl); Delete(mname); Delete(type); Delete(prefix); Delete(init); Delete(temp); } void set_name(const_String_or_char_ptr cn, const_String_or_char_ptr rn, const_String_or_char_ptr valn) { /* Original C/C++ class (or struct) name */ Clear(cname); Append(cname, cn); /* Mangled name */ Delete(mname); mname = Swig_name_mangle_string(cname); /* Renamed class name */ Clear(name); Append(name, valn); /* Variable name for the VALUE that refers to the Ruby Class object */ Clear(vname); Printf(vname, "SwigClass%s.klass", name); /* Variable name for the VALUE that refers to the Ruby Class object */ Clear(mImpl); Printf(mImpl, "SwigClass%s.mImpl", name); /* Prefix */ Clear(prefix); Printv(prefix, (rn ? rn : cn), "_", NIL); } char *strip(const_String_or_char_ptr s) { Clear(temp); if (Strncmp(s, prefix, Len(prefix)) == 0) { Append(temp, Char(s) + Len(prefix)); } else { Append(temp, s); } return Char(temp); } }; /* flags for the make_autodoc function */ namespace { enum autodoc_t { AUTODOC_CLASS, AUTODOC_CTOR, AUTODOC_DTOR, AUTODOC_STATICFUNC, AUTODOC_FUNC, AUTODOC_METHOD, AUTODOC_GETTER, AUTODOC_SETTER, AUTODOC_NONE }; } static const char *usage = "\ Ruby Options (available with -ruby)\n\ -autorename - Enable renaming of classes and methods to follow Ruby coding standards\n\ -globalmodule - Wrap everything into the global module\n\ -initname - Set entry function to Init_ (used by `require')\n\ -minherit - Attempt to support multiple inheritance\n\ -noautorename - Disable renaming of classes and methods (default)\n\ -prefix - Set a prefix to be prepended to all names\n\ "; #define RCLASS(hash, name) (RClass*)(Getattr(hash, name) ? Data(Getattr(hash, name)) : 0) #define SET_RCLASS(hash, name, klass) Setattr(hash, name, NewVoid(klass, 0)) class RUBY:public Language { private: String *module; String *modvar; String *feature; String *prefix; int current; Hash *classes; /* key=cname val=RClass */ RClass *klass; /* Currently processing class */ Hash *special_methods; /* Python style special method name table */ File *f_directors; File *f_directors_h; File *f_directors_helpers; File *f_begin; File *f_runtime; File *f_runtime_h; File *f_header; File *f_wrappers; File *f_init; File *f_initbeforefunc; bool useGlobalModule; bool multipleInheritance; // Wrap modes enum WrapperMode { NO_CPP, MEMBER_FUNC, CONSTRUCTOR_ALLOCATE, CONSTRUCTOR_INITIALIZE, DESTRUCTOR, MEMBER_VAR, CLASS_CONST, STATIC_FUNC, STATIC_VAR }; /* ------------------------------------------------------------ * autodoc level declarations * ------------------------------------------------------------ */ enum autodoc_l { NO_AUTODOC = -2, // no autodoc STRING_AUTODOC = -1, // use provided string NAMES_AUTODOC = 0, // only parameter names TYPES_AUTODOC = 1, // parameter names and types EXTEND_AUTODOC = 2, // extended documentation and parameter names EXTEND_TYPES_AUTODOC = 3 // extended documentation and parameter types + names }; autodoc_t last_mode; String* last_autodoc; autodoc_l autodoc_level(String *autodoc) { autodoc_l dlevel = NO_AUTODOC; char *c = Char(autodoc); if (c) { if (isdigit(c[0])) { dlevel = (autodoc_l) atoi(c); } else { if (strcmp(c, "extended") == 0) { dlevel = EXTEND_AUTODOC; } else { dlevel = STRING_AUTODOC; } } } return dlevel; } /* ------------------------------------------------------------ * have_docstring() * Check if there is a docstring directive and it has text, * or there is an autodoc flag set * ------------------------------------------------------------ */ bool have_docstring(Node *n) { String *str = Getattr(n, "feature:docstring"); return (str && Len(str) > 0) || (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc")); } /* ------------------------------------------------------------ * docstring() * Get the docstring text, stripping off {} if necessary, * and enclose in triple double quotes. If autodoc is also * set then it will build a combined docstring. * ------------------------------------------------------------ */ String *docstring(Node *n, autodoc_t ad_type) { String *str = Getattr(n, "feature:docstring"); bool have_ds = (str && Len(str) > 0); bool have_auto = (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc")); String *autodoc = NULL; String *doc = NULL; if (have_ds) { char *t = Char(str); if (*t == '{') { Delitem(str, 0); Delitem(str, DOH_END); } } if (have_auto) { autodoc = make_autodoc(n, ad_type); have_auto = (autodoc && Len(autodoc) > 0); } if (have_auto || have_ds) doc = NewString("/*"); if (have_auto && have_ds) { // Both autodoc and docstring are present Printv(doc, "\n", autodoc, "\n", str, "\n", NIL); } else if (!have_auto && have_ds) { // only docstring Printv(doc, str, NIL); } else if (have_auto && !have_ds) { // only autodoc Printv(doc, "\n", autodoc, "\n", NIL); } else { doc = NewString(""); } if (have_auto || have_ds) Append(doc, "*/\n"); // Save the generated strings in the parse tree in case they are used later // by post processing tools Setattr(n, "ruby:docstring", doc); Setattr(n, "ruby:autodoc", autodoc); return doc; } /* ----------------------------------------------------------------------------- * addMissingParameterNames() * For functions that have not had nameless parameters set in the Language class. * * Inputs: * plist - entire parameter list * arg_offset - argument number for first parameter * Side effects: * The "lname" attribute in each parameter in plist will be contain a parameter name * ----------------------------------------------------------------------------- */ void addMissingParameterNames(Node* n, ParmList *plist, int arg_offset) { Parm *p = plist; int i = arg_offset; while (p) { if (!Getattr(p, "lname")) { String *name = makeParameterName(n, p, i); Setattr(p, "lname", name); Delete(name); } i++; p = nextSibling(p); } } /* ------------------------------------------------------------ * make_autodocParmList() * Generate the documentation for the function parameters * ------------------------------------------------------------ */ String *make_autodocParmList(Node *n, bool showTypes) { String *doc = NewString(""); String *pdocs = 0; ParmList *plist = CopyParmList(Getattr(n, "parms")); Parm *p; Parm *pnext; int lines = 0; int arg_num = is_wrapping_class() ? 1 : 0; const int maxwidth = 80; addMissingParameterNames(n, plist, arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms Swig_typemap_attach_parms("in", plist, 0); Swig_typemap_attach_parms("doc", plist, 0); if (Strcmp(ParmList_protostr(plist), "void") == 0) { //No parameters actually return doc; } for (p = plist; p; p = pnext, arg_num++) { String *tm = Getattr(p, "tmap:in"); if (tm) { pnext = Getattr(p, "tmap:in:next"); if (checkAttribute(p, "tmap:in:numinputs", "0")) { continue; } } else { pnext = nextSibling(p); } String *name = 0; String *type = 0; String *value = 0; String *pdoc = Getattr(p, "tmap:doc"); if (pdoc) { name = Getattr(p, "tmap:doc:name"); type = Getattr(p, "tmap:doc:type"); value = Getattr(p, "tmap:doc:value"); } // Note: the generated name should be consistent with that in kwnames[] String *made_name = 0; if (!name) { name = made_name = makeParameterName(n, p, arg_num); } type = type ? type : Getattr(p, "type"); value = value ? value : Getattr(p, "value"); if (SwigType_isvarargs(type)) break; // Skip the 'self' parameter which in ruby is implicit if ( Cmp(name, "self") == 0 ) continue; // Make __p parameters just p (as used in STL) Replace( name, "__", "", DOH_REPLACE_FIRST ); if (Len(doc)) { // add a comma to the previous one if any Append(doc, ", "); // Do we need to wrap a long line? if ((Len(doc) - lines * maxwidth) > maxwidth) { Printf(doc, "\n%s", tab4); lines += 1; } } // Do the param type too? Node *nn = classLookup(Getattr(p, "type")); String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0); if (showTypes) Printf(doc, "%s ", type_str); Append(doc, name); if (pdoc) { if (!pdocs) pdocs = NewString("Parameters:\n"); Printf(pdocs, " %s.\n", pdoc); } if (value) { String *new_value = convertValue(value, Getattr(p, "numval"), Getattr(p, "stringval"), Getattr(p, "type")); if (new_value) { value = new_value; } else { Node *lookup = Swig_symbol_clookup(value, 0); if (lookup) value = Getattr(lookup, "sym:name"); } Printf(doc, "=%s", value); if (new_value) Delete(new_value); } Delete(type_str); Delete(made_name); } if (pdocs) Setattr(n, "feature:pdocs", pdocs); Delete(plist); return doc; } /* ------------------------------------------------------------ * make_autodoc() * Build a docstring for the node, using parameter and other * info in the parse tree. If the value of the autodoc * attribute is "0" then do not include parameter types, if * it is "1" (the default) then do. If it has some other * value then assume it is supplied by the extension writer * and use it directly. * ------------------------------------------------------------ */ String *make_autodoc(Node *n, autodoc_t ad_type) { int extended = 0; // If the function is overloaded then this function is called // for the last one. Rewind to the first so the docstrings are // in order. while (Getattr(n, "sym:previousSibling")) n = Getattr(n, "sym:previousSibling"); Node *pn = Swig_methodclass(n); String* super_names = NewString(""); String* class_name = Getattr(pn, "sym:name") ; if ( !class_name ) { class_name = NewString(""); } else { class_name = Copy(class_name); List *baselist = Getattr(pn, "bases"); if (baselist && Len(baselist)) { Iterator base = First(baselist); while (base.item && GetFlag(base.item, "feature:ignore")) { base = Next(base); } int count = 0; for ( ;base.item; ++count) { if ( count ) Append(super_names, ", "); String *basename = Getattr(base.item, "sym:name"); String* basenamestr = NewString(basename); Node* parent = parentNode(base.item); while (parent) { String *parent_name = Copy( Getattr(parent, "sym:name") ); if ( !parent_name ) { Node* mod = Getattr(parent, "module"); if ( mod ) parent_name = Copy( Getattr(mod, "name") ); if ( parent_name ) (Char(parent_name))[0] = (char)toupper((Char(parent_name))[0]); } if ( parent_name ) { Insert(basenamestr, 0, "::"); Insert(basenamestr, 0, parent_name); Delete(parent_name); } parent = parentNode(parent); } Append(super_names, basenamestr ); Delete(basenamestr); base = Next(base); } } } String* full_name; if ( module ) { full_name = NewString(module); if (Len(class_name) > 0) Append(full_name, "::"); } else full_name = NewString(""); Append(full_name, class_name); String* symname = Getattr(n, "sym:name"); if ( Getattr( special_methods, symname ) ) symname = Getattr( special_methods, symname ); String* methodName = NewString(full_name); Append(methodName, symname); // Each overloaded function will try to get documented, // so we keep the name of the last overloaded function and its type. // Documenting just from functionWrapper() is not possible as // sym:name has already been changed to include the class name if ( last_mode == ad_type && Cmp(methodName, last_autodoc) == 0 ) { Delete(full_name); Delete(class_name); Delete(super_names); Delete(methodName); return NewString(""); } last_mode = ad_type; last_autodoc = Copy(methodName); String *doc = NewString(""); int counter = 0; bool skipAuto = false; Node* on = n; for ( ; n; ++counter ) { String *type_str = NULL; skipAuto = false; bool showTypes = false; String *autodoc = Getattr(n, "feature:autodoc"); autodoc_l dlevel = autodoc_level(autodoc); switch (dlevel) { case NO_AUTODOC: break; case NAMES_AUTODOC: showTypes = false; break; case TYPES_AUTODOC: showTypes = true; break; case EXTEND_AUTODOC: extended = 1; showTypes = false; break; case EXTEND_TYPES_AUTODOC: extended = 1; showTypes = true; break; case STRING_AUTODOC: skipAuto = true; break; } SwigType *type = Getattr(n, "type"); if (type) { if (Strcmp(type, "void") == 0) { type_str = NULL; } else { SwigType *qt = SwigType_typedef_resolve_all(type); if (SwigType_isenum(qt)) { type_str = NewString("int"); } else { Node *nn = classLookup(type); type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0); } } } if (counter == 0) { switch (ad_type) { case AUTODOC_CLASS: Printf(doc, " Document-class: %s", full_name); if ( Len(super_names) > 0 ) Printf( doc, " < %s", super_names); Append(doc, "\n\n"); break; case AUTODOC_CTOR: Printf(doc, " Document-method: %s.new\n\n", full_name); break; case AUTODOC_DTOR: break; case AUTODOC_STATICFUNC: Printf(doc, " Document-method: %s.%s\n\n", full_name, symname); break; case AUTODOC_FUNC: case AUTODOC_METHOD: case AUTODOC_GETTER: Printf(doc, " Document-method: %s.%s\n\n", full_name, symname); break; case AUTODOC_SETTER: Printf(doc, " Document-method: %s.%s=\n\n", full_name, symname); break; case AUTODOC_NONE: break; } } if (skipAuto) { if ( counter == 0 ) Printf(doc, " call-seq:\n"); switch( ad_type ) { case AUTODOC_STATICFUNC: case AUTODOC_FUNC: case AUTODOC_METHOD: case AUTODOC_GETTER: { String *paramList = make_autodocParmList(n, showTypes); if (Len(paramList)) Printf(doc, " %s(%s)", symname, paramList); else Printf(doc, " %s", symname); if (type_str) Printf(doc, " -> %s", type_str); break; } case AUTODOC_SETTER: { Printf(doc, " %s=(x)", symname); if (type_str) Printf(doc, " -> %s", type_str); break; } default: break; } } else { switch (ad_type) { case AUTODOC_CLASS: { // Only do the autodoc if there isn't a docstring for the class String *str = Getattr(n, "feature:docstring"); if (counter == 0 && (str == 0 || Len(str) == 0)) { if (CPlusPlus) { Printf(doc, " Proxy of C++ %s class", full_name); } else { Printf(doc, " Proxy of C %s struct", full_name); } } } break; case AUTODOC_CTOR: if (counter == 0) Printf(doc, " call-seq:\n"); if (Strcmp(class_name, symname) == 0) { String *paramList = make_autodocParmList(n, showTypes); if (Len(paramList)) Printf(doc, " %s.new(%s)", class_name, paramList); else Printf(doc, " %s.new", class_name); } else { Printf(doc, " %s.new(%s)", class_name, make_autodocParmList(n, showTypes)); } break; case AUTODOC_DTOR: break; case AUTODOC_STATICFUNC: case AUTODOC_FUNC: case AUTODOC_METHOD: case AUTODOC_GETTER: { if (counter == 0) Printf(doc, " call-seq:\n"); String *paramList = make_autodocParmList(n, showTypes); if (Len(paramList)) Printf(doc, " %s(%s)", symname, paramList); else Printf(doc, " %s", symname); if (type_str) Printf(doc, " -> %s", type_str); break; } case AUTODOC_SETTER: { Printf(doc, " call-seq:\n"); Printf(doc, " %s=(x)", symname); if (type_str) Printf(doc, " -> %s", type_str); break; } case AUTODOC_NONE: break; } } // if it's overloaded then get the next decl and loop around again n = Getattr(n, "sym:nextSibling"); if (n) Append(doc, "\n"); Delete(type_str); } Printf(doc, "\n\n"); if (!skipAuto) { switch (ad_type) { case AUTODOC_CLASS: case AUTODOC_DTOR: break; case AUTODOC_CTOR: Printf(doc, "Class constructor.\n"); break; case AUTODOC_STATICFUNC: Printf(doc, "A class method.\n"); break; case AUTODOC_FUNC: Printf(doc, "A module function.\n"); break; case AUTODOC_METHOD: Printf(doc, "An instance method.\n"); break; case AUTODOC_GETTER: Printf(doc, "Get value of attribute.\n"); break; case AUTODOC_SETTER: Printf(doc, "Set new value for attribute.\n"); break; case AUTODOC_NONE: break; } } n = on; while ( n ) { String *autodoc = Getattr(n, "feature:autodoc"); autodoc_l dlevel = autodoc_level(autodoc); switch (dlevel) { case NO_AUTODOC: case NAMES_AUTODOC: case TYPES_AUTODOC: extended = 0; break; case STRING_AUTODOC: extended = 2; Replaceall( autodoc, "$class", class_name ); Printv(doc, autodoc, ".", NIL); break; case EXTEND_AUTODOC: case EXTEND_TYPES_AUTODOC: extended = 1; break; } if (extended) { String *pdocs = Getattr(n, "feature:pdocs"); if (pdocs) { Printv(doc, "\n\n", pdocs, NULL); break; } if ( extended == 2 ) break; } n = Getattr(n, "sym:nextSibling"); } Delete(full_name); Delete(class_name); Delete(super_names); Delete(methodName); return doc; } /* ------------------------------------------------------------ * convertValue() * Check if string v can be a Ruby value literal, * (eg. number or string), or translate it to a Ruby literal. * ------------------------------------------------------------ */ String *convertValue(String *v, String *numval, String *stringval, SwigType *type) { if (stringval) { return NewStringf("\"%(escape)s\"", stringval); } if (numval) { SwigType *resolved_type = SwigType_typedef_resolve_all(type); SwigType *unqualified_type = NIL; if (SwigType_isreference(resolved_type)) { SwigType *t = Copy(resolved_type); t = SwigType_del_reference(t); unqualified_type = SwigType_strip_qualifiers(t); Delete(t); } else { unqualified_type = SwigType_strip_qualifiers(resolved_type); } if (Equal(unqualified_type, "bool")) { Delete(resolved_type); Delete(unqualified_type); return NewString(*Char(numval) == '0' ? "false" : "true"); } Delete(resolved_type); Delete(unqualified_type); if (SwigType_ispointer(type) && Equal(v, "0")) return NewString("None"); return Copy(v); } if (v && Len(v) > 0) { if (Equal(v, "NULL") || Equal(v, "nullptr")) return SwigType_ispointer(type) ? NewString("nil") : NewString("0"); // FIXME: TRUE and FALSE are not standard and could be defined in other ways if (Equal(v, "TRUE")) return NewString("true"); if (Equal(v, "FALSE")) return NewString("false"); } return 0; } public: /* --------------------------------------------------------------------- * RUBY() * * Initialize member data * --------------------------------------------------------------------- */ RUBY() : module(0), modvar(0), feature(0), prefix(0), current(0), classes(0), klass(0), special_methods(0), f_directors(0), f_directors_h(0), f_directors_helpers(0), f_begin(0), f_runtime(0), f_runtime_h(0), f_header(0), f_wrappers(0), f_init(0), f_initbeforefunc(0), useGlobalModule(false), multipleInheritance(false), last_mode(AUTODOC_NONE), last_autodoc(NewString("")) { current = NO_CPP; director_prot_ctor_code = NewString(""); Printv(director_prot_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " rb_raise(rb_eRuntimeError,\"accessing abstract class or protected constructor\"); \n", " return Qnil;\n", "}\n", NIL); director_multiple_inheritance = 0; directorLanguage(); } /* --------------------------------------------------------------------- * main() * * Parse command line options and initializes variables. * --------------------------------------------------------------------- */ virtual void main(int argc, char *argv[]) { int autorename = 0; /* Set location of SWIG library */ SWIG_library_directory("ruby"); /* Look for certain command line options */ for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-initname") == 0) { if (argv[i + 1]) { char *name = argv[i + 1]; feature = NewString(name); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-globalmodule") == 0) { useGlobalModule = true; Swig_mark_arg(i); } else if (strcmp(argv[i], "-minherit") == 0) { multipleInheritance = true; director_multiple_inheritance = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-autorename") == 0) { autorename = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-noautorename") == 0) { autorename = 0; Swig_mark_arg(i); } else if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { char *name = argv[i + 1]; prefix = NewString(name); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-help") == 0) { Printf(stdout, "%s\n", usage); } else if (strcmp(argv[i], "-cppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is now always on.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is no longer supported.\n", argv[i]); Swig_mark_arg(i); Exit(EXIT_FAILURE); } } } if (autorename) { /* Turn on the autorename mode */ Preprocessor_define((DOH *) "SWIG_RUBY_AUTORENAME", 0); } /* Add a symbol to the parser for conditional compilation */ Preprocessor_define("SWIGRUBY 1", 0); SWIG_config_file("ruby.swg"); allow_overloading(); } /** * Generate initialization code to define the Ruby module(s), * accounting for nested modules as necessary. */ void defineRubyModule() { List *modules = Split(module, ':', INT_MAX); if (modules != 0 && Len(modules) > 0) { String *mv = 0; Iterator m; m = First(modules); while (m.item) { if (Len(m.item) > 0) { if (mv != 0) { Printv(f_init, tab4, modvar, " = rb_define_module_under(", modvar, ", \"", m.item, "\");\n", NIL); } else { Printv(f_init, tab4, modvar, " = rb_define_module(\"", m.item, "\");\n", NIL); mv = NewString(modvar); } } m = Next(m); } Delete(mv); Delete(modules); } } void registerMagicMethods() { special_methods = NewHash(); /* Python->Ruby style special method name. */ /* Basic */ Setattr(special_methods, "__repr__", "inspect"); Setattr(special_methods, "__str__", "to_s"); Setattr(special_methods, "__cmp__", "<=>"); Setattr(special_methods, "__hash__", "hash"); Setattr(special_methods, "__nonzero__", "nonzero?"); /* Callable */ Setattr(special_methods, "__call__", "call"); /* Collection */ Setattr(special_methods, "__len__", "length"); Setattr(special_methods, "__getitem__", "[]"); Setattr(special_methods, "__setitem__", "[]="); /* Operators */ Setattr(special_methods, "__add__", "+"); Setattr(special_methods, "__pos__", "+@"); Setattr(special_methods, "__sub__", "-"); Setattr(special_methods, "__neg__", "-@"); Setattr(special_methods, "__mul__", "*"); Setattr(special_methods, "__div__", "/"); Setattr(special_methods, "__mod__", "%"); Setattr(special_methods, "__lshift__", "<<"); Setattr(special_methods, "__rshift__", ">>"); Setattr(special_methods, "__and__", "&"); Setattr(special_methods, "__or__", "|"); Setattr(special_methods, "__xor__", "^"); Setattr(special_methods, "__invert__", "~"); Setattr(special_methods, "__lt__", "<"); Setattr(special_methods, "__le__", "<="); Setattr(special_methods, "__gt__", ">"); Setattr(special_methods, "__ge__", ">="); Setattr(special_methods, "__eq__", "=="); /* Other numeric */ Setattr(special_methods, "__divmod__", "divmod"); Setattr(special_methods, "__pow__", "**"); Setattr(special_methods, "__abs__", "abs"); Setattr(special_methods, "__int__", "to_i"); Setattr(special_methods, "__float__", "to_f"); Setattr(special_methods, "__coerce__", "coerce"); } /* --------------------------------------------------------------------- * top() * --------------------------------------------------------------------- */ virtual int top(Node *n) { String *mod_docstring = NULL; /** * See if any Ruby module options have been specified as options * to the %module directive. */ Node *swigModule = Getattr(n, "module"); if (swigModule) { Node *options = Getattr(swigModule, "options"); if (options) { if (Getattr(options, "directors")) { allow_directors(); } if (Getattr(options, "dirprot")) { allow_dirprot(); } if (Getattr(options, "ruby_globalmodule")) { useGlobalModule = true; } if (Getattr(options, "ruby_minherit")) { multipleInheritance = true; director_multiple_inheritance = 1; } mod_docstring = Getattr(options, "docstring"); } } /* Set comparison with none for ConstructorToFunction */ setSubclassInstanceCheck(NewStringf("strcmp(rb_obj_classname(self), classname) != 0")); // setSubclassInstanceCheck(NewString("CLASS_OF(self) != cFoo.klass")); /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = Getattr(n, "outfile_h"); if (!outfile) { Printf(stderr, "Unable to determine outfile\n"); Exit(EXIT_FAILURE); } f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); f_directors_helpers = NewString(""); f_initbeforefunc = NewString(""); if (Swig_directors_enabled()) { if (!outfile_h) { Printf(stderr, "Unable to determine outfile_h\n"); Exit(EXIT_FAILURE); } f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); Swig_register_filebyname("director_helpers", f_directors_helpers); Swig_register_filebyname("initbeforefunc", f_initbeforefunc); modvar = 0; current = NO_CPP; klass = 0; classes = NewHash(); registerMagicMethods(); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "RUBY"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); } Printf(f_runtime, "\n"); /* typedef void *VALUE */ SwigType *value = NewSwigType(T_VOID); SwigType_add_pointer(value); SwigType_typedef(value, "VALUE"); Delete(value); /* Set module name */ set_module(Char(Getattr(n, "name"))); if (Swig_directors_enabled()) { /* Build a version of the module name for use in a C macro name. */ String *module_macro = Copy(module); Replaceall(module_macro, "::", "__"); Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module_macro); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module_macro); Printf(f_directors_h, "namespace Swig {\n"); Printf(f_directors_h, " class Director;\n"); Printf(f_directors_h, "}\n\n"); Printf(f_directors_helpers, "/* ---------------------------------------------------\n"); Printf(f_directors_helpers, " * C++ director class helpers\n"); Printf(f_directors_helpers, " * --------------------------------------------------- */\n\n"); Printf(f_directors, "\n\n"); Printf(f_directors, "/* ---------------------------------------------------\n"); Printf(f_directors, " * C++ director class methods\n"); Printf(f_directors, " * --------------------------------------------------- */\n\n"); if (outfile_h) { String *filename = Swig_file_filename(outfile_h); Printf(f_directors, "#include \"%s\"\n\n", filename); Delete(filename); } Delete(module_macro); } Printf(f_header, "#define SWIG_init Init_%s\n", feature); Printf(f_header, "#define SWIG_name \"%s\"\n\n", module); if (mod_docstring) { if (Len(mod_docstring)) { Printf(f_header, "/*\n Document-module: %s\n\n%s\n*/\n", module, mod_docstring); } Delete(mod_docstring); mod_docstring = NULL; } if (!useGlobalModule) Printf(f_header, "static VALUE %s;\n", modvar); /* Start generating the initialization function */ String* docs = docstring(n, AUTODOC_CLASS); Printf(f_init, "/*\n%s\n*/", docs ); Printv(f_init, "\n", "#ifdef __cplusplus\n", "extern \"C\"\n", "#endif\n", "SWIGEXPORT void Init_", feature, "(void) {\n", "size_t i;\n", "\n", NIL); Printv(f_init, tab4, "SWIG_InitRuntime();\n", NIL); if (!useGlobalModule) defineRubyModule(); Printv(f_init, "\n", "SWIG_InitializeModule(0);\n", "for (i = 0; i < swig_module.size; i++) {\n", "SWIG_define_class(swig_module.types[i]);\n", "}\n", NIL); Printf(f_init, "\n"); /* Initialize code to keep track of objects */ Printf(f_init, "SWIG_RubyInitializeTrackings();\n"); Language::top(n); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (make it occur before %header section) Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director_guard.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } /* Finish off our init function */ Printf(f_init, "}\n"); SwigType_emit_type_table(f_runtime, f_wrappers); /* Close all of the files */ Dump(f_runtime, f_begin); Dump(f_header, f_begin); if (Swig_directors_enabled()) { Dump(f_directors_helpers, f_begin); Dump(f_directors, f_begin); Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); Delete(f_runtime_h); } Dump(f_wrappers, f_begin); Dump(f_initbeforefunc, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_initbeforefunc); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* ----------------------------------------------------------------------------- * importDirective() * ----------------------------------------------------------------------------- */ virtual int importDirective(Node *n) { String *modname = Getattr(n, "module"); if (modname) { if (prefix) { Insert(modname, 0, prefix); } List *modules = Split(modname, ':', INT_MAX); if (modules && Len(modules) > 0) { modname = NewString(""); String *last = NULL; Iterator m = First(modules); while (m.item) { if (Len(m.item) > 0) { if (last) { Append(modname, "/"); } Append(modname, m.item); last = m.item; } m = Next(m); } Printf(f_init, "rb_require(\"%s\");\n", modname); Delete(modname); } Delete(modules); } return Language::importDirective(n); } /* --------------------------------------------------------------------- * set_module(const char *mod_name) * * Sets the module name. Does nothing if it's already set (so it can * be overridden as a command line option). *---------------------------------------------------------------------- */ void set_module(const char *s) { String *mod_name = NewString(s); if (module == 0) { /* Start with the empty string */ module = NewString(""); if (prefix) { Insert(mod_name, 0, prefix); } /* Account for nested modules */ List *modules = Split(mod_name, ':', INT_MAX); if (modules != 0 && Len(modules) > 0) { String *last = 0; Iterator m = First(modules); while (m.item) { if (Len(m.item) > 0) { String *cap = NewString(m.item); (Char(cap))[0] = (char)toupper((Char(cap))[0]); if (last != 0) { Append(module, "::"); } Append(module, cap); last = m.item; } m = Next(m); } if (last) { if (feature == 0) { feature = Copy(last); } (Char(last))[0] = (char)toupper((Char(last))[0]); modvar = NewStringf("m%s", last); } } Delete(modules); } Delete(mod_name); } /* -------------------------------------------------------------------------- * nativeWrapper() * -------------------------------------------------------------------------- */ virtual int nativeWrapper(Node *n) { String *funcname = Getattr(n, "wrap:name"); Swig_warning(WARN_LANG_NATIVE_UNIMPL, input_file, line_number, "Adding native function %s not supported (ignored).\n", funcname); return SWIG_NOWRAP; } /** * Process the comma-separated list of aliases (if any). */ void defineAliases(Node *n, const_String_or_char_ptr iname) { String *aliasv = Getattr(n, "feature:alias"); if (aliasv) { List *aliases = Split(aliasv, ',', INT_MAX); if (aliases && Len(aliases) > 0) { Iterator alias = First(aliases); while (alias.item) { if (Len(alias.item) > 0) { if (current == NO_CPP) { if (useGlobalModule) { Printv(f_init, tab4, "rb_define_alias(rb_cObject, \"", alias.item, "\", \"", iname, "\");\n", NIL); } else { Printv(f_init, tab4, "rb_define_alias(rb_singleton_class(", modvar, "), \"", alias.item, "\", \"", iname, "\");\n", NIL); } } else if (multipleInheritance) { Printv(klass->init, tab4, "rb_define_alias(", klass->mImpl, ", \"", alias.item, "\", \"", iname, "\");\n", NIL); } else { Printv(klass->init, tab4, "rb_define_alias(", klass->vname, ", \"", alias.item, "\", \"", iname, "\");\n", NIL); } } alias = Next(alias); } } Delete(aliases); } } /* --------------------------------------------------------------------- * create_command(Node *n, char *iname) * * Creates a new command from a C function. * iname = Name of function in scripting language * * A note about what "protected" and "private" mean in Ruby: * * A private method is accessible only within the class or its subclasses, * and it is callable only in "function form", with 'self' (implicit or * explicit) as a receiver. * * A protected method is callable only from within its class, but unlike * a private method, it can be called with a receiver other than self, such * as another instance of the same class. * --------------------------------------------------------------------- */ void create_command(Node *n, const_String_or_char_ptr iname) { String *alloc_func = Swig_name_wrapper(iname); String *wname = Swig_name_wrapper(iname); if (CPlusPlus) { Insert(wname, 0, "VALUEFUNC("); Append(wname, ")"); } if (current != NO_CPP) iname = klass->strip(iname); if (Getattr(special_methods, iname)) { iname = GetChar(special_methods, iname); } String *s = NewString(""); String *temp = NewString(""); #ifdef SWIG_PROTECTED_TARGET_METHODS const char *rb_define_method = is_public(n) ? "rb_define_method" : "rb_define_protected_method"; #else const char *rb_define_method = "rb_define_method"; #endif switch (current) { case MEMBER_FUNC: { if (multipleInheritance) { Printv(klass->init, tab4, rb_define_method, "(", klass->mImpl, ", \"", iname, "\", ", wname, ", -1);\n", NIL); } else { Printv(klass->init, tab4, rb_define_method, "(", klass->vname, ", \"", iname, "\", ", wname, ", -1);\n", NIL); } } break; case CONSTRUCTOR_ALLOCATE: Printv(s, tab4, "rb_define_alloc_func(", klass->vname, ", ", alloc_func, ");\n", NIL); Replaceall(klass->init, "$allocator", s); break; case CONSTRUCTOR_INITIALIZE: Printv(s, tab4, rb_define_method, "(", klass->vname, ", \"initialize\", ", wname, ", -1);\n", NIL); Replaceall(klass->init, "$initializer", s); break; case MEMBER_VAR: Append(temp, iname); /* Check for _set or _get at the end of the name. */ if (Len(temp) > 4) { const char *p = Char(temp) + (Len(temp) - 4); if (strcmp(p, "_set") == 0) { Delslice(temp, Len(temp) - 4, DOH_END); Append(temp, "="); } else if (strcmp(p, "_get") == 0) { Delslice(temp, Len(temp) - 4, DOH_END); } } if (multipleInheritance) { Printv(klass->init, tab4, "rb_define_method(", klass->mImpl, ", \"", temp, "\", ", wname, ", -1);\n", NIL); } else { Printv(klass->init, tab4, "rb_define_method(", klass->vname, ", \"", temp, "\", ", wname, ", -1);\n", NIL); } break; case STATIC_FUNC: Printv(klass->init, tab4, "rb_define_singleton_method(", klass->vname, ", \"", iname, "\", ", wname, ", -1);\n", NIL); break; case NO_CPP: if (!useGlobalModule) { Printv(s, tab4, "rb_define_module_function(", modvar, ", \"", iname, "\", ", wname, ", -1);\n", NIL); Printv(f_init, s, NIL); } else { Printv(s, tab4, "rb_define_global_function(\"", iname, "\", ", wname, ", -1);\n", NIL); Printv(f_init, s, NIL); } break; case DESTRUCTOR: case CLASS_CONST: case STATIC_VAR: default: assert(false); // Should not have gotten here for these types } defineAliases(n, iname); Delete(temp); Delete(s); Delete(wname); Delete(alloc_func); } /* --------------------------------------------------------------------- * applyInputTypemap() * * Look up the appropriate "in" typemap for this parameter (p), * substitute the correct strings for the typemap parameters, and dump the * resulting code to the wrapper file. * --------------------------------------------------------------------- */ Parm *applyInputTypemap(Parm *p, String *source, Wrapper *f, String *symname) { String *tm; SwigType *pt = Getattr(p, "type"); if ((tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", source); Replaceall(tm, "$symname", symname); if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Setattr(p, "emit:input", Copy(source)); Printf(f->code, "%s\n", tm); p = Getattr(p, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); p = nextSibling(p); } return p; } Parm *skipIgnoredArgs(Parm *p) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } return p; } /* --------------------------------------------------------------------- * marshalInputArgs() * * Process all of the arguments passed into the scripting language * method and convert them into C/C++ function arguments using the * supplied typemaps. * --------------------------------------------------------------------- */ void marshalInputArgs(Node *n, ParmList *l, int numarg, int numreq, String *kwargs, bool allow_kwargs, Wrapper *f) { int i; Parm *p; String *tm; String *source; source = NewString(""); bool ctor_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n)); /** * The 'start' value indicates which of the C/C++ function arguments * produced here corresponds to the first value in Ruby's argv[] array. * The value of start is either zero or one. If start is zero, then * the first argument (with name arg1) is based on the value of argv[0]. * If start is one, then arg1 is based on the value of argv[1]. */ int start = (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) ? 1 : 0; int varargs = emit_isvarargs(l); Printf(kwargs, "{ "); for (i = 0, p = l; i < numarg; i++) { p = skipIgnoredArgs(p); String *pn = Getattr(p, "name"); /* Produce string representation of source argument */ Clear(source); /* First argument is a special case */ if (i == 0) { Printv(source, (start == 0) ? "argv[0]" : "self", NIL); } else { Printf(source, "argv[%d]", i - start); } if (i >= (numreq)) { /* Check if parsing an optional argument */ Printf(f->code, " if (argc > %d) {\n", i - start); } /* Record argument name for keyword argument handling */ if (Len(pn)) { Printf(kwargs, "\"%s\",", pn); } else { Printf(kwargs, "\"arg%d\",", i + 1); } /* Look for an input typemap */ p = applyInputTypemap(p, source, f, Getattr(n, "name")); if (i >= numreq) { Printf(f->code, "}\n"); } } /* Finish argument marshalling */ Printf(kwargs, " NULL }"); if (allow_kwargs) { // kwarg support not implemented // Printv(f->locals, tab4, "const char *kwnames[] = ", kwargs, ";\n", NIL); } /* Trailing varargs */ if (varargs) { if (p && (tm = Getattr(p, "tmap:in"))) { Clear(source); Printf(source, "argv[%d]", i - start); Replaceall(tm, "$input", source); Setattr(p, "emit:input", Copy(source)); Printf(f->code, "if (argc > %d) {\n", i - start); Printv(f->code, tm, "\n", NIL); Printf(f->code, "}\n"); } } Delete(source); } /* --------------------------------------------------------------------- * insertConstraintCheckingCode(ParmList *l, Wrapper *f) * * Checks each of the parameters in the parameter list for a "check" * typemap and (if it finds one) inserts the typemapping code into * the function wrapper. * --------------------------------------------------------------------- */ void insertConstraintCheckingCode(ParmList *l, Wrapper *f) { Parm *p; String *tm; for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } } /* --------------------------------------------------------------------- * insertCleanupCode(ParmList *l, String *cleanup) * * Checks each of the parameters in the parameter list for a "freearg" * typemap and (if it finds one) inserts the typemapping code into * the function wrapper. * --------------------------------------------------------------------- */ void insertCleanupCode(ParmList *l, String *cleanup) { String *tm; for (Parm *p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { if (Len(tm) != 0) { Printv(cleanup, tm, "\n", NIL); } p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } } /* --------------------------------------------------------------------- * insertArgOutputCode(ParmList *l, String *outarg, int& need_result) * * Checks each of the parameters in the parameter list for a "argout" * typemap and (if it finds one) inserts the typemapping code into * the function wrapper. * --------------------------------------------------------------------- */ void insertArgOutputCode(ParmList *l, String *outarg, int &need_result) { String *tm; for (Parm *p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { Replaceall(tm, "$result", "vresult"); Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); need_result += 1; p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } } /* --------------------------------------------------------------------- * validIdentifier() * * Is this a valid identifier in the scripting language? * Ruby method names can include any combination of letters, numbers * and underscores. A Ruby method name may optionally end with * a question mark ("?"), exclamation point ("!") or equals sign ("="). * * Methods whose names end with question marks are, by convention, * predicate methods that return true or false (e.g. Array#empty?). * * Methods whose names end with exclamation points are, by convention, * called bang methods that modify the instance in place (e.g. Array#sort!). * * Methods whose names end with an equals sign are attribute setters * (e.g. Thread#critical=). * --------------------------------------------------------------------- */ virtual int validIdentifier(String *s) { char *c = Char(s); while (*c) { if (!(isalnum(*c) || (*c == '_') || (*c == '?') || (*c == '!') || (*c == '='))) return 0; c++; } return 1; } /* --------------------------------------------------------------------- * functionWrapper() * * Create a function declaration and register it with the interpreter. * --------------------------------------------------------------------- */ virtual int functionWrapper(Node *n) { String *nodeType; bool destructor; String *symname = Copy(Getattr(n, "sym:name")); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); int director_method = 0; String *tm; int need_result = 0; /* Ruby needs no destructor wrapper */ if (current == DESTRUCTOR) return SWIG_NOWRAP; nodeType = Getattr(n, "nodeType"); destructor = (!Cmp(nodeType, "destructor")); /* If the C++ class constructor is overloaded, we only want to * write out the "new" singleton method once since it is always * the same. (It's the "initialize" method that will handle the * overloading). */ if (current == CONSTRUCTOR_ALLOCATE && Swig_symbol_isoverloaded(n) && Getattr(n, "sym:nextSibling") != 0) return SWIG_OK; String *overname = 0; if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } else { if (!addSymbol(symname, n)) return SWIG_ERROR; } String *cleanup = NewString(""); String *outarg = NewString(""); String *kwargs = NewString(""); Wrapper *f = NewWrapper(); /* Rename predicate methods */ if (GetFlag(n, "feature:predicate")) { Append(symname, "?"); } /* Rename bang methods */ if (GetFlag(n, "feature:bang")) { Append(symname, "!"); } /* Determine the name of the SWIG wrapper function */ String *wname = Swig_name_wrapper(symname); if (overname && current != CONSTRUCTOR_ALLOCATE) { Append(wname, overname); } /* Emit arguments */ if (current != CONSTRUCTOR_ALLOCATE) { emit_parameter_variables(l, f); } /* Attach standard typemaps */ if (current != CONSTRUCTOR_ALLOCATE) { emit_attach_parmmaps(l, f); } Setattr(n, "wrap:parms", l); /* Get number of arguments */ int numarg = emit_num_arguments(l); int numreq = emit_num_required(l); int varargs = emit_isvarargs(l); bool allow_kwargs = GetFlag(n, "feature:kwargs") ? true : false; bool ctor_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n)); int start = (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) ? 1 : 0; /* Now write the wrapper function itself */ if (current == CONSTRUCTOR_ALLOCATE) { Printv(f->def, "SWIGINTERN VALUE\n", NIL); Printf(f->def, "#ifdef HAVE_RB_DEFINE_ALLOC_FUNC\n"); Printv(f->def, wname, "(VALUE self)\n", NIL); Printf(f->def, "#else\n"); Printv(f->def, wname, "(int argc, VALUE *argv, VALUE self)\n", NIL); Printf(f->def, "#endif\n"); Printv(f->def, "{\n", NIL); } else if (current == CONSTRUCTOR_INITIALIZE) { Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); if (!varargs) { Printf(f->code, "if ((argc < %d) || (argc > %d)) ", numreq - start, numarg - start); } else { Printf(f->code, "if (argc < %d) ", numreq - start); } Printf(f->code, "{rb_raise(rb_eArgError, \"wrong # of arguments(%%d for %d)\",argc); SWIG_fail;}\n", numreq - start); } else { if ( current == NO_CPP ) { String* docs = docstring(n, AUTODOC_FUNC); Printf(f_wrappers, "%s", docs); Delete(docs); } Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); if (!varargs) { Printf(f->code, "if ((argc < %d) || (argc > %d)) ", numreq - start, numarg - start); } else { Printf(f->code, "if (argc < %d) ", numreq - start); } Printf(f->code, "{rb_raise(rb_eArgError, \"wrong # of arguments(%%d for %d)\",argc); SWIG_fail;}\n", numreq - start); } /* Now walk the function parameter list and generate code */ /* to get arguments */ if (current != CONSTRUCTOR_ALLOCATE) { marshalInputArgs(n, l, numarg, numreq, kwargs, allow_kwargs, f); } // FIXME? if (ctor_director) { numarg--; numreq--; } /* Insert constraint checking code */ insertConstraintCheckingCode(l, f); /* Insert cleanup code */ insertCleanupCode(l, cleanup); /* Insert argument output code */ insertArgOutputCode(l, outarg, need_result); /* if the object is a director, and the method call originated from its * underlying Ruby object, resolve the call by going up the c++ * inheritance chain. otherwise try to resolve the method in Ruby. * without this check an infinite loop is set up between the director and * shadow class method calls. */ // NOTE: this code should only be inserted if this class is the // base class of a director class. however, in general we haven't // yet analyzed all classes derived from this one to see if they are // directors. furthermore, this class may be used as the base of // a director class defined in a completely different module at a // later time, so this test must be included whether or not directorbase // is true. we do skip this code if directors have not been enabled // at the command line to preserve source-level compatibility with // non-polymorphic swig. also, if this wrapper is for a smart-pointer // method, there is no need to perform the test since the calling object // (the smart-pointer) and the director object (the "pointee") are // distinct. director_method = is_member_director(n) && !is_smart_pointer() && !destructor; if (director_method) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Printf(f->code, "director = dynamic_cast(arg1);\n"); Wrapper_add_local(f, "upcall", "bool upcall = false"); Append(f->code, "upcall = (director && (director->swig_get_self() == self));\n"); } /* Now write code to make the function call */ if (current != CONSTRUCTOR_ALLOCATE) { if (current == CONSTRUCTOR_INITIALIZE) { Node *pn = Swig_methodclass(n); String *symname = Getattr(pn, "sym:name"); String *action = Getattr(n, "wrap:action"); if (Swig_directors_enabled()) { String *classname = NewStringf("const char *classname SWIGUNUSED = \"%s::%s\"", module, symname); Wrapper_add_local(f, "classname", classname); } if (action) { SwigType *smart = Getattr(pn, "smart"); String *result_name = NewStringf("%s%s", smart ? "smart" : "", Swig_cresult_name()); if (smart) { String *result_var = NewStringf("%s *%s = 0", SwigType_namestr(smart), result_name); Wrapper_add_local(f, result_name, result_var); Printf(action, "\n%s = new %s(%s);", result_name, SwigType_namestr(smart), Swig_cresult_name()); } Printf(action, "\nDATA_PTR(self) = %s;", result_name); if (GetFlag(pn, "feature:trackobjects")) { Printf(action, "\nSWIG_RubyAddTracking(%s, self);", result_name); } Delete(result_name); } } /* Emit the function call */ if (director_method) { Printf(f->code, "try {\n"); } Setattr(n, "wrap:name", wname); Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); if (director_method) { Printf(actioncode, "} catch (Swig::DirectorException& e) {\n"); Printf(actioncode, " rb_exc_raise(e.getError());\n"); Printf(actioncode, " SWIG_fail;\n"); Printf(actioncode, "}\n"); } /* Return value if necessary */ if (SwigType_type(returntype) != T_VOID && current != CONSTRUCTOR_INITIALIZE) { need_result = 1; if (GetFlag(n, "feature:predicate")) { Printv(actioncode, tab4, "vresult = (", Swig_cresult_name(), " ? Qtrue : Qfalse);\n", NIL); } else { tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode); actioncode = 0; if (tm) { Replaceall(tm, "$result", "vresult"); if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "SWIG_POINTER_OWN"); else Replaceall(tm, "$owner", "0"); // Unwrap return values that are director classes so that the original Ruby object is returned instead. if (Swig_director_can_unwrap(n)) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Printf(f->code, "director = dynamic_cast(%s);\n", Swig_cresult_name()); Printf(f->code, "if (director) {\n"); Printf(f->code, " vresult = director->swig_get_self();\n"); Printf(f->code, "} else {\n"); Printf(f->code, "%s\n", tm); Printf(f->code, "}\n"); director_method = 0; } else { Printf(f->code, "%s\n", tm); } Delete(tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s.\n", SwigType_str(returntype, 0)); } } } if (actioncode) { Append(f->code, actioncode); Delete(actioncode); } emit_return_variable(n, returntype, f); } /* Extra code needed for new and initialize methods */ if (current == CONSTRUCTOR_ALLOCATE) { Node *pn = Swig_methodclass(n); SwigType *smart = Getattr(pn, "smart"); SwigType *psmart = smart ? Copy(smart) : 0; if (psmart) SwigType_add_pointer(psmart); SwigType *classtype = psmart ? psmart : returntype; need_result = 1; Printf(f->code, "VALUE vresult = SWIG_NewClassInstance(self, SWIGTYPE%s);\n", Char(SwigType_manglestr(classtype))); Printf(f->code, "#ifndef HAVE_RB_DEFINE_ALLOC_FUNC\n"); Printf(f->code, "rb_obj_call_init(vresult, argc, argv);\n"); Printf(f->code, "#endif\n"); Delete(psmart); } else if (current == CONSTRUCTOR_INITIALIZE) { need_result = 1; } else { if ( need_result > 1 ) { if ( SwigType_type(returntype) == T_VOID ) Printf(f->code, "vresult = rb_ary_new();\n"); else { Printf(f->code, "if (vresult == Qnil) vresult = rb_ary_new();\n"); Printf(f->code, "else vresult = SWIG_Ruby_AppendOutput( " "rb_ary_new(), vresult);\n"); } } } /* Dump argument output code; */ Printv(f->code, outarg, NIL); /* Dump the argument cleanup code */ int need_cleanup = (current != CONSTRUCTOR_ALLOCATE) && (Len(cleanup) != 0); if (need_cleanup) { Printv(f->code, cleanup, NIL); } /* Look for any remaining cleanup. This processes the %newobject directive */ if (current != CONSTRUCTOR_ALLOCATE && GetFlag(n, "feature:new")) { tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0); if (tm) { Printv(f->code, tm, "\n", NIL); Delete(tm); } } /* Special processing on return value. */ tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0); if (tm) { Printv(f->code, tm, NIL); Delete(tm); } if (director_method) { if ((tm = Swig_typemap_lookup("directorfree", n, Swig_cresult_name(), 0))) { Replaceall(tm, "$input", Swig_cresult_name()); Replaceall(tm, "$result", "vresult"); Printf(f->code, "%s\n", tm); } } /* Wrap things up (in a manner of speaking) */ if (need_result) { if (current == CONSTRUCTOR_ALLOCATE) { Printv(f->code, tab4, "return vresult;\n", NIL); } else if (current == CONSTRUCTOR_INITIALIZE) { Printv(f->code, tab4, "return self;\n", NIL); Printv(f->code, "fail:\n", NIL); if (need_cleanup) { Printv(f->code, cleanup, NIL); } Printv(f->code, tab4, "return Qnil;\n", NIL); } else { Wrapper_add_local(f, "vresult", "VALUE vresult = Qnil"); Printv(f->code, tab4, "return vresult;\n", NIL); Printv(f->code, "fail:\n", NIL); if (need_cleanup) { Printv(f->code, cleanup, NIL); } Printv(f->code, tab4, "return Qnil;\n", NIL); } } else { Printv(f->code, tab4, "return Qnil;\n", NIL); Printv(f->code, "fail:\n", NIL); if (need_cleanup) { Printv(f->code, cleanup, NIL); } Printv(f->code, tab4, "return Qnil;\n", NIL); } Printf(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", symname); /* Emit the function */ Wrapper_print(f, f_wrappers); /* Now register the function with the interpreter */ if (!Swig_symbol_isoverloaded(n)) { create_command(n, symname); } else { if (current == CONSTRUCTOR_ALLOCATE) { create_command(n, symname); } else { if (!Getattr(n, "sym:nextSibling")) dispatchFunction(n); } } Delete(kwargs); Delete(cleanup); Delete(outarg); DelWrapper(f); Delete(symname); return SWIG_OK; } /* ------------------------------------------------------------ * dispatchFunction() * ------------------------------------------------------------ */ void dispatchFunction(Node *n) { /* Last node in overloaded chain */ int maxargs; bool check_emitted = false; String *tmp = NewString(""); String *dispatch = Swig_overload_dispatch(n, "return %s(nargs, args, self);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *f = NewWrapper(); String *symname = Getattr(n, "sym:name"); String *wname = Swig_name_wrapper(symname); Printv(f->def, "SWIGINTERN VALUE ", wname, "(int nargs, VALUE *args, VALUE self) {", NIL); Wrapper_add_local(f, "argc", "int argc"); bool ctor_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n)); if (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) { Printf(tmp, "VALUE argv[%d]", maxargs + 1); } else { Printf(tmp, "VALUE argv[%d]", maxargs); } Wrapper_add_local(f, "argv", tmp); Wrapper_add_local(f, "ii", "int ii"); if (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) { maxargs += 1; Printf(f->code, "argc = nargs + 1;\n"); Printf(f->code, "argv[0] = self;\n"); Printf(f->code, "if (argc > %d) SWIG_fail;\n", maxargs); Printf(f->code, "for (ii = 1; (ii < argc); ++ii) {\n"); Printf(f->code, "argv[ii] = args[ii-1];\n"); Printf(f->code, "}\n"); } else { Printf(f->code, "argc = nargs;\n"); Printf(f->code, "if (argc > %d) SWIG_fail;\n", maxargs); Printf(f->code, "for (ii = 0; (ii < argc); ++ii) {\n"); Printf(f->code, "argv[ii] = args[ii];\n"); Printf(f->code, "}\n"); } Replaceall(dispatch, "$args", "nargs, args, self"); Printv(f->code, dispatch, "\n", NIL); // Generate prototype list, go to first node Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up // Constructors will be treated specially String *siblNodeType = Getattr(sibl, "nodeType"); const bool isCtor = (Equal(siblNodeType, "constructor")); const bool isMethod = (Equal(siblNodeType, "cdecl") && GetFlag(sibl, "ismember") && !isCtor); // Construct real method name String* methodName = NewString(""); if ( isMethod ) { // Sometimes a method node has no parent (SF#3034054). // This value is used in an exception message, so just skip the class // name in this case so at least we don't segfault. This is probably // just working around a problem elsewhere though. Node *parent_node = parentNode(sibl); if (parent_node) Printv( methodName, Getattr(parent_node,"sym:name"), ".", NIL ); } Append( methodName, Getattr(sibl,"sym:name" ) ); if ( isCtor ) Append( methodName, ".new" ); // Generate prototype list String *protoTypes = NewString(""); do { Append( protoTypes, "\n\" "); if (!isCtor && !Equal(siblNodeType, "using")) { SwigType *type = SwigType_str(Getattr(sibl, "type"), NULL); Printv(protoTypes, type, " ", NIL); Delete(type); } Printv(protoTypes, methodName, NIL ); Parm* p = Getattr(sibl, "wrap:parms"); if (p && (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) ) p = nextSibling(p); // skip self Append( protoTypes, "(" ); while(p) { Append( protoTypes, SwigType_str(Getattr(p,"type"), Getattr(p,"name")) ); if ( ( p = nextSibling(p)) ) Append(protoTypes, ", "); } Append( protoTypes, ")\\n\"" ); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Append(f->code, "fail:\n"); Printf(f->code, "Ruby_Format_OverloadedError( argc, %d, \"%s\", %s);\n", maxargs, methodName, protoTypes); Append(f->code, "\nreturn Qnil;\n"); Delete(methodName); Delete(protoTypes); Printv(f->code, "}\n", NIL); Wrapper_print(f, f_wrappers); create_command(n, Char(symname)); DelWrapper(f); Delete(dispatch); Delete(tmp); Delete(wname); } /* --------------------------------------------------------------------- * variableWrapper() * --------------------------------------------------------------------- */ virtual int variableWrapper(Node *n) { String* docs = docstring(n, AUTODOC_GETTER); Printf(f_wrappers, "%s", docs); Delete(docs); char *name = GetChar(n, "name"); char *iname = GetChar(n, "sym:name"); SwigType *t = Getattr(n, "type"); String *tm; String *getfname, *setfname; Wrapper *getf, *setf; int assignable = !is_immutable(n); // Determine whether virtual global variables shall be used // which have different getter and setter signatures, // see https://docs.ruby-lang.org/en/2.6.0/extension_rdoc.html#label-Global+Variables+Shared+Between+C+and+Ruby const bool use_virtual_var = (current == NO_CPP && useGlobalModule); getf = NewWrapper(); setf = NewWrapper(); /* create getter */ int addfail = 0; String *getname = Swig_name_get(NSPACE_TODO, iname); getfname = Swig_name_wrapper(getname); Setattr(n, "wrap:name", getfname); Printv(getf->def, "SWIGINTERN VALUE\n", getfname, "(", NIL); Printf(getf->def, (use_virtual_var) ? "ID id, VALUE *data" : "VALUE self"); Printf(getf->def, ") {"); Wrapper_add_local(getf, "_val", "VALUE _val"); tm = Swig_typemap_lookup("varout", n, name, 0); if (tm) { Replaceall(tm, "$result", "_val"); /* Printv(getf->code,tm, NIL); */ addfail = emit_action_code(n, getf->code, tm); } else { Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); } Printv(getf->code, tab4, "return _val;\n", NIL); if (addfail) { Append(getf->code, "fail:\n"); Append(getf->code, " return Qnil;\n"); } Append(getf->code, "}\n"); Wrapper_print(getf, f_wrappers); if (!assignable) { setfname = NewString("(rb_gvar_setter_t *)NULL"); } else { /* create setter */ String* docs = docstring(n, AUTODOC_SETTER); Printf(f_wrappers, "%s", docs); Delete(docs); String *setname = Swig_name_set(NSPACE_TODO, iname); setfname = Swig_name_wrapper(setname); Setattr(n, "wrap:name", setfname); Printf(setf->def, "SWIGINTERN "); if (use_virtual_var) { Printv(setf->def, "void\n", setfname, "(VALUE _val, ID id, VALUE *data) {", NIL); } else { Printv(setf->def, "VALUE\n", setfname, "(VALUE self, VALUE _val) {", NIL); } tm = Swig_typemap_lookup("varin", n, name, 0); if (tm) { Replaceall(tm, "$input", "_val"); /* Printv(setf->code,tm,"\n",NIL); */ emit_action_code(n, setf->code, tm); } else { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s\n", SwigType_str(t, 0)); } if (use_virtual_var) { Printf(setf->code, "fail:\n"); Printv(setf->code, tab4, "return;\n", NIL); } else { Printv(setf->code, tab4, "return _val;\n", NIL); Printf(setf->code, "fail:\n"); Printv(setf->code, tab4, "return Qnil;\n", NIL); } Printf(setf->code, "}\n"); Wrapper_print(setf, f_wrappers); Delete(setname); } /* define accessor methods */ Insert(getfname, 0, "VALUEFUNC("); Append(getfname, ")"); Insert(setfname, 0, (use_virtual_var) ? "SWIG_RUBY_VOID_ANYARGS_FUNC(" : "VALUEFUNC("); Append(setfname, ")"); String *s = NewString(""); switch (current) { case STATIC_VAR: /* C++ class variable */ Printv(s, tab4, "rb_define_singleton_method(", klass->vname, ", \"", klass->strip(iname), "\", ", getfname, ", 0);\n", NIL); if (assignable) { Printv(s, tab4, "rb_define_singleton_method(", klass->vname, ", \"", klass->strip(iname), "=\", ", setfname, ", 1);\n", NIL); } Printv(klass->init, s, NIL); break; default: /* C global variable */ /* wrapped in Ruby module attribute */ assert(current == NO_CPP); if (!useGlobalModule) { Printv(s, tab4, "rb_define_singleton_method(", modvar, ", \"", iname, "\", ", getfname, ", 0);\n", NIL); if (assignable) { Printv(s, tab4, "rb_define_singleton_method(", modvar, ", \"", iname, "=\", ", setfname, ", 1);\n", NIL); } } else { Printv(s, tab4, "rb_define_virtual_variable(\"$", iname, "\", ", getfname, ", ", setfname, ");\n", NIL); } Printv(f_init, s, NIL); Delete(s); break; } Delete(getname); Delete(getfname); Delete(setfname); DelWrapper(setf); DelWrapper(getf); return SWIG_OK; } /* --------------------------------------------------------------------- * validate_const_name(char *name) * * Validate constant name. * --------------------------------------------------------------------- */ char *validate_const_name(char *name, const char *reason) { if (!name || name[0] == '\0') return name; if (isupper(name[0])) return name; if (islower(name[0])) { name[0] = (char)toupper(name[0]); Swig_warning(WARN_RUBY_WRONG_NAME, input_file, line_number, "Wrong %s name (corrected to `%s')\n", reason, name); return name; } Swig_warning(WARN_RUBY_WRONG_NAME, input_file, line_number, "Wrong %s name %s\n", reason, name); return name; } /* --------------------------------------------------------------------- * constantWrapper() * --------------------------------------------------------------------- */ virtual int constantWrapper(Node *n) { Swig_require("constantWrapper", n, "*sym:name", "type", "value", NIL); char *iname = GetChar(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); if (current == CLASS_CONST) { iname = klass->strip(iname); } validate_const_name(iname, "constant"); SetChar(n, "sym:name", iname); /* Special hook for member pointer */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(iname); Printf(f_header, "static %s = %s;\n", SwigType_str(type, wname), value); value = Char(wname); } String *tm = Swig_typemap_lookup("constant", n, value, 0); if (!tm) tm = Swig_typemap_lookup("constcode", n, value, 0); if (tm) { Replaceall(tm, "$symname", iname); Replaceall(tm, "$value", value); if (current == CLASS_CONST) { if (multipleInheritance) { Replaceall(tm, "$module", klass->mImpl); Printv(klass->init, tm, "\n", NIL); } else { Replaceall(tm, "$module", klass->vname); Printv(klass->init, tm, "\n", NIL); } } else { if (!useGlobalModule) { Replaceall(tm, "$module", modvar); } else { Replaceall(tm, "$module", "rb_cObject"); } Printf(f_init, "%s\n", tm); } } else { Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value %s = %s\n", SwigType_str(type, 0), value); } Swig_restore(n); return SWIG_OK; } /* ----------------------------------------------------------------------------- * classDeclaration() * * Records information about classes---even classes that might be defined in * other modules referenced by %import. * ----------------------------------------------------------------------------- */ virtual int classDeclaration(Node *n) { if (!Getattr(n, "feature:onlychildren")) { String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); String *namestr = SwigType_namestr(name); klass = RCLASS(classes, Char(namestr)); if (!klass) { klass = new RClass(); String *valid_name = NewString(symname ? symname : namestr); validate_const_name(Char(valid_name), "class"); klass->set_name(namestr, symname, valid_name); SET_RCLASS(classes, Char(namestr), klass); Delete(valid_name); } Delete(namestr); } return Language::classDeclaration(n); } /** * Process the comma-separated list of mixed-in module names (if any). */ void includeRubyModules(Node *n) { String *mixin = Getattr(n, "feature:mixin"); if (mixin) { List *modules = Split(mixin, ',', INT_MAX); if (modules && Len(modules) > 0) { Iterator mod = First(modules); while (mod.item) { if (Len(mod.item) > 0) { Printf(klass->init, "rb_include_module(%s, rb_eval_string(\"%s\"));\n", klass->vname, mod.item); } mod = Next(mod); } } Delete(modules); } } void handleBaseClasses(Node *n) { List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator base = First(baselist); while (base.item && GetFlag(base.item, "feature:ignore")) { base = Next(base); } while (base.item) { String *basename = Getattr(base.item, "name"); String *basenamestr = SwigType_namestr(basename); RClass *super = RCLASS(classes, Char(basenamestr)); Delete(basenamestr); if (super) { SwigType *btype = NewString(basename); SwigType_add_pointer(btype); SwigType_remember(btype); SwigType *smart = Getattr(base.item, "smart"); SwigType *psmart = smart ? Copy(smart) : 0; if (psmart) { SwigType_add_pointer(psmart); SwigType_remember(psmart); } String *bmangle = SwigType_manglestr(psmart ? psmart : btype); if (multipleInheritance) { Insert(bmangle, 0, "((swig_class *) SWIGTYPE"); Append(bmangle, "->clientdata)->mImpl"); Printv(klass->init, "rb_include_module(", klass->mImpl, ", ", bmangle, ");\n", NIL); } else { Insert(bmangle, 0, "((swig_class *) SWIGTYPE"); Append(bmangle, "->clientdata)->klass"); Replaceall(klass->init, "$super", bmangle); } Delete(bmangle); Delete(psmart); Delete(btype); } base = Next(base); while (base.item && GetFlag(base.item, "feature:ignore")) { base = Next(base); } if (!multipleInheritance) { /* Warn about multiple inheritance for additional base class(es) */ while (base.item) { if (GetFlag(base.item, "feature:ignore")) { base = Next(base); continue; } String *proxyclassname = SwigType_str(Getattr(n, "classtypeobj"), 0); String *baseclassname = SwigType_str(Getattr(base.item, "name"), 0); Swig_warning(WARN_RUBY_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, base %s ignored. Multiple inheritance is not supported in Ruby.\n", proxyclassname, baseclassname); base = Next(base); } } } } } /** * Check to see if a %markfunc was specified. */ void handleMarkFuncDirective(Node *n) { String *markfunc = Getattr(n, "feature:markfunc"); if (markfunc) { Printf(klass->init, "SwigClass%s.mark = (void (*)(void *)) %s;\n", klass->name, markfunc); } else { Printf(klass->init, "SwigClass%s.mark = 0;\n", klass->name); } } /** * Check to see if a %freefunc was specified. */ void handleFreeFuncDirective(Node *n) { String *freefunc = Getattr(n, "feature:freefunc"); if (freefunc) { Printf(klass->init, "SwigClass%s.destroy = (void (*)(void *)) %s;\n", klass->name, freefunc); } else { if (klass->destructor_defined) { Printf(klass->init, "SwigClass%s.destroy = (void (*)(void *)) free_%s;\n", klass->name, klass->mname); } } } /** * Check to see if tracking is enabled for this class. */ void handleTrackDirective(Node *n) { int trackObjects = GetFlag(n, "feature:trackobjects"); if (trackObjects) { Printf(klass->init, "SwigClass%s.trackObjects = 1;\n", klass->name); } else { Printf(klass->init, "SwigClass%s.trackObjects = 0;\n", klass->name); } } /* ---------------------------------------------------------------------- * classHandler() * ---------------------------------------------------------------------- */ virtual int classHandler(Node *n) { String* docs = docstring(n, AUTODOC_CLASS); Printf(f_wrappers, "%s", docs); Delete(docs); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); String *namestr = SwigType_namestr(name); // does template expansion klass = RCLASS(classes, Char(namestr)); assert(klass != 0); Delete(namestr); String *valid_name = NewString(symname); validate_const_name(Char(valid_name), "class"); Clear(klass->type); Printv(klass->type, Getattr(n, "classtype"), NIL); Printv(f_wrappers, "static swig_class SwigClass", valid_name, ";\n\n", NIL); Printv(klass->init, "\n", tab4, NIL); if (!useGlobalModule) { Printv(klass->init, klass->vname, " = rb_define_class_under(", modvar, ", \"", klass->name, "\", $super);\n", NIL); } else { Printv(klass->init, klass->vname, " = rb_define_class(\"", klass->name, "\", $super);\n", NIL); } if (multipleInheritance) { Printv(klass->init, klass->mImpl, " = rb_define_module_under(", klass->vname, ", \"Impl\");\n", NIL); } SwigType *tt = NewString(name); SwigType_add_pointer(tt); SwigType_remember(tt); SwigType *smart = Getattr(n, "smart"); SwigType *psmart = smart ? Copy(smart) : 0; if (psmart) { SwigType_add_pointer(psmart); SwigType_remember(psmart); } String *tm = SwigType_manglestr(psmart ? psmart : tt); Printf(klass->init, "SWIG_TypeClientData(SWIGTYPE%s, (void *) &SwigClass%s);\n", tm, valid_name); Delete(tm); Delete(psmart); Delete(tt); Delete(valid_name); includeRubyModules(n); Printv(klass->init, "$allocator", NIL); Printv(klass->init, "$initializer", NIL); Language::classHandler(n); handleBaseClasses(n); handleMarkFuncDirective(n); handleFreeFuncDirective(n); handleTrackDirective(n); if (multipleInheritance) { Printv(klass->init, "rb_include_module(", klass->vname, ", ", klass->mImpl, ");\n", NIL); } String *s = NewString(""); Printv(s, tab4, "rb_undef_alloc_func(", klass->vname, ");\n", NIL); Replaceall(klass->init, "$allocator", s); Replaceall(klass->init, "$initializer", ""); if (GetFlag(n, "feature:exceptionclass")) { Replaceall(klass->init, "$super", "rb_eRuntimeError"); } else { Replaceall(klass->init, "$super", "rb_cObject"); } Delete(s); Printv(f_init, klass->init, NIL); klass = 0; return SWIG_OK; } /* ---------------------------------------------------------------------- * memberfunctionHandler() * * Method for adding C++ member function * * By default, we're going to create a function of the form : * * Foo_bar(this,args) * * Where Foo is the classname, bar is the member name and the this pointer * is explicitly attached to the beginning. * * The renaming only applies to the member function part, not the full * classname. * * --------------------------------------------------------------------- */ virtual int memberfunctionHandler(Node *n) { current = MEMBER_FUNC; String* docs = docstring(n, AUTODOC_METHOD); Printf(f_wrappers, "%s", docs); Delete(docs); Language::memberfunctionHandler(n); current = NO_CPP; return SWIG_OK; } /* --------------------------------------------------------------------- * constructorHandler() * * Method for adding C++ member constructor * -------------------------------------------------------------------- */ void set_director_ctor_code(Node *n) { /* director ctor code is specific for each class */ Delete(director_prot_ctor_code); director_prot_ctor_code = NewString(""); Node *pn = Swig_methodclass(n); String *symname = Getattr(pn, "sym:name"); String *name = Copy(symname); char *cname = Char(name); if (cname) cname[0] = (char)toupper(cname[0]); Printv(director_prot_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " rb_raise(rb_eNameError,\"accessing abstract class or protected constructor\"); \n", " return Qnil;\n", "}\n", NIL); Delete(director_ctor_code); director_ctor_code = NewString(""); Printv(director_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " $nondirector_new \n", "}\n", NIL); Delete(name); } virtual int constructorHandler(Node *n) { int use_director = Swig_directorclass(n); if (use_director) { set_director_ctor_code(n); } /* First wrap the allocate method */ current = CONSTRUCTOR_ALLOCATE; Swig_name_register("construct", "%n%c_allocate"); Language::constructorHandler(n); String* docs = docstring(n, AUTODOC_CTOR); Printf(f_wrappers, "%s", docs); Delete(docs); /* * If we're wrapping the constructor of a C++ director class, prepend a new parameter * to receive the scripting language object (e.g. 'self') * */ Swig_save("ruby:constructorHandler", n, "parms", NIL); if (use_director) { Parm *parms = Getattr(n, "parms"); Parm *self; String *name = NewString("self"); String *type = NewString("VALUE"); self = NewParm(type, name, n); Delete(type); Delete(name); Setattr(self, "lname", "Qnil"); if (parms) set_nextSibling(self, parms); Setattr(n, "parms", self); Setattr(n, "wrap:self", "1"); Delete(self); } /* Now do the instance initialize method */ current = CONSTRUCTOR_INITIALIZE; Swig_name_register("construct", "new_%n%c"); Language::constructorHandler(n); /* Restore original parameter list */ Delattr(n, "wrap:self"); Swig_restore(n); /* Done */ Swig_name_unregister("construct"); current = NO_CPP; klass->constructor_defined = 1; return SWIG_OK; } virtual int copyconstructorHandler(Node *n) { int use_director = Swig_directorclass(n); if (use_director) { set_director_ctor_code(n); } /* First wrap the allocate method */ current = CONSTRUCTOR_ALLOCATE; Swig_name_register("construct", "%n%c_allocate"); return Language::copyconstructorHandler(n); } /* --------------------------------------------------------------------- * destructorHandler() * -------------------------------------------------------------------- */ virtual int destructorHandler(Node *n) { /* Do no spit free function if user defined his own for this class */ Node *pn = Swig_methodclass(n); String *freefunc = Getattr(pn, "feature:freefunc"); if (freefunc) return SWIG_OK; current = DESTRUCTOR; Language::destructorHandler(n); freefunc = NewString(""); String *freebody = NewString(""); String *pname0 = Swig_cparm_name(0, 0); Printv(freefunc, "free_", klass->mname, NIL); Printv(freebody, "SWIGINTERN void\n", freefunc, "(void *self) {\n", NIL); Printv(freebody, tab4, klass->type, " *", pname0, " = (", klass->type, " *)self;\n", NIL); Printv(freebody, tab4, NIL); /* Check to see if object tracking is activated for the class that owns this destructor. */ if (GetFlag(pn, "feature:trackobjects")) { Printf(freebody, "SWIG_RubyRemoveTracking(%s);\n", pname0); Printv(freebody, tab4, NIL); } if (Extend) { String *wrap = Getattr(n, "wrap:code"); if (wrap) { Printv(f_wrappers, wrap, NIL); } /* Printv(freebody, Swig_name_destroy(name), "(", pname0, ")", NIL); */ Printv(freebody, Getattr(n, "wrap:action"), "\n", NIL); } else { String *action = Getattr(n, "wrap:action"); if (action) { Printv(freebody, action, "\n", NIL); } else { /* In the case swig emits no destroy function. */ if (CPlusPlus) Printf(freebody, "delete %s;\n", pname0); else Printf(freebody, "free((char*) %s);\n", pname0); } } Printv(freebody, "}\n\n", NIL); Printv(f_wrappers, freebody, NIL); klass->destructor_defined = 1; current = NO_CPP; Delete(freefunc); Delete(freebody); Delete(pname0); return SWIG_OK; } /* --------------------------------------------------------------------- * membervariableHandler() * * This creates a pair of functions to set/get the variable of a member. * -------------------------------------------------------------------- */ virtual int membervariableHandler(Node *n) { String* docs = docstring(n, AUTODOC_GETTER); Printf(f_wrappers, "%s", docs); Delete(docs); if (!is_immutable(n)) { String* docs = docstring(n, AUTODOC_SETTER); Printf(f_wrappers, "%s", docs); Delete(docs); } current = MEMBER_VAR; Language::membervariableHandler(n); current = NO_CPP; return SWIG_OK; } /* ----------------------------------------------------------------------- * staticmemberfunctionHandler() * * Wrap a static C++ function * ---------------------------------------------------------------------- */ virtual int staticmemberfunctionHandler(Node *n) { String* docs = docstring(n, AUTODOC_STATICFUNC); Printf(f_wrappers, "%s", docs); Delete(docs); current = STATIC_FUNC; Language::staticmemberfunctionHandler(n); current = NO_CPP; return SWIG_OK; } /* ---------------------------------------------------------------------- * memberconstantHandler() * * Create a C++ constant * --------------------------------------------------------------------- */ virtual int memberconstantHandler(Node *n) { String* docs = docstring(n, AUTODOC_STATICFUNC); Printf(f_wrappers, "%s", docs); Delete(docs); current = CLASS_CONST; Language::memberconstantHandler(n); current = NO_CPP; return SWIG_OK; } /* --------------------------------------------------------------------- * staticmembervariableHandler() * --------------------------------------------------------------------- */ virtual int staticmembervariableHandler(Node *n) { String* docs = docstring(n, AUTODOC_GETTER); Printf(f_wrappers, "%s", docs); Delete(docs); if (!is_immutable(n)) { String* docs = docstring(n, AUTODOC_SETTER); Printf(f_wrappers, "%s", docs); Delete(docs); } current = STATIC_VAR; Language::staticmembervariableHandler(n); current = NO_CPP; return SWIG_OK; } /* C++ director class generation */ virtual int classDirector(Node *n) { return Language::classDirector(n); } virtual int classDirectorInit(Node *n) { String *declaration; declaration = Swig_director_declaration(n); Printf(f_directors_h, "\n"); Printf(f_directors_h, "%s\n", declaration); Printf(f_directors_h, "public:\n"); Delete(declaration); return Language::classDirectorInit(n); } virtual int classDirectorEnd(Node *n) { Printf(f_directors_h, "};\n\n"); return Language::classDirectorEnd(n); } /* ------------------------------------------------------------ * classDirectorConstructor() * ------------------------------------------------------------ */ virtual int classDirectorConstructor(Node *n) { Node *parent = Getattr(n, "parentNode"); String *sub = NewString(""); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *classname = NewString(""); Printf(classname, "SwigDirector_%s", supername); /* insert self parameter */ Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("VALUE"); p = NewParm(type, NewString("self"), n); set_nextSibling(p, parms); parms = p; if (!Getattr(n, "defaultargs")) { /* constructor */ { Wrapper *w = NewWrapper(); String *call; String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, classname, parms, 0); call = Swig_csuperclass_call(0, basetype, superparms); Printf(w->def, "%s::%s: %s, Swig::Director(self) { }", classname, target, call); Delete(target); Wrapper_print(w, f_directors); Delete(call); DelWrapper(w); } /* constructor header */ { String *target = Swig_method_decl(0, decl, classname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(classname); Delete(supername); Delete(parms); return Language::classDirectorConstructor(n); } /* ------------------------------------------------------------ * classDirectorDefaultConstructor() * ------------------------------------------------------------ */ virtual int classDirectorDefaultConstructor(Node *n) { String *classname; Wrapper *w; classname = Swig_class_name(n); w = NewWrapper(); Printf(w->def, "SwigDirector_%s::SwigDirector_%s(VALUE self) : Swig::Director(self) { }", classname, classname); Wrapper_print(w, f_directors); DelWrapper(w); Printf(f_directors_h, " SwigDirector_%s(VALUE self);\n", classname); Delete(classname); return Language::classDirectorDefaultConstructor(n); } /* --------------------------------------------------------------- * exceptionSafeMethodCall() * * Emit a virtual director method to pass a method call on to the * underlying Ruby instance. * * --------------------------------------------------------------- */ void exceptionSafeMethodCall(String *className, Node *n, Wrapper *w, int argc, String *args, bool initstack) { Wrapper *body = NewWrapper(); Wrapper *rescue = NewWrapper(); String *methodName = Getattr(n, "sym:name"); String *bodyName = NewStringf("%s_%s_body", className, methodName); String *rescueName = NewStringf("%s_%s_rescue", className, methodName); String *depthCountName = NewStringf("%s_%s_call_depth", className, methodName); // Check for an exception typemap of some kind String *tm = Swig_typemap_lookup("director:except", n, Swig_cresult_name(), 0); if (!tm) { tm = Getattr(n, "feature:director:except"); } if ((tm != 0) && (Len(tm) > 0) && (Strcmp(tm, "1") != 0)) { // Declare a global to hold the depth count if (!Getattr(n, "sym:nextSibling")) { Printf(body->def, "static int %s = 0;\n", depthCountName); // Function body Printf(body->def, "VALUE %s(VALUE data) {\n", bodyName); Wrapper_add_localv(body, "args", "Swig::body_args *", "args", "= reinterpret_cast(data)", NIL); Wrapper_add_localv(body, Swig_cresult_name(), "VALUE", Swig_cresult_name(), "= Qnil", NIL); Printf(body->code, "%s++;\n", depthCountName); Printv(body->code, Swig_cresult_name(), " = rb_funcall2(args->recv, args->id, args->argc, args->argv);\n", NIL); Printf(body->code, "%s--;\n", depthCountName); Printv(body->code, "return ", Swig_cresult_name(), ";\n", NIL); Printv(body->code, "}", NIL); // Exception handler Printf(rescue->def, "VALUE %s(VALUE args, VALUE error) {\n", rescueName); Replaceall(tm, "$error", "error"); Printf(rescue->code, "%s--;\n", depthCountName); Printf(rescue->code, "if (%s == 0) ", depthCountName); Printv(rescue->code, Str(tm), "\n", NIL); Printv(rescue->code, "rb_exc_raise(error);\n", NIL); Printv(rescue->code, "return Qnil;\n", NIL); Printv(rescue->code, "}", NIL); } // Main code Wrapper_add_localv(w, "args", "Swig::body_args", "args", NIL); Wrapper_add_localv(w, "status", "int", "status", NIL); Printv(w->code, "args.recv = swig_get_self();\n", NIL); Printf(w->code, "args.id = rb_intern(\"%s\");\n", methodName); Printf(w->code, "args.argc = %d;\n", argc); if (argc > 0) { Printf(w->code, "args.argv = new VALUE[%d];\n", argc); for (int i = 0; i < argc; i++) { Printf(w->code, "args.argv[%d] = obj%d;\n", i, i); } } else { Printv(w->code, "args.argv = 0;\n", NIL); } Printf(w->code, "%s = rb_protect(PROTECTFUNC(%s), reinterpret_cast(&args), &status);\n", Swig_cresult_name(), bodyName); if ( initstack ) Printf(w->code, "SWIG_RELEASE_STACK;\n"); Printf(w->code, "if (status) {\n"); Printf(w->code, "VALUE lastErr = rb_gv_get(\"$!\");\n"); Printf(w->code, "%s(reinterpret_cast(&args), lastErr);\n", rescueName); Printf(w->code, "}\n"); if (argc > 0) { Printv(w->code, "delete [] args.argv;\n", NIL); } // Dump wrapper code Wrapper_print(body, f_directors_helpers); Wrapper_print(rescue, f_directors_helpers); } else { if (argc > 0) { Printf(w->code, "%s = rb_funcall(swig_get_self(), rb_intern(\"%s\"), %d%s);\n", Swig_cresult_name(), methodName, argc, args); } else { Printf(w->code, "%s = rb_funcall(swig_get_self(), rb_intern(\"%s\"), 0, Qnil);\n", Swig_cresult_name(), methodName); } if ( initstack ) Printf(w->code, "SWIG_RELEASE_STACK;\n"); } // Clean up Delete(bodyName); Delete(rescueName); Delete(depthCountName); DelWrapper(body); DelWrapper(rescue); } virtual int classDirectorMethod(Node *n, Node *parent, String *super) { int is_void = 0; int is_pointer = 0; String *decl = Getattr(n, "decl"); String *name = Getattr(n, "name"); String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *symname = Getattr(n, "sym:name"); String *declaration = NewString(""); ParmList *l = Getattr(n, "parms"); Wrapper *w = NewWrapper(); String *tm; String *wrap_args = NewString(""); SwigType *returntype = Getattr(n, "type"); Parm *p; String *value = Getattr(n, "value"); String *storage = Getattr(n, "storage"); bool pure_virtual = false; int status = SWIG_OK; int idx; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; bool asvoid = checkAttribute( n, "feature:numoutputs", "0") ? true : false; bool initstack = checkAttribute( n, "feature:initstack", "1") ? true : false; if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { pure_virtual = true; } } String *overnametmp = NewString(Getattr(n, "sym:name")); if (Getattr(n, "sym:overloaded")) { Printf(overnametmp, "::%s", Getattr(n, "sym:overname")); } /* determine if the method returns a pointer */ is_pointer = SwigType_ispointer_return(decl); is_void = (!Cmp(returntype, "void") && !is_pointer); /* virtual method definition */ String *target; String *pclassname = NewStringf("SwigDirector_%s", classname); String *qualified_name = NewStringf("%s::%s", pclassname, name); SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); /* header declaration */ target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Get any exception classes in the throws typemap if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = 0; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { Parm *p; int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } Printf(w->def, "%s", SwigType_str(Getattr(p, "type"), 0)); Printf(declaration, "%s", SwigType_str(Getattr(p, "type"), 0)); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); if (initstack && !(ignored_method && !pure_virtual)) { Append(w->def, "\nSWIG_INIT_STACK;\n"); } /* declare method return value * if the return value is a reference or const reference, a specialized typemap must * handle it, including declaration of c_result ($result). */ if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (ignored_method) { if (!pure_virtual) { if (!is_void) Printf(w->code, "return "); String *super_call = Swig_method_call(super, l); Printf(w->code, "%s;\n", super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); } } else { /* attach typemaps to arguments (C/C++ -> Ruby) */ String *arglist = NewString(""); Swig_director_parms_fixup(l); Swig_typemap_attach_parms("in", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("directorargout", l, w); char source[256]; int outputs = 0; if (!is_void && !asvoid) outputs++; /* build argument list and type conversion string */ idx = 0; p = l; while ( p ) { if (Getattr(p, "tmap:directorargout") != 0) outputs++; if ( checkAttribute( p, "tmap:in:numinputs", "0") ) { p = Getattr(p, "tmap:in:next"); continue; } String *parameterName = Getattr(p, "name"); String *parameterType = Getattr(p, "type"); Putc(',', arglist); if ((tm = Getattr(p, "tmap:directorin")) != 0) { sprintf(source, "obj%d", idx++); String *input = NewString(source); Setattr(p, "emit:directorinput", input); Replaceall(tm, "$input", input); Replaceall(tm, "$owner", "0"); Delete(input); Printv(wrap_args, tm, "\n", NIL); Wrapper_add_localv(w, source, "VALUE", source, "= Qnil", NIL); Printv(arglist, source, NIL); p = Getattr(p, "tmap:directorin:next"); continue; } else if (Cmp(parameterType, "void")) { /** * Special handling for pointers to other C++ director classes. * Ideally this would be left to a typemap, but there is currently no * way to selectively apply the dynamic_cast<> to classes that have * directors. In other words, the type "SwigDirector_$1_lname" only exists * for classes with directors. We avoid the problem here by checking * module.wrap::directormap, but it's not clear how to get a typemap to * do something similar. Perhaps a new default typemap (in addition * to SWIGTYPE) called DIRECTORTYPE? */ if (SwigType_ispointer(parameterType) || SwigType_isreference(parameterType)) { Node *modname = Getattr(parent, "module"); Node *target = Swig_directormap(modname, parameterType); sprintf(source, "obj%d", idx++); String *nonconst = 0; /* strip pointer/reference --- should move to Swig/stype.c */ String *nptype = NewString(Char(parameterType) + 2); /* name as pointer */ String *ppname = Copy(parameterName); if (SwigType_isreference(parameterType)) { Insert(ppname, 0, "&"); } /* if necessary, cast away const since Ruby doesn't support it! */ if (SwigType_isconst(nptype)) { nonconst = NewStringf("nc_tmp_%s", parameterName); String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(parameterType, 0), ppname); Wrapper_add_localv(w, nonconst, SwigType_lstr(parameterType, 0), nonconst, nonconst_i, NIL); Delete(nonconst_i); Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number, "Target language argument '%s' discards const in director method %s::%s.\n", SwigType_str(parameterType, parameterName), SwigType_namestr(c_classname), SwigType_namestr(name)); } else { nonconst = Copy(ppname); } Delete(nptype); Delete(ppname); String *mangle = SwigType_manglestr(parameterType); if (target) { String *director = NewStringf("director_%s", mangle); Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); Wrapper_add_localv(w, source, "VALUE", source, "= Qnil", NIL); Printf(wrap_args, "%s = dynamic_cast(%s);\n", director, nonconst); Printf(wrap_args, "if (!%s) {\n", director); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); Printf(wrap_args, "} else {\n"); Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director); Printf(wrap_args, "}\n"); Delete(director); Printv(arglist, source, NIL); } else { Wrapper_add_localv(w, source, "VALUE", source, "= Qnil", NIL); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); //Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE_p_%s, 0);\n", // source, nonconst, base); Printv(arglist, source, NIL); } Delete(mangle); Delete(nonconst); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(parameterType, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_NOWRAP; break; } } p = nextSibling(p); } /* declare Ruby return value */ String *value_result = NewStringf("VALUE SWIGUNUSED %s", Swig_cresult_name()); Wrapper_add_local(w, Swig_cresult_name(), value_result); Delete(value_result); /* wrap complex arguments to VALUEs */ Printv(w->code, wrap_args, NIL); /* pass the method call on to the Ruby object */ exceptionSafeMethodCall(classname, n, w, idx, arglist, initstack); /* * Ruby method may return a simple object, or an Array of objects. * For in/out arguments, we have to extract the appropriate VALUEs from the Array, * then marshal everything back to C/C++ (return value and output arguments). */ /* Marshal return value and other outputs (if any) from VALUE to C/C++ type */ String *cleanup = NewString(""); String *outarg = NewString(""); if (outputs > 1) { Wrapper_add_local(w, "output", "VALUE output"); Printf(w->code, "if (TYPE(%s) != T_ARRAY) {\n", Swig_cresult_name()); Printf(w->code, "Ruby_DirectorTypeMismatchException(\"Ruby method failed to return an array.\");\n"); Printf(w->code, "}\n"); } idx = 0; /* Marshal return value */ if (!is_void) { tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w); if (tm != 0) { if (outputs > 1 && !asvoid ) { Printf(w->code, "output = rb_ary_entry(%s, %d);\n", Swig_cresult_name(), idx++); Replaceall(tm, "$input", "output"); } else { Replaceall(tm, "$input", Swig_cresult_name()); } /* TODO check this */ if (Getattr(n, "wrap:disown")) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Replaceall(tm, "$result", "c_result"); Printv(w->code, tm, "\n", NIL); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_ERROR; } } /* Marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { if (outputs > 1) { Printf(w->code, "output = rb_ary_entry(%s, %d);\n", Swig_cresult_name(), idx++); Replaceall(tm, "$result", "output"); } else { Replaceall(tm, "$result", Swig_cresult_name()); } Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } Delete(arglist); Delete(cleanup); Delete(outarg); } /* any existing helper functions to handle this? */ if (!is_void) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { Printf(w->code, "return (%s) c_result;\n", rettype); } else { Printf(w->code, "return (%s) *c_result;\n", rettype); } Delete(rettype); } } Printf(w->code, "}\n"); // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK) { Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } /* clean up */ Delete(wrap_args); Delete(pclassname); DelWrapper(w); return status; } virtual int classDirectorConstructors(Node *n) { return Language::classDirectorConstructors(n); } virtual int classDirectorMethods(Node *n) { return Language::classDirectorMethods(n); } virtual int classDirectorDisown(Node *n) { return Language::classDirectorDisown(n); } String *runtimeCode() { String *s = NewString(""); String *shead = Swig_include_sys("rubyhead.swg"); if (!shead) { Printf(stderr, "*** Unable to open 'rubyhead.swg'\n"); } else { Append(s, shead); Delete(shead); } String *serrors = Swig_include_sys("rubyerrors.swg"); if (!serrors) { Printf(stderr, "*** Unable to open 'rubyerrors.swg'\n"); } else { Append(s, serrors); Delete(serrors); } String *strack = Swig_include_sys("rubytracking.swg"); if (!strack) { Printf(stderr, "*** Unable to open 'rubytracking.swg'\n"); } else { Append(s, strack); Delete(strack); } String *sapi = Swig_include_sys("rubyapi.swg"); if (!sapi) { Printf(stderr, "*** Unable to open 'rubyapi.swg'\n"); } else { Append(s, sapi); Delete(sapi); } String *srun = Swig_include_sys("rubyrun.swg"); if (!srun) { Printf(stderr, "*** Unable to open 'rubyrun.swg'\n"); } else { Append(s, srun); Delete(srun); } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigrubyrun.h"); } /*---------------------------------------------------------------------- * kwargsSupport() *--------------------------------------------------------------------*/ bool kwargsSupport() const { // kwargs support isn't actually implemented, but changing to return false may break something now as it turns on compactdefaultargs return true; } }; /* class RUBY */ /* ----------------------------------------------------------------------------- * swig_ruby() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_ruby() { return new RUBY(); } extern "C" Language *swig_ruby(void) { return new_swig_ruby(); } /* * Local Variables: * c-basic-offset: 2 * End: */ swig-4.4.0/Source/Modules/r.cxx0000664000175000017500000024662115075443613016230 0ustar williamwilliam /* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * r.cxx * * R language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" static String* replaceInitialDash(const String *name) { String *retval; if (!Strncmp(name, "_", 1)) { retval = Copy(name); Insert(retval, 0, "s"); } else { retval = Copy(name); } return retval; } static String * getRTypeName(SwigType *t, int *outCount = NULL) { String *b = SwigType_base(t); List *els = SwigType_split(t); int count = 0; int i; if(Strncmp(b, "struct ", 7) == 0) Replace(b, "struct ", "", DOH_REPLACE_FIRST); for(i = 0; i < Len(els); i++) { String *el = Getitem(els, i); if(Strcmp(el, "p.") == 0 || Strncmp(el, "a(", 2) == 0) { count++; Append(b, "Ref"); } } if(outCount) *outCount = count; String *tmp = NewString(""); char *retName = Char(SwigType_manglestr(t)); Insert(tmp, 0, retName); return tmp; } /* -------------------------------------------------------------- * Tries to get the resolved name, with options of adding * or removing a layer of references. Take care not * to request both * --------------------------------------------------------------*/ static String *getRClassName(String *retType, int deRef=0, int upRef=0) { SwigType *resolved = SwigType_typedef_resolve_all(retType); int ispointer = SwigType_ispointer(resolved); int isreference = SwigType_isreference(resolved); if (upRef) { SwigType_add_pointer(resolved); } if (deRef) { if (ispointer) { SwigType_del_pointer(resolved); } if (isreference) { SwigType_del_reference(resolved); } } String *tmp = NewString(""); Insert(tmp, 0, Char(SwigType_manglestr(resolved))); return(tmp); } /* -------------------------------------------------------------- * Tries to get the name of the R class corresponding to the given type * e.g. struct A * is ARef, struct A** is ARefRef. * Now handles arrays, i.e. struct A[2] * --------------------------------------------------------------*/ static String * getRClassNameCopyStruct(String *retType, int addRef) { String *tmp = NewString(""); List *l = SwigType_split(retType); int n = Len(l); if(!l || n == 0) { #ifdef R_SWIG_VERBOSE Printf(stdout, "SwigType_split return an empty list for %s\n", retType); #endif return(tmp); } String *el = Getitem(l, n-1); char *ptr = Char(el); if(strncmp(ptr, "struct ", 7) == 0) ptr += 7; Printf(tmp, "%s", ptr); if(addRef) { for(int i = 0; i < n; i++) { if(Strcmp(Getitem(l, i), "p.") == 0 || Strncmp(Getitem(l, i), "a(", 2) == 0) Printf(tmp, "Ref"); } } return tmp; } /* ------------------------------------------------------------- * Write the elements of a list to the File*, one element per line. * If quote is true, surround the element with "element". * This takes care of inserting a tab in front of each line and also * a comma after each element, except the last one. * --------------------------------------------------------------*/ static void writeListByLine(List *l, File *out, bool quote = 0) { int i, n = Len(l); for(i = 0; i < n; i++) Printf(out, "%s%s%s%s%s\n", tab8, quote ? "\"" :"", Getitem(l, i), quote ? "\"" :"", i < n-1 ? "," : ""); } static const char *usage = "\ R Options (available with -r)\n\ -copystruct - Emit R code to copy C structs (on by default)\n\ -debug - Output debug\n\ -dll - Name of the DLL (without the .dll or .so suffix).\n\ Default is the module name.\n\ -gc - Aggressive garbage collection\n\ -memoryprof - Add memory profile\n\ -namespace - Output NAMESPACE file\n\ -no-init-code - Turn off the generation of the R_init_ code\n\ (registration information still generated)\n\ -package - Package name for the PACKAGE argument of the R .Call()\n\ invocations. Default is the module name.\n\ "; /* ------------------------------------------------------------- * Display the help for this module on the screen/console. * --------------------------------------------------------------*/ static void showUsage() { fputs(usage, stdout); } static bool expandTypedef(SwigType *t) { if (SwigType_isenum(t)) return false; String *prefix = SwigType_prefix(t); if (Strncmp(prefix, "f", 1)) return false; if (Strncmp(prefix, "p.f", 3)) return false; return true; } /* ------------------------------------------------------------- * Determine whether we should add a .copy argument to the S function * that wraps/interfaces to the routine that returns the given type. * --------------------------------------------------------------*/ static int addCopyParameter(SwigType *type) { int ok = 0; ok = Strncmp(type, "struct ", 7) == 0 || Strncmp(type, "p.struct ", 9) == 0; if(!ok) { ok = Strncmp(type, "p.", 2); } return(ok); } static void replaceRClass(String *tm, SwigType *type) { String *tmp = getRClassName(type, 0, 0); String *tmp_base = getRClassName(type, 1, 0); String *tmp_ref = getRClassName(type, 0, 1); Replaceall(tm, "$R_class", tmp); Replaceall(tm, "$*R_class", tmp_base); Replaceall(tm, "$&R_class", tmp_ref); Delete(tmp); Delete(tmp_base); Delete(tmp_ref); } class R : public Language { public: R(); void registerClass(Node *n); void main(int argc, char *argv[]); int top(Node *n); void dispatchFunction(Node *n); int functionWrapper(Node *n); int constantWrapper(Node *n); int variableWrapper(Node *n); int classDeclaration(Node *n); int enumDeclaration(Node *n); String *enumValue(Node *n); virtual int enumvalueDeclaration(Node *n); int membervariableHandler(Node *n); int typedefHandler(Node *n); static List *Swig_overload_rank(Node *n, bool script_lang_wrapping); int memberfunctionHandler(Node *n) { if (debugMode) Printf(stdout, " %s %s\n", Getattr(n, "name"), Getattr(n, "type")); member_name = Getattr(n, "sym:name"); processing_class_member_function = 1; int status = Language::memberfunctionHandler(n); processing_class_member_function = 0; return status; } /* Grab the name of the current class being processed so that we can deal with members of that class. */ int classHandler(Node *n){ if(!ClassMemberTable) ClassMemberTable = NewHash(); class_name = Getattr(n, "name"); int status = Language::classHandler(n); class_name = NULL; return status; } String *runtimeCode(); void replaceSpecialVariables(String *method, String *tm, Parm *parm); protected: int addRegistrationRoutine(String *rname, int nargs); int outputRegistrationRoutines(File *out); int outputCommandLineArguments(File *out); int generateCopyRoutines(Node *n); int DumpCode(Node *n); int OutputMemberReferenceMethod(String *className, int isSet, List *memberList, List *nameList, List *typeList, File *out); int defineArrayAccessors(SwigType *type); void addNamespaceFunction(String *name) { if(!namespaceFunctions) namespaceFunctions = NewList(); Append(namespaceFunctions, name); } void addNamespaceMethod(String *name) { if(!namespaceMethods) namespaceMethods = NewList(); Append(namespaceMethods, name); } String* processType(SwigType *t, Node *n, int *nargs = NULL); String *createFunctionPointerHandler(SwigType *t, Node *n, int *nargs); int addFunctionPointerProxy(String *name, Node *n, SwigType *t, String *s_paramTypes) { /*XXX Do we need to put the t in there to get the return type later. */ if(!functionPointerProxyTable) functionPointerProxyTable = NewHash(); Setattr(functionPointerProxyTable, name, n); Setattr(SClassDefs, name, name); Printv(s_classes, "setClass('", name, "',\n", tab8, "prototype = list(parameterTypes = c(", s_paramTypes, "),\n", tab8, tab8, tab8, "returnType = '", SwigType_manglestr(t), "'),\n", tab8, "contains = 'CRoutinePointer')\n\n##\n", NIL); return SWIG_OK; } void addSMethodInfo(String *name, String *argType, int nargs); // Simple initialization such as constant strings that can be reused. void init(); void addAccessor(String *memberName, Wrapper *f, String *name, String *methodSetGet); static int getFunctionPointerNumArgs(Node *n, SwigType *tt); // filtering of class member lists by function type. Used in constructing accessors // are we allowed to use stl style functors to customise this? List* filterMemberList(List *class_member_function_types, List *class_member_other, String *R_MEMBER, bool equal); protected: bool copyStruct; bool memoryProfile; bool aggressiveGc; // Strings into which we cumulate the generated code that is to be written //vto the files. String *enum_values; String *enum_def_calls; String *sfile; String *f_init; String *s_classes; String *f_begin; String *f_runtime; String *f_wrapper; String *s_header; String *f_wrappers; String *s_init; String *s_init_routine; String *s_namespace; // State variables that carry information across calls to functionWrapper() // from member accessors and class declarations. String *opaqueClassDeclaration; int processing_variable; int processing_member_access_function; String *member_name; String *class_name; String *R_MEMBER_NORMAL; String *R_MEMBER_SET; String *R_MEMBER_GET; int processing_class_member_function; // Spread out the lists so that they are simpler to process // by storing the type of the method (i.e. set, get or nothing) // and having separate lists for name, membername and wrapper List *class_member_function_types; List *class_member_function_names; List *class_member_function_membernames; List *class_member_function_wrappernames; /* */ Hash *ClassMemberTable; Hash *ClassMethodsTable; Hash *SClassDefs; Hash *SMethodInfo; // Information about routines that are generated and to be registered with // R for dynamic lookup. Hash *registrationTable; Hash *functionPointerProxyTable; List *namespaceFunctions; List *namespaceMethods; List *namespaceClasses; // Probably can do this from ClassMemberTable. // Store a copy of the command line. // Need only keep a string that has it formatted. char **Argv; int Argc; bool inCPlusMode; // State variables that we remember from the command line settings // potentially that govern the code we generate. String *DllName; String *Rpackage; bool noInitializationCode; bool outputNamespaceInfo; String *UnProtectWrapupCode; // Static members static bool debugMode; }; R::R() : copyStruct(false), memoryProfile(false), aggressiveGc(false), enum_values(0), enum_def_calls(0), sfile(0), f_init(0), s_classes(0), f_begin(0), f_runtime(0), f_wrapper(0), s_header(0), f_wrappers(0), s_init(0), s_init_routine(0), s_namespace(0), opaqueClassDeclaration(0), processing_variable(0), processing_member_access_function(0), member_name(0), class_name(0), R_MEMBER_NORMAL(NewString("normal")), R_MEMBER_SET(NewString("set")), R_MEMBER_GET(NewString("get")), processing_class_member_function(0), class_member_function_types(0), class_member_function_names(0), class_member_function_membernames(0), class_member_function_wrappernames(0), ClassMemberTable(0), ClassMethodsTable(0), SClassDefs(0), SMethodInfo(0), registrationTable(0), functionPointerProxyTable(0), namespaceFunctions(0), namespaceMethods(0), namespaceClasses(0), Argv(0), Argc(0), inCPlusMode(false), DllName(0), Rpackage(0), noInitializationCode(false), outputNamespaceInfo(false), UnProtectWrapupCode(0) { } bool R::debugMode = false; int R::getFunctionPointerNumArgs(Node *n, SwigType *tt) { (void) tt; n = Getattr(n, "type"); if (debugMode) Printf(stdout, "type: %s\n", n); ParmList *parms = Getattr(n, "parms"); if (debugMode) Printf(stdout, "parms = %p\n", parms); return ParmList_len(parms); } void R::addSMethodInfo(String *name, String *argType, int nargs) { (void) argType; if(!SMethodInfo) SMethodInfo = NewHash(); if (debugMode) Printf(stdout, "[addMethodInfo] %s\n", name); Hash *tb = Getattr(SMethodInfo, name); if(!tb) { tb = NewHash(); Setattr(SMethodInfo, name, tb); } String *str = Getattr(tb, "max"); int max = -1; if(str) max = atoi(Char(str)); if(max < nargs) { if(str) Delete(str); str = NewStringf("%d", max); Setattr(tb, "max", str); } } /* ---------------------------------------- * Returns the name of the new routine. * ------------------------------------------ */ String * R::createFunctionPointerHandler(SwigType *t, Node *n, int *numArgs) { String *funName = SwigType_manglestr(t); /* See if we have already processed this one. */ if(functionPointerProxyTable && Getattr(functionPointerProxyTable, funName)) return funName; if (debugMode) Printf(stdout, " Defining %s\n", t); SwigType *rettype = Copy(Getattr(n, "type")); SwigType *funcparams = SwigType_functionpointer_decompose(rettype); String *rtype = SwigType_str(rettype, 0); // ParmList *parms = Getattr(n, "parms"); // memory leak ParmList *parms = SwigType_function_parms(SwigType_del_pointer(Copy(t)), n); if (debugMode) { Printf(stdout, "Type: %s\n", t); Printf(stdout, "Return type: %s\n", SwigType_base(t)); } bool isVoidType = Strcmp(rettype, "void") == 0; if (debugMode) Printf(stdout, "%s is void ? %s (%s)\n", funName, isVoidType ? "yes" : "no", rettype); Wrapper *f = NewWrapper(); /* Go through argument list, attach lnames for arguments */ int i = 0; Parm *p = parms; for (i = 0; p; p = nextSibling(p), ++i) { String *arg = Getattr(p, "name"); String *lname; if (!arg && Cmp(Getattr(p, "type"), "void")) { lname = NewStringf("arg%d", i+1); Setattr(p, "name", lname); } else lname = arg; Setattr(p, "lname", lname); } Swig_typemap_attach_parms("out", parms, f); Swig_typemap_attach_parms("scoerceout", parms, f); Swig_typemap_attach_parms("scheck", parms, f); Printf(f->def, "%s %s(", rtype, funName); emit_parameter_variables(parms, f); emit_return_variable(n, rettype, f); // emit_attach_parmmaps(parms,f); /* Using weird name and struct to avoid potential conflicts. */ Wrapper_add_local(f, "r_swig_cb_data", "RCallbackFunctionData *r_swig_cb_data = R_SWIG_getCallbackFunctionData()"); String *lvar = NewString("r_swig_cb_data"); bool r_tmp_needed = false; p = parms; int nargs = ParmList_len(parms); if(numArgs) { *numArgs = nargs; if (debugMode) Printf(stdout, "Setting number of parameters to %d\n", *numArgs); } String *setExprElements = NewString(""); String *s_paramTypes = NewString(""); for(i = 0; p; i++) { SwigType *tt = Getattr(p, "type"); SwigType *name = Getattr(p, "name"); SwigType *swig_parm_name = NewStringf("swigarg_%s", name); String *tm = Getattr(p, "tmap:out"); bool isVoidParm = Strcmp(tt, "void") == 0; if (isVoidParm) Printf(f->def, "%s", SwigType_str(tt, 0)); else Printf(f->def, "%s %s", SwigType_str(tt, 0), swig_parm_name); if (tm) { String *lstr = SwigType_lstr(tt, 0); if (SwigType_isreference(tt) || SwigType_isrvalue_reference(tt)) { Printf(f->code, "%s = (%s) &%s;\n", Getattr(p, "lname"), lstr, swig_parm_name); } else if (!isVoidParm) { Printf(f->code, "%s = (%s) %s;\n", Getattr(p, "lname"), lstr, swig_parm_name); } Replaceall(tm, "$1", name); Replaceall(tm, "$result", "r_tmp"); if (debugMode) { Printf(stdout, "Calling Replace A: %s\n", Getattr(p,"type")); } replaceRClass(tm, Getattr(p,"type")); Replaceall(tm,"$owner", "0"); Delete(lstr); } r_tmp_needed = true; Printf(setExprElements, "%s\n", tm); Printf(setExprElements, "SETCAR(r_swig_cb_data->el, %s);\n", "r_tmp"); Printf(setExprElements, "r_swig_cb_data->el = CDR(r_swig_cb_data->el);\n\n"); Printf(s_paramTypes, "'%s'", SwigType_manglestr(tt)); p = nextSibling(p); if(p) { Printf(f->def, ", "); Printf(s_paramTypes, ", "); } } Printf(f->def, ") {\n"); if (r_tmp_needed) Wrapper_add_local(f, "r_tmp", "SEXP r_tmp"); // for use in converting arguments to R objects for call. Wrapper_add_local(f, "r_nprotect", "int r_nprotect = 0"); // for use in converting arguments to R objects for call. Wrapper_add_local(f, "r_vmax", "char * r_vmax= 0"); // for use in converting arguments to R objects for call. Printf(f->code, "Rf_protect(%s->expr = Rf_allocVector(LANGSXP, %d));\n", lvar, nargs + 1); Printf(f->code, "r_nprotect++;\n"); Printf(f->code, "r_swig_cb_data->el = r_swig_cb_data->expr;\n\n"); Printf(f->code, "SETCAR(r_swig_cb_data->el, r_swig_cb_data->fun);\n"); Printf(f->code, "r_swig_cb_data->el = CDR(r_swig_cb_data->el);\n\n"); Printf(f->code, "%s\n\n", setExprElements); Printv(f->code, "r_swig_cb_data->retValue = R_tryEval(", "r_swig_cb_data->expr,", " R_GlobalEnv,", " &r_swig_cb_data->errorOccurred", ");\n", NIL); Printv(f->code, "\n", "if(r_swig_cb_data->errorOccurred) {\n", "R_SWIG_popCallbackFunctionData(1);\n", "Rf_error(\"error in calling R function as a function pointer (", funName, ")\");\n", "}\n", NIL); if(!isVoidType) { /* Need to deal with the return type of the function pointer, not the function pointer itself. So build a new node that has the relevant pieces. XXX Have to be a little more clever so that we can deal with struct A * - the * is getting lost. Is this still true? If so, will a SwigType_push() solve things? */ Parm *bbase = NewParmNode(rettype, n); String *returnTM = Swig_typemap_lookup("in", bbase, Swig_cresult_name(), f); if(returnTM) { String *tm = returnTM; Replaceall(tm,"$input", "r_swig_cb_data->retValue"); replaceRClass(tm, rettype); Replaceall(tm,"$owner", "0"); Replaceall(tm,"$disown","0"); Printf(f->code, "%s\n", tm); } Delete(bbase); } Printv(f->code, "R_SWIG_popCallbackFunctionData(1);\n", NIL); Printv(f->code, "\n", UnProtectWrapupCode, NIL); if (SwigType_isreference(rettype)) { Printv(f->code, "return *", Swig_cresult_name(), ";\n", NIL); } else if (SwigType_isrvalue_reference(rettype)) { Printv(f->code, "return std::move(*", Swig_cresult_name(), ");\n", NIL); } else if (!isVoidType) { Printv(f->code, "return ", Swig_cresult_name(), ";\n", NIL); } Printv(f->code, "\n}\n", NIL); Replaceall(f->code, "SWIG_exception_fail", "SWIG_exception_noreturn"); /* To coerce correctly in S, we really want to have an extra/intermediate function that handles the scoerceout. We need to check if any of the argument types have an entry in that map. If none do, the ignore and call the function straight. Otherwise, generate a marshalling function. Need to be able to find it in S. Or use an entirely generic one that evaluates the expressions. Handle errors in the evaluation of the function by restoring the stack, if there is one in use for this function (i.e. no userData). */ Wrapper_print(f, f_wrapper); addFunctionPointerProxy(funName, n, t, s_paramTypes); Delete(s_paramTypes); Delete(rtype); Delete(rettype); Delete(funcparams); DelWrapper(f); return funName; } void R::init() { UnProtectWrapupCode = NewStringf("%s", "vmaxset(r_vmax);\nif(r_nprotect) Rf_unprotect(r_nprotect);\n\n"); SClassDefs = NewHash(); sfile = NewString(""); f_init = NewString(""); s_header = NewString(""); f_begin = NewString(""); f_runtime = NewString(""); f_wrapper = NewString(""); s_classes = NewString(""); s_init = NewString(""); s_init_routine = NewString(""); enum_def_calls = NewString(""); } /* ------------------------------------------------------------- * Method from Language that is called to start the entire * processing off, i.e. the generation of the code. * It is called after the input has been read and parsed. * Here we open the output streams and generate the code. * ------------------------------------------------------------- */ int R::top(Node *n) { String *module = Getattr(n, "name"); if (debugMode) { Printf(stdout, " %s\n", module); } if(!Rpackage) Rpackage = Copy(module); if(!DllName) DllName = Copy(module); if(outputNamespaceInfo) { s_namespace = NewString(""); Swig_register_filebyname("snamespace", s_namespace); Printf(s_namespace, "useDynLib(%s)\n", DllName); } // Register the naming functions Swig_name_register("wrapper", "R_swig_%f"); /* Associate the different streams with names so that they can be used in %insert directives by the typemap code. */ Swig_register_filebyname("sinit", s_init); Swig_register_filebyname("sinitroutine", s_init_routine); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("header", s_header); Swig_register_filebyname("wrapper", f_wrapper); Swig_register_filebyname("s", sfile); Swig_register_filebyname("sclasses", s_classes); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "R"); Swig_banner_target_lang(s_init, "#"); outputCommandLineArguments(s_init); Printf(f_wrapper, "#ifdef __cplusplus\n"); Printf(f_wrapper, "extern \"C\" {\n"); Printf(f_wrapper, "#endif\n\n"); Language::top(n); Printf(f_wrapper, "#ifdef __cplusplus\n"); Printf(f_wrapper, "}\n"); Printf(f_wrapper, "#endif\n"); String *type_table = NewString(""); SwigType_emit_type_table(f_runtime,f_wrapper); Delete(type_table); if(ClassMemberTable) { //XXX OutputClassAccessInfo(ClassMemberTable, sfile); Delete(ClassMemberTable); ClassMemberTable = NULL; } Printf(f_init,"}\n"); if(registrationTable) outputRegistrationRoutines(f_init); /* Now arrange to write the 2 files - .S and .c. */ DumpCode(n); Delete(sfile); Delete(s_classes); Delete(s_init); Delete(f_wrapper); Delete(f_init); Delete(s_header); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* ------------------------------------------------------------- * Write the generated code to the .S and the .c files. * ------------------------------------------------------------- */ int R::DumpCode(Node *n) { String *output_filename = NewString(""); /* The name of the file in which we will generate the S code. */ Printf(output_filename, "%s%s.R", SWIG_output_directory(), Rpackage); #ifdef R_SWIG_VERBOSE Printf(stdout, "Writing S code to %s\n", output_filename); #endif File *scode = NewFile(output_filename, "w", SWIG_output_files()); if (!scode) { FileErrorDisplay(output_filename); Exit(EXIT_FAILURE); } Delete(output_filename); Printf(scode, "%s\n\n", s_init); Printf(scode, "%s\n\n", s_classes); Printf(scode, "%s\n", sfile); Printf(scode, "%s\n", enum_def_calls); Delete(scode); String *outfile = Getattr(n,"outfile"); File *runtime = NewFile(outfile,"w", SWIG_output_files()); if (!runtime) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } Printf(runtime, "%s", f_begin); Printf(runtime, "%s\n", f_runtime); Printf(runtime, "%s\n", s_header); Printf(runtime, "%s\n", f_wrapper); Printf(runtime, "%s\n", f_init); Delete(runtime); if(outputNamespaceInfo) { output_filename = NewString(""); Printf(output_filename, "%sNAMESPACE", SWIG_output_directory()); File *ns = NewFile(output_filename, "w", SWIG_output_files()); if (!ns) { FileErrorDisplay(output_filename); Exit(EXIT_FAILURE); } Delete(output_filename); Printf(ns, "%s\n", s_namespace); Printf(ns, "\nexport(\n"); writeListByLine(namespaceFunctions, ns); Printf(ns, ")\n"); Printf(ns, "\nexportMethods(\n"); writeListByLine(namespaceMethods, ns, 1); Printf(ns, ")\n"); Delete(ns); Delete(s_namespace); } return SWIG_OK; } List *R::filterMemberList(List *class_member_types, List *class_member_other, String *R_MEMBER, bool equal) { // filters class_member_other based on whether corresponding elements of // class_member_function_types are equal or notequal to R_MEMBER List *CM = NewList(); Iterator ftype, other; for (ftype = First(class_member_types), other = First(class_member_other); ftype.item; ftype=Next(ftype), other=Next(other)) { // verbose, clean up later if the overall structure works if (equal) { if (ftype.item == R_MEMBER) { Append(CM, other.item); } } else { if (ftype.item != R_MEMBER) { Append(CM, other.item); } } } return(CM); } # if 0 // not called /* ------------------------------------------------------------- * We may need to do more.... so this is left as a * stub for the moment. * -------------------------------------------------------------*/ int R::OutputClassAccessInfo(Hash *tb, File *out) { int n = OutputClassMemberTable(tb, out); OutputClassMethodsTable(out); return n; } /* ------------------------------------------------------------- * Currently this just writes the information collected about the * different methods of the C++ classes that have been processed * to the console. * This will be used later to define S4 generics and methods. * --------------------------------------------------------------*/ int R::OutputClassMethodsTable(File *) { Hash *tb = ClassMethodsTable; if(!tb) return SWIG_OK; List *keys = Keys(tb); String *key; int i, n = Len(keys); if (debugMode) { for(i = 0; i < n ; i++ ) { key = Getitem(keys, i); Printf(stdout, "%d) %s\n", i, key); List *els = Getattr(tb, key); int nels = Len(els); Printf(stdout, "\t"); for(int j = 0; j < nels; j+=2) { Printf(stdout, "%s%s", Getitem(els, j), j < nels - 1 ? ", " : ""); Printf(stdout, "%s\n", Getitem(els, j+1)); } Printf(stdout, "\n"); } } return SWIG_OK; } /* -------------------------------------------------------------- * Iterate over the _set and <>_get * elements and generate the $ and $<- functions * that provide constrained access to the member * fields in these elements. * tb - a hash table that is built up in functionWrapper * as we process each membervalueHandler. * The entries are indexed by _set and * _get. Each entry is a List *. * out - the stream where the code is to be written. This is the S * code stream as we generate only S code here. * --------------------------------------------------------------*/ int R::OutputClassMemberTable(Hash *tb, File *out) { List *keys = Keys(tb), *el; String *key; int i, n = Len(keys); /* Loop over all the _set and _get entries in the table. */ /* This function checks for names ending in _set - perhaps it should */ /* use attributes of some other form, as it potentially clashes with */ /* methods ending in _set */ if(n && outputNamespaceInfo) { Printf(s_namespace, "exportClasses("); } for(i = 0; i < n; i++) { key = Getitem(keys, i); el = Getattr(tb, key); String *className = Getitem(el, 0); char *ptr = Char(key); int klen = Len(key); int isSet = 0; if (klen > 4) { ptr = &ptr[klen - 4]; isSet = strcmp(ptr, "_set") == 0; } if(outputNamespaceInfo) Printf(s_namespace, "\"%s\"%s", className, i < n-1 ? "," : ""); } if(n && outputNamespaceInfo) { Printf(s_namespace, ")\n"); } return n; } // end not used #endif /* -------------------------------------------------------------- * Write the methods for $ or $<- for accessing a member field in an * struct or union (or class). * className - the name of the struct or union (e.g. Bar for struct Bar) * isSet - a logical value indicating whether the method is for * modifying ($<-) or accessing ($) the member field. * el - a list of length 2 * # accessible member elements + 1. * The first element is the name of the class. * The other pairs are member name and the name of the R function to access it. * out - the stream where we write the code. * --------------------------------------------------------------*/ int R::OutputMemberReferenceMethod(String *className, int isSet, List *memberList, List *nameList, List *typeList, File *out) { int numMems = Len(memberList), j; int varaccessor = 0; if (numMems == 0) return SWIG_OK; Wrapper *f = NewWrapper(), *attr = NewWrapper(); Printf(f->def, "function(x, name%s)", isSet ? ", value" : ""); Printf(attr->def, "function(x, i, j, ...%s)", isSet ? ", value" : ""); Printf(f->code, "{\n"); Printf(f->code, "%saccessorFuns = list(", tab8); Node *itemList = NewHash(); bool has_prev = false; for(j = 0; j < numMems; j++) { String *item = Getitem(memberList, j); String *dup = Getitem(nameList, j); String *setgetmethod = Getitem(typeList, j); if (setgetmethod == R_MEMBER_GET) varaccessor++; if (Getattr(itemList, item)) continue; Setattr(itemList, item, "1"); String *pitem; if (!Strcmp(item, "operator ()")) { pitem = NewString("call"); } else if (!Strcmp(item, "operator ->")) { pitem = NewString("deref"); } else if (!Strcmp(item, "operator +")) { pitem = NewString("add"); } else if (!Strcmp(item, "operator -")) { pitem = NewString("sub"); } else { pitem = Copy(item); } if (has_prev) Printf(f->code, ", "); Printf(f->code, "'%s' = %s", pitem, dup); has_prev = true; Delete(pitem); } Delete(itemList); Printf(f->code, ");\n"); if (!isSet && varaccessor > 0) { Printf(f->code, "%svaccessors = c(", tab8); bool first = true; for(j = 0; j < numMems; j++) { String *item = Getitem(memberList, j); String *setgetmethod = Getitem(typeList, j); // Check the type here instead of the name if (setgetmethod == R_MEMBER_GET) { Printf(f->code, "%s'%s'", first ? "" : ", ", item); first = false; } } Printf(f->code, ");\n"); } Printv(f->code, ";", tab8, "idx = pmatch(name, names(accessorFuns));\n", tab8, "if(is.na(idx)) \n", tab8, tab4, NIL); Printf(f->code, "return(callNextMethod(x, name%s));\n", isSet ? ", value" : ""); Printv(f->code, tab8, "f = accessorFuns[[idx]];\n", NIL); if(isSet) { Printv(f->code, tab8, "f(x, value);\n", NIL); Printv(f->code, tab8, "x;\n", NIL); // make certain to return the S value. } else { if (varaccessor) { Printv(f->code, tab8, "if (is.na(match(name, vaccessors))) function(...){f(x, ...)} else f(x);\n", NIL); } else { Printv(f->code, tab8, "function(...){f(x, ...)};\n", NIL); } } Printf(f->code, "}\n"); String *classname_str = SwigType_namestr(className); Printf(out, "# Start of accessor method for %s\n", classname_str); Printf(out, "setMethod('$%s', '_p%s', ", isSet ? "<-" : "", getRClassName(className)); Wrapper_print(f, out); Printf(out, ");\n"); if(isSet) { Printf(out, "setMethod('[[<-', c('_p%s', 'character'),", getRClassName(className)); Insert(f->code, 2, "name = i;\n"); Printf(attr->code, "%s", f->code); Wrapper_print(attr, out); Printf(out, ");\n"); } Printf(out, "# end of accessor method for %s\n", classname_str); Delete(classname_str); DelWrapper(attr); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------- * Called when a enumeration is to be processed. * We want to call the R function defineEnumeration(). * tdname is the typedef of the enumeration, i.e. giving its name. * --------------------------------------------------------------*/ int R::enumDeclaration(Node *n) { if (!ImportMode) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; String *symname = Getattr(n, "sym:name"); // TODO - deal with anonymous enumerations // Previous enum code for R didn't wrap them if (!symname || Getattr(n, "unnamedinstance")) return SWIG_NOWRAP; // create mangled name for the enum // This will have content if the %nspace feature is set on // the input file String *nspace = Getattr(n, "sym:nspace"); // NSpace/getNSpace() only works during Language::enumDeclaration call String *ename; String *name = Getattr(n, "name"); ename = getRClassName(name); if (debugMode) { Node *current_class = getCurrentClass(); String *cl = NewString(""); if (current_class) { cl = getEnumClassPrefix(); } Printf(stdout, "enumDeclaration: %s, %s, %s, %s, %s\n", name, symname, nspace, ename, cl); } Delete(name); // set up a call to create the R enum structure. The list of // individual elements will be built in enum_code enum_values = 0; // Emit each enum item Language::enumDeclaration(n); Printf(enum_def_calls, "defineEnumeration(\"%s\",\n .values=c(%s))\n\n", ename, enum_values); Delete(enum_values); Delete(ename); } return SWIG_OK; } /* ------------------------------------------------------------- * --------------------------------------------------------------*/ int R::enumvalueDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) { Printf(stdout, "evd: Not public\n"); return SWIG_NOWRAP; } Swig_require("enumvalueDeclaration", n, "*name", "?value", NIL); String *symname = Getattr(n, "sym:name"); String *value = Getattr(n, "value"); String *name = Getattr(n, "name"); Node *parent = parentNode(n); String *parent_name = Getattr(parent, "name"); String *newsymname = 0; String *tmpValue; // Strange hack from parent method if (value) tmpValue = NewString(value); else tmpValue = NewString(name); // Note that this is used in enumValue() amongst other places Setattr(n, "value", tmpValue); // Deal with enum values that are not int int swigtype = SwigType_type(Getattr(n, "type")); if (swigtype == T_CHAR) { if (Getattr(n, "enumstringval")) { String *val = NewStringf("'%(escape)s'", Getattr(n, "enumstringval")); Setattr(n, "enumvalue", val); Delete(val); } } else { String *numval = Getattr(n, "enumnumval"); if (numval) Setattr(n, "enumvalue", numval); } if (GetFlag(parent, "scopedenum")) { newsymname = Swig_name_member(0, Getattr(parent, "sym:name"), symname); symname = newsymname; } { // Wrap C/C++ enums with constant integers or use the typesafe enum pattern SwigType *typemap_lookup_type = parent_name ? parent_name : NewString("enum "); if (debugMode) { Printf(stdout, "Setting type: %s\n", Copy(typemap_lookup_type)); } Setattr(n, "type", typemap_lookup_type); // Simple integer constants // Note these are always generated for anonymous enums, no matter what enum_feature is specified // Code generated is the same for SimpleEnum and TypeunsafeEnum -> the class it is generated into is determined later String *value = enumValue(n); if (enum_values) { Printf(enum_values, ",\n\"%s\" = %s", name, value); } else { enum_values = NewString(""); Printf(enum_values, "\"%s\" = %s", name, value); } Delete(value); } return SWIG_OK; } /* ------------------------------------------------------------- * Create accessor functions for variables. * Does not create equivalent wrappers for enumerations, * which are handled differently * --------------------------------------------------------------*/ int R::variableWrapper(Node *n) { String *name = Getattr(n, "sym:name"); if (debugMode) { Printf(stdout, "variableWrapper %s\n", n); } processing_variable = 1; Language::variableWrapper(n); // Force the emission of the _set and _get function wrappers. processing_variable = 0; SwigType *ty = Getattr(n, "type"); String *nodeType = nodeType(n); int addCopyParam = addCopyParameter(ty); //XXX processType(ty, n); if (nodeType && !Strcmp(nodeType, "enumitem")) { /* special wrapper for enums - don't want the R _set, _get functions*/ if (debugMode) { Printf(stdout, "variableWrapper enum branch\n"); } } else if(!SwigType_isconst(ty)) { Wrapper *f = NewWrapper(); Printf(f->def, "%s = \nfunction(value%s)\n{\n", name, addCopyParam ? ", .copy = FALSE" : ""); Printv(f->code, "if(missing(value)) {\n", name, "_get(", addCopyParam ? ".copy" : "", ")\n}", NIL); Printv(f->code, " else {\n", name, "_set(value)\n}\n}", NIL); Wrapper_print(f, sfile); DelWrapper(f); } else { Printf(sfile, "%s = %s_get\n", name, name); } return SWIG_OK; } /* ------------------------------------------------------------- * Creates accessor functions for class members. * ToDo - this version depends on naming conventions and needs * to be replaced. * --------------------------------------------------------------*/ void R::addAccessor(String *memberName, Wrapper *wrapper, String *name, String *methodSetGet) { if (!class_member_function_names) { class_member_function_names = NewList(); class_member_function_membernames = NewList(); class_member_function_wrappernames = NewList(); class_member_function_types = NewList(); } Append(class_member_function_types, methodSetGet); Append(class_member_function_names, name); Append(class_member_function_membernames, memberName); String *tmp = NewString(""); Wrapper_print(wrapper, tmp); Append(class_member_function_wrappernames, tmp); // if we could put the wrapper in directly: Append(l, Copy(sfun)); if (debugMode) Printf(stdout, "Adding accessor: %s (%s) => %s\n", memberName, name, tmp); } #define MAX_OVERLOAD 256 namespace { struct Overloaded { Node *n; /* Node */ int argc; /* Argument count */ ParmList *parms; /* Parameters used for overload check */ int error; /* Ambiguity error */ }; } List * R::Swig_overload_rank(Node *n, bool script_lang_wrapping) { Overloaded nodes[MAX_OVERLOAD]; int nnodes = 0; Node *o = Getattr(n,"sym:overloaded"); if (!o) return 0; Node *c = o; while (c) { if (Getattr(c,"error")) { c = Getattr(c,"sym:nextSibling"); continue; } /* Make a list of all the declarations (methods) that are overloaded with * this one particular method name */ if (Getattr(c,"wrap:name")) { nodes[nnodes].n = c; nodes[nnodes].parms = Getattr(c,"wrap:parms"); nodes[nnodes].argc = emit_num_required(nodes[nnodes].parms); nodes[nnodes].error = 0; nnodes++; } c = Getattr(c,"sym:nextSibling"); } /* Sort the declarations by required argument count */ { int i,j; for (i = 0; i < nnodes; i++) { for (j = i+1; j < nnodes; j++) { if (nodes[i].argc > nodes[j].argc) { Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; } } } } /* Sort the declarations by argument types */ { int i,j; for (i = 0; i < nnodes-1; i++) { if (nodes[i].argc == nodes[i+1].argc) { for (j = i+1; (j < nnodes) && (nodes[j].argc == nodes[i].argc); j++) { Parm *p1 = nodes[i].parms; Parm *p2 = nodes[j].parms; int differ = 0; int num_checked = 0; while (p1 && p2 && (num_checked < nodes[i].argc)) { if (debugMode) { Printf(stdout,"p1 = '%s', p2 = '%s'\n", Getattr(p1,"type"), Getattr(p2,"type")); } if (checkAttribute(p1,"tmap:in:numinputs","0")) { p1 = Getattr(p1,"tmap:in:next"); continue; } if (checkAttribute(p2,"tmap:in:numinputs","0")) { p2 = Getattr(p2,"tmap:in:next"); continue; } String *t1 = Getattr(p1,"tmap:typecheck:precedence"); String *t2 = Getattr(p2,"tmap:typecheck:precedence"); if (debugMode) { Printf(stdout,"t1 = '%s', t2 = '%s'\n", t1, t2); } if ((!t1) && (!nodes[i].error)) { Swig_warning(WARN_TYPEMAP_TYPECHECK, Getfile(nodes[i].n), Getline(nodes[i].n), "Overloaded method %s not supported (incomplete type checking rule - no precedence level in typecheck typemap for '%s').\n", Swig_name_decl(nodes[i].n), SwigType_str(Getattr(p1, "type"), 0)); nodes[i].error = 1; } else if ((!t2) && (!nodes[j].error)) { Swig_warning(WARN_TYPEMAP_TYPECHECK, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s not supported (incomplete type checking rule - no precedence level in typecheck typemap for '%s').\n", Swig_name_decl(nodes[j].n), SwigType_str(Getattr(p2, "type"), 0)); nodes[j].error = 1; } if (t1 && t2) { int t1v, t2v; t1v = atoi(Char(t1)); t2v = atoi(Char(t2)); differ = t1v-t2v; } else if (!t1 && t2) differ = 1; else if (t1 && !t2) differ = -1; else if (!t1 && !t2) differ = -1; num_checked++; if (differ > 0) { Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; break; } else if ((differ == 0) && (Strcmp(t1,"0") == 0)) { t1 = Getattr(p1,"ltype"); if (!t1) { t1 = SwigType_ltype(Getattr(p1,"type")); if (Getattr(p1,"tmap:typecheck:SWIGTYPE")) { SwigType_add_pointer(t1); } Setattr(p1,"ltype",t1); } t2 = Getattr(p2,"ltype"); if (!t2) { t2 = SwigType_ltype(Getattr(p2,"type")); if (Getattr(p2,"tmap:typecheck:SWIGTYPE")) { SwigType_add_pointer(t2); } Setattr(p2,"ltype",t2); } /* Need subtype check here. If t2 is a subtype of t1, then we need to change the order */ if (SwigType_issubtype(t2,t1)) { Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; } if (Strcmp(t1,t2) != 0) { differ = 1; break; } } else if (differ) { break; } if (Getattr(p1,"tmap:in:next")) { p1 = Getattr(p1,"tmap:in:next"); } else { p1 = nextSibling(p1); } if (Getattr(p2,"tmap:in:next")) { p2 = Getattr(p2,"tmap:in:next"); } else { p2 = nextSibling(p2); } } if (!differ) { /* See if declarations differ by const only */ String *d1 = Getattr(nodes[i].n, "decl"); String *d2 = Getattr(nodes[j].n, "decl"); if (d1 && d2) { String *dq1 = Copy(d1); String *dq2 = Copy(d2); if (SwigType_isconst(d1)) { Delete(SwigType_pop(dq1)); } if (SwigType_isconst(d2)) { Delete(SwigType_pop(dq2)); } if (Strcmp(dq1, dq2) == 0) { if (SwigType_isconst(d1) && !SwigType_isconst(d2)) { if (script_lang_wrapping) { // Swap nodes so that the const method gets ignored (shadowed by the non-const method) Overloaded t = nodes[i]; nodes[i] = nodes[j]; nodes[j] = t; } differ = 1; if (!nodes[j].error) { if (script_lang_wrapping) { Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[i].n), Getline(nodes[i].n), "using non-const method %s instead.\n", Swig_name_decl(nodes[i].n)); } else { if (!Getattr(nodes[j].n, "overload:ignore")) { Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[i].n), Getline(nodes[i].n), "using %s instead.\n", Swig_name_decl(nodes[i].n)); } } } nodes[j].error = 1; } else if (!SwigType_isconst(d1) && SwigType_isconst(d2)) { differ = 1; if (!nodes[j].error) { if (script_lang_wrapping) { Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_CONST, Getfile(nodes[i].n), Getline(nodes[i].n), "using non-const method %s instead.\n", Swig_name_decl(nodes[i].n)); } else { if (!Getattr(nodes[j].n, "overload:ignore")) { Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[i].n), Getline(nodes[i].n), "using %s instead.\n", Swig_name_decl(nodes[i].n)); } } } nodes[j].error = 1; } } Delete(dq1); Delete(dq2); } } if (!differ) { if (!nodes[j].error) { if (script_lang_wrapping) { Swig_warning(WARN_LANG_OVERLOAD_SHADOW, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s effectively ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_SHADOW, Getfile(nodes[i].n), Getline(nodes[i].n), "as it is shadowed by %s.\n", Swig_name_decl(nodes[i].n)); } else { if (!Getattr(nodes[j].n, "overload:ignore")) { Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[j].n), Getline(nodes[j].n), "Overloaded method %s ignored,\n", Swig_name_decl(nodes[j].n)); Swig_warning(WARN_LANG_OVERLOAD_IGNORED, Getfile(nodes[i].n), Getline(nodes[i].n), "using %s instead.\n", Swig_name_decl(nodes[i].n)); } } nodes[j].error = 1; } } } } } } List *result = NewList(); { int i; for (i = 0; i < nnodes; i++) { if (nodes[i].error) Setattr(nodes[i].n, "overload:ignore", "1"); Append(result,nodes[i].n); } } return result; } void R::dispatchFunction(Node *n) { Wrapper *f = NewWrapper(); String *symname = Getattr(n, "sym:name"); String *nodeType = Getattr(n, "nodeType"); bool constructor = (!Cmp(nodeType, "constructor")); String *sfname = NewString(symname); if (constructor) Replace(sfname, "new_", "", DOH_REPLACE_FIRST); Printf(f->def, "`%s` <- function(...) {", sfname); if (debugMode) { Swig_print_node(n); } List *dispatch = Swig_overload_rank(n, true); int nfunc = Len(dispatch); Printv(f->code, "argtypes <- mapply(class, list(...));\n", "argv <- list(...);\n", "argc <- length(argtypes);\n", "f <- NULL;\n", NIL); Printf(f->code, "# dispatch functions %d\n", nfunc); int cur_args = -1; bool first_compare = true; for (int i=0; i < nfunc; i++) { Node *ni = Getitem(dispatch,i); Parm *pi = Getattr(ni,"wrap:parms"); int num_arguments = emit_num_arguments(pi); String *overname = Getattr(ni,"sym:overname"); if (cur_args != num_arguments) { if (cur_args != -1) { Printv(f->code, "} else ", NIL); } Printf(f->code, "if (argc == %d) {", num_arguments); cur_args = num_arguments; first_compare = true; } Parm *p; int j; if (num_arguments > 0) { if (!first_compare) { Printv(f->code, " else ", NIL); } else { first_compare = false; } Printv(f->code, "if (", NIL); for (p = pi, j = 0 ; j < num_arguments ; j++) { SwigType *pt = Getattr(p, "type"); if (debugMode) { Swig_print_node(p); } String *tm = Swig_typemap_lookup("rtype", p, "", 0); if (tm) { replaceRClass(tm, pt); } /* Check if type have a %typemap(rtypecheck) */ String *tmcheck = Getattr(p,"tmap:rtypecheck"); if (tmcheck) { tmcheck = Copy(tmcheck); String *tmp_argtype = NewStringf("argtypes[%d]", j+1); Replaceall(tmcheck, "$argtype", tmp_argtype); String *tmp_arg = NewStringf("argv[[%d]]", j+1); Replaceall(tmcheck, "$arg", tmp_arg); replaceRClass(tmcheck, pt); if (debugMode) { Printf(stdout, "%s\n", tmcheck); } if (num_arguments == 1) { Printf(f->code, "%s", tmcheck); } else { Printf(f->code, "%s(%s)", j == 0 ? "" : " && ", tmcheck); } Delete(tmcheck); Delete(tmp_arg); Delete(tmp_argtype); } else { Swig_warning(WARN_R_TYPEMAP_RTYPECHECK_UNDEF, input_file, line_number, "No rtypecheck typemap defined for %s\n", SwigType_str(pt, 0)); } p = Getattr(p, "tmap:in:next"); } Printf(f->code, ") { f <- %s%s; }\n", sfname, overname); } else { Printf(f->code, "f <- %s%s; ", sfname, overname); } } if (cur_args != -1) { Printf(f->code, "};\n"); } Printf(f->code, "if (is.null(f)) {\n" "stop(\"cannot find overloaded function for %s with argtypes (\"," "toString(argtypes),\")\");\n" "}", sfname); Printv(f->code, ";\nf(...)", NIL); Printv(f->code, ";\n}", NIL); Wrapper_print(f, sfile); Printv(sfile, "# Dispatch function\n", NIL); DelWrapper(f); } /*-------------------------------------------------------------- * --------------------------------------------------------------*/ int R::functionWrapper(Node *n) { String *fname = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); String *returntype = Getattr(n, "type"); if (debugMode) { Printf(stdout, " %s %s %s\n", fname, iname, returntype); } String *overname = 0; String *nodeType = Getattr(n, "nodeType"); bool constructor = (!Cmp(nodeType, "constructor")); bool destructor = (!Cmp(nodeType, "destructor")); String *sfname = NewString(iname); if (constructor) Replace(sfname, "new_", "", DOH_REPLACE_FIRST); if (Getattr(n,"sym:overloaded")) { overname = Getattr(n,"sym:overname"); Append(sfname, overname); } if (debugMode) Printf(stdout, " processing parameters\n"); ParmList *l = Getattr(n, "parms"); Parm *p; String *tm; p = l; while(p) { SwigType *resultType = Getattr(p, "type"); if (expandTypedef(resultType) && SwigType_istypedef(resultType)) { SwigType *resolved = SwigType_typedef_resolve_all(resultType); if (expandTypedef(resolved)) { if (debugMode) { Printf(stdout, "Setting type: %s\n", resolved); } Setattr(p, "type", Copy(resolved)); } } p = nextSibling(p); } String *unresolved_return_type = Copy(returntype); if (expandTypedef(returntype) && SwigType_istypedef(returntype)) { SwigType *resolved = SwigType_typedef_resolve_all(returntype); if (debugMode) Printf(stdout, " resolved %s\n", Copy(unresolved_return_type)); if (expandTypedef(resolved)) { returntype = Copy(resolved); Setattr(n, "type", returntype); } } if (debugMode) Printf(stdout, " unresolved_return_type %s\n", unresolved_return_type); if(processing_member_access_function) { if (debugMode) Printf(stdout, " '%s' '%s' '%s' '%s'\n", fname, iname, member_name, class_name); if(opaqueClassDeclaration) return SWIG_OK; /* Add the name of this member to a list for this class_name. We will dump all these at the end. */ bool isSet = GetFlag(n, "memberset") ? true : false; String *tmp = NewString(isSet ? Swig_name_set(NSPACE_TODO, class_name) : Swig_name_get(NSPACE_TODO, class_name)); List *memList = Getattr(ClassMemberTable, tmp); if(!memList) { memList = NewList(); Append(memList, class_name); Setattr(ClassMemberTable, tmp, memList); } Delete(tmp); Append(memList, member_name); Append(memList, iname); } int i; int nargs; String *wname = Swig_name_wrapper(iname); if(overname) Append(wname, overname); Setattr(n,"wrap:name", wname); Wrapper *f = NewWrapper(); Wrapper *sfun = NewWrapper(); int isVoidReturnType = (Strcmp(returntype, "void") == 0); // Need to use the unresolved returntype since // typedef resolution removes the const which causes a // mismatch with the function action emit_return_variable(n, unresolved_return_type, f); SwigType *rtype = Getattr(n, "type"); int addCopyParam = 0; if(!isVoidReturnType) addCopyParam = addCopyParameter(rtype); if (debugMode) Printf(stdout, "Adding a .copy argument to %s for %s = %s\n", iname, returntype, addCopyParam ? "yes" : "no"); Printv(f->def, "SWIGEXPORT SEXP\n", wname, " ( ", NIL); Printf(sfun->def, "# Start of %s\n", iname); Printv(sfun->def, "\n`", sfname, "` = function(", NIL); if(outputNamespaceInfo) {//XXX Need to be a little more discriminating if (constructor) { String *niname = Copy(iname); Replace(niname, "new_", "", DOH_REPLACE_FIRST); addNamespaceFunction(niname); Delete(niname); } else { addNamespaceFunction(iname); } } Swig_typemap_attach_parms("scoercein", l, f); Swig_typemap_attach_parms("scoerceout", l, f); Swig_typemap_attach_parms("scheck", l, f); Swig_typemap_attach_parms("rtypecheck", l, f); emit_parameter_variables(l, f); emit_attach_parmmaps(l,f); Setattr(n,"wrap:parms",l); nargs = emit_num_arguments(l); Wrapper_add_local(f, "r_nprotect", "unsigned int r_nprotect = 0"); Wrapper_add_localv(f, "r_ans", "SEXP", "r_ans = R_NilValue", NIL); Wrapper_add_localv(f, "r_vmax", "VMAXTYPE", "r_vmax = vmaxget()", NIL); String *sargs = NewString(""); String *s_inputTypes = NewString(""); String *s_inputMap = NewString(""); bool inFirstArg = true; bool inFirstType = true; Parm *curP; for (p =l, i = 0 ; i < nargs ; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *tt = Getattr(p, "type"); int nargs = -1; String *funcptr_name = processType(tt, p, &nargs); String *name = makeParameterName(n, p, i+1, false); String *lname = Getattr(p, "lname"); if (name) { /* If we have a :: in the parameter name because we are accessing a static member of a class, say, then we need to remove that prefix. */ while (Strstr(name, "::")) { String *oldname = name; name = NewStringf("%s", Strchr(name, ':') + 2); if (debugMode) Printf(stdout, "+++ parameter name with :: in it %s\n", name); Delete(oldname); } } name = replaceInitialDash(name); if (!Strncmp(name, "arg", 3)) { name = Copy(name); Insert(name, 0, "s_"); } if(processing_variable) { name = Copy(name); Insert(name, 0, "s_"); } if(!Strcmp(name, fname)) { name = Copy(name); Insert(name, 0, "s_"); } Printf(sargs, "%s, ", name); String *tm; if((tm = Getattr(p, "tmap:scoercein"))) { Replaceall(tm, "$input", name); replaceRClass(tm, Getattr(p, "type")); if(funcptr_name) { //XXX need to get this to return non-zero if(nargs == -1) nargs = getFunctionPointerNumArgs(p, tt); String *snargs = NewStringf("%d", nargs); Printv(sfun->code, "if(is.function(", name, ")) {", "\n", "assert('...' %in% names(formals(", name, ")) || length(formals(", name, ")) >= ", snargs, ");\n} ", NIL); Delete(snargs); Printv(sfun->code, "else {\n", "if(is.character(", name, ")) {\n", name, " = getNativeSymbolInfo(", name, ");", "\n};\n", "if(is(", name, ", \"NativeSymbolInfo\")) {\n", name, " = ", name, "$address", ";\n};\n", "if(is(", name, ", \"ExternalReference\")) {\n", name, " = ", name, "@ref;\n}\n", "}; \n", NIL); } else { Printf(sfun->code, "%s\n", tm); } } Printv(sfun->def, inFirstArg ? "" : ", ", name, NIL); if ((tm = Getattr(p,"tmap:scheck"))) { Replaceall(tm,"$input", name); replaceRClass(tm, Getattr(p, "type")); Printf(sfun->code,"%s\n",tm); } curP = p; if ((tm = Getattr(p,"tmap:in"))) { Replaceall(tm,"$input", name); if (Getattr(p,"wrap:disown") || (Getattr(p,"tmap:in:disown"))) { Replaceall(tm,"$disown","SWIG_POINTER_DISOWN"); } else { Replaceall(tm,"$disown","0"); } if(funcptr_name) { /* have us a function pointer */ Printf(f->code, "if(TYPEOF(%s) != CLOSXP) {\n", name); Replaceall(tm,"$R_class", ""); } else { replaceRClass(tm, Getattr(p, "type")); } Printf(f->code,"%s\n",tm); if(funcptr_name) Printf(f->code, "} else {\n%s = %s;\nR_SWIG_pushCallbackFunctionData(%s, NULL);\n}\n", lname, funcptr_name, name); Printv(f->def, inFirstArg ? "" : ", ", "SEXP ", name, NIL); if (Len(name) != 0) inFirstArg = false; p = Getattr(p,"tmap:in:next"); } else { p = nextSibling(p); } tm = Swig_typemap_lookup("rtype", curP, "", 0); if(tm) { replaceRClass(tm, Getattr(curP, "type")); } Printf(s_inputTypes, "%s'%s'", inFirstType ? "" : ", ", tm); Printf(s_inputMap, "%s%s='%s'", inFirstType ? "" : ", ", name, tm); inFirstType = false; if(funcptr_name) Delete(funcptr_name); } /* end of looping over parameters. */ if(addCopyParam) { Printf(sfun->def, "%s.copy = FALSE", nargs > 0 ? ", " : ""); Printf(f->def, "%sSEXP s_swig_copy", nargs > 0 ? ", " : ""); Printf(sargs, "as.logical(.copy), "); } Printv(f->def, ")\n{\n", NIL); // SWIG_fail in R leads to a call to Rf_error() which calls longjmp() // which means the destructors of any live function-local C++ objects won't // get run. To avoid this happening, we wrap almost everything in the // function in a block, and end that right before Rf_error() at which // point those destructors will get called. if (CPlusPlus) Append(f->def, "{\n"); Printv(sfun->def, ")\n{\n", NIL); /* Insert cleanup code */ String *cleanup = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { if (tm && (Len(tm) != 0)) { Printv(cleanup, tm, "\n", NIL); } p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } String *outargs = NewString(""); int numOutArgs = isVoidReturnType ? -1 : 0; for(p = l, i = 0; p; i++) { if((tm = Getattr(p, "tmap:argout"))) { // String *lname = Getattr(p, "lname"); numOutArgs++; String *pos = NewStringf("%d", numOutArgs); Replaceall(tm,"$result", "r_ans"); Replaceall(tm,"$n", pos); // The position into which to store the answer. Replaceall(tm,"$arg", Getattr(p, "emit:input")); Replaceall(tm,"$input", Getattr(p, "emit:input")); Replaceall(tm,"$owner", "0"); Printf(outargs, "%s\n", tm); p = Getattr(p,"tmap:argout:next"); } else p = nextSibling(p); } String *actioncode = emit_action(n); /* Deal with the explicit return value. */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { SwigType *retType = Getattr(n, "type"); Replaceall(tm,"$1", Swig_cresult_name()); Replaceall(tm,"$result", "r_ans"); if (debugMode){ Printf(stdout, "Calling replace D: %s, %s, %s\n", retType, n, tm); } replaceRClass(tm, retType); if (GetFlag(n,"feature:new")) { Replaceall(tm, "$owner", "SWIG_POINTER_OWN"); } else { Replaceall(tm,"$owner", "0"); } Printf(f->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), fname); } if(Len(outargs)) { Wrapper_add_local(f, "R_OutputValues", "SEXP R_OutputValues"); String *tmp = NewString(""); if(!isVoidReturnType) Printf(tmp, "Rf_protect(r_ans);\n"); Printf(tmp, "Rf_protect(R_OutputValues = Rf_allocVector(VECSXP,%d));\nr_nprotect += %d;\n", numOutArgs + !isVoidReturnType, isVoidReturnType ? 1 : 2); if(!isVoidReturnType) Printf(tmp, "SET_VECTOR_ELT(R_OutputValues, 0, r_ans);\n"); Printf(tmp, "r_ans = R_OutputValues;\n"); Insert(outargs, 0, tmp); Delete(tmp); Printv(f->code, outargs, NIL); Delete(outargs); } /* Output cleanup code */ int need_cleanup = Len(cleanup) != 0; if (need_cleanup) { Printv(f->code, cleanup, NIL); } /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } } /* See if there is any return cleanup code */ if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } Printv(f->code, UnProtectWrapupCode, NIL); /*If the user gave us something to convert the result in */ if ((tm = Swig_typemap_lookup("scoerceout", n, Swig_cresult_name(), sfun))) { Replaceall(tm,"$result","ans"); if (debugMode) { Printf(stdout, "Calling replace B: %s, %s, %s\n", Getattr(n, "type"), Getattr(n, "sym:name"), getNSpace()); } replaceRClass(tm, Getattr(n, "type")); Chop(tm); } Printv(sfun->code, ";", (Len(tm) ? "ans = " : ""), ".Call('", wname, "', ", sargs, "PACKAGE='", Rpackage, "');\n", NIL); if(Len(tm)) { Printf(sfun->code, "%s\n\n", tm); if (constructor) { String *finalizer = NewString(iname); Replace(finalizer, "new_", "", DOH_REPLACE_FIRST); Printf(sfun->code, "reg.finalizer(ans@ref, delete_%s);\n", finalizer); } Printf(sfun->code, "ans\n"); } if (destructor) Printv(f->code, "R_ClearExternalPtr(self);\n", NIL); Printv(f->code, "return r_ans;\n", NIL); /* Error handling code */ Printv(f->code, "fail: SWIGUNUSED;\n", NIL); if (need_cleanup) { Printv(f->code, cleanup, NIL); } if (CPlusPlus) Append(f->code, "}\n"); Printv(f->code, " Rf_error(\"%s %s\", SWIG_ErrorType(SWIG_lasterror_code), SWIG_lasterror_msg);\n", NIL); Printv(f->code, " return R_NilValue;\n", NIL); Delete(cleanup); Printv(f->code, "}\n", NIL); Printv(sfun->code, "\n}", NIL); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code,"$symname",iname); Wrapper_print(f, f_wrapper); Wrapper_print(sfun, sfile); Printf(sfun->code, "\n# End of %s\n", iname); tm = Swig_typemap_lookup("rtype", n, "", 0); if(tm) { SwigType *retType = Getattr(n, "type"); if (debugMode) { Printf(stdout, "Calling replace C: %s\n", Copy(retType)); } replaceRClass(tm, retType); } Printv(sfile, "attr(`", sfname, "`, 'returnType') = '", isVoidReturnType ? "void" : (tm ? tm : ""), "'\n", NIL); if(nargs > 0) Printv(sfile, "attr(`", sfname, "`, \"inputTypes\") = c(", s_inputTypes, ")\n", NIL); Printv(sfile, "class(`", sfname, "`) = c(\"SWIGFunction\", class('", sfname, "'))\n\n", NIL); if (memoryProfile) { Printv(sfile, "memory.profile()\n", NIL); } if (aggressiveGc) { Printv(sfile, "gc()\n", NIL); } // Printv(sfile, "setMethod('", name, "', '", name, "', ", iname, ")\n\n\n"); /* If we are dealing with a method in an C++ class, then add the name of the R function and its definition. XXX need to figure out how to store the Wrapper if possible in the hash/list. Would like to be able to do this so that we can potentially insert */ if(processing_member_access_function || processing_class_member_function) { String *method_type = R_MEMBER_NORMAL; if (GetFlag(n, "memberset")) { method_type = R_MEMBER_SET; } else if (GetFlag(n, "memberget")) { method_type = R_MEMBER_GET; } addAccessor(member_name, sfun, iname, method_type); } if (Getattr(n, "sym:overloaded") && !Getattr(n, "sym:nextSibling")) { dispatchFunction(n); } addRegistrationRoutine(wname, addCopyParam ? nargs +1 : nargs); DelWrapper(f); DelWrapper(sfun); Delete(sargs); Delete(sfname); return SWIG_OK; } /* ---------------------------------------------------------------------- * R::constantWrapper() * ---------------------------------------------------------------------- */ int R::constantWrapper(Node *n) { (void) n; // TODO return SWIG_OK; } /*-------------------------------------------------------------- * Add the specified routine name to the collection of * generated routines that are called from R functions. * This is used to register the routines with R for * resolving symbols. * rname - the name of the routine * nargs - the number of arguments it expects. * --------------------------------------------------------------*/ int R::addRegistrationRoutine(String *rname, int nargs) { if(!registrationTable) registrationTable = NewHash(); String *el = NewStringf("{\"%s\", (DL_FUNC) &%s, %d}", rname, rname, nargs); Setattr(registrationTable, rname, el); return SWIG_OK; } /* ------------------------------------------------------------- * Write the registration information to an array and * create the initialization routine for registering * these. * --------------------------------------------------------------*/ int R::outputRegistrationRoutines(File *out) { int i, n; if(!registrationTable) return(0); if(inCPlusMode) Printf(out, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); Printf(out, "#include \n\n"); if(inCPlusMode) Printf(out, "#ifdef __cplusplus\n}\n#endif\n\n"); Printf(out, "SWIGINTERN R_CallMethodDef CallEntries[] = {\n"); List *keys = Keys(registrationTable); n = Len(keys); for(i = 0; i < n; i++) Printf(out, " %s,\n", Getattr(registrationTable, Getitem(keys, i))); Printf(out, " {NULL, NULL, 0}\n};\n\n"); if(!noInitializationCode) { if (inCPlusMode) Printv(out, "extern \"C\" ", NIL); { /* R allows pckage names to have '.' in the name, which is not allowed in C++ var names we simply replace all occurrences of '.' with '_' to construct the var name */ String * Rpackage_sane = Copy(Rpackage); Replace(Rpackage_sane, ".", "_", DOH_REPLACE_ANY); Printf(out, "SWIGEXPORT void R_init_%s(DllInfo *dll) {\n", Rpackage_sane); Delete(Rpackage_sane); } Printf(out, "%sR_registerRoutines(dll, NULL, CallEntries, NULL, NULL);\n", tab4); if(Len(s_init_routine)) { Printf(out, "\n%s\n", s_init_routine); } Printf(out, "}\n"); } return n; } /* ------------------------------------------------------------- * Process a struct, union or class declaration in the source code, * or an anonymous typedef struct * --------------------------------------------------------------*/ //XXX What do we need to do here - // Define an S4 class to refer to this. void R::registerClass(Node *n) { String *name = Getattr(n, "name"); if (debugMode) Swig_print_node(n); String *sname = NewStringf("_p%s", SwigType_manglestr(name)); if(!Getattr(SClassDefs, sname)) { Setattr(SClassDefs, sname, sname); String *base; if (CPlusPlus && (Strcmp(nodeType(n), "class") == 0)) { base = NewString(""); List *l = Getattr(n, "bases"); if(Len(l)) { Printf(base, "c("); for(int i = 0; i < Len(l); i++) { registerClass(Getitem(l, i)); Printf(base, "'_p%s'%s", SwigType_manglestr(Getattr(Getitem(l, i), "name")), i < Len(l)-1 ? ", " : ""); } Printf(base, ")"); } else { base = NewString("'C++Reference'"); } } else base = NewString("'ExternalReference'"); Printf(s_classes, "setClass('%s', contains = %s)\n", sname, base); Delete(base); } } int R::classDeclaration(Node *n) { String *name = Getattr(n, "name"); String *kind = Getattr(n, "kind"); if (debugMode) Swig_print_node(n); registerClass(n); /* If we have a typedef union { ... } U, then we never get to see the typedef via a regular call to typedefHandler. Instead, */ if(Getattr(n, "unnamed") && Getattr(n, "storage") && Strcmp(Getattr(n, "storage"), "typedef") == 0 && Getattr(n, "tdname") && Strcmp(Getattr(n, "tdname"), name) == 0) { if (debugMode) Printf(stdout, "Typedef in the class declaration for %s\n", name); // typedefHandler(n); } bool opaque = GetFlag(n, "feature:opaque") ? true : false; if(opaque) opaqueClassDeclaration = name; int status = Language::classDeclaration(n); opaqueClassDeclaration = NULL; if (class_member_function_types) { // collect the "set" methods List *class_set_membernames = filterMemberList(class_member_function_types, class_member_function_membernames, R_MEMBER_SET, true); List *class_set_functionnames = filterMemberList(class_member_function_types, class_member_function_names, R_MEMBER_SET, true); // this one isn't used - collecting to keep code simpler List *class_set_functiontypes = filterMemberList(class_member_function_types, class_member_function_types, R_MEMBER_SET, true); // collect the others List *class_other_membernames = filterMemberList(class_member_function_types, class_member_function_membernames, R_MEMBER_SET, false); List *class_other_functionnames = filterMemberList(class_member_function_types, class_member_function_names, R_MEMBER_SET, false); List *class_other_functiontypes = filterMemberList(class_member_function_types, class_member_function_types, R_MEMBER_SET, false); if (Len(class_other_membernames) > 0) { OutputMemberReferenceMethod(name, 0, class_other_membernames, class_other_functionnames, class_other_functiontypes, sfile); } if (Len(class_set_membernames) > 0) { OutputMemberReferenceMethod(name, 1, class_set_membernames, class_set_functionnames, class_set_functiontypes, sfile); } Delete(class_set_membernames); Delete(class_set_functionnames); Delete(class_set_functiontypes); Delete(class_other_membernames); Delete(class_other_functionnames); Delete(class_other_functiontypes); } if (class_member_function_types) { Delete(class_member_function_types); class_member_function_types = NULL; Delete(class_member_function_names); class_member_function_names = NULL; Delete(class_member_function_membernames); class_member_function_membernames = NULL; Delete(class_member_function_wrappernames); class_member_function_wrappernames = NULL; } if (Getattr(n, "has_destructor")) { Printf(sfile, "setMethod('delete', '_p%s', function(obj) {delete%s(obj)})\n", getRClassName(name), getRClassName(name)); } if(!opaque && !Strcmp(kind, "struct") && copyStruct) { String *def = NewStringf("setClass(\"%s\",\n%srepresentation(\n", name, tab4); bool firstItem = true; for(Node *c = firstChild(n); c; ) { String *elName; String *tp; elName = Getattr(c, "name"); String *elKind = Getattr(c, "kind"); if (!Equal(elKind, "variable")) { c = nextSibling(c); continue; } if (!Len(elName)) { c = nextSibling(c); continue; } tp = Swig_typemap_lookup("rtype", c, "", 0); if(!tp) { c = nextSibling(c); continue; } if (Strstr(tp, "R_class")) { c = nextSibling(c); continue; } if (Strcmp(tp, "character") && Strstr(Getattr(c, "decl"), "p.")) { c = nextSibling(c); continue; } if (!firstItem) { Printf(def, ",\n"); } // else //XXX How can we tell if this is already done. // SwigType_push(elType, elDecl); // returns "" tp = processType(elType, c, NULL); // Printf(stdout, " elType %p\n", elType); // tp = getRClassNameCopyStruct(Getattr(c, "type"), 1); String *elNameT = replaceInitialDash(elName); Printf(def, "%s%s = \"%s\"", tab8, elNameT, tp); firstItem = false; Delete(tp); Delete(elNameT); c = nextSibling(c); } Printf(def, "),\n%scontains = \"RSWIGStruct\")\n", tab8); Printf(s_classes, "%s\n\n# End class %s\n\n", def, name); generateCopyRoutines(n); Delete(def); } return status; } /* ------------------------------------------------------------- * Create the C routines that copy an S object of the class given * by the given struct definition in Node *n to the C value * and also the routine that goes from the C routine to an object * of this S class. * --------------------------------------------------------------*/ /*XXX Clean up the toCRef - make certain the names are correct for the types, etc. in all cases. */ int R::generateCopyRoutines(Node *n) { Wrapper *copyToR = NewWrapper(); Wrapper *copyToC = NewWrapper(); String *name = Getattr(n, "name"); String *tdname = Getattr(n, "tdname"); String *kind = Getattr(n, "kind"); String *type; if(Len(tdname)) { type = Copy(tdname); } else { type = NewStringf("%s %s", kind, name); } String *mangledName = SwigType_manglestr(name); if (debugMode) Printf(stdout, "generateCopyRoutines: name = %s, %s\n", name, type); Printf(copyToR->def, "CopyToR%s = function(value, obj = new(\"%s\"))\n{\n", mangledName, name); Printf(copyToC->def, "CopyToC%s = function(value, obj)\n{\n", mangledName); Node *c = firstChild(n); for(; c; c = nextSibling(c)) { String *elName = Getattr(c, "name"); if (!Len(elName)) { continue; } String *elKind = Getattr(c, "kind"); if (!Equal(elKind, "variable")) { continue; } String *tp = Swig_typemap_lookup("rtype", c, "", 0); if(!tp) { continue; } if (Strstr(tp, "R_class")) { continue; } if (Strcmp(tp, "character") && Strstr(Getattr(c, "decl"), "p.")) { continue; } /* The S functions to get and set the member value. */ String *elNameT = replaceInitialDash(elName); Printf(copyToR->code, "obj@%s = value$%s;\n", elNameT, elNameT); Printf(copyToC->code, "obj$%s = value@%s;\n", elNameT, elNameT); Delete(elNameT); } Printf(copyToR->code, "obj;\n}\n\n"); String *rclassName = getRClassNameCopyStruct(type, 0); // without the Ref. Printf(sfile, "# Start definition of copy functions & methods for %s\n", rclassName); Wrapper_print(copyToR, sfile); Printf(copyToC->code, "obj\n}\n\n"); Wrapper_print(copyToC, sfile); Printf(sfile, "# Start definition of copy methods for %s\n", rclassName); Printf(sfile, "setMethod('copyToR', '_p%s', CopyToR%s);\n", mangledName, mangledName); Printf(sfile, "setMethod('copyToC', '%s', CopyToC%s);\n\n", rclassName, mangledName); Printf(sfile, "# End definition of copy methods for %s\n", rclassName); Printf(sfile, "# End definition of copy functions & methods for %s\n", rclassName); String *m = NewStringf("%sCopyToR", name); addNamespaceMethod(m); char *tt = Char(m); tt[Len(m)-1] = 'C'; addNamespaceMethod(m); Delete(m); Delete(rclassName); Delete(mangledName); DelWrapper(copyToR); DelWrapper(copyToC); return SWIG_OK; } /* ------------------------------------------------------------- * Called when there is a typedef to be invoked. * * XXX Needs to be enhanced or split to handle the case where we have a * typedef within a classDeclaration emission because the struct/union/etc. * is anonymous. * --------------------------------------------------------------*/ int R::typedefHandler(Node *n) { SwigType *tp = Getattr(n, "type"); String *type = Getattr(n, "type"); if (debugMode) Printf(stdout, " %s\n", Getattr(n, "name")); processType(tp, n); if(Strncmp(type, "struct ", 7) == 0) { String *name = Getattr(n, "name"); char *trueName = Char(type); trueName += 7; if (debugMode) Printf(stdout, " Defining S class %s\n", trueName); Printf(s_classes, "setClass('_p%s', contains = 'ExternalReference')\n", SwigType_manglestr(name)); } return Language::typedefHandler(n); } /* -------------------------------------------------------------- * Called when processing a field in a "class", i.e. struct, union or * actual class. We set a state variable so that we can correctly * interpret the resulting functionWrapper() call and understand that * it is for a field element. * --------------------------------------------------------------*/ int R::membervariableHandler(Node *n) { SwigType *t = Getattr(n, "type"); processType(t, n, NULL); processing_member_access_function = 1; member_name = Getattr(n,"sym:name"); if (debugMode) Printf(stdout, " name = %s, sym:name = %s\n", Getattr(n, "name"), member_name); int status(Language::membervariableHandler(n)); if(!opaqueClassDeclaration && debugMode) Printf(stdout, " %s %s\n", Getattr(n, "name"), Getattr(n, "type")); processing_member_access_function = 0; member_name = NULL; return status; } /* This doesn't seem to get used so leave it out for the moment. */ String * R::runtimeCode() { String *s = Swig_include_sys("rrun.swg"); if (!s) { Printf(stdout, "*** Unable to open 'rrun.swg'\n"); s = NewString(""); } return s; } /*---------------------------------------------------------------------- * replaceSpecialVariables() *--------------------------------------------------------------------*/ void R::replaceSpecialVariables(String *method, String *tm, Parm *parm) { (void)method; SwigType *type = Getattr(parm, "type"); replaceRClass(tm, type); } /* ----------------------------------------------------------------------- * Called when SWIG wants to initialize this * We initialize anythin we want here. * Most importantly, tell SWIG where to find the files (e.g. r.swg) for this module. * Use Swig_mark_arg() to tell SWIG that it is understood and not to * throw an error. * --------------------------------------------------------------*/ void R::main(int argc, char *argv[]) { init(); Preprocessor_define("SWIGR 1", 0); SWIG_library_directory("r"); SWIG_config_file("r.swg"); debugMode = false; copyStruct = true; memoryProfile = false; aggressiveGc = false; inCPlusMode = false; outputNamespaceInfo = false; noInitializationCode = false; this->Argc = argc; this->Argv = argv; allow_overloading();// can we support this? for(int i = 0; i < argc; i++) { if(strcmp(argv[i], "-package") == 0) { Swig_mark_arg(i); i++; Swig_mark_arg(i); Rpackage = argv[i]; } else if(strcmp(argv[i], "-dll") == 0) { Swig_mark_arg(i); i++; Swig_mark_arg(i); DllName = argv[i]; } else if(strcmp(argv[i], "-help") == 0) { showUsage(); } else if(strcmp(argv[i], "-namespace") == 0) { outputNamespaceInfo = true; Swig_mark_arg(i); } else if(!strcmp(argv[i], "-no-init-code")) { noInitializationCode = true; Swig_mark_arg(i); } else if(!strcmp(argv[i], "-c++")) { inCPlusMode = true; Swig_mark_arg(i); Printf(s_classes, "setClass('C++Reference', contains = 'ExternalReference')\n"); } else if(!strcmp(argv[i], "-debug")) { debugMode = true; Swig_mark_arg(i); } else if (!strcmp(argv[i],"-copystruct")) { copyStruct = true; Swig_mark_arg(i); } else if (!strcmp(argv[i], "-nocopystruct")) { copyStruct = false; Swig_mark_arg(i); } else if (!strcmp(argv[i], "-memoryprof")) { memoryProfile = true; Swig_mark_arg(i); } else if (!strcmp(argv[i], "-nomemoryprof")) { memoryProfile = false; Swig_mark_arg(i); } else if (!strcmp(argv[i], "-aggressivegc")) { aggressiveGc = true; Swig_mark_arg(i); } else if (!strcmp(argv[i], "-noaggressivegc")) { aggressiveGc = false; Swig_mark_arg(i); } else if (strcmp(argv[i], "-cppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is now always on.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is no longer supported.\n", argv[i]); Swig_mark_arg(i); Exit(EXIT_FAILURE); } if (debugMode) { Swig_typemap_search_debug_set(); Swig_typemap_used_debug_set(); Swig_typemap_register_debug_set(); Swig_file_debug_set(); } /// copyToR copyToC functions. } } /* ----------------------------------------------------------------------- * Could make this work for String or File and then just store the resulting string * rather than the collection of arguments and argc. * ----------------------------------------------------------------------- */ int R::outputCommandLineArguments(File *out) { if(Argc < 1 || !Argv || !Argv[0]) return(-1); Printf(out, "\n## Generated via the command line invocation:\n##\t"); for(int i = 0; i < Argc ; i++) { Printf(out, " %s", Argv[i]); } Printf(out, "\n\n\n"); return Argc; } /* How SWIG instantiates an object from this module. See swigmain.cxx */ extern "C" Language *swig_r(void) { return new R(); } /* ----------------------------------------------------------------------- * Needs to be reworked. *----------------------------------------------------------------------- */ String * R::processType(SwigType *t, Node *n, int *nargs) { //XXX Need to handle typedefs, e.g. // a type which is a typedef to a function pointer. SwigType *tmp = Getattr(n, "tdname"); if (debugMode) Printf(stdout, "processType %s (tdname = %s)(SwigType = %s)\n", Getattr(n, "name"), tmp, Copy(t)); SwigType *td = t; if (expandTypedef(t) && SwigType_istypedef(t)) { SwigType *resolved = SwigType_typedef_resolve_all(t); if (expandTypedef(resolved)) { td = Copy(resolved); } } if(!td) { int count = 0; String *b = getRTypeName(t, &count); if(count && b && !Getattr(SClassDefs, b)) { if (debugMode) Printf(stdout, " Defining class %s\n", b); Printf(s_classes, "setClass('%s', contains = 'ExternalReference')\n", b); Setattr(SClassDefs, b, b); } } if(td) t = td; if(SwigType_isfunctionpointer(t)) { if (debugMode) Printf(stdout, " Defining pointer handler %s\n", t); String *tmp = createFunctionPointerHandler(t, n, nargs); return tmp; } return NULL; } /* ----------------------------------------------------------------------- * enumValue() * This method will return a string with an enum value to use in from R when * setting up an enum variable * ------------------------------------------------------------------------ */ String *R::enumValue(Node *n) { String *symname = Getattr(n, "sym:name"); String *value = Getattr(n, "value"); String *newsymname = 0; Node *parent = parentNode(n); symname = Getattr(n, "sym:name"); // parent enumtype has namespace mangled in String *etype = Getattr(parent, "enumtype"); // we have to directly call the c wrapper function, as the // R wrapper to the enum is designed to be used after the enum // structures have been created on the R side. This means // that we'll need to construct a .Call expression // change the type for variableWrapper if (debugMode) { Printf(stdout, " type set: %s\n", etype); } Setattr(n, "type", etype); if (!getCurrentClass()) { newsymname = Swig_name_member(0, Getattr(parent, "sym:name"), symname); // Strange hack to change the name Setattr(n, "name", Getattr(n, "value")); Setattr(n, "sym:name", newsymname); variableWrapper(n); value = Swig_name_get(NSPACE_TODO, newsymname); } else { String *enumClassPrefix = getEnumClassPrefix(); newsymname = Swig_name_member(0, enumClassPrefix, symname); Setattr(n, "name", Getattr(n, "value")); Setattr(n, "sym:name", newsymname); variableWrapper(n); value = Swig_name_get(NSPACE_TODO, newsymname); } value = Swig_name_wrapper(value); Replace(value, "_wrap", "R_swig", DOH_REPLACE_FIRST); String *valuecall=NewString(""); Printv(valuecall, ".Call('", value, "',FALSE, PACKAGE='", Rpackage, "')", NIL); Delete(value); return valuecall; } swig-4.4.0/Source/Modules/csharp.cxx0000664000175000017500000054530415075443613017247 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * csharp.cxx * * C# language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include // for INT_MAX #include #include /* Hash type used for upcalls from C/C++ */ typedef DOH UpcallData; class CSHARP:public Language { static const char *usage; const String *empty_string; const String *public_string; const String *protected_string; Hash *swig_types_hash; File *f_begin; File *f_runtime; File *f_runtime_h; File *f_header; File *f_wrappers; File *f_init; File *f_directors; File *f_directors_h; File *f_single_out; List *filenames_list; bool proxy_flag; // Flag for generating proxy classes bool native_function_flag; // Flag for when wrapping a native function bool enum_constant_flag; // Flag for when wrapping an enum or constant bool static_flag; // Flag for when wrapping a static functions or member variables bool variable_wrapper_flag; // Flag for when wrapping a nonstatic member variable bool wrapping_member_flag; // Flag for when wrapping a member variable/enum/const bool global_variable_flag; // Flag for when wrapping a global variable bool old_variable_names; // Flag for old style variable names in the intermediary class bool generate_property_declaration_flag; // Flag for generating properties bool doxygen; // Flag for Doxygen generation String *imclass_name; // intermediary class name String *module_class_name; // module class name String *imclass_class_code; // intermediary class code String *proxy_class_def; String *proxy_class_code; String *interface_class_code; // if %feature("interface") was declared for a class, here goes the interface declaration String *module_class_code; String *proxy_class_name; // proxy class name String *full_imclass_name; // fully qualified intermediary class name when using nspace feature, otherwise same as imclass_name String *variable_name; //Name of a variable being wrapped String *proxy_class_constants_code; String *module_class_constants_code; String *common_begin_code; String *enum_code; String *dllimport; // DllImport attribute name String *namespce; // Optional namespace name String *imclass_imports; //intermediary class imports from %pragma String *module_imports; //module imports from %pragma String *imclass_baseclass; //inheritance for intermediary class class from %pragma String *module_baseclass; //inheritance for module class from %pragma String *imclass_interfaces; //interfaces for intermediary class class from %pragma String *module_interfaces; //interfaces for module class from %pragma String *imclass_class_modifiers; //class modifiers for intermediary class overridden by %pragma String *module_class_modifiers; //class modifiers for module class overridden by %pragma String *upcasts_code; //C++ casts for inheritance hierarchies C++ code String *imclass_cppcasts_code; //C++ casts up inheritance hierarchies intermediary class code String *director_callback_typedefs; // Director function pointer typedefs for callbacks String *director_callbacks; // Director callback function pointer member variables String *director_delegate_callback; // Director callback method that delegates are set to call String *director_delegate_definitions; // Director delegates definitions in proxy class String *director_delegate_instances; // Director delegates member variables in proxy class String *director_method_types; // Director method types String *director_connect_parms; // Director delegates parameter list for director connect call String *destructor_call; //C++ destructor call if any String *output_file; // File name for single file mode. If set all generated code will be written to this file // Director method stuff: List *dmethods_seq; Hash *dmethods_table; int n_dmethods; int n_directors; int first_class_dmethod; int curr_class_dmethod; int nesting_depth; enum EnumFeature { SimpleEnum, TypeunsafeEnum, TypesafeEnum, ProperEnum }; public: /* ----------------------------------------------------------------------------- * CSHARP() * ----------------------------------------------------------------------------- */ CSHARP():empty_string(NewString("")), public_string(NewString("public")), protected_string(NewString("protected")), swig_types_hash(NULL), f_begin(NULL), f_runtime(NULL), f_runtime_h(NULL), f_header(NULL), f_wrappers(NULL), f_init(NULL), f_directors(NULL), f_directors_h(NULL), f_single_out(NULL), filenames_list(NULL), proxy_flag(true), native_function_flag(false), enum_constant_flag(false), static_flag(false), variable_wrapper_flag(false), wrapping_member_flag(false), global_variable_flag(false), old_variable_names(false), generate_property_declaration_flag(false), doxygen(false), imclass_name(NULL), module_class_name(NULL), imclass_class_code(NULL), proxy_class_def(NULL), proxy_class_code(NULL), interface_class_code(NULL), module_class_code(NULL), proxy_class_name(NULL), full_imclass_name(NULL), variable_name(NULL), proxy_class_constants_code(NULL), module_class_constants_code(NULL), common_begin_code(NULL), enum_code(NULL), dllimport(NULL), namespce(NULL), imclass_imports(NULL), module_imports(NULL), imclass_baseclass(NULL), module_baseclass(NULL), imclass_interfaces(NULL), module_interfaces(NULL), imclass_class_modifiers(NULL), module_class_modifiers(NULL), upcasts_code(NULL), imclass_cppcasts_code(NULL), director_callback_typedefs(NULL), director_callbacks(NULL), director_delegate_callback(NULL), director_delegate_definitions(NULL), director_delegate_instances(NULL), director_method_types(NULL), director_connect_parms(NULL), destructor_call(NULL), output_file(NULL), dmethods_seq(NULL), dmethods_table(NULL), n_dmethods(0), n_directors(0), first_class_dmethod(0), curr_class_dmethod(0), nesting_depth(0){ /* for now, multiple inheritance in directors is disabled, this should be easy to implement though */ director_multiple_inheritance = 0; directorLanguage(); } /* ----------------------------------------------------------------------------- * ~CSHARP() * ----------------------------------------------------------------------------- */ ~CSHARP() { delete doxygenTranslator; } /* ----------------------------------------------------------------------------- * getProxyName() * * Test to see if a type corresponds to something wrapped with a proxy class. * Return NULL if not otherwise the proxy class name, fully qualified with * a namespace if the nspace feature is used. * ----------------------------------------------------------------------------- */ String *getProxyName(SwigType *t) { String *proxyname = NULL; if (proxy_flag) { Node *n = classLookup(t); if (n) { proxyname = Getattr(n, "proxyname"); if (!proxyname) { String *nspace = Getattr(n, "sym:nspace"); String *symname = Copy(Getattr(n, "sym:name")); if (symname && !GetFlag(n, "feature:flatnested")) { for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) { if (String* name = Getattr(outer_class, "sym:name")) { Push(symname, "."); Push(symname, name); } else return NULL; } } if (nspace) { if (namespce) proxyname = NewStringf("%s.%s.%s", namespce, nspace, symname); else proxyname = NewStringf("%s.%s", nspace, symname); } else { proxyname = Copy(symname); } Setattr(n, "proxyname", proxyname); Delete(proxyname); Delete(symname); } } } return proxyname; } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { SWIG_library_directory("csharp"); int doxygen_translator_flags = 0; // Look for certain command line options for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-dllimport") == 0) { if (argv[i + 1]) { dllimport = NewString(""); Printf(dllimport, argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-namespace") == 0) { if (argv[i + 1]) { namespce = NewString(""); Printf(namespce, argv[i + 1]); if (Len(namespce) == 0) { Delete(namespce); namespce = 0; } Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if ((strcmp(argv[i], "-noproxy") == 0)) { Swig_mark_arg(i); proxy_flag = false; } else if (strcmp(argv[i], "-oldvarnames") == 0) { Swig_mark_arg(i); old_variable_names = true; } else if (strcmp(argv[i], "-outfile") == 0) { if (argv[i + 1]) { output_file = NewString(""); Printf(output_file, argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-doxygen") == 0) { doxygen = true; scan_doxygen_comments = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-doxygen-translator") == 0) { doxygen_translator_flags |= DoxygenTranslator::debug_translator; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-doxygen-parser") == 0) { doxygen_translator_flags |= DoxygenTranslator::debug_parser; Swig_mark_arg(i); } else if (strcmp(argv[i], "-help") == 0) { Printf(stdout, "%s\n", usage); } } } if (doxygen) doxygenTranslator = new CSharpDocConverter(doxygen_translator_flags); // Add a symbol to the parser for conditional compilation Preprocessor_define("SWIGCSHARP 1", 0); SWIG_config_file("csharp.swg"); allow_overloading(); Swig_interface_feature_enable(); } /* --------------------------------------------------------------------- * top() * --------------------------------------------------------------------- */ virtual int top(Node *n) { // Get any options set in the module directive Node *module = Getattr(n, "module"); Node *optionsnode = Getattr(module, "options"); if (optionsnode) { if (Getattr(optionsnode, "imclassname")) imclass_name = Copy(Getattr(optionsnode, "imclassname")); /* check if directors are enabled for this module. note: this * is a "master" switch, without which no director code will be * emitted. %feature("director") statements are also required * to enable directors for individual classes or methods. * * use %module(directors="1") modulename at the start of the * interface file to enable director generation. */ if (Getattr(optionsnode, "directors")) { allow_directors(); } if (Getattr(optionsnode, "dirprot")) { allow_dirprot(); } allow_allprotected(GetFlag(optionsnode, "allprotected")); common_begin_code = Getattr(optionsnode, "csbegin"); if (common_begin_code) Printf(common_begin_code, "\n"); } /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = Getattr(n, "outfile_h"); if (!outfile) { Printf(stderr, "Unable to determine outfile\n"); Exit(EXIT_FAILURE); } f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } if (Swig_directors_enabled()) { if (!outfile_h) { Printf(stderr, "Unable to determine outfile_h\n"); Exit(EXIT_FAILURE); } f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); swig_types_hash = NewHash(); filenames_list = NewList(); // Make the intermediary class and module class names. The intermediary class name can be set in the module directive. if (!imclass_name) { imclass_name = NewStringf("%sPINVOKE", Getattr(n, "name")); module_class_name = Copy(Getattr(n, "name")); } else { // Rename the module name if it is the same as intermediary class name - a backwards compatibility solution if (Cmp(imclass_name, Getattr(n, "name")) == 0) module_class_name = NewStringf("%sModule", Getattr(n, "name")); else module_class_name = Copy(Getattr(n, "name")); } // module class and intermediary classes are always created if (!addSymbol(imclass_name, module)) return SWIG_ERROR; if (!addSymbol(module_class_name, module)) return SWIG_ERROR; imclass_class_code = NewString(""); proxy_class_def = NewString(""); proxy_class_code = NewString(""); module_class_constants_code = NewString(""); imclass_baseclass = NewString(""); imclass_interfaces = NewString(""); imclass_class_modifiers = NewString(""); module_class_code = NewString(""); module_baseclass = NewString(""); module_interfaces = NewString(""); module_imports = NewString(""); module_class_modifiers = NewString(""); imclass_imports = NewString(""); imclass_cppcasts_code = NewString(""); director_connect_parms = NewString(""); upcasts_code = NewString(""); dmethods_seq = NewList(); dmethods_table = NewHash(); n_dmethods = 0; n_directors = 0; if (!dllimport) dllimport = Copy(module_class_name); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "CSHARP"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); /* Emit initial director header and director code: */ Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module_class_name); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module_class_name); Printf(f_directors, "\n\n"); Printf(f_directors, "/* ---------------------------------------------------\n"); Printf(f_directors, " * C++ director class methods\n"); Printf(f_directors, " * --------------------------------------------------- */\n\n"); if (outfile_h) { String *filename = Swig_file_filename(outfile_h); Printf(f_directors, "#include \"%s\"\n\n", filename); Delete(filename); } } Printf(f_runtime, "\n"); if (namespce) { String *wrapper_name = NewStringf(""); Printf(wrapper_name, "CSharp_%s_%%f", namespce); Swig_name_register("wrapper", wrapper_name); Delete(wrapper_name); } else { Swig_name_register("wrapper", "CSharp_%f"); } if (old_variable_names) { Swig_name_register("set", "set_%n%v"); Swig_name_register("get", "get_%n%v"); } Printf(f_wrappers, "\n#ifdef __cplusplus\n"); Printf(f_wrappers, "extern \"C\" {\n"); Printf(f_wrappers, "#endif\n\n"); /* Emit code */ Language::top(n); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (make it occur before %header section) Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } // Generate the intermediary class { File *f_im = getOutputFile(SWIG_output_directory(), imclass_name); addOpenNamespace(0, f_im); if (imclass_imports) Printf(f_im, "%s\n", imclass_imports); if (Len(imclass_class_modifiers) > 0) Printf(f_im, "%s ", imclass_class_modifiers); Printf(f_im, "%s ", imclass_name); if (imclass_baseclass && *Char(imclass_baseclass)) Printf(f_im, ": %s ", imclass_baseclass); if (Len(imclass_interfaces) > 0) Printv(f_im, "implements ", imclass_interfaces, " ", NIL); Printf(f_im, "{\n"); // Add the intermediary class methods Replaceall(imclass_class_code, "$module", module_class_name); Replaceall(imclass_class_code, "$imclassname", imclass_name); Replaceall(imclass_class_code, "$dllimport", dllimport); Printv(f_im, imclass_class_code, NIL); Printv(f_im, imclass_cppcasts_code, NIL); // Finish off the class Printf(f_im, "}\n"); addCloseNamespace(0, f_im); if (f_im != f_single_out) Delete(f_im); f_im = NULL; } // Generate the C# module class { File *f_module = getOutputFile(SWIG_output_directory(), module_class_name); addOpenNamespace(0, f_module); if (module_imports) Printf(f_module, "%s\n", module_imports); if (Len(module_class_modifiers) > 0) Printf(f_module, "%s ", module_class_modifiers); Printf(f_module, "%s ", module_class_name); if (module_baseclass && *Char(module_baseclass)) Printf(f_module, ": %s ", module_baseclass); if (Len(module_interfaces) > 0) Printv(f_module, "implements ", module_interfaces, " ", NIL); Printf(f_module, "{\n"); Replaceall(module_class_code, "$module", module_class_name); Replaceall(module_class_constants_code, "$module", module_class_name); Replaceall(module_class_code, "$imclassname", imclass_name); Replaceall(module_class_constants_code, "$imclassname", imclass_name); Replaceall(module_class_code, "$dllimport", dllimport); Replaceall(module_class_constants_code, "$dllimport", dllimport); // Add the wrapper methods Printv(f_module, module_class_code, NIL); // Write out all the global constants Printv(f_module, module_class_constants_code, NIL); // Finish off the class Printf(f_module, "}\n"); addCloseNamespace(0, f_module); if (f_module != f_single_out) Delete(f_module); f_module = NULL; } if (upcasts_code) Printv(f_wrappers, upcasts_code, NIL); Printf(f_wrappers, "#ifdef __cplusplus\n"); Printf(f_wrappers, "}\n"); Printf(f_wrappers, "#endif\n"); // Output a C# type wrapper class for each SWIG type for (Iterator swig_type = First(swig_types_hash); swig_type.key; swig_type = Next(swig_type)) { emitTypeWrapperClass(swig_type.key, swig_type.item); } // Check for overwriting file problems on filesystems that are case insensitive Iterator it1; Iterator it2; for (it1 = First(filenames_list); it1.item; it1 = Next(it1)) { String *item1_lower = Swig_string_lower(it1.item); for (it2 = Next(it1); it2.item; it2 = Next(it2)) { String *item2_lower = Swig_string_lower(it2.item); if (it1.item && it2.item) { if (Strcmp(item1_lower, item2_lower) == 0) { Swig_warning(WARN_LANG_PORTABILITY_FILENAME, input_file, line_number, "Portability warning: File %s will be overwritten by %s on case insensitive filesystems such as " "Windows' FAT32 and NTFS unless the class/module name is renamed\n", it1.item, it2.item); } } Delete(item2_lower); } Delete(item1_lower); } Delete(swig_types_hash); swig_types_hash = NULL; Delete(filenames_list); filenames_list = NULL; Delete(imclass_name); imclass_name = NULL; Delete(imclass_class_code); imclass_class_code = NULL; Delete(proxy_class_def); proxy_class_def = NULL; Delete(proxy_class_code); proxy_class_code = NULL; Delete(module_class_constants_code); module_class_constants_code = NULL; Delete(imclass_baseclass); imclass_baseclass = NULL; Delete(imclass_interfaces); imclass_interfaces = NULL; Delete(imclass_class_modifiers); imclass_class_modifiers = NULL; Delete(module_class_name); module_class_name = NULL; Delete(module_class_code); module_class_code = NULL; Delete(module_baseclass); module_baseclass = NULL; Delete(module_interfaces); module_interfaces = NULL; Delete(module_imports); module_imports = NULL; Delete(module_class_modifiers); module_class_modifiers = NULL; Delete(imclass_imports); imclass_imports = NULL; Delete(imclass_cppcasts_code); imclass_cppcasts_code = NULL; Delete(upcasts_code); upcasts_code = NULL; Delete(dmethods_seq); dmethods_seq = NULL; Delete(dmethods_table); dmethods_table = NULL; Delete(namespce); namespce = NULL; n_dmethods = 0; /* Close all of the files */ Dump(f_runtime, f_begin); Dump(f_header, f_begin); if (Swig_directors_enabled()) { Dump(f_directors, f_begin); Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); Delete(f_runtime_h); f_runtime_h = NULL; Delete(f_directors); f_directors = NULL; Delete(f_directors_h); f_directors_h = NULL; } if (f_single_out) { Dump(f_single_out, f_begin); Delete(f_single_out); f_single_out = NULL; } Dump(f_wrappers, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* ----------------------------------------------------------------------------- * emitBanner() * ----------------------------------------------------------------------------- */ void emitBanner(File *f) { Printf(f, "//------------------------------------------------------------------------------\n"); Printf(f, "// \n"); Printf(f, "//\n"); Swig_banner_target_lang(f, "//"); Printf(f, "//------------------------------------------------------------------------------\n\n"); Printv(f, common_begin_code, NIL); } /* ----------------------------------------------------------------------------- * getOutputFile() * * Prepares a File object by creating the file in the file system and * writing the banner for auto-generated files to it (emitBanner). * If '-outfile' is provided (single file mode) the supplied parameters will * be ignored and the returned file will always be: * / * Otherwise the file will be: * /.cs * ----------------------------------------------------------------------------- */ File *getOutputFile(const String *dir, const String *name) { if (output_file) { if (!f_single_out) { String *filen = NewStringf("%s%s", SWIG_output_directory(), output_file); f_single_out = NewFile(filen, "w", SWIG_output_files()); if (!f_single_out) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; emitBanner(f_single_out); } return f_single_out; } else { String *filen = NewStringf("%s%s.cs", dir, name); File *f = NewFile(filen, "w", SWIG_output_files()); if (!f) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Append(filenames_list, Copy(filen)); Delete(filen); filen = NULL; emitBanner(f); return f; } } /*----------------------------------------------------------------------- * Add new director upcall signature *----------------------------------------------------------------------*/ UpcallData *addUpcallMethod(String *imclass_method, String *class_method, String *decl, String *overloaded_name) { String *key = NewStringf("%s|%s", imclass_method, decl); ++curr_class_dmethod; String *class_methodidx = NewStringf("%d", n_dmethods - first_class_dmethod); n_dmethods++; Hash *new_udata = NewHash(); Append(dmethods_seq, new_udata); Setattr(dmethods_table, key, new_udata); Setattr(new_udata, "method", Copy(class_method)); Setattr(new_udata, "class_methodidx", class_methodidx); Setattr(new_udata, "decl", Copy(decl)); Setattr(new_udata, "overname", Copy(overloaded_name)); Delete(key); return new_udata; } /*----------------------------------------------------------------------- * Get director upcall signature *----------------------------------------------------------------------*/ /* UpcallData * getUpcallMethodData(String *director_class, String *decl) { String *key = NewStringf("%s|%s", director_class, decl); UpcallData *udata = Getattr(dmethods_table, key); Delete(key); return udata; } */ /* ---------------------------------------------------------------------- * nativeWrapper() * ---------------------------------------------------------------------- */ virtual int nativeWrapper(Node *n) { String *wrapname = Getattr(n, "wrap:name"); if (!addSymbol(wrapname, n, imclass_name)) return SWIG_ERROR; if (Getattr(n, "type")) { Swig_save("nativeWrapper", n, "name", NIL); Setattr(n, "name", wrapname); native_function_flag = true; functionWrapper(n); Swig_restore(n); native_function_flag = false; } else { Swig_error(input_file, line_number, "No return type for %%native method %s.\n", Getattr(n, "wrap:name")); } return SWIG_OK; } /* ---------------------------------------------------------------------- * functionWrapper() * ---------------------------------------------------------------------- */ virtual int functionWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *c_return_type = NewString(""); String *im_return_type = NewString(""); String *cleanup = NewString(""); String *outarg = NewString(""); String *body = NewString(""); String *im_outattributes = 0; int num_arguments = 0; bool is_void_return; String *overloaded_name = getOverloadedName(n); if (!Getattr(n, "sym:overloaded")) { if (!addSymbol(symname, n, imclass_name)) return SWIG_ERROR; } /* The rest of this function deals with generating the intermediary class wrapper function (that wraps a c/c++ function) and generating the PInvoke c code. Each C# wrapper function has a matching PInvoke c function call. */ // A new wrapper function object Wrapper *f = NewWrapper(); // Make a wrapper name for this function String *wname = Swig_name_wrapper(overloaded_name); /* Attach the non-standard typemaps to the parameter list. */ Swig_typemap_attach_parms("ctype", l, f); Swig_typemap_attach_parms("imtype", l, f); /* Get return types */ if ((tm = Swig_typemap_lookup("ctype", n, "", 0))) { String *ctypeout = Getattr(n, "tmap:ctype:out"); // the type in the ctype typemap's out attribute overrides the type in the typemap if (ctypeout) tm = ctypeout; Printf(c_return_type, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(returntype, 0)); } if ((tm = Swig_typemap_lookup("imtype", n, "", 0))) { String *imtypeout = Getattr(n, "tmap:imtype:out"); // the type in the imtype typemap's out attribute overrides the type in the typemap if (imtypeout) tm = imtypeout; Printf(im_return_type, "%s", tm); im_outattributes = Getattr(n, "tmap:imtype:outattributes"); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s\n", SwigType_str(returntype, 0)); } is_void_return = Cmp(c_return_type, "void") == 0; if (!is_void_return) Wrapper_add_localv(f, "jresult", c_return_type, "jresult", NIL); Printv(f->def, " SWIGEXPORT ", c_return_type, " SWIGSTDCALL ", wname, "(", NIL); // Emit all of the local variables for holding arguments. emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); // Parameter overloading Setattr(n, "wrap:parms", l); Setattr(n, "wrap:name", wname); // Wrappers not wanted for some methods where the parameters cannot be overloaded in C# if (Getattr(n, "sym:overloaded")) { // Emit warnings for the few cases that can't be overloaded in C# and give up on generating wrapper Swig_overload_check(n); if (Getattr(n, "overload:ignore")) { DelWrapper(f); return SWIG_OK; } } Printv(imclass_class_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL); if (im_outattributes) Printf(imclass_class_code, " %s\n", im_outattributes); Printf(imclass_class_code, " public static extern %s %s(", im_return_type, overloaded_name); /* Get number of required and total arguments */ num_arguments = emit_num_arguments(l); int gencomma = 0; // Now walk the function parameter list and generate code to get arguments for (i = 0, p = l; i < num_arguments; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); String *im_param_type = NewString(""); String *c_param_type = NewString(""); String *arg = NewString(""); Printf(arg, "j%s", ln); /* Get the ctype types of the parameter */ if ((tm = Getattr(p, "tmap:ctype"))) { Printv(c_param_type, tm, NIL); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(pt, 0)); } /* Get the intermediary class parameter types of the parameter */ if ((tm = Getattr(p, "tmap:imtype"))) { const String *inattributes = Getattr(p, "tmap:imtype:inattributes"); Printf(im_param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to intermediary class method */ if (gencomma) Printf(imclass_class_code, ", "); Printf(imclass_class_code, "%s %s", im_param_type, arg); // Add parameter to C function Printv(f->def, gencomma ? ", " : "", c_param_type, " ", arg, NIL); gencomma = 1; // Get typemap for this argument if ((tm = Getattr(p, "tmap:in"))) { canThrow(n, "in", p); Replaceall(tm, "$arg", arg); /* deprecated? */ Replaceall(tm, "$input", arg); Setattr(p, "emit:input", arg); Printf(f->code, "%s\n", tm); p = Getattr(p, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); p = nextSibling(p); } Delete(im_param_type); Delete(c_param_type); Delete(arg); } /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { canThrow(n, "check", p); Replaceall(tm, "$arg", Getattr(p, "emit:input")); /* deprecated? */ Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { canThrow(n, "freearg", p); Replaceall(tm, "$arg", Getattr(p, "emit:input")); /* deprecated? */ Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { canThrow(n, "argout", p); Replaceall(tm, "$arg", Getattr(p, "emit:input")); /* deprecated? */ Replaceall(tm, "$result", "jresult"); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } // Look for usage of throws typemap and the canthrow flag ParmList *throw_parm_list = NULL; if ((throw_parm_list = Getattr(n, "catchlist"))) { Swig_typemap_attach_parms("throws", throw_parm_list, f); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { canThrow(n, "throws", p); } } } String *null_attribute = 0; // Now write code to make the function call if (!native_function_flag) { Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { canThrow(n, "out", n); Replaceall(tm, "$result", "jresult"); if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "1"); else Replaceall(tm, "$owner", "0"); Printf(f->code, "%s", tm); null_attribute = Getattr(n, "tmap:out:null"); if (Len(tm)) Printf(f->code, "\n"); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), Getattr(n, "name")); } emit_return_variable(n, returntype, f); } /* Output argument output code */ Printv(f->code, outarg, NIL); /* Output cleanup code */ Printv(f->code, cleanup, NIL); /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { canThrow(n, "newfree", n); Printf(f->code, "%s\n", tm); } } /* See if there is any return cleanup code */ if (!native_function_flag) { if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { canThrow(n, "ret", n); Printf(f->code, "%s\n", tm); } } /* Finish C function and intermediary class function definitions */ Printf(imclass_class_code, ")"); Printf(imclass_class_code, ";\n"); Printf(f->def, ") {"); if (!is_void_return) Printv(f->code, " return jresult;\n", NIL); Printf(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); Replaceall(f->code, "$isvoid", is_void_return ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", symname); /* Contract macro modification */ if (Replaceall(f->code, "SWIG_contract_assert(", "SWIG_contract_assert($null, ") > 0) { Setattr(n, "csharp:canthrow", "1"); } if (!null_attribute) Replaceall(f->code, "$null", "0"); else Replaceall(f->code, "$null", null_attribute); /* Dump the function out */ if (!native_function_flag) { Wrapper_print(f, f_wrappers); // Handle %csexception which sets the canthrow attribute if (Getattr(n, "feature:except:canthrow")) Setattr(n, "csharp:canthrow", "1"); // A very simple check (it is not foolproof) to help typemap/feature writers for // throwing C# exceptions from unmanaged code. It checks for the common methods which // set a pending C# exception... the 'canthrow' typemap/feature attribute must be set // so that code which checks for pending exceptions is added in the C# proxy method. if (!Getattr(n, "csharp:canthrow")) { if (Strstr(f->code, "SWIG_exception")) { Swig_warning(WARN_CSHARP_CANTHROW, input_file, line_number, "Unmanaged code contains a call to SWIG_exception and C# code does not handle pending exceptions via the canthrow attribute.\n"); } else if (Strstr(f->code, "SWIG_CSharpSetPendingException")) { Swig_warning(WARN_CSHARP_CANTHROW, input_file, line_number, "Unmanaged code contains a call to a SWIG_CSharpSetPendingException method and C# code does not handle pending exceptions via the canthrow attribute.\n"); } } } if (!(proxy_flag && is_wrapping_class()) && !enum_constant_flag) { moduleClassFunctionHandler(n); } /* * Generate the proxy class properties for public member variables. * Not for enums and constants. */ if (proxy_flag && wrapping_member_flag && !enum_constant_flag) { // Capitalize the first letter in the variable in the getter/setter function name bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) != 0; String *getter_setter_name = NewString(""); if (!getter_flag) Printf(getter_setter_name, "set"); else Printf(getter_setter_name, "get"); Putc(toupper((int) *Char(variable_name)), getter_setter_name); Printf(getter_setter_name, "%s", Char(variable_name) + 1); Setattr(n, "proxyfuncname", getter_setter_name); Setattr(n, "imfuncname", symname); proxyClassFunctionHandler(n); Delete(getter_setter_name); } Delete(c_return_type); Delete(im_return_type); Delete(cleanup); Delete(outarg); Delete(body); Delete(overloaded_name); DelWrapper(f); return SWIG_OK; } /* ----------------------------------------------------------------------- * variableWrapper() * ----------------------------------------------------------------------- */ virtual int variableWrapper(Node *n) { Language::variableWrapper(n); return SWIG_OK; } /* ----------------------------------------------------------------------- * globalvariableHandler() * ------------------------------------------------------------------------ */ virtual int globalvariableHandler(Node *n) { generate_property_declaration_flag = true; variable_name = Getattr(n, "sym:name"); global_variable_flag = true; int ret = Language::globalvariableHandler(n); global_variable_flag = false; generate_property_declaration_flag = false; if (proxy_flag) { Printf(module_class_code, "\n }\n\n"); } return ret; } String *getCurrentScopeName(String *nspace) { String *scope = 0; if (nspace || getCurrentClass()) { scope = NewString(""); if (nspace) Printf(scope, "%s", nspace); if (Node *cls = getCurrentClass()) { if (Node *outer = Getattr(cls, "nested:outer")) { String *outerClassesPrefix = Copy(Getattr(outer, "sym:name")); for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) { Push(outerClassesPrefix, "."); Push(outerClassesPrefix, Getattr(outer, "sym:name")); } Printv(scope, nspace ? "." : "", outerClassesPrefix, ".", proxy_class_name, NIL); Delete(outerClassesPrefix); } else Printv(scope, nspace ? "." : "", proxy_class_name, NIL); } } return scope; } /* ---------------------------------------------------------------------- * enumDeclaration() * * C/C++ enums can be mapped in one of 4 ways, depending on the cs:enum feature specified: * 1) Simple enums - simple constant within the proxy class or module class * 2) Typeunsafe enums - simple constant in a C# class (class named after the c++ enum name) * 3) Typesafe enum - typesafe enum pattern (class named after the c++ enum name) * 4) Proper enums - proper C# enum * Anonymous enums always default to 1) * ---------------------------------------------------------------------- */ virtual int enumDeclaration(Node *n) { if (!ImportMode) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; String *nspace = Getattr(n, "sym:nspace"); // NSpace/getNSpace() only works during Language::enumDeclaration call enum_code = NewString(""); String *symname = Getattr(n, "sym:name"); String *constants_code = (proxy_flag && is_wrapping_class())? proxy_class_constants_code : module_class_constants_code; EnumFeature enum_feature = decodeEnumFeature(n); String *typemap_lookup_type = Getattr(n, "name"); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab0); Printv(enum_code, ds, NIL); Delete(ds); } if ((enum_feature != SimpleEnum) && symname && typemap_lookup_type) { // Wrap (non-anonymous) C/C++ enum within a typesafe, typeunsafe or proper C# enum String *scope = getCurrentScopeName(nspace); if (!addSymbol(symname, n, scope)) return SWIG_ERROR; // Enum base (underlying enum type) Node *attributes = NewHash(); const String *pure_baseclass = typemapLookup(n, "csbase", typemap_lookup_type, WARN_NONE, attributes); bool purebase_replace = GetFlag(attributes, "tmap:csbase:replace") ? true : false; Delete(attributes); String *baseclass = NULL; if (!purebase_replace) { String *underlying_enum_type = Getattr(n, "enumbase"); if (underlying_enum_type) { const String *typemap_baseclass = typemapLookup(n, "cstype", underlying_enum_type, WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF); baseclass = Copy(typemap_baseclass); substituteClassname(underlying_enum_type, baseclass); } } const String *wanted_base = baseclass ? baseclass : pure_baseclass; if (purebase_replace) { wanted_base = pure_baseclass; } else if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) { Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, enum base %s ignored. Multiple enum bases is not supported in C# enums. " "Perhaps you need the 'replace' attribute in the csbase typemap?\n", typemap_lookup_type, pure_baseclass); } // Class attributes const String *csattributes = typemapLookup(n, "csattributes", typemap_lookup_type, WARN_NONE); if (csattributes && *Char(csattributes)) Printf(enum_code, "%s\n", csattributes); // Emit the enum Printv(enum_code, typemapLookup(n, "csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers (enum modifiers really) " ", symname, *Char(wanted_base) ? " : " : "", wanted_base, " {\n", NIL); Delete(scope); } else { // Wrap C++ enum with integers - just indicate start of enum with a comment, no comment for anonymous enums of any sort if (symname && !Getattr(n, "unnamedinstance")) Printf(constants_code, " // %s \n", symname); } if (proxy_flag && !is_wrapping_class()) { // Global enums / enums in a namespace assert(!full_imclass_name); if (!nspace) { full_imclass_name = NewStringf("%s", imclass_name); } else { if (namespce) { full_imclass_name = NewStringf("%s.%s", namespce, imclass_name); } else { full_imclass_name = NewStringf("%s", imclass_name); } } } // Emit each enum item Language::enumDeclaration(n); if (proxy_flag && !is_wrapping_class()) { Delete(full_imclass_name); full_imclass_name = 0; } if ((enum_feature != SimpleEnum) && symname && typemap_lookup_type) { // Wrap (non-anonymous) C/C++ enum within a typesafe, typeunsafe or proper C# enum // Finish the enum declaration // Typemaps are used to generate the enum definition in a similar manner to proxy classes. Printv(enum_code, (enum_feature == ProperEnum) ? "\n" : typemapLookup(n, "csbody", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class typemapLookup(n, "cscode", typemap_lookup_type, WARN_NONE), // extra C# code "}", NIL); Replaceall(enum_code, "$csclassname", symname); // Substitute $enumvalues - intended usage is for typesafe enums if (Getattr(n, "enumvalues")) Replaceall(enum_code, "$enumvalues", Getattr(n, "enumvalues")); else Replaceall(enum_code, "$enumvalues", ""); if (proxy_flag && is_wrapping_class()) { // Enums defined within the C++ class are defined within the proxy class // Add extra indentation Replaceall(enum_code, "\n", "\n "); Replaceall(enum_code, " \n", "\n"); Printv(proxy_class_constants_code, " ", enum_code, "\n\n", NIL); } else { // Global enums are defined in their own file String *output_directory = outputDirectory(nspace); File *f_enum = getOutputFile(output_directory, symname); addOpenNamespace(nspace, f_enum); Printv(f_enum, typemapLookup(n, "csimports", typemap_lookup_type, WARN_NONE), // Import statements "\n", enum_code, "\n", NIL); addCloseNamespace(nspace, f_enum); if (f_enum != f_single_out) Delete(f_enum); f_enum = NULL; Delete(output_directory); } } else { // Wrap C++ enum with simple constant Printf(enum_code, "\n"); if (proxy_flag && is_wrapping_class()) Printv(proxy_class_constants_code, enum_code, NIL); else Printv(module_class_constants_code, enum_code, NIL); } Delete(enum_code); enum_code = NULL; } return SWIG_OK; } /* ---------------------------------------------------------------------- * enumvalueDeclaration() * ---------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; Swig_require("enumvalueDeclaration", n, "*name", "?value", NIL); String *symname = Getattr(n, "sym:name"); String *value = Getattr(n, "value"); String *name = Getattr(n, "name"); Node *parent = parentNode(n); int unnamedinstance = GetFlag(parent, "unnamedinstance"); String *parent_name = Getattr(parent, "name"); String *nspace = getNSpace(); String *newsymname = 0; String *tmpValue; // Strange hack from parent method if (value) tmpValue = NewString(value); else tmpValue = NewString(name); // Note that this is used in enumValue() amongst other places Setattr(n, "value", tmpValue); // Deal with enum values that are not int int swigtype = SwigType_type(Getattr(n, "type")); if (swigtype == T_CHAR) { String *enumstringval = Getattr(n, "enumstringval"); if (enumstringval) { // Escape character literal for C#. String *val = NewStringf("'%(csharpescape)s'", enumstringval); Setattr(n, "enumvalue", val); Delete(val); } } else { String *numval = Getattr(n, "enumnumval"); if (numval) Setattr(n, "enumvalue", numval); } { EnumFeature enum_feature = decodeEnumFeature(parent); if ((enum_feature == SimpleEnum) && GetFlag(parent, "scopedenum")) { newsymname = Swig_name_member(0, Getattr(parent, "sym:name"), symname); symname = newsymname; } // Add to language symbol table String *scope = 0; if (unnamedinstance || !parent_name || enum_feature == SimpleEnum) { String *enumClassPrefix = getEnumClassPrefix(); if (enumClassPrefix) { scope = NewString(""); if (nspace) Printf(scope, "%s.", nspace); Printf(scope, "%s", enumClassPrefix); } else { scope = Copy(module_class_name); } } else { scope = getCurrentScopeName(nspace); if (!scope) scope = Copy(Getattr(parent, "sym:name")); else Printf(scope, ".%s", Getattr(parent, "sym:name")); } if (!addSymbol(symname, n, scope)) return SWIG_ERROR; const String *csattributes = Getattr(n, "feature:cs:attributes"); if ((enum_feature == ProperEnum) && parent_name && !unnamedinstance) { // Wrap (non-anonymous) C/C++ enum with a proper C# enum // Emit the enum item. if (!GetFlag(n, "firstenumitem")) Printf(enum_code, ",\n"); if (csattributes) Printf(enum_code, " %s\n", csattributes); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab2); Printv(enum_code, ds, NIL); Delete(ds); } Printf(enum_code, " %s", symname); // Check for the %csconstvalue feature String *value = Getattr(n, "feature:cs:constvalue"); // Note that the enum value must be a true constant and cannot be set from a PINVOKE call, thus no support for %csconst(0) value = value ? value : Getattr(n, "enumvalue"); if (value) { Printf(enum_code, " = %s", value); } } else { // Wrap C/C++ enums with constant integers or use the typesafe enum pattern SwigType *typemap_lookup_type = parent_name ? parent_name : NewString("enum "); Setattr(n, "type", typemap_lookup_type); const String *tm = typemapLookup(n, "cstype", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF); String *return_type = Copy(tm); substituteClassname(typemap_lookup_type, return_type); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); if (csattributes) Printf(enum_code, " %s\n", csattributes); if ((enum_feature == TypesafeEnum) && parent_name && !unnamedinstance) { // Wrap (non-anonymous) enum using the typesafe enum pattern if (Getattr(n, "enumvalue")) { String *value = enumValue(n); Printf(enum_code, " %s static readonly %s %s = new %s(\"%s\", %s);\n", methodmods, return_type, symname, return_type, symname, value); Delete(value); } else { Printf(enum_code, " %s static readonly %s %s = new %s(\"%s\");\n", methodmods, return_type, symname, return_type, symname); } } else { // Simple integer constants // Note these are always generated for anonymous enums, no matter what enum_feature is specified // Code generated is the same for SimpleEnum and TypeunsafeEnum -> the class it is generated into is determined later // The %csconst feature determines how the constant value is obtained int const_feature_flag = GetFlag(n, "feature:cs:const"); const char *const_readonly = const_feature_flag ? "const" : "static readonly"; String *value = enumValue(n); Printf(enum_code, " %s %s %s %s = %s;\n", methodmods, const_readonly, return_type, symname, value); Delete(value); } Delete(return_type); } // Add the enum value to the comma separated list being constructed in the enum declaration. String *enumvalues = Getattr(parent, "enumvalues"); if (!enumvalues) Setattr(parent, "enumvalues", Copy(symname)); else Printv(enumvalues, ", ", symname, NIL); Delete(scope); } Delete(newsymname); Delete(tmpValue); Swig_restore(n); return SWIG_OK; } /* ----------------------------------------------------------------------- * constantWrapper() * Used for wrapping constants - #define or %constant. * Also for inline initialised const static primitive type member variables (short, int, double, enums etc). * C# static const variables are generated for these. * If the %csconst(1) feature is used then the C constant value is used to initialise the C# const variable. * If not, a PINVOKE method is generated to get the C constant value for initialisation of the C# const variable. * However, if the %csconstvalue feature is used, it overrides all other ways to generate the initialisation. * Also note that this method might be called for wrapping enum items (when the enum is using %csconst(0)). * ------------------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; String *return_type = NewString(""); String *constants_code = NewString(""); // The value as C# code. String *csvalue = Copy(Getattr(n, "value")); Swig_save("constantWrapper", n, "tmap:ctype:out", "tmap:imtype:out", "tmap:cstype:out", "tmap:out:null", "tmap:imtype:outattributes", "tmap:cstype:outattributes", NIL); bool is_enum_item = (Cmp(nodeType(n), "enumitem") == 0); const String *itemname = (proxy_flag && wrapping_member_flag) ? variable_name : symname; if (!is_enum_item) { String *scope = 0; if (proxy_class_name) { String *nspace = getNSpace(); scope = NewString(""); if (nspace) Printf(scope, "%s.", nspace); Printf(scope, "%s", proxy_class_name); } else { scope = Copy(module_class_name); } if (!addSymbol(itemname, n, scope)) return SWIG_ERROR; Delete(scope); } // The %csconst feature determines how the constant value is obtained int const_feature_flag = GetFlag(n, "feature:cs:const"); /* Adjust the enum type for the Swig_typemap_lookup. * We want the same jstype typemap for all the enum items so we use the enum type (parent node). */ if (is_enum_item) { t = Getattr(parentNode(n), "enumtype"); Setattr(n, "type", t); } /* Attach the non-standard typemaps to the parameter list. */ Swig_typemap_attach_parms("cstype", l, NULL); /* Get C# return types */ bool classname_substituted_flag = false; if ((tm = Swig_typemap_lookup("cstype", n, "", 0))) { String *cstypeout = Getattr(n, "tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap if (cstypeout) tm = cstypeout; classname_substituted_flag = substituteClassname(t, tm); Printf(return_type, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(t, 0)); } if (Getattr(n, "stringval")) { char quote = 0; switch (SwigType_type(t)) { case T_STRING: case T_WSTRING: quote = '\"'; break; case T_CHAR: case T_WCHAR: quote = '\''; break; } if (quote) { // Escape character literal for C#. Delete(csvalue); csvalue = NewStringf("%c%(csharpescape)s%c", quote, Getattr(n, "stringval"), quote); } } const String *outattributes = Getattr(n, "tmap:cstype:outattributes"); if (outattributes) Printf(constants_code, " %s\n", outattributes); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab2); Printv(constants_code, ds, NIL); Delete(ds); } Printf(constants_code, " %s %s %s %s = ", methodmods, (const_feature_flag ? "const" : "static readonly"), return_type, itemname); // Check for the %csconstvalue feature String *value = Getattr(n, "feature:cs:constvalue"); if (value) { Printf(constants_code, "%s;\n", value); } else if (!const_feature_flag) { // Default enum and constant handling will work with any type of C constant and initialises the C# variable from C through a PINVOKE call. if (classname_substituted_flag) { if (SwigType_isenum(t)) { // This handles wrapping of inline initialised const enum static member variables (not when wrapping enum items - ignored later on) Printf(constants_code, "(%s)%s.%s();\n", return_type, full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } else { // This handles function pointers using the %constant directive Printf(constants_code, "new %s(%s.%s(), false);\n", return_type, full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } } else { Printf(constants_code, "%s.%s();\n", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } // Each constant and enum value is wrapped with a separate PInvoke function call SetFlag(n, "feature:immutable"); enum_constant_flag = true; variableWrapper(n); enum_constant_flag = false; } else { // Alternative constant handling will use the C syntax to make a true C# constant and hope that it compiles as C# code if (Getattr(n, "wrappedasconstant")) { if (SwigType_type(t) == T_CHAR) { String *stringval = Getattr(n, "stringval"); if (stringval) Printf(constants_code, "'%(csharpescape)s';\n", stringval); else Printf(constants_code, "(char)%s;\n", Getattr(n, "staticmembervariableHandler:value")); } else { Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); } } else { Printf(constants_code, "%s;\n", csvalue); } } // Emit the generated code to appropriate place // Enums only emit the intermediate and PINVOKE methods, so no proxy or module class wrapper methods needed if (!is_enum_item) { if (proxy_flag && wrapping_member_flag) Printv(proxy_class_constants_code, constants_code, NIL); else Printv(module_class_constants_code, constants_code, NIL); } // Cleanup Swig_restore(n); Delete(return_type); Delete(constants_code); Delete(csvalue); return SWIG_OK; } /* ----------------------------------------------------------------------------- * insertDirective() * ----------------------------------------------------------------------------- */ virtual int insertDirective(Node *n) { int ret = SWIG_OK; String *code = Getattr(n, "code"); String *section = Getattr(n, "section"); Replaceall(code, "$module", module_class_name); Replaceall(code, "$imclassname", imclass_name); Replaceall(code, "$dllimport", dllimport); if (!ImportMode && (Cmp(section, "proxycode") == 0)) { if (proxy_class_code) { Swig_typemap_replace_embedded_typemap(code, n); int offset = Len(code) > 0 && *Char(code) == '\n' ? 1 : 0; Printv(proxy_class_code, Char(code) + offset, "\n", NIL); } } else { ret = Language::insertDirective(n); } return ret; } /* ----------------------------------------------------------------------------- * pragmaDirective() * * Valid Pragmas: * imclassbase - base (extends) for the intermediary class * imclassclassmodifiers - class modifiers for the intermediary class * imclasscode - text (C# code) is copied verbatim to the intermediary class * imclassimports - import statements for the intermediary class * imclassinterfaces - interface (implements) for the intermediary class * * modulebase - base (extends) for the module class * moduleclassmodifiers - class modifiers for the module class * modulecode - text (C# code) is copied verbatim to the module class * moduleimports - import statements for the module class * moduleinterfaces - interface (implements) for the module class * * ----------------------------------------------------------------------------- */ virtual int pragmaDirective(Node *n) { if (!ImportMode) { String *lang = Getattr(n, "lang"); String *code = Getattr(n, "name"); String *value = Getattr(n, "value"); if (Strcmp(lang, "csharp") == 0) { String *strvalue = NewString(value); Replaceall(strvalue, "\\\"", "\""); if (Strcmp(code, "imclassbase") == 0) { Delete(imclass_baseclass); imclass_baseclass = Copy(strvalue); } else if (Strcmp(code, "imclassclassmodifiers") == 0) { Delete(imclass_class_modifiers); imclass_class_modifiers = Copy(strvalue); } else if (Strcmp(code, "imclasscode") == 0) { Printf(imclass_class_code, "%s\n", strvalue); } else if (Strcmp(code, "imclassimports") == 0) { Delete(imclass_imports); imclass_imports = Copy(strvalue); } else if (Strcmp(code, "imclassinterfaces") == 0) { Delete(imclass_interfaces); imclass_interfaces = Copy(strvalue); } else if (Strcmp(code, "modulebase") == 0) { Delete(module_baseclass); module_baseclass = Copy(strvalue); } else if (Strcmp(code, "moduleclassmodifiers") == 0) { Delete(module_class_modifiers); module_class_modifiers = Copy(strvalue); } else if (Strcmp(code, "modulecode") == 0) { Printf(module_class_code, "%s\n", strvalue); } else if (Strcmp(code, "moduleimports") == 0) { Delete(module_imports); module_imports = Copy(strvalue); } else if (Strcmp(code, "moduleinterfaces") == 0) { Delete(module_interfaces); module_interfaces = Copy(strvalue); } else { Swig_error(input_file, line_number, "Unrecognized pragma.\n"); } Delete(strvalue); } } return Language::pragmaDirective(n); } /* ----------------------------------------------------------------------------- * getQualifiedInterfaceName() * ----------------------------------------------------------------------------- */ String *getQualifiedInterfaceName(Node *n) { String *ret = Getattr(n, "interface:qname"); if (!ret) { String *nspace = Getattr(n, "sym:nspace"); String *interface_name = Getattr(n, "interface:name"); if (nspace) { if (namespce) ret = NewStringf("%s.%s.%s", namespce, nspace, interface_name); else ret = NewStringf("%s.%s", nspace, interface_name); } else { ret = Copy(interface_name); } Setattr(n, "interface:qname", ret); } return ret; } /* ----------------------------------------------------------------------------- * getInterfaceName() * ----------------------------------------------------------------------------- */ String *getInterfaceName(SwigType *t, bool qualified) { String *interface_name = NULL; if (proxy_flag) { Node *n = classLookup(t); if (n && Getattr(n, "interface:name")) interface_name = qualified ? getQualifiedInterfaceName(n) : Getattr(n, "interface:name"); } return interface_name; } /* ----------------------------------------------------------------------------- * addInterfaceNameAndUpcasts() * ----------------------------------------------------------------------------- */ void addInterfaceNameAndUpcasts(SwigType *smart, String *interface_list, String *interface_upcasts, List *base_list, SwigType *c_classname) { for (Iterator it = First(base_list); it.item; it = Next(it)) { Node *base = it.item; SwigType *c_baseclassname = Getattr(base, "name"); String *interface_name = Getattr(base, "interface:name"); SwigType *bsmart = Getattr(base, "smart"); if (Len(interface_list)) Append(interface_list, ", "); Append(interface_list, interface_name); Node *attributes = NewHash(); String *interface_code = Copy(typemapLookup(base, "csinterfacecode", Getattr(base, "classtypeobj"), WARN_CSHARP_TYPEMAP_INTERFACECODE_UNDEF, attributes)); String *cptr_method_name = 0; if (interface_code) { Replaceall(interface_code, "$interfacename", interface_name); Printv(interface_upcasts, interface_code, NIL); cptr_method_name = Copy(Getattr(attributes, "tmap:csinterfacecode:cptrmethod")); } if (!cptr_method_name) cptr_method_name = NewStringf("%s_GetInterfaceCPtr", interface_name); Replaceall(cptr_method_name, ".", "_"); Replaceall(cptr_method_name, "$interfacename", interface_name); String *upcast_method_name = Swig_name_member(getNSpace(), getClassPrefix(), cptr_method_name); upcastsCode(smart, bsmart, upcast_method_name, c_classname, c_baseclassname); Delete(upcast_method_name); Delete(cptr_method_name); Delete(interface_code); } } /* ----------------------------------------------------------------------------- * upcastsCode() * * Add code for C++ casting to base class * ----------------------------------------------------------------------------- */ void upcastsCode(SwigType *smart, SwigType *bsmart, String *upcast_method_name, SwigType *c_classname, SwigType *c_baseclassname) { String *wname = Swig_name_wrapper(upcast_method_name); Printv(imclass_cppcasts_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL); Printf(imclass_cppcasts_code, " public static extern global::System.IntPtr %s(global::System.IntPtr jarg1);\n", upcast_method_name); Replaceall(imclass_cppcasts_code, "$csclassname", proxy_class_name); if (smart) { if (bsmart) { String *smartnamestr = SwigType_namestr(smart); String *bsmartnamestr = SwigType_namestr(bsmart); Printv(upcasts_code, "SWIGEXPORT ", bsmartnamestr, " * SWIGSTDCALL ", wname, "(", smartnamestr, " *jarg1) {\n", " return jarg1 ? new ", bsmartnamestr, "(*jarg1) : 0;\n" "}\n", "\n", NIL); Delete(bsmartnamestr); Delete(smartnamestr); } } else { String *classname = SwigType_namestr(c_classname); String *baseclassname = SwigType_namestr(c_baseclassname); Printv(upcasts_code, "SWIGEXPORT ", baseclassname, " * SWIGSTDCALL ", wname, "(", classname, " *jarg1) {\n", " return (", baseclassname, " *)jarg1;\n" "}\n", "\n", NIL); Delete(baseclassname); Delete(classname); } Delete(wname); } /* ----------------------------------------------------------------------------- * emitProxyClassDefAndCPPCasts() * ----------------------------------------------------------------------------- */ void emitProxyClassDefAndCPPCasts(Node *n) { SwigType *c_classname = Getattr(n, "name"); SwigType *c_baseclassname = NULL; String *baseclass = NULL; String *interface_list = NewStringEmpty(); String *interface_upcasts = NewStringEmpty(); SwigType *typemap_lookup_type = Getattr(n, "classtypeobj"); bool feature_director = Swig_directorclass(n) ? true : false; bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested"); SwigType *smart = Getattr(n, "smart"); SwigType *bsmart = 0; // Inheritance from pure C# classes Node *attributes = NewHash(); const String *pure_baseclass = typemapLookup(n, "csbase", typemap_lookup_type, WARN_NONE, attributes); bool purebase_replace = GetFlag(attributes, "tmap:csbase:replace") ? true : false; bool purebase_notderived = GetFlag(attributes, "tmap:csbase:notderived") ? true : false; Delete(attributes); // C++ inheritance if (!purebase_replace) { List *baselist = Getattr(n, "bases"); if (baselist) { Iterator base = First(baselist); while (base.item) { if (!(GetFlag(base.item, "feature:ignore") || GetFlag(base.item, "feature:interface"))) { SwigType *baseclassname = Getattr(base.item, "name"); if (!c_baseclassname) { String *name = getProxyName(baseclassname); if (name) { c_baseclassname = baseclassname; baseclass = name; bsmart = Getattr(base.item, "smart"); } } else { /* Warn about multiple inheritance for additional base class(es) */ String *proxyclassname = Getattr(n, "classtypeobj"); Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, base %s ignored. Multiple inheritance is not supported in C#.\n", SwigType_namestr(proxyclassname), SwigType_namestr(baseclassname)); } } base = Next(base); } } } List *interface_bases = Getattr(n, "interface:bases"); if (interface_bases) addInterfaceNameAndUpcasts(smart, interface_list, interface_upcasts, interface_bases, c_classname); bool derived = baseclass != 0; if (derived && purebase_notderived) pure_baseclass = empty_string; const String *wanted_base = baseclass ? baseclass : pure_baseclass; if (purebase_replace) { wanted_base = pure_baseclass; derived = false; baseclass = NULL; if (purebase_notderived) Swig_error(Getfile(n), Getline(n), "The csbase typemap for proxy %s must contain just one of the 'replace' or 'notderived' attributes.\n", typemap_lookup_type); } else if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) { Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, Getfile(n), Getline(n), "Warning for %s, base %s ignored. Multiple inheritance is not supported in C#. " "Perhaps you need one of the 'replace' or 'notderived' attributes in the csbase typemap?\n", typemap_lookup_type, pure_baseclass); } // Pure C# interfaces const String *pure_interfaces = typemapLookup(n, derived ? "csinterfaces_derived" : "csinterfaces", typemap_lookup_type, WARN_NONE); if (*Char(interface_list) && *Char(pure_interfaces)) Append(interface_list, ", "); Append(interface_list, pure_interfaces); // Start writing the proxy class if (!has_outerclass) Printv(proxy_class_def, typemapLookup(n, "csimports", typemap_lookup_type, WARN_NONE), // Import statements "\n", NIL); // Class attributes const String *csattributes = typemapLookup(n, "csattributes", typemap_lookup_type, WARN_NONE); if (csattributes && *Char(csattributes)) Printf(proxy_class_def, "%s\n", csattributes); Printv(proxy_class_def, typemapLookup(n, "csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers " $csclassname", // Class name and base class (*Char(wanted_base) || *Char(interface_list)) ? " : " : "", wanted_base, (*Char(wanted_base) && *Char(interface_list)) ? // Interfaces ", " : "", interface_list, " {", derived ? typemapLookup(n, "csbody_derived", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF) : // main body of class typemapLookup(n, "csbody", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class NIL); // C++ destructor is wrapped by the Finalize and Dispose methods const char *tmap_method = derived ? "csdestruct_derived" : "csdestruct"; const String *tm = typemapExists(n, tmap_method, typemap_lookup_type); if (tm) { Swig_error(Getfile(tm), Getline(tm), "A deprecated %s typemap was found for %s, please remove it and replace all csdestruct, csdestruct_derived and csfinalize typemaps by the csdispose, csdispose_derived, csdisposing and csdisposing_derived typemaps.\n", tmap_method, proxy_class_name); } tmap_method = "csfinalize"; tm = typemapExists(n, tmap_method, typemap_lookup_type); if (tm) { Swig_error(Getfile(tm), Getline(tm), "A deprecated %s typemap was found for %s, please remove it and replace all csdestruct, csdestruct_derived and csfinalize typemaps by the csdispose, csdispose_derived, csdisposing and csdisposing_derived typemaps.\n", tmap_method, proxy_class_name); } tmap_method = derived ? "csdisposing_derived" : "csdisposing"; String *destruct = NewString(""); attributes = NewHash(); const String *destruct_methodname = NULL; const String *destruct_methodmodifiers = NULL; const String *destruct_parameters = NULL; if (derived) { tm = typemapLookup(n, "csdisposing_derived", typemap_lookup_type, WARN_NONE, attributes); destruct_methodname = Getattr(attributes, "tmap:csdisposing_derived:methodname"); destruct_methodmodifiers = Getattr(attributes, "tmap:csdisposing_derived:methodmodifiers"); destruct_parameters = Getattr(attributes, "tmap:csdisposing_derived:parameters"); } else { tm = typemapLookup(n, "csdisposing", typemap_lookup_type, WARN_NONE, attributes); destruct_methodname = Getattr(attributes, "tmap:csdisposing:methodname"); destruct_methodmodifiers = Getattr(attributes, "tmap:csdisposing:methodmodifiers"); destruct_parameters = Getattr(attributes, "tmap:csdisposing:parameters"); } if (tm && *Char(tm)) { if (!destruct_methodname) { Swig_error(Getfile(n), Getline(n), "No methodname attribute defined in %s typemap for %s\n", tmap_method, proxy_class_name); } if (!destruct_methodmodifiers) { Swig_error(Getfile(n), Getline(n), "No methodmodifiers attribute defined in %s typemap for %s.\n", tmap_method, proxy_class_name); } if (!destruct_parameters) destruct_parameters = empty_string; } // Emit the Finalize and Dispose methods if (tm) { // Finalize and Dispose methods Printv(proxy_class_def, typemapLookup(n, derived ? "csdispose_derived" : "csdispose", typemap_lookup_type, WARN_NONE), NIL); // Dispose(bool disposing) method Printv(destruct, tm, NIL); if (*Char(destructor_call)) Replaceall(destruct, "$imcall", destructor_call); else Replaceall(destruct, "$imcall", "throw new global::System.MethodAccessException(\"C++ destructor does not have public access\")"); if (*Char(destruct)) { Printv(proxy_class_def, "\n ", NIL); const String *methodmods = Getattr(n, "destructmethodmodifiers"); if (methodmods) Printv(proxy_class_def, methodmods, NIL); else Printv(proxy_class_def, destruct_methodmodifiers, " ", derived ? "override" : "virtual", NIL); Printv(proxy_class_def, " void ", destruct_methodname, "(", destruct_parameters, ") ", destruct, "\n", NIL); } } if (*Char(interface_upcasts)) Printv(proxy_class_def, interface_upcasts, NIL); if (feature_director) { // Generate director connect method // put this in classDirectorEnd ??? Printf(proxy_class_code, " private void SwigDirectorConnect() {\n"); int i; for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *method = Getattr(udata, "method"); String *methid = Getattr(udata, "class_methodidx"); String *overname = Getattr(udata, "overname"); Printf(proxy_class_code, " if (SwigDerivedClassHasMethod(\"%s\", swigMethodTypes%s))\n", method, methid); Printf(proxy_class_code, " swigDelegate%s = new SwigDelegate%s_%s(SwigDirectorMethod%s);\n", methid, proxy_class_name, methid, overname); } String *director_connect_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect"); Printf(proxy_class_code, " %s.%s(swigCPtr", imclass_name, director_connect_method_name); for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *methid = Getattr(udata, "class_methodidx"); Printf(proxy_class_code, ", swigDelegate%s", methid); } Printf(proxy_class_code, ");\n"); Printf(proxy_class_code, " }\n"); if (first_class_dmethod < curr_class_dmethod) { // Only emit if there is at least one director method Printf(proxy_class_code, "\n"); Printf(proxy_class_code, " private bool SwigDerivedClassHasMethod(string methodName, global::System.Type[] methodTypes) {\n"); Printf(proxy_class_code, " global::System.Reflection.MethodInfo[] methodInfos = this.GetType().GetMethods(\n"); Printf(proxy_class_code, " global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.NonPublic | global::System.Reflection.BindingFlags.Instance);\n"); Printf(proxy_class_code, " foreach (global::System.Reflection.MethodInfo methodInfo in methodInfos) {\n"); Printf(proxy_class_code, " if (methodInfo.DeclaringType == null)\n"); Printf(proxy_class_code, " continue;\n\n"); Printf(proxy_class_code, " if (methodInfo.Name != methodName)\n"); Printf(proxy_class_code, " continue;\n\n"); Printf(proxy_class_code, " var parameters = methodInfo.GetParameters();\n"); Printf(proxy_class_code, " if (parameters.Length != methodTypes.Length)\n"); Printf(proxy_class_code, " continue;\n\n"); Printf(proxy_class_code, " bool parametersMatch = true;\n"); Printf(proxy_class_code, " for (var i = 0; i < parameters.Length; i++) {\n"); Printf(proxy_class_code, " if (parameters[i].ParameterType != methodTypes[i]) {\n"); Printf(proxy_class_code, " parametersMatch = false;\n"); Printf(proxy_class_code, " break;\n"); Printf(proxy_class_code, " }\n"); Printf(proxy_class_code, " }\n\n"); Printf(proxy_class_code, " if (!parametersMatch)\n"); Printf(proxy_class_code, " continue;\n\n"); Printf(proxy_class_code, " if (methodInfo.IsVirtual && (methodInfo.DeclaringType.IsSubclassOf(typeof(%s))) &&\n", proxy_class_name); Printf(proxy_class_code, " methodInfo.DeclaringType != methodInfo.GetBaseDefinition().DeclaringType) {\n"); Printf(proxy_class_code, " return true;\n"); Printf(proxy_class_code, " }\n"); Printf(proxy_class_code, " }\n\n"); Printf(proxy_class_code, " return false;\n"); /* Could add this code to cover corner case where the GetMethod() returns a method which allows type * promotion, eg it will return foo(double), if looking for foo(int). if (hasDerivedMethod) { hasDerivedMethod = false; if (methodInfo != null) { hasDerivedMethod = true; ParameterInfo[] parameterArray1 = methodInfo.GetParameters(); for (int i=0; i 0) Printv(proxy_class_code, director_delegate_callback, NIL); if (Len(director_delegate_definitions) > 0) Printv(proxy_class_code, "\n", director_delegate_definitions, NIL); if (Len(director_delegate_instances) > 0) Printv(proxy_class_code, "\n", director_delegate_instances, NIL); if (Len(director_method_types) > 0) Printv(proxy_class_code, "\n", director_method_types, NIL); Delete(director_callback_typedefs); director_callback_typedefs = NULL; Delete(director_callbacks); director_callbacks = NULL; Delete(director_delegate_callback); director_delegate_callback = NULL; Delete(director_delegate_definitions); director_delegate_definitions = NULL; Delete(director_delegate_instances); director_delegate_instances = NULL; Delete(director_method_types); director_method_types = NULL; Delete(director_connect_parms); director_connect_parms = NULL; Delete(director_connect_method_name); } Delete(interface_upcasts); Delete(interface_list); Delete(attributes); Delete(destruct); // Emit extra user code Printv(proxy_class_def, typemapLookup(n, "cscode", typemap_lookup_type, WARN_NONE), // extra C# code "\n", NIL); if (derived) { String *upcast_method_name = Swig_name_member(getNSpace(), getClassPrefix(), smart != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast"); upcastsCode(smart, bsmart, upcast_method_name, c_classname, c_baseclassname); Delete(upcast_method_name); } } /* ---------------------------------------------------------------------- * emitInterfaceDeclaration() * ---------------------------------------------------------------------- */ void emitInterfaceDeclaration(Node *n, String *interface_name, File *f_interface) { Printv(f_interface, typemapLookup(n, "csimports", Getattr(n, "classtypeobj"), WARN_NONE), "\n", NIL); Printv(f_interface, typemapLookup(n, "csinterfacemodifiers", Getattr(n, "classtypeobj"), WARN_CSHARP_TYPEMAP_INTERFACEMODIFIERS_UNDEF), NIL); Printf(f_interface, " %s", interface_name); String *additional = Getattr(n, "feature:interface:additional"); String *bases = additional ? NewStringf(" : %s", additional) : 0; if (List *baselist = Getattr(n, "bases")) { for (Iterator base = First(baselist); base.item; base = Next(base)) { if (GetFlag(base.item, "feature:ignore") || !GetFlag(base.item, "feature:interface")) continue; // TODO: warn about skipped non-interface bases String *base_iname = Getattr(base.item, "interface:name"); if (!bases) bases = NewStringf(" : %s", base_iname); else { Append(bases, ", "); Append(bases, base_iname); } } } if (bases) { Printv(f_interface, bases, NIL); Delete(bases); } Printf(f_interface, " {\n"); Node *attributes = NewHash(); String *interface_code = Copy(typemapLookup(n, "csinterfacecode", Getattr(n, "classtypeobj"), WARN_CSHARP_TYPEMAP_INTERFACECODE_UNDEF, attributes)); if (interface_code) { String *interface_declaration = Copy(Getattr(attributes, "tmap:csinterfacecode:declaration")); if (interface_declaration) { Replaceall(interface_declaration, "$interfacename", interface_name); Printv(f_interface, interface_declaration, NIL); Delete(interface_declaration); } Delete(interface_code); } } /* ---------------------------------------------------------------------- * classHandler() * ---------------------------------------------------------------------- */ virtual int classHandler(Node *n) { String *nspace = getNSpace(); File *f_proxy = NULL; File *f_interface = NULL; // save class local variables String *old_proxy_class_name = proxy_class_name; String *old_full_imclass_name = full_imclass_name; String *old_destructor_call = destructor_call; String *old_proxy_class_constants_code = proxy_class_constants_code; String *old_proxy_class_def = proxy_class_def; String *old_proxy_class_code = proxy_class_code; bool has_outerclass = Getattr(n, "nested:outer") && !GetFlag(n, "feature:flatnested"); String *old_interface_class_code = interface_class_code; interface_class_code = 0; if (proxy_flag) { proxy_class_name = NewString(Getattr(n, "sym:name")); String *interface_name = GetFlag(n, "feature:interface") ? Getattr(n, "interface:name") : 0; if (Node *outer = Getattr(n, "nested:outer")) { String *outerClassesPrefix = Copy(Getattr(outer, "sym:name")); for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) { Push(outerClassesPrefix, "."); Push(outerClassesPrefix, Getattr(outer, "sym:name")); } String *fnspace = nspace ? NewStringf("%s.%s", nspace, outerClassesPrefix) : outerClassesPrefix; if (!addSymbol(proxy_class_name, n, fnspace)) return SWIG_ERROR; if (interface_name && !addInterfaceSymbol(interface_name, n, fnspace)) return SWIG_ERROR; if (nspace) Delete(fnspace); Delete(outerClassesPrefix); } else { if (!addSymbol(proxy_class_name, n, nspace)) return SWIG_ERROR; if (interface_name && !addInterfaceSymbol(interface_name, n, nspace)) return SWIG_ERROR; } if (!nspace) { full_imclass_name = NewStringf("%s", imclass_name); if (Cmp(proxy_class_name, imclass_name) == 0) { Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name); Exit(EXIT_FAILURE); } if (Cmp(proxy_class_name, module_class_name) == 0) { Printf(stderr, "Class name cannot be equal to module class name: %s\n", proxy_class_name); Exit(EXIT_FAILURE); } } else { if (namespce) { full_imclass_name = NewStringf("%s.%s", namespce, imclass_name); } else { full_imclass_name = NewStringf("%s", imclass_name); } } if (!has_outerclass) { String *output_directory = outputDirectory(nspace); f_proxy = getOutputFile(output_directory, proxy_class_name); addOpenNamespace(nspace, f_proxy); Delete(output_directory); } else ++nesting_depth; proxy_class_def = NewString(""); proxy_class_code = NewString(""); destructor_call = NewString(""); proxy_class_constants_code = NewString(""); if (GetFlag(n, "feature:interface")) { interface_class_code = NewString(""); String *output_directory = outputDirectory(nspace); f_interface = getOutputFile(output_directory, interface_name); addOpenNamespace(nspace, f_interface); emitInterfaceDeclaration(n, interface_name, interface_class_code); Delete(output_directory); } } Language::classHandler(n); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab0); Printv(proxy_class_def, ds, NIL); Delete(ds); } if (proxy_flag) { emitProxyClassDefAndCPPCasts(n); String *csclazzname = Swig_name_member(getNSpace(), getClassPrefix(), ""); // mangled full proxy class name Replaceall(proxy_class_def, "$csclassname", proxy_class_name); Replaceall(proxy_class_code, "$csclassname", proxy_class_name); Replaceall(proxy_class_constants_code, "$csclassname", proxy_class_name); Replaceall(interface_class_code, "$csclassname", proxy_class_name); Replaceall(proxy_class_def, "$csclazzname", csclazzname); Replaceall(proxy_class_code, "$csclazzname", csclazzname); Replaceall(proxy_class_constants_code, "$csclazzname", csclazzname); Replaceall(interface_class_code, "$csclazzname", csclazzname); Replaceall(proxy_class_def, "$module", module_class_name); Replaceall(proxy_class_code, "$module", module_class_name); Replaceall(proxy_class_constants_code, "$module", module_class_name); Replaceall(interface_class_code, "$module", module_class_name); Replaceall(proxy_class_def, "$imclassname", full_imclass_name); Replaceall(proxy_class_code, "$imclassname", full_imclass_name); Replaceall(proxy_class_constants_code, "$imclassname", full_imclass_name); Replaceall(interface_class_code, "$imclassname", full_imclass_name); Replaceall(proxy_class_def, "$dllimport", dllimport); Replaceall(proxy_class_code, "$dllimport", dllimport); Replaceall(proxy_class_constants_code, "$dllimport", dllimport); Replaceall(interface_class_code, "$dllimport", dllimport); if (!has_outerclass) Printv(f_proxy, proxy_class_def, proxy_class_code, NIL); else { Swig_offset_string(proxy_class_def, nesting_depth); Append(old_proxy_class_code, proxy_class_def); Swig_offset_string(proxy_class_code, nesting_depth); Append(old_proxy_class_code, proxy_class_code); } // Write out all the constants if (Len(proxy_class_constants_code) != 0) { if (!has_outerclass) Printv(f_proxy, proxy_class_constants_code, NIL); else { Swig_offset_string(proxy_class_constants_code, nesting_depth); Append(old_proxy_class_code, proxy_class_constants_code); } } if (!has_outerclass) { Printf(f_proxy, "}\n"); addCloseNamespace(nspace, f_proxy); if (f_proxy != f_single_out) Delete(f_proxy); f_proxy = NULL; } else { for (int i = 0; i < nesting_depth; ++i) Append(old_proxy_class_code, " "); Append(old_proxy_class_code, "}\n\n"); --nesting_depth; } if (f_interface) { Printv(f_interface, interface_class_code, "}\n", NIL); addCloseNamespace(nspace, f_interface); if (f_interface != f_single_out) Delete(f_interface); f_interface = 0; } emitDirectorExtraMethods(n); Delete(interface_class_code); interface_class_code = old_interface_class_code; Delete(csclazzname); Delete(proxy_class_name); proxy_class_name = old_proxy_class_name; Delete(full_imclass_name); full_imclass_name = old_full_imclass_name; Delete(destructor_call); destructor_call = old_destructor_call; Delete(proxy_class_constants_code); proxy_class_constants_code = old_proxy_class_constants_code; Delete(proxy_class_def); proxy_class_def = old_proxy_class_def; Delete(proxy_class_code); proxy_class_code = old_proxy_class_code; } return SWIG_OK; } void printArgumentDeclaration(Node *n, Parm *p, String *param_type, String *arg, String *code) { String *specifiedoverridekey = NewString("feature:cs:defaultargs:"); Append(specifiedoverridekey, arg); String *specifiedoverridevalue = Getattr(n, specifiedoverridekey); if (specifiedoverridevalue) { Printf(code, "%s %s=%s", param_type, arg, specifiedoverridevalue); } else { String *cppvalue = NULL; //if they've not specified defaultargs, then fall back to //the normal default handling of specifying one overload per possible //set of arguments. If they have, then use the default argument from //c++ as a literal csharp expression. if (Getattr(n, "feature:cs:defaultargs")) cppvalue = Getattr(p, "value"); if (cppvalue) Printf(code, "%s %s=%s", param_type, arg, cppvalue); else Printf(code, "%s %s", param_type, arg); } Delete(specifiedoverridekey); } /* ---------------------------------------------------------------------- * memberfunctionHandler() * ---------------------------------------------------------------------- */ virtual int memberfunctionHandler(Node *n) { Language::memberfunctionHandler(n); if (proxy_flag) { String *overloaded_name = getOverloadedName(n); String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name); Setattr(n, "proxyfuncname", Getattr(n, "sym:name")); Setattr(n, "imfuncname", intermediary_function_name); proxyClassFunctionHandler(n); Delete(overloaded_name); } return SWIG_OK; } /* ---------------------------------------------------------------------- * staticmemberfunctionHandler() * ---------------------------------------------------------------------- */ virtual int staticmemberfunctionHandler(Node *n) { static_flag = true; Language::staticmemberfunctionHandler(n); if (proxy_flag) { String *overloaded_name = getOverloadedName(n); String *intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name); Setattr(n, "proxyfuncname", Getattr(n, "sym:name")); Setattr(n, "imfuncname", intermediary_function_name); proxyClassFunctionHandler(n); Delete(overloaded_name); } static_flag = false; return SWIG_OK; } /* ----------------------------------------------------------------------------- * proxyClassFunctionHandler() * * Function called for creating a C# wrapper function around a c++ function in the * proxy class. Used for both static and non-static C++ class functions. * C++ class static functions map to C# static functions. * Two extra attributes in the Node must be available. These are "proxyfuncname" - * the name of the C# class proxy function, which in turn will call "imfuncname" - * the intermediary (PInvoke) function name in the intermediary class. * ----------------------------------------------------------------------------- */ void proxyClassFunctionHandler(Node *n) { SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *intermediary_function_name = Getattr(n, "imfuncname"); String *proxy_function_name = Getattr(n, "proxyfuncname"); String *tm; Parm *p; Parm *last_parm = 0; int i; String *comment_code = NewString(""); String *imcall = NewString(""); String *return_type = NewString(""); String *function_code = NewString(""); bool setter_flag = false; String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); bool is_interface = GetFlag(parentNode(n), "feature:interface") && !checkAttribute(n, "kind", "variable") && !static_flag && Getattr(n, "interface:owner") == 0; if (!proxy_flag) return; // Wrappers not wanted for some methods where the parameters cannot be overloaded in C# if (Getattr(n, "overload:ignore")) return; if (Getattr(n, "feature:cs:defaultargs") && Getattr(n, "defaultargs")) return; // Don't generate proxy method for additional explicitcall method used in directors if (GetFlag(n, "explicitcall")) return; if (l) { if (SwigType_type(Getattr(l, "type")) == T_VOID) { l = nextSibling(l); } } /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("in", l, NULL); Swig_typemap_attach_parms("cstype", l, NULL); Swig_typemap_attach_parms("csin", l, NULL); /* Get return types */ if ((tm = Swig_typemap_lookup("cstype", n, "", 0))) { // Note that in the case of polymorphic (covariant) return types, the method's return type is changed to be the base of the C++ return type SwigType *covariant = Getattr(n, "covariant"); String *cstypeout = Getattr(n, "tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap if (cstypeout) tm = cstypeout; substituteClassname(covariant ? covariant : t, tm); Printf(return_type, "%s", tm); if (covariant) Swig_warning(WARN_CSHARP_COVARIANT_RET, input_file, line_number, "Covariant return types not supported in C#. Proxy method will return %s.\n", SwigType_str(covariant, 0)); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(t, 0)); } if (wrapping_member_flag && !enum_constant_flag) { // Properties setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) == 0); if (setter_flag) Swig_typemap_attach_parms("csvarin", l, NULL); } /* Start generating the proxy function */ const String *outattributes = Getattr(n, "tmap:cstype:outattributes"); if (outattributes) Printf(function_code, " %s\n", outattributes); const String *csattributes = Getattr(n, "feature:cs:attributes"); if (csattributes) Printf(function_code, " %s\n", csattributes); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); if (methodmods) { if (is_smart_pointer()) { // Smart pointer classes do not mirror the inheritance hierarchy of the underlying pointer type, so no virtual/override/new required. String *mmods = Copy(methodmods); Replaceall(mmods, "override", ""); Replaceall(mmods, "virtual", ""); Replaceall(mmods, "new", ""); Chop(mmods); // remove trailing whitespace Printf(function_code, " %s ", mmods); Delete(mmods); } else { Printf(function_code, " %s ", methodmods); } } else { methodmods = (is_public(n) ? public_string : protected_string); Printf(function_code, " %s ", methodmods); if (!is_smart_pointer()) { // Smart pointer classes do not mirror the inheritance hierarchy of the underlying pointer type, so no virtual/override/new required. if (Getattr(n, "override")) Printf(function_code, "override "); else if (checkAttribute(n, "storage", "virtual")) Printf(function_code, "virtual "); if (Getattr(n, "hides")) Printf(function_code, "new "); } } if (static_flag) Printf(function_code, "static "); Printf(function_code, "%s %s(", return_type, proxy_function_name); if (is_interface) Printf(interface_class_code, " %s %s(", return_type, proxy_function_name); Printv(imcall, full_imclass_name, ".$imfuncname(", NIL); if (!static_flag) Printf(imcall, "swigCPtr"); emit_mark_varargs(l); int gencomma = !static_flag; /* Output each parameter */ for (i = 0, p = l; p; i++) { /* Ignored varargs */ if (checkAttribute(p, "varargs:ignore", "1")) { p = nextSibling(p); continue; } /* Ignored parameters */ if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } /* Ignore the 'this' argument for variable wrappers */ if (!(variable_wrapper_flag && i == 0)) { SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); if (setter_flag) last_parm = p; /* Get the C# parameter type */ if ((tm = Getattr(p, "tmap:cstype"))) { substituteClassname(pt, tm); const String *inattributes = Getattr(p, "tmap:cstype:inattributes"); Printf(param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, setter_flag); // Use typemaps to transform type used in C# wrapper function (in proxy class) to type used in PInvoke function (in intermediary class) if ((tm = Getattr(p, "tmap:csin"))) { substituteClassname(pt, tm); Replaceall(tm, "$csinput", arg); String *pre = Getattr(p, "tmap:csin:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$csinput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:csin:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$csinput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:csin:terminator"); if (terminator) { substituteClassname(pt, terminator); Replaceall(terminator, "$csinput", arg); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number, "No csin typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to proxy function */ if (gencomma >= 2) { Printf(function_code, ", "); if (is_interface) Printf(interface_class_code, ", "); } gencomma = 2; printArgumentDeclaration(n, p, param_type, arg, function_code); if (is_interface) printArgumentDeclaration(n, p, param_type, arg, interface_class_code); Delete(arg); Delete(param_type); } p = Getattr(p, "tmap:in:next"); } Printf(imcall, ")"); Printf(function_code, ")"); if (is_interface) Printf(interface_class_code, ");\n"); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab2); Printv(comment_code, ds, NIL); Delete(ds); } // Transform return type used in PInvoke function (in intermediary class) to type used in C# wrapper function (in proxy class) if ((tm = Swig_typemap_lookup("csout", n, "", 0))) { excodeSubstitute(n, tm, "csout", n); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code || is_post_code || is_terminator_code) { Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap if (is_post_code) { Insert(tm, 0, "\n try "); Printv(tm, " finally {\n", post_code, "\n }", NIL); } else { Insert(tm, 0, "\n "); } if (is_pre_code) { Insert(tm, 0, pre_code); Insert(tm, 0, "\n"); } if (is_terminator_code) { Printv(tm, "\n", terminator_code, NIL); } Insert(tm, 0, "{"); Printf(tm, "\n }"); } if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); substituteClassname(t, tm); // For director methods: generate code to selectively make a normal polymorphic call or // an explicit method call - needed to prevent infinite recursion calls in director methods. Node *explicit_n = Getattr(n, "explicitcallnode"); if (explicit_n) { String *ex_overloaded_name = getOverloadedName(explicit_n); String *ex_intermediary_function_name = Swig_name_member(getNSpace(), getClassPrefix(), ex_overloaded_name); String *ex_imcall = Copy(imcall); Replaceall(ex_imcall, "$imfuncname", ex_intermediary_function_name); Replaceall(imcall, "$imfuncname", intermediary_function_name); String *excode = NewString(""); Node *directorNode = Getattr(n, "directorNode"); UpcallData *udata = directorNode ? Getattr(directorNode, "upcalldata") : 0; if (udata) { String *methid = Getattr(udata, "class_methodidx"); if (!Cmp(return_type, "void")) Printf(excode, "if (SwigDerivedClassHasMethod(\"%s\", swigMethodTypes%s)) %s; else %s", proxy_function_name, methid, ex_imcall, imcall); else Printf(excode, "(SwigDerivedClassHasMethod(\"%s\", swigMethodTypes%s) ? %s : %s)", proxy_function_name, methid, ex_imcall, imcall); Clear(imcall); Printv(imcall, excode, NIL); } else { // probably an ignored method or nodirector } Delete(excode); Delete(ex_overloaded_name); } else { Replaceall(imcall, "$imfuncname", intermediary_function_name); } Replaceall(tm, "$imfuncname", intermediary_function_name); Replaceall(tm, "$imcall", imcall); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csout typemap defined for %s\n", SwigType_str(t, 0)); } if (wrapping_member_flag && !enum_constant_flag) { // Properties if (generate_property_declaration_flag) { // Ensure the declaration is generated just once should the property contain both a set and get // Get the C# variable type - obtained differently depending on whether a setter is required. String *variable_type = return_type; if (setter_flag) { assert(last_parm); // (last parameter is the only parameter for properties) /* Get variable type - ensure the variable name is fully resolved during typemap lookup via the symbol table set in NewParmNode */ SwigType *cvariable_type = Getattr(last_parm, "type"); Parm *variable_parm = NewParmNode(cvariable_type, n); if ((tm = Swig_typemap_lookup("cstype", variable_parm, "", 0))) { String *cstypeout = Getattr(variable_parm, "tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap if (cstypeout) tm = cstypeout; substituteClassname(cvariable_type, tm); variable_type = tm; } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(cvariable_type, 0)); } } const String *csattributes = Getattr(n, "feature:cs:attributes"); if (csattributes) Printf(proxy_class_code, " %s\n", csattributes); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); if (!methodmods) methodmods = (is_public(n) ? public_string : protected_string); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab2); Printv(proxy_class_code, ds, NIL); Delete(ds); } // Start property declaration Printf(proxy_class_code, " %s %s%s %s {", methodmods, static_flag ? "static " : "", variable_type, variable_name); } generate_property_declaration_flag = false; if (setter_flag) { // Setter method assert(last_parm); // (last parameter is the only parameter for properties) SwigType *cvariable_type = Getattr(last_parm, "type"); Parm *variable_parm = NewParmNode(cvariable_type, n); if ((tm = Swig_typemap_lookup("csvarin", variable_parm, "", 0))) { substituteClassname(cvariable_type, tm); Replaceall(tm, "$csinput", "value"); Replaceall(tm, "$imfuncname", intermediary_function_name); Replaceall(tm, "$imcall", imcall); excodeSubstitute(n, tm, "csvarin", variable_parm); Printf(proxy_class_code, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csvarin typemap defined for %s\n", SwigType_str(cvariable_type, 0)); } } else { // Getter method if ((tm = Swig_typemap_lookup("csvarout", n, "", 0))) { if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); substituteClassname(t, tm); Replaceall(tm, "$imfuncname", intermediary_function_name); Replaceall(tm, "$imcall", imcall); excodeSubstitute(n, tm, "csvarout", n); Printf(proxy_class_code, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csvarout typemap defined for %s\n", SwigType_str(t, 0)); } } } else { // Normal function call Printf(function_code, " %s\n\n", tm ? (const String *) tm : empty_string); Printv(proxy_class_code, comment_code, NIL); Printv(proxy_class_code, function_code, NIL); } Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(function_code); Delete(return_type); Delete(imcall); } /* ---------------------------------------------------------------------- * constructorHandler() * ---------------------------------------------------------------------- */ virtual int constructorHandler(Node *n) { ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; int i; String *function_code = NewString(""); String *comment_code = NewString(""); String *helper_code = NewString(""); // Holds code for the constructor helper method generated only when the csin typemap has code in the pre or post attributes String *helper_args = NewString(""); String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); String *im_return_type = NewString(""); bool feature_director = (parentNode(n) && Swig_directorclass(n)); Language::constructorHandler(n); // Wrappers not wanted for some methods where the parameters cannot be overloaded in C# if (Getattr(n, "overload:ignore")) return SWIG_OK; if (Getattr(n, "feature:cs:defaultargs") && Getattr(n, "defaultargs")) return SWIG_OK; if (proxy_flag) { String *overloaded_name = getOverloadedName(n); String *mangled_overname = Swig_name_construct(getNSpace(), overloaded_name); String *imcall = NewString(""); const String *csattributes = Getattr(n, "feature:cs:attributes"); if (csattributes) { Printf(function_code, " %s\n", csattributes); Printf(helper_code, " %s\n", csattributes); } const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); tm = Getattr(n, "tmap:imtype"); // typemaps were attached earlier to the node String *imtypeout = Getattr(n, "tmap:imtype:out"); // the type in the imtype typemap's out attribute overrides the type in the typemap if (imtypeout) tm = imtypeout; Printf(im_return_type, "%s", tm); Printf(function_code, " %s %s(", methodmods, proxy_class_name); Printf(helper_code, " static private %s SwigConstruct%s(", im_return_type, proxy_class_name); Printv(imcall, full_imclass_name, ".", mangled_overname, "(", NIL); /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("in", l, NULL); Swig_typemap_attach_parms("cstype", l, NULL); Swig_typemap_attach_parms("csin", l, NULL); emit_mark_varargs(l); int gencomma = 0; /* Output each parameter */ for (i = 0, p = l; p; i++) { /* Ignored varargs */ if (checkAttribute(p, "varargs:ignore", "1")) { p = nextSibling(p); continue; } /* Ignored parameters */ if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); /* Get the C# parameter type */ if ((tm = Getattr(p, "tmap:cstype"))) { substituteClassname(pt, tm); const String *inattributes = Getattr(p, "tmap:cstype:inattributes"); Printf(param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, false); String *cshin = 0; // Use typemaps to transform type used in C# wrapper function (in proxy class) to type used in PInvoke function (in intermediary class) if ((tm = Getattr(p, "tmap:csin"))) { substituteClassname(pt, tm); Replaceall(tm, "$csinput", arg); String *pre = Getattr(p, "tmap:csin:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$csinput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:csin:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$csinput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:csin:terminator"); if (terminator) { substituteClassname(pt, terminator); Replaceall(terminator, "$csinput", arg); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } cshin = Getattr(p, "tmap:csin:cshin"); if (cshin) Replaceall(cshin, "$csinput", arg); Printv(imcall, tm, NIL); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number, "No csin typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to proxy function */ if (gencomma) { Printf(function_code, ", "); Printf(helper_code, ", "); Printf(helper_args, ", "); } printArgumentDeclaration(n, p, param_type, arg, function_code); Printf(helper_code, "%s %s", param_type, arg); Printf(helper_args, "%s", cshin ? cshin : arg); ++gencomma; Delete(cshin); Delete(arg); Delete(param_type); p = Getattr(p, "tmap:in:next"); } Printf(imcall, ")"); Printf(function_code, ")"); Printf(helper_code, ")"); /* Insert the csconstruct typemap, doing the replacement for $directorconnect, as needed */ Hash *attributes = NewHash(); String *typemap_lookup_type = Getattr(getCurrentClass(), "classtypeobj"); String *construct_tm = Copy(typemapLookup(n, "csconstruct", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSCONSTRUCT_UNDEF, attributes)); if (construct_tm) { if (!feature_director) { Replaceall(construct_tm, "$directorconnect", ""); } else { String *connect_attr = Getattr(attributes, "tmap:csconstruct:directorconnect"); if (connect_attr) { Replaceall(construct_tm, "$directorconnect", connect_attr); } else { Swig_warning(WARN_CSHARP_NO_DIRECTORCONNECT_ATTR, input_file, line_number, "\"directorconnect\" attribute missing in %s \"csconstruct\" typemap.\n", Getattr(n, "name")); Replaceall(construct_tm, "$directorconnect", ""); } } Printv(function_code, " ", construct_tm, NIL); } excodeSubstitute(n, function_code, "csconstruct", attributes); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code || is_post_code || is_terminator_code) { Printf(helper_code, " {\n"); if (is_pre_code) { Printv(helper_code, pre_code, "\n", NIL); } if (is_post_code) { Printf(helper_code, " try {\n"); Printv(helper_code, " return ", imcall, ";\n", NIL); Printv(helper_code, " } finally {\n", post_code, "\n }", NIL); } else { Printv(helper_code, " return ", imcall, ";", NIL); } if (is_terminator_code) { Printv(helper_code, "\n", terminator_code, NIL); } Printf(helper_code, "\n }\n"); String *helper_name = NewStringf("%s.SwigConstruct%s(%s)", proxy_class_name, proxy_class_name, helper_args); String *im_outattributes = Getattr(n, "tmap:imtype:outattributes"); if (im_outattributes) Printf(proxy_class_code, " %s\n", im_outattributes); Printv(proxy_class_code, helper_code, "\n", NIL); Replaceall(function_code, "$imcall", helper_name); Delete(helper_name); } else { Replaceall(function_code, "$imcall", imcall); } // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab2); Printv(comment_code, ds, NIL); Delete(ds); } Printv(proxy_class_code, comment_code, NIL); Printv(proxy_class_code, function_code, "\n", NIL); Delete(helper_args); Delete(im_return_type); Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(construct_tm); Delete(attributes); Delete(overloaded_name); Delete(imcall); } return SWIG_OK; } /* ---------------------------------------------------------------------- * destructorHandler() * ---------------------------------------------------------------------- */ virtual int destructorHandler(Node *n) { Language::destructorHandler(n); String *symname = Getattr(n, "sym:name"); if (proxy_flag) { Printv(destructor_call, full_imclass_name, ".", Swig_name_destroy(getNSpace(), symname), "(swigCPtr)", NIL); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); if (methodmods) Setattr(getCurrentClass(), "destructmethodmodifiers", methodmods); } return SWIG_OK; } /* ---------------------------------------------------------------------- * membervariableHandler() * ---------------------------------------------------------------------- */ virtual int membervariableHandler(Node *n) { generate_property_declaration_flag = true; variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; variable_wrapper_flag = true; Language::membervariableHandler(n); wrapping_member_flag = false; variable_wrapper_flag = false; generate_property_declaration_flag = false; // End property declaration Printf(proxy_class_code, "\n }\n\n"); return SWIG_OK; } /* ---------------------------------------------------------------------- * staticmembervariableHandler() * ---------------------------------------------------------------------- */ virtual int staticmembervariableHandler(Node *n) { generate_property_declaration_flag = true; variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; static_flag = true; Language::staticmembervariableHandler(n); wrapping_member_flag = false; static_flag = false; generate_property_declaration_flag = false; if (!GetFlag(n, "wrappedasconstant")) { // End property declaration Printf(proxy_class_code, "\n }\n\n"); } return SWIG_OK; } /* ---------------------------------------------------------------------- * memberconstantHandler() * ---------------------------------------------------------------------- */ virtual int memberconstantHandler(Node *n) { variable_name = Getattr(n, "sym:name"); wrapping_member_flag = true; Language::memberconstantHandler(n); wrapping_member_flag = false; return SWIG_OK; } /* ----------------------------------------------------------------------------- * getOverloadedName() * ----------------------------------------------------------------------------- */ String *getOverloadedName(Node *n) { /* A C# HandleRef is used for all classes in the SWIG intermediary class. * The intermediary class methods are thus mangled when overloaded to give * a unique name. */ String *overloaded_name = Copy(Getattr(n, "sym:name")); if (Getattr(n, "sym:overloaded")) { Printv(overloaded_name, Getattr(n, "sym:overname"), NIL); } return overloaded_name; } /* ----------------------------------------------------------------------------- * moduleClassFunctionHandler() * ----------------------------------------------------------------------------- */ void moduleClassFunctionHandler(Node *n) { SwigType *t = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *tm; Parm *p; Parm *last_parm = 0; int i; String *imcall = NewString(""); String *return_type = NewString(""); String *function_code = NewString(""); int num_arguments = 0; String *overloaded_name = getOverloadedName(n); String *func_name = NULL; bool setter_flag = false; String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); if (Getattr(n, "feature:cs:defaultargs") && Getattr(n, "defaultargs")) return; if (l) { if (SwigType_type(Getattr(l, "type")) == T_VOID) { l = nextSibling(l); } } /* Attach the non-standard typemaps to the parameter list */ Swig_typemap_attach_parms("cstype", l, NULL); Swig_typemap_attach_parms("csin", l, NULL); /* Get return types */ if ((tm = Swig_typemap_lookup("cstype", n, "", 0))) { String *cstypeout = Getattr(n, "tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap if (cstypeout) tm = cstypeout; substituteClassname(t, tm); Printf(return_type, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(t, 0)); } /* Change function name for global variables */ if (proxy_flag && global_variable_flag) { // Capitalize the first letter in the variable to create the getter/setter function name func_name = NewString(""); setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(getNSpace(), variable_name)) == 0); if (setter_flag) Printf(func_name, "set"); else Printf(func_name, "get"); Putc(toupper((int) *Char(variable_name)), func_name); Printf(func_name, "%s", Char(variable_name) + 1); if (setter_flag) Swig_typemap_attach_parms("csvarin", l, NULL); } else { func_name = Copy(Getattr(n, "sym:name")); } /* Start generating the function */ const String *outattributes = Getattr(n, "tmap:cstype:outattributes"); if (outattributes) Printf(function_code, " %s\n", outattributes); const String *csattributes = Getattr(n, "feature:cs:attributes"); if (csattributes) Printf(function_code, " %s\n", csattributes); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); methodmods = methodmods ? methodmods : (is_public(n) ? public_string : protected_string); // Translate documentation comments if (have_docstring(n)) { String *ds = docstring(n, tab2); Printv(function_code, ds, NIL); Delete(ds); } Printf(function_code, " %s static %s %s(", methodmods, return_type, func_name); Printv(imcall, imclass_name, ".", overloaded_name, "(", NIL); /* Get number of required and total arguments */ num_arguments = emit_num_arguments(l); bool global_or_member_variable = global_variable_flag || (wrapping_member_flag && !enum_constant_flag); int gencomma = 0; /* Output each parameter */ for (i = 0, p = l; i < num_arguments; i++) { /* Ignored parameters */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *param_type = NewString(""); last_parm = p; /* Get the C# parameter type */ if ((tm = Getattr(p, "tmap:cstype"))) { substituteClassname(pt, tm); const String *inattributes = Getattr(p, "tmap:cstype:inattributes"); Printf(param_type, "%s%s", inattributes ? inattributes : empty_string, tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(pt, 0)); } if (gencomma) Printf(imcall, ", "); String *arg = makeParameterName(n, p, i, global_or_member_variable); // Use typemaps to transform type used in C# wrapper function (in proxy class) to type used in PInvoke function (in intermediary class) if ((tm = Getattr(p, "tmap:csin"))) { substituteClassname(pt, tm); Replaceall(tm, "$csinput", arg); String *pre = Getattr(p, "tmap:csin:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$csinput", arg); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:csin:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$csinput", arg); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:csin:terminator"); if (terminator) { substituteClassname(pt, terminator); Replaceall(terminator, "$csinput", arg); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } Printv(imcall, tm, NIL); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number, "No csin typemap defined for %s\n", SwigType_str(pt, 0)); } /* Add parameter to module class function */ if (gencomma >= 2) Printf(function_code, ", "); gencomma = 2; printArgumentDeclaration(n, p, param_type, arg, function_code); p = Getattr(p, "tmap:in:next"); Delete(arg); Delete(param_type); } Printf(imcall, ")"); Printf(function_code, ")"); // Transform return type used in PInvoke function (in intermediary class) to type used in C# wrapper function (in module class) if ((tm = Swig_typemap_lookup("csout", n, "", 0))) { excodeSubstitute(n, tm, "csout", n); bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code || is_post_code || is_terminator_code) { Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap if (is_post_code) { Insert(tm, 0, "\n try "); Printv(tm, " finally {\n", post_code, "\n }", NIL); } else { Insert(tm, 0, "\n "); } if (is_pre_code) { Insert(tm, 0, pre_code); Insert(tm, 0, "\n"); } if (is_terminator_code) { Printv(tm, "\n", terminator_code, NIL); } Insert(tm, 0, "{"); Printf(tm, "\n }"); } if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); substituteClassname(t, tm); Replaceall(tm, "$imfuncname", overloaded_name); Replaceall(tm, "$imcall", imcall); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csout typemap defined for %s\n", SwigType_str(t, 0)); } if (proxy_flag && global_variable_flag) { // Properties if (generate_property_declaration_flag) { // Ensure the declaration is generated just once should the property contain both a set and get // Get the C# variable type - obtained differently depending on whether a setter is required. String *variable_type = return_type; if (setter_flag) { p = last_parm; // (last parameter is the only parameter for properties) SwigType *pt = Getattr(p, "type"); if ((tm = Getattr(p, "tmap:cstype"))) { substituteClassname(pt, tm); String *cstypeout = Getattr(p, "tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap variable_type = cstypeout ? cstypeout : tm; } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csvarin typemap defined for %s\n", SwigType_str(pt, 0)); } } const String *csattributes = Getattr(n, "feature:cs:attributes"); if (csattributes) Printf(module_class_code, " %s\n", csattributes); const String *methodmods = Getattr(n, "feature:cs:methodmodifiers"); if (!methodmods) methodmods = (is_public(n) ? public_string : protected_string); Printf(module_class_code, " %s static %s %s {", methodmods, variable_type, variable_name); } generate_property_declaration_flag = false; if (setter_flag) { // Setter method p = last_parm; // (last parameter is the only parameter for properties) SwigType *pt = Getattr(p, "type"); if ((tm = Getattr(p, "tmap:csvarin"))) { substituteClassname(pt, tm); Replaceall(tm, "$csinput", "value"); Replaceall(tm, "$imfuncname", overloaded_name); Replaceall(tm, "$imcall", imcall); excodeSubstitute(n, tm, "csvarin", p); Printf(module_class_code, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csvarin typemap defined for %s\n", SwigType_str(pt, 0)); } } else { // Getter method if ((tm = Swig_typemap_lookup("csvarout", n, "", 0))) { if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "true"); else Replaceall(tm, "$owner", "false"); substituteClassname(t, tm); Replaceall(tm, "$imfuncname", overloaded_name); Replaceall(tm, "$imcall", imcall); excodeSubstitute(n, tm, "csvarout", n); Printf(module_class_code, "%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number, "No csvarout typemap defined for %s\n", SwigType_str(t, 0)); } } } else { // Normal function call Printf(function_code, " %s\n\n", tm ? (const String *) tm : empty_string); Printv(module_class_code, function_code, NIL); } Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(function_code); Delete(return_type); Delete(imcall); Delete(func_name); } /*---------------------------------------------------------------------- * replaceSpecialVariables() *--------------------------------------------------------------------*/ virtual void replaceSpecialVariables(String *method, String *tm, Parm *parm) { (void)method; SwigType *type = Getattr(parm, "type"); substituteClassname(type, tm); } /*---------------------------------------------------------------------- * decodeEnumFeature() * Decode the possible enum features, which are one of: * %csenum(simple) * %csenum(typeunsafe) - default * %csenum(typesafe) * %csenum(proper) *--------------------------------------------------------------------*/ EnumFeature decodeEnumFeature(Node *n) { EnumFeature enum_feature = TypeunsafeEnum; String *feature = Getattr(n, "feature:cs:enum"); if (feature) { if (Cmp(feature, "simple") == 0) enum_feature = SimpleEnum; else if (Cmp(feature, "typesafe") == 0) enum_feature = TypesafeEnum; else if (Cmp(feature, "proper") == 0) enum_feature = ProperEnum; } return enum_feature; } /* ----------------------------------------------------------------------- * enumValue() * This method will return a string with an enum value to use in C# generated * code. If the %csconst feature is not used, the string will contain the intermediary * class call to obtain the enum value. The intermediary class and PINVOKE methods to obtain * the enum value will be generated. Otherwise the C/C++ enum value will be used if there * is one and hopefully it will compile as C# code - e.g. 20 as in: enum E{e=20}; * The %csconstvalue feature overrides all other ways to generate the constant value. * The caller must delete memory allocated for the returned string. * ------------------------------------------------------------------------ */ String *enumValue(Node *n) { String *symname = Getattr(n, "sym:name"); // Check for the %csconstvalue feature String *value = Getattr(n, "feature:cs:constvalue"); if (!value) { // The %csconst feature determines how the constant value is obtained int const_feature_flag = GetFlag(n, "feature:cs:const"); if (const_feature_flag) { // Use the C syntax to make a true C# constant and hope that it compiles as C# code value = Getattr(n, "enumvalue") ? Copy(Getattr(n, "enumvalue")) : Copy(Getattr(n, "enumvalueex")); } else { String *newsymname = 0; if (!getCurrentClass() || !proxy_flag) { String *enumClassPrefix = getEnumClassPrefix(); if (enumClassPrefix) { // A global scoped enum newsymname = Swig_name_member(0, enumClassPrefix, symname); symname = newsymname; } } // Get the enumvalue from a PINVOKE call if (!getCurrentClass() || !cparse_cplusplus || !proxy_flag) { // Strange hack to change the name Setattr(n, "name", Getattr(n, "value")); /* for wrapping of enums in a namespace when emit_action is used */ constantWrapper(n); value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname)); } else { memberconstantHandler(n); value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), Swig_name_member(0, getEnumClassPrefix(), symname))); } } } return value; } /* ----------------------------------------------------------------------------- * getEnumName() * ----------------------------------------------------------------------------- */ String *getEnumName(SwigType *t) { Node *enumname = NULL; Node *n = enumLookup(t); if (n) { enumname = Getattr(n, "enumname"); if (!enumname) { String *symname = Getattr(n, "sym:name"); if (symname) { // Add in class scope when referencing enum if not a global enum String *scopename_prefix = Swig_scopename_prefix(Getattr(n, "name")); String *proxyname = 0; if (scopename_prefix) { proxyname = getProxyName(scopename_prefix); } if (proxyname) { enumname = NewStringf("%s.%s", proxyname, symname); } else { // global enum or enum in a namespace String *nspace = Getattr(n, "sym:nspace"); if (nspace) { if (namespce) enumname = NewStringf("%s.%s.%s", namespce, nspace, symname); else enumname = NewStringf("%s.%s", nspace, symname); } else { enumname = Copy(symname); } } Setattr(n, "enumname", enumname); Delete(enumname); Delete(scopename_prefix); } } } return enumname; } /* ----------------------------------------------------------------------------- * substituteClassname() * * Substitute the special variable $csclassname with the proxy class name for classes/structs/unions * that SWIG knows about. Also substitutes enums with enum name. * Otherwise use the $descriptor name for the C# class name. Note that the $&csclassname substitution * is the same as a $&descriptor substitution, ie one pointer added to descriptor name. * Inputs: * pt - parameter type * tm - typemap contents that might contain the special variable to be replaced * Outputs: * tm - typemap contents complete with the special variable substitution * Return: * substitution_performed - flag indicating if a substitution was performed * ----------------------------------------------------------------------------- */ bool substituteClassname(SwigType *pt, String *tm) { bool substitution_performed = false; SwigType *type = Copy(SwigType_typedef_resolve_all(pt)); SwigType *strippedtype = SwigType_strip_qualifiers(type); if (Strstr(tm, "$csclassname")) { SwigType *classnametype = Copy(strippedtype); substituteClassnameSpecialVariable(classnametype, tm, "$csclassname"); substitution_performed = true; Delete(classnametype); } if (Strstr(tm, "$*csclassname")) { SwigType *classnametype = Copy(strippedtype); Delete(SwigType_pop(classnametype)); if (Len(classnametype) > 0) { substituteClassnameSpecialVariable(classnametype, tm, "$*csclassname"); substitution_performed = true; } Delete(classnametype); } if (Strstr(tm, "$&csclassname")) { SwigType *classnametype = Copy(strippedtype); SwigType_add_pointer(classnametype); substituteClassnameSpecialVariable(classnametype, tm, "$&csclassname"); substitution_performed = true; Delete(classnametype); } if (Strstr(tm, "$csinterfacename")) { SwigType *interfacenametype = Copy(strippedtype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$csinterfacename", true); substitution_performed = true; Delete(interfacenametype); } if (Strstr(tm, "$*csinterfacename")) { SwigType *interfacenametype = Copy(strippedtype); Delete(SwigType_pop(interfacenametype)); if (Len(interfacenametype) > 0) { substituteInterfacenameSpecialVariable(interfacenametype, tm, "$*csinterfacename", true); substitution_performed = true; } Delete(interfacenametype); } if (Strstr(tm, "$&csinterfacename")) { SwigType *interfacenametype = Copy(strippedtype); SwigType_add_pointer(interfacenametype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$&csinterfacename", true); substitution_performed = true; Delete(interfacenametype); } if (Strstr(tm, "$interfacename")) { SwigType *interfacenametype = Copy(strippedtype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$interfacename", false); substitution_performed = true; Delete(interfacenametype); } if (Strstr(tm, "$*interfacename")) { SwigType *interfacenametype = Copy(strippedtype); Delete(SwigType_pop(interfacenametype)); if (Len(interfacenametype) > 0) { substituteInterfacenameSpecialVariable(interfacenametype, tm, "$*interfacename", false); substitution_performed = true; } Delete(interfacenametype); } if (Strstr(tm, "$&interfacename")) { SwigType *interfacenametype = Copy(strippedtype); SwigType_add_pointer(interfacenametype); substituteInterfacenameSpecialVariable(interfacenametype, tm, "$&interfacename", false); substitution_performed = true; Delete(interfacenametype); } Delete(strippedtype); Delete(type); return substitution_performed; } /* ----------------------------------------------------------------------------- * substituteClassnameSpecialVariable() * ----------------------------------------------------------------------------- */ void substituteClassnameSpecialVariable(SwigType *classnametype, String *tm, const char *classnamespecialvariable) { String *replacementname; if (SwigType_isenum(classnametype)) { String *enumname = getEnumName(classnametype); if (enumname) { replacementname = Copy(enumname); } else { bool anonymous_enum = (Cmp(classnametype, "enum ") == 0); if (anonymous_enum) { replacementname = NewString("int"); } else { // An unknown enum - one that has not been parsed (neither a C enum forward reference nor a definition) or an ignored enum replacementname = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype)); Replace(replacementname, "enum ", "", DOH_REPLACE_ANY); Setattr(swig_types_hash, replacementname, classnametype); } } } else { String *classname = getProxyName(classnametype); // getProxyName() works for pointers to classes too if (classname) { replacementname = Copy(classname); } else { // use $descriptor if SWIG does not know anything about this type. Note that any typedefs are resolved. replacementname = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype)); // Add to hash table so that the type wrapper classes can be created later Setattr(swig_types_hash, replacementname, classnametype); } } Replaceall(tm, classnamespecialvariable, replacementname); Delete(replacementname); } /* ----------------------------------------------------------------------------- * substituteInterfacenameSpecialVariable() * ----------------------------------------------------------------------------- */ void substituteInterfacenameSpecialVariable(SwigType *interfacenametype, String *tm, const char *interfacenamespecialvariable, bool qualified) { String *interfacename = getInterfaceName(interfacenametype, qualified); if (interfacename) { String *replacementname = Copy(interfacename); Replaceall(tm, interfacenamespecialvariable, replacementname); Delete(replacementname); } } /* ----------------------------------------------------------------------------- * emitTypeWrapperClass() * ----------------------------------------------------------------------------- */ void emitTypeWrapperClass(String *classname, SwigType *type) { Node *n = NewHash(); Setfile(n, input_file); Setline(n, line_number); String *swigtype = NewString(""); File *f_swigtype = getOutputFile(SWIG_output_directory(), classname); addOpenNamespace(0, f_swigtype); // Pure C# baseclass and interfaces const String *pure_baseclass = typemapLookup(n, "csbase", type, WARN_NONE); const String *pure_interfaces = typemapLookup(n, "csinterfaces", type, WARN_NONE); // Emit the class Printv(swigtype, typemapLookup(n, "csimports", type, WARN_NONE), // Import statements "\n", NIL); // Class attributes const String *csattributes = typemapLookup(n, "csattributes", type, WARN_NONE); if (csattributes && *Char(csattributes)) Printf(swigtype, "%s\n", csattributes); Printv(swigtype, typemapLookup(n, "csclassmodifiers", type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers " $csclassname", // Class name and base class (*Char(pure_baseclass) || *Char(pure_interfaces)) ? " : " : "", pure_baseclass, ((*Char(pure_baseclass)) && *Char(pure_interfaces)) ? // Interfaces ", " : "", pure_interfaces, " {", typemapLookup(n, "csbody", type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class typemapLookup(n, "cscode", type, WARN_NONE), // extra C# code "}\n", NIL); Replaceall(swigtype, "$csclassname", classname); Replaceall(swigtype, "$module", module_class_name); Replaceall(swigtype, "$imclassname", imclass_name); Replaceall(swigtype, "$dllimport", dllimport); // For unknown enums Replaceall(swigtype, "$enumvalues", ""); Printv(f_swigtype, swigtype, NIL); addCloseNamespace(0, f_swigtype); if (f_swigtype != f_single_out) Delete(f_swigtype); f_swigtype = NULL; Delete(swigtype); Delete(n); } /* ----------------------------------------------------------------------------- * typemapLookup() * n - for input only and must contain info for Getfile(n) and Getline(n) to work * tmap_method - typemap method name * type - typemap type to lookup * warning - warning number to issue if no typemaps found * typemap_attributes - the typemap attributes are attached to this node and will * also be used for temporary storage if non null * return is never NULL, unlike Swig_typemap_lookup() * ----------------------------------------------------------------------------- */ const String *typemapLookup(Node *n, const_String_or_char_ptr tmap_method, SwigType *type, int warning, Node *typemap_attributes = 0) { Node *node = !typemap_attributes ? NewHash() : typemap_attributes; Setattr(node, "type", type); Setfile(node, Getfile(n)); Setline(node, Getline(n)); const String *tm = Swig_typemap_lookup(tmap_method, node, "", 0); if (!tm) { tm = empty_string; if (warning != WARN_NONE) Swig_warning(warning, Getfile(n), Getline(n), "No %s typemap defined for %s\n", tmap_method, SwigType_str(type, 0)); } if (!typemap_attributes) Delete(node); return tm; } /* ----------------------------------------------------------------------------- * typemapExists() * n - for input only and must contain info for Getfile(n) and Getline(n) to work * tmap_method - typemap method name * type - typemap type to lookup * returns found typemap or NULL if not found * ----------------------------------------------------------------------------- */ const String *typemapExists(Node *n, const_String_or_char_ptr tmap_method, SwigType *type) { Node *node = NewHash(); Setattr(node, "type", type); Setfile(node, Getfile(n)); Setline(node, Getline(n)); const String *tm = Swig_typemap_lookup(tmap_method, node, "", 0); Delete(node); return tm; } /* ----------------------------------------------------------------------------- * canThrow() * Determine whether the code in the typemap can throw a C# exception. * If so, note it for later when excodeSubstitute() is called. * ----------------------------------------------------------------------------- */ void canThrow(Node *n, const String *typemap, Node *parameter) { String *canthrow_attribute = NewStringf("tmap:%s:canthrow", typemap); String *canthrow = Getattr(parameter, canthrow_attribute); if (canthrow) Setattr(n, "csharp:canthrow", "1"); Delete(canthrow_attribute); } /* ----------------------------------------------------------------------------- * excodeSubstitute() * If a method can throw a C# exception, additional exception code is added to * check for the pending exception so that it can then throw the exception. The * $excode special variable is replaced by the exception code in the excode * typemap attribute. * ----------------------------------------------------------------------------- */ void excodeSubstitute(Node *n, String *code, const String *typemap, Node *parameter) { String *excode_attribute = NewStringf("tmap:%s:excode", typemap); String *excode = Getattr(parameter, excode_attribute); if (Getattr(n, "csharp:canthrow")) { int count = Replaceall(code, "$excode", excode); if (count < 1 || !excode) { Swig_warning(WARN_CSHARP_EXCODE, input_file, line_number, "C# exception may not be thrown - no $excode or excode attribute in '%s' typemap.\n", typemap); } } else { Replaceall(code, "$excode", empty_string); } Delete(excode_attribute); } /* ----------------------------------------------------------------------------- * addOpenNamespace() * ----------------------------------------------------------------------------- */ void addOpenNamespace(const String *nspace, File *file) { if (namespce || nspace) { Printf(file, "namespace "); if (namespce) Printv(file, namespce, nspace ? "." : "", NIL); if (nspace) Printv(file, nspace, NIL); Printf(file, " {\n"); } } /* ----------------------------------------------------------------------------- * addCloseNamespace() * ----------------------------------------------------------------------------- */ void addCloseNamespace(const String *nspace, File *file) { if (namespce || nspace) Printf(file, "\n}\n"); } /* ----------------------------------------------------------------------------- * outputDirectory() * * Return the directory to use for generating C# classes/enums and create the * subdirectory (does not create if language specific outdir does not exist). * ----------------------------------------------------------------------------- */ String *outputDirectory(String *nspace) { String *output_directory = Copy(SWIG_output_directory()); if (nspace) { String *nspace_subdirectory = Copy(nspace); Replaceall(nspace_subdirectory, ".", SWIG_FILE_DELIMITER); String *newdir_error = Swig_new_subdirectory(output_directory, nspace_subdirectory); if (newdir_error) { Printf(stderr, "%s\n", newdir_error); Delete(newdir_error); Exit(EXIT_FAILURE); } Printv(output_directory, nspace_subdirectory, SWIG_FILE_DELIMITER, 0); Delete(nspace_subdirectory); } return output_directory; } /*---------------------------------------------------------------------- * Start of director methods *--------------------------------------------------------------------*/ #if 0 /*---------------------------------------------------------------------- * emitDirectorUpcalls() *--------------------------------------------------------------------*/ void emitDirectorUpcalls() { if (n_dmethods) { Wrapper *w = NewWrapper(); String *dmethod_data = NewString(""); int n_methods = 0; Iterator udata_iter; udata_iter = First(dmethods_seq); while (udata_iter.item) { UpcallData *udata = udata_iter.item; Printf(dmethod_data, " { \"%s\", \"%s\" }", Getattr(udata, "imclass_method"), Getattr(udata, "imclass_fdesc")); ++n_methods; udata_iter = Next(udata_iter); if (udata_iter.item) Putc(',', dmethod_data); Putc('\n', dmethod_data); } Wrapper_print(w, f_wrappers); Delete(dmethod_data); Delete(swig_module_init); DelWrapper(w); } } #endif /*---------------------------------------------------------------------- * emitDirectorExtraMethods() * * This is where the director connect method is generated. *--------------------------------------------------------------------*/ void emitDirectorExtraMethods(Node *n) { if (!Swig_directorclass(n)) return; // Output the director connect method: String *norm_name = SwigType_namestr(Getattr(n, "name")); String *dirclassname = directorClassName(n); String *swig_director_connect = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect"); String *wname = Swig_name_wrapper(swig_director_connect); String *sym_name = Getattr(n, "sym:name"); String *qualified_classname = Copy(sym_name); String *nspace = getNSpace(); String *dirClassName = directorClassName(n); SwigType *smart = Getattr(n, "smart"); String *smartptr = smart ? SwigType_namestr(smart) : 0; if (!GetFlag(n, "feature:flatnested")) { for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) { Push(qualified_classname, "."); Push(qualified_classname, Getattr(outer_class, "sym:name")); } } if (nspace) Insert(qualified_classname, 0, NewStringf("%s.", nspace)); Printv(imclass_class_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL); Printf(imclass_class_code, " public static extern void %s(global::System.Runtime.InteropServices.HandleRef jarg1", swig_director_connect); Wrapper *code_wrap = NewWrapper(); Printf(code_wrap->def, "SWIGEXPORT void SWIGSTDCALL %s(void *objarg", wname); if (smartptr) { Printf(code_wrap->code, " %s *obj = (%s *)objarg;\n", smartptr, smartptr); Printf(code_wrap->code, " // Keep a local instance of the smart pointer around while we are using the raw pointer\n"); Printf(code_wrap->code, " // Avoids using smart pointer specific API.\n"); Printf(code_wrap->code, " %s *director = static_cast<%s *>(obj->operator->());\n", dirClassName, dirClassName); } else { Printf(code_wrap->code, " %s *obj = (%s *)objarg;\n", norm_name, norm_name); Printf(code_wrap->code, " %s *director = static_cast<%s *>(obj);\n", dirClassName, dirClassName); } Printf(code_wrap->code, " director->swig_connect_director("); for (int i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *methid = Getattr(udata, "class_methodidx"); Printf(code_wrap->def, ", "); if (i != first_class_dmethod) Printf(code_wrap->code, ", "); Printf(code_wrap->def, "%s::SWIG_Callback%s_t callback%s", dirclassname, methid, methid); Printf(code_wrap->code, "callback%s", methid); Printf(imclass_class_code, ", %s.SwigDelegate%s_%s delegate%s", qualified_classname, sym_name, methid, methid); } Printf(code_wrap->def, ") {\n"); Printf(code_wrap->code, ");\n"); Printf(imclass_class_code, ");\n"); Printf(code_wrap->code, "}\n"); Wrapper_print(code_wrap, f_wrappers); DelWrapper(code_wrap); Delete(wname); Delete(swig_director_connect); Delete(qualified_classname); Delete(dirclassname); } /* --------------------------------------------------------------- * classDirectorMethod() * * Emit a virtual director method to pass a method call on to the * underlying C# object. * * --------------------------------------------------------------- */ int classDirectorMethod(Node *n, Node *parent, String *super) { String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); String *overloaded_name = 0; String *storage = Getattr(n, "storage"); String *value = Getattr(n, "value"); String *decl = Getattr(n, "decl"); String *declaration = NewString(""); String *pre_code = NewString(""); String *post_code = NewString(""); String *terminator_code = NewString(""); String *tm; Parm *p; int i; Wrapper *w = NewWrapper(); ParmList *l = Getattr(n, "parms"); bool is_void = !(Cmp(returntype, "void")); String *qualified_return = 0; bool pure_virtual = (!(Cmp(storage, "virtual")) && !(Cmp(value, "0"))); int status = SWIG_OK; bool output_director = true; String *dirclassname = directorClassName(parent); String *qualified_name = NewStringf("%s::%s", dirclassname, name); SwigType *c_ret_type = NULL; String *jupcall_args = NewString(""); String *callback_typedef_parms = NewString(""); String *delegate_parms = NewString(""); String *proxy_method_types = NewString(""); String *callback_def = NewString(""); String *callback_code = NewString(""); String *imcall_args = NewString(""); bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; // Kludge Alert: functionWrapper sets sym:overload properly, but it // isn't at this point, so we have to manufacture it ourselves. At least // we're consistent with the sym:overload name in functionWrapper. (?? when // does the overloaded method name get set?) if (!ignored_method) overloaded_name = getOverloadedName(n); qualified_return = SwigType_rcaststr(returntype, "c_result"); if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { String *base_typename = SwigType_base(returntype); String *resolved_typename = SwigType_typedef_resolve_all(base_typename); Symtab *symtab = Getattr(n, "sym:symtab"); Node *typenode = Swig_symbol_clookup(resolved_typename, symtab); if (SwigType_ispointer(returntype) || (typenode && Getattr(typenode, "abstracts"))) { /* initialize pointers to something sane. Same for abstract classes when a reference is returned. */ Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } else { /* If returning a reference, initialize the pointer to a sane default - if a C# exception occurs, then the pointer returns something other than a NULL-initialized reference. */ SwigType *noref_type = SwigType_del_reference(Copy(returntype)); String *noref_ltype = SwigType_lstr(noref_type, 0); String *return_ltype = SwigType_lstr(returntype, 0); Wrapper_add_localv(w, "result_default", "static", noref_ltype, "result_default", NIL); Wrapper_add_localv(w, "c_result", return_ltype, "c_result", NIL); Printf(w->code, "result_default = SwigValueInit< %s >();\n", noref_ltype); Printf(w->code, "c_result = &result_default;\n"); Delete(return_ltype); Delete(noref_ltype); Delete(noref_type); } Delete(base_typename); Delete(resolved_typename); } } else { SwigType *vt; vt = cplus_value_type(returntype); if (!vt) { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), NIL); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(vt, "c_result"), NIL); Delete(vt); } } } if (!ignored_method) { /* Create the intermediate class wrapper */ tm = Swig_typemap_lookup("imtype", n, "", 0); if (tm) { String *imtypeout = Getattr(n, "tmap:imtype:out"); // the type in the imtype typemap's out attribute overrides the type in the typemap if (imtypeout) tm = imtypeout; const String *im_directoroutattributes = Getattr(n, "tmap:imtype:directoroutattributes"); if (im_directoroutattributes) { Printf(callback_def, " %s\n", im_directoroutattributes); if (!ignored_method) Printf(director_delegate_definitions, " %s\n", im_directoroutattributes); } Printf(callback_def, " private %s SwigDirectorMethod%s(", tm, overloaded_name); const String *csdirectordelegatemodifiers = Getattr(n, "feature:csdirectordelegatemodifiers"); String *modifiers = (csdirectordelegatemodifiers ? NewStringf("%s%s", csdirectordelegatemodifiers, Len(csdirectordelegatemodifiers) > 0 ? " " : "") : NewStringf("public ")); Printf(director_delegate_definitions, " %sdelegate %s", modifiers, tm); Delete(modifiers); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s\n", SwigType_str(returntype, 0)); } } if ((c_ret_type = Swig_typemap_lookup("ctype", n, "", 0))) { if (!is_void && !ignored_method) { String *jretval_decl = NewStringf("%s jresult", c_ret_type); Wrapper_add_localv(w, "jresult", jretval_decl, "= 0", NIL); Delete(jretval_decl); } } else { Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Swig_director_parms_fixup(l); /* Attach the standard typemaps */ Swig_typemap_attach_parms("out", l, 0); Swig_typemap_attach_parms("ctype", l, 0); Swig_typemap_attach_parms("imtype", l, 0); Swig_typemap_attach_parms("cstype", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("csdirectorin", l, 0); Swig_typemap_attach_parms("directorargout", l, w); /* Preamble code */ if (!ignored_method) Printf(w->code, "if (!swig_callback%s) {\n", overloaded_name); if (!pure_virtual) { String *super_call = Swig_method_call(super, l); if (is_void) { Printf(w->code, "%s;\n", super_call); if (!ignored_method) Printf(w->code, "return;\n"); } else { Printf(w->code, "return %s;\n", super_call); } Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"%s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); if (!is_void) Printf(w->code, "return %s;", qualified_return); else if (!ignored_method) Printf(w->code, "return;\n"); } if (!ignored_method) Printf(w->code, "} else {\n"); /* Go through argument list, convert from native to C# */ for (i = 0, p = l; p; ++i) { /* Is this superfluous? */ while (checkAttribute(p, "tmap:directorin:numinputs", "0")) { p = Getattr(p, "tmap:directorin:next"); } SwigType *pt = Getattr(p, "type"); String *ln = makeParameterName(n, p, i, false); String *c_param_type = NULL; String *c_decl = NewString(""); String *arg = NewString(""); Printf(arg, "j%s", ln); /* And add to the upcall args */ if (i > 0) Printf(jupcall_args, ", "); Printf(jupcall_args, "%s", arg); /* Get parameter's intermediary C type */ if ((c_param_type = Getattr(p, "tmap:ctype"))) { String *ctypeout = Getattr(p, "tmap:ctype:out"); // the type in the ctype typemap's out attribute overrides the type in the typemap if (ctypeout) c_param_type = ctypeout; /* Add to local variables */ Printf(c_decl, "%s %s", c_param_type, arg); if (!ignored_method) Wrapper_add_localv(w, arg, c_decl, (!(SwigType_ispointer(pt) || SwigType_isreference(pt)) ? "" : "= 0"), NIL); /* Add input marshalling code */ if ((tm = Getattr(p, "tmap:directorin"))) { Setattr(p, "emit:directorinput", arg); Replaceall(tm, "$input", arg); Replaceall(tm, "$owner", "0"); if (Len(tm)) if (!ignored_method) Printf(w->code, "%s\n", tm); /* Add C type to callback typedef */ if (i > 0) Printf(callback_typedef_parms, ", "); Printf(callback_typedef_parms, "%s", c_param_type); /* Add parameter to the intermediate class code if generating the * intermediate's upcall code */ if ((tm = Getattr(p, "tmap:imtype"))) { String *imtypeout = Getattr(p, "tmap:imtype:out"); // the type in the imtype typemap's out attribute overrides the type in the typemap if (imtypeout) tm = imtypeout; const String *im_directorinattributes = Getattr(p, "tmap:imtype:directorinattributes"); String *din = Copy(Getattr(p, "tmap:csdirectorin")); if (din) { Replaceall(din, "$module", module_class_name); Replaceall(din, "$imclassname", imclass_name); substituteClassname(pt, din); Replaceall(din, "$iminput", ln); // pre and post attribute support String *pre = Getattr(p, "tmap:csdirectorin:pre"); if (pre) { substituteClassname(pt, pre); Replaceall(pre, "$iminput", ln); if (Len(pre_code) > 0) Printf(pre_code, "\n"); Printv(pre_code, pre, NIL); } String *post = Getattr(p, "tmap:csdirectorin:post"); if (post) { substituteClassname(pt, post); Replaceall(post, "$iminput", ln); if (Len(post_code) > 0) Printf(post_code, "\n"); Printv(post_code, post, NIL); } String *terminator = Getattr(p, "tmap:csdirectorin:terminator"); if (terminator) { substituteClassname(pt, terminator); Replaceall(terminator, "$iminput", ln); if (Len(terminator_code) > 0) Insert(terminator_code, 0, "\n"); Insert(terminator_code, 0, terminator); } if (i > 0) { Printf(delegate_parms, ", "); Printf(proxy_method_types, ", "); Printf(imcall_args, ", "); } Printf(delegate_parms, "%s%s %s", im_directorinattributes ? im_directorinattributes : empty_string, tm, ln); if (Cmp(din, ln)) { Printv(imcall_args, din, NIL); } else Printv(imcall_args, ln, NIL); /* Get the C# parameter type */ if ((tm = Getattr(p, "tmap:cstype"))) { substituteClassname(pt, tm); int flags = DOH_REPLACE_FIRST | DOH_REPLACE_ID_BEGIN | DOH_REPLACE_NOCOMMENT; if (Replace(tm, "ref ", "", flags) || Replace(tm, "ref\t", "", flags)) { Printf(proxy_method_types, "typeof(%s).MakeByRefType()", tm); } else if (Replace(tm, "out ", "", flags) || Replace(tm, "out\t", "", flags)) { Printf(proxy_method_types, "typeof(%s).MakeByRefType()", tm); } else { Printf(proxy_method_types, "typeof(%s)", tm); } } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(pt, 0)); } } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSDIRECTORIN_UNDEF, input_file, line_number, "No csdirectorin typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number, "No imtype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } p = Getattr(p, "tmap:directorin:next"); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSDIRECTORIN_UNDEF, input_file, line_number, "No or improper directorin typemap defined for argument %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); p = nextSibling(p); output_director = false; } } else { Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s for use in %s::%s (skipping director method)\n", SwigType_str(pt, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; p = nextSibling(p); } Delete(ln); Delete(arg); Delete(c_decl); } /* header declaration, start wrapper definition */ String *target; SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Add any exception specifications to the methods in the director class if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = NULL; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } Printf(w->def, "%s", SwigType_str(Getattr(p, "type"), 0)); Printf(declaration, "%s", SwigType_str(Getattr(p, "type"), 0)); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); /* Finish off the inherited upcall's definition */ Printf(callback_def, "%s)", delegate_parms); Printf(callback_def, " {\n"); /* Emit the intermediate class's upcall to the actual class */ String *upcall = NewStringf("%s(%s)", symname, imcall_args); if ((tm = Swig_typemap_lookup("csdirectorout", n, "", 0))) { substituteClassname(returntype, tm); Replaceall(tm, "$cscall", upcall); if (!is_void) Insert(tm, 0, "return "); Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap // pre and post attribute support bool is_pre_code = Len(pre_code) > 0; bool is_post_code = Len(post_code) > 0; bool is_terminator_code = Len(terminator_code) > 0; if (is_pre_code && is_post_code) Printf(callback_code, "%s\n try {\n %s;\n } finally {\n%s\n }\n", pre_code, tm, post_code); else if (is_pre_code) Printf(callback_code, "%s\n %s;\n", pre_code, tm); else if (is_post_code) Printf(callback_code, " try {\n %s;\n } finally {\n%s\n }\n", tm, post_code); else Printf(callback_code, " %s;\n", tm); if (is_terminator_code) Printv(callback_code, "\n", terminator_code, NIL); } Printf(callback_code, " }\n"); Delete(upcall); if (!ignored_method) { if (!is_void) Printf(w->code, "jresult = (%s) ", c_ret_type); Printf(w->code, "swig_callback%s(%s);\n", overloaded_name, jupcall_args); if (!is_void) { String *jresult_str = NewString("jresult"); String *result_str = NewString("c_result"); /* Copy jresult into c_result... */ if ((tm = Swig_typemap_lookup("directorout", n, result_str, w))) { Replaceall(tm, "$input", jresult_str); Replaceall(tm, "$result", result_str); Printf(w->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s used in %s::%s (skipping director method)\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); output_director = false; } Delete(jresult_str); Delete(result_str); } /* Marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout"))) { canThrow(n, "directorargout", p); Replaceall(tm, "$result", "jresult"); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } /* Terminate wrapper code */ Printf(w->code, "}\n"); if (!is_void) Printf(w->code, "return %s;", qualified_return); } Printf(w->code, "}"); // We expose virtual protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK && output_director) { if (!is_void) { Replaceall(w->code, "$null", qualified_return); } else { Replaceall(w->code, "$null", ""); } Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!ignored_method) Printv(director_delegate_callback, "\n", callback_def, callback_code, NIL); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } if (!ignored_method) { /* Emit the actual upcall through */ String *member_name = Swig_name_member(getNSpace(), getClassPrefix(), overloaded_name); String *imclass_dmethod = NewStringf("SwigDirector_%s", member_name); UpcallData *udata = addUpcallMethod(imclass_dmethod, symname, decl, overloaded_name); String *methid = Getattr(udata, "class_methodidx"); Setattr(n, "upcalldata", udata); /* Printf(stdout, "setting upcalldata, nodeType: %s %s::%s %p\n", nodeType(n), classname, Getattr(n, "name"), n); */ Printf(director_callback_typedefs, " typedef %s (SWIGSTDCALL* SWIG_Callback%s_t)(", c_ret_type, methid); Printf(director_callback_typedefs, "%s);\n", callback_typedef_parms); Printf(director_callbacks, " SWIG_Callback%s_t swig_callback%s;\n", methid, overloaded_name); Printf(director_delegate_definitions, " SwigDelegate%s_%s(%s);\n", classname, methid, delegate_parms); Printf(director_delegate_instances, " private SwigDelegate%s_%s swigDelegate%s;\n", classname, methid, methid); Printf(director_method_types, " private static global::System.Type[] swigMethodTypes%s = new global::System.Type[] { %s };\n", methid, proxy_method_types); Printf(director_connect_parms, "SwigDirector%s%s delegate%s", classname, methid, methid); Delete(imclass_dmethod); Delete(member_name); } Delete(pre_code); Delete(post_code); Delete(terminator_code); Delete(qualified_return); Delete(declaration); Delete(callback_typedef_parms); Delete(delegate_parms); Delete(proxy_method_types); Delete(callback_def); Delete(callback_code); Delete(dirclassname); DelWrapper(w); return status; } /* ------------------------------------------------------------ * classDirectorConstructor() * ------------------------------------------------------------ */ int classDirectorConstructor(Node *n) { Node *parent = parentNode(n); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *dirclassname = directorClassName(parent); String *sub = NewString(""); Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms; int argidx = 0; /* Assign arguments to superclass's parameters, if not already done */ for (p = superparms; p; p = nextSibling(p)) { String *pname = Getattr(p, "name"); if (!pname) { pname = NewStringf("arg%d", argidx++); Setattr(p, "name", pname); } } // TODO: Is this copy needed? parms = CopyParmList(superparms); if (!Getattr(n, "defaultargs")) { /* constructor */ { String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, dirclassname, parms, 0); String *call = Swig_csuperclass_call(0, basetype, superparms); Printf(f_directors, "%s::%s : %s, %s {\n", dirclassname, target, call, Getattr(parent, "director:ctor")); Printf(f_directors, " swig_init_callbacks();\n"); Printf(f_directors, "}\n\n"); Delete(target); Delete(call); } /* constructor header */ { String *target = Swig_method_decl(0, decl, dirclassname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(supername); Delete(parms); Delete(dirclassname); return Language::classDirectorConstructor(n); } /* ------------------------------------------------------------ * classDirectorDefaultConstructor() * ------------------------------------------------------------ */ int classDirectorDefaultConstructor(Node *n) { String *dirclassname = directorClassName(n); String *classtype = SwigType_namestr(Getattr(n, "name")); Wrapper *w = NewWrapper(); Printf(w->def, "%s::%s() : %s {", dirclassname, dirclassname, Getattr(n, "director:ctor")); Printf(w->code, "}\n"); Wrapper_print(w, f_directors); Printf(f_directors_h, " %s();\n", dirclassname); DelWrapper(w); Delete(classtype); Delete(dirclassname); return Language::classDirectorDefaultConstructor(n); } /* ------------------------------------------------------------ * classDirectorInit() * ------------------------------------------------------------ */ int classDirectorInit(Node *n) { Delete(none_comparison); none_comparison = NewString(""); // not used Delete(director_ctor_code); director_ctor_code = NewString("$director_new"); directorDeclaration(n); Printf(f_directors_h, "%s {\n", Getattr(n, "director:decl")); Printf(f_directors_h, "\npublic:\n"); /* Keep track of the director methods for this class */ first_class_dmethod = curr_class_dmethod = n_dmethods; director_callback_typedefs = NewString(""); director_callbacks = NewString(""); director_delegate_callback = NewString(""); director_delegate_definitions = NewString(""); director_delegate_instances = NewString(""); director_method_types = NewString(""); director_connect_parms = NewString(""); return Language::classDirectorInit(n); } int classDeclaration(Node *n) { String *old_director_callback_typedefs = director_callback_typedefs; String *old_director_callbacks = director_callbacks; String *old_director_delegate_callback = director_delegate_callback; String *old_director_delegate_definitions = director_delegate_definitions; String *old_director_delegate_instances = director_delegate_instances; String *old_director_method_types = director_method_types; String *old_director_connect_parms = director_connect_parms; int ret = Language::classDeclaration(n); // these variables are deleted in emitProxyClassDefAndCPPCasts, hence no Delete here director_callback_typedefs = old_director_callback_typedefs; director_callbacks = old_director_callbacks; director_delegate_callback = old_director_delegate_callback; director_delegate_definitions = old_director_delegate_definitions; director_delegate_instances = old_director_delegate_instances; director_method_types = old_director_method_types; director_connect_parms = old_director_connect_parms; return ret; } /* ---------------------------------------------------------------------- * classDirectorDestructor() * ---------------------------------------------------------------------- */ int classDirectorDestructor(Node *n) { Node *current_class = getCurrentClass(); String *dirclassname = directorClassName(current_class); Wrapper *w = NewWrapper(); if (Getattr(n, "noexcept")) { Printf(f_directors_h, " virtual ~%s() noexcept;\n", dirclassname); Printf(w->def, "%s::~%s() noexcept {\n", dirclassname, dirclassname); } else if (Getattr(n, "throw")) { Printf(f_directors_h, " virtual ~%s() throw();\n", dirclassname); Printf(w->def, "%s::~%s() throw() {\n", dirclassname, dirclassname); } else { Printf(f_directors_h, " virtual ~%s();\n", dirclassname); Printf(w->def, "%s::~%s() {\n", dirclassname, dirclassname); } Printv(w->code, "}\n", NIL); Wrapper_print(w, f_directors); DelWrapper(w); Delete(dirclassname); return SWIG_OK; } /* ------------------------------------------------------------ * classDirectorEnd() * ------------------------------------------------------------ */ int classDirectorEnd(Node *n) { int i; String *dirclassname = directorClassName(n); Wrapper *w = NewWrapper(); if (Len(director_callback_typedefs) > 0) { Printf(f_directors_h, "\n%s", director_callback_typedefs); } Printf(f_directors_h, " void swig_connect_director("); Printf(w->def, "void %s::swig_connect_director(", dirclassname); for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *methid = Getattr(udata, "class_methodidx"); String *overname = Getattr(udata, "overname"); Printf(f_directors_h, "SWIG_Callback%s_t callback%s", methid, overname); Printf(w->def, "SWIG_Callback%s_t callback%s", methid, overname); Printf(w->code, "swig_callback%s = callback%s;\n", overname, overname); if (i != curr_class_dmethod - 1) { Printf(f_directors_h, ", "); Printf(w->def, ", "); } } Printf(f_directors_h, ");\n"); Printf(w->def, ") {"); if (Len(director_callbacks) > 0) { Printf(f_directors_h, "\nprivate:\n%s", director_callbacks); } Printf(f_directors_h, " void swig_init_callbacks();\n"); Printf(f_directors_h, "};\n\n"); Printf(w->code, "}\n\n"); Printf(w->code, "void %s::swig_init_callbacks() {\n", dirclassname); for (i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); String *overname = Getattr(udata, "overname"); Printf(w->code, "swig_callback%s = 0;\n", overname); } Printf(w->code, "}"); Wrapper_print(w, f_directors); DelWrapper(w); Delete(dirclassname); return Language::classDirectorEnd(n); } /* -------------------------------------------------------------------- * classDirectorDisown() * ------------------------------------------------------------------*/ virtual int classDirectorDisown(Node *n) { (void) n; return SWIG_OK; } /*---------------------------------------------------------------------- * extraDirectorProtectedCPPMethodsRequired() *--------------------------------------------------------------------*/ bool extraDirectorProtectedCPPMethodsRequired() const { return false; } /*---------------------------------------------------------------------- * directorDeclaration() * * Generate the director class's declaration * e.g. "class SwigDirector_myclass : public myclass, public Swig::Director {" *--------------------------------------------------------------------*/ void directorDeclaration(Node *n) { String *base = Getattr(n, "classtype"); String *class_ctor = NewString("Swig::Director()"); String *dirclassname = directorClassName(n); String *declaration = Swig_class_declaration(n, dirclassname); Printf(declaration, " : public %s, public Swig::Director", base); // Stash stuff for later. Setattr(n, "director:decl", declaration); Setattr(n, "director:ctor", class_ctor); Delete(dirclassname); } /* ------------------------------------------------------------ * have_docstring() * * Check for Doxygen comments *--------------------------------------------------------------------*/ bool have_docstring(Node *n) { /* autodoc and docstring features not supported in C# String *str = Getattr(n, "feature:docstring"); return ((str && Len(str) > 0) || (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc")) || (doxygen && doxygenTranslator->hasDocumentation(n)) ); */ return doxygen && doxygenTranslator->hasDocumentation(n); } /* ------------------------------------------------------------ * docstring() * * Get documentation comments, if any * * Return new documentation string to be deleted by caller (never NULL but * may be empty if there is no docstring). *--------------------------------------------------------------------*/ String *docstring(Node *n, const char *indent = "") { String *docstr = NULL; if (doxygen && doxygenTranslator->hasDocumentation(n)) { docstr = doxygenTranslator->getDocumentation(n, 0); } if (!docstr) docstr = NewString(""); // If there is more than one line then make docstrings like this: // // This is line1 // And here is line2 followed by the rest of them // // otherwise, put it all on a single line if (Strchr(docstr, '\n')) { String *tmp = NewString(""); Append(tmp, indent_docstring(docstr, indent)); Delete(docstr); docstr = tmp; } else { String *tmp = NewString(indent); Append(tmp, "/// "); Append(tmp, docstr); Append(tmp, "\n"); Delete(docstr); docstr = tmp; } return docstr; } /* ------------------------------------------------------------ * indent_docstring() * * Format (indent) a CSharp docstring. * Remove leading whitespace from 'code' and re-indent using * the indentation string in 'indent'. * ------------------------------------------------------------ */ String *indent_docstring(const String *code, const char *indent) { String *out = NewString(""); String *temp; char *t; if (!indent) indent = ""; temp = NewString(code); t = Char(temp); if (*t == '{') { Delitem(temp, 0); Delitem(temp, DOH_END); } /* Split the input text into lines */ List *clist = SplitLines(temp); Delete(temp); Iterator si; int truncate_characters_count = INT_MAX; for (si = First(clist); si.item; si = Next(si)) { const char *c = Char(si.item); int i; for (i = 0; isspace((unsigned char)c[i]); i++) { // Scan forward until we find a non-space (which may be a null byte). } char ch = c[i]; if (ch) { // Found a line which isn't just whitespace if (i < truncate_characters_count) truncate_characters_count = i; } } if (truncate_characters_count == INT_MAX) truncate_characters_count = 0; for (si = First(clist); si.item; si = Next(si)) { const char *c = Char(si.item); int i; for (i = 0; isspace((unsigned char)c[i]); i++) { // Scan forward until we find a non-space (which may be a null byte). } char ch = c[i]; if (!ch) { // Line is just whitespace - emit an empty line. Printv(out, indent, "///", NIL); Putc('\n', out); continue; } Printv(out, indent, "/// ", c + truncate_characters_count, "\n", NIL); } Delete(clist); return out; } /*---------------------------------------------------------------------- * nestedClassesSupport() *--------------------------------------------------------------------*/ NestedClassSupport nestedClassesSupport() const { return NCS_Full; } }; /* class CSHARP */ /* ----------------------------------------------------------------------------- * swig_csharp() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_csharp() { return new CSHARP(); } extern "C" Language *swig_csharp(void) { return new_swig_csharp(); } /* ----------------------------------------------------------------------------- * Static member variables * ----------------------------------------------------------------------------- */ const char *CSHARP::usage = "\ C# Options (available with -csharp)\n\ -dllimport
- Override DllImport attribute name to
\n\ -namespace - Generate wrappers into C# namespace \n\ -noproxy - Generate the low-level functional interface instead\n\ of proxy classes\n\ -oldvarnames - Old intermediary method names for variable wrappers\n\ -outfile - Write all C# into a single located in the output directory\n\ \n"; swig-4.4.0/Source/Modules/scilab.cxx0000664000175000017500000013170215075443613017215 0ustar williamwilliam/* ---------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * scilab.cxx * * Scilab language module for SWIG. * --------------------------------------------------------------------------*/ #include "swigmod.h" #include #include static const int SCILAB_IDENTIFIER_NAME_CHAR_MAX = 24; static const char *usage = (char *) " \ Scilab options (available with -scilab)\n \ -builder - Generate a Scilab builder script\n \ -buildercflags - Add to the builder compiler flags\n \ -builderflagscript - Set the Scilab script to use by builder to configure the build flags\n \ -builderldflags - Add to the builder linker flags\n \ -buildersources - Add the (comma separated) files to the builder sources\n \ -builderverbositylevel - Set the builder verbosity level to (default 0: off, 2: high)\n \ -gatewayxml - Generate gateway xml with the given \n \ -gatewayxml6 - Generate gateway xml for Scilab 6\n \ \n"; class SCILAB:public Language { protected: /* General objects used for holding the strings */ File *beginSection; File *runtimeSection; File *headerSection; File *wrappersSection; File *initSection; String *variablesCode; bool generateBuilder; File *builderFile; String *builderCode; String *builderCode5; String *builderCode6; int builderFunctionCount; List *sourceFileList; List *cflags; List *ldflags; String *verboseBuildLevel; String *buildFlagsScript; String *gatewayHeader; String *gatewayHeaderV5; String *gatewayHeaderV6; bool createGatewayXML; File *gatewayXMLFile; String *gatewayXML; String *gatewayID; int primitiveID; bool createGatewayXMLV6; File *gatewayXMLFileV6; String *gatewayXMLV6; bool createLoader; File *loaderFile; String *loaderScript; String *loaderScript5; String *loaderScript6; int loaderFunctionCount; public: /* ------------------------------------------------------------------------ * main() * ----------------------------------------------------------------------*/ virtual void main(int argc, char *argv[]) { generateBuilder = false; sourceFileList = NewList(); cflags = NewList(); ldflags = NewList(); verboseBuildLevel = NULL; buildFlagsScript = NULL; gatewayHeader = NULL; gatewayHeaderV5 = NULL; gatewayHeaderV6 = NULL; createGatewayXML = false; gatewayXML = NULL; gatewayXMLFile = NULL; gatewayID = NULL; createGatewayXMLV6 = false; gatewayXMLV6 = NULL; gatewayXMLFileV6 = NULL; createLoader = true; loaderFile = NULL; loaderScript = NULL; /* Manage command line arguments */ for (int argIndex = 1; argIndex < argc; argIndex++) { if (argv[argIndex] != NULL) { if (strcmp(argv[argIndex], "-help") == 0) { Printf(stdout, "%s\n", usage); } else if (strcmp(argv[argIndex], "-builder") == 0) { Swig_mark_arg(argIndex); generateBuilder = true; createLoader = false; } else if (strcmp(argv[argIndex], "-buildersources") == 0) { if (argv[argIndex + 1] != NULL) { Swig_mark_arg(argIndex); char *sourceFile = strtok(argv[argIndex + 1], ","); while (sourceFile != NULL) { Insert(sourceFileList, Len(sourceFileList), sourceFile); sourceFile = strtok(NULL, ","); } Swig_mark_arg(argIndex + 1); } } else if (strcmp(argv[argIndex], "-buildercflags") == 0) { Swig_mark_arg(argIndex); if (argv[argIndex + 1] != NULL) { Insert(cflags, Len(cflags), argv[argIndex + 1]); Swig_mark_arg(argIndex + 1); } } else if (strcmp(argv[argIndex], "-builderldflags") == 0) { Swig_mark_arg(argIndex); if (argv[argIndex + 1] != NULL) { Insert(ldflags, Len(ldflags), argv[argIndex + 1]); Swig_mark_arg(argIndex + 1); } } else if (strcmp(argv[argIndex], "-builderverbositylevel") == 0) { Swig_mark_arg(argIndex); verboseBuildLevel = NewString(argv[argIndex + 1]); Swig_mark_arg(argIndex + 1); } else if (strcmp(argv[argIndex], "-builderflagscript") == 0) { Swig_mark_arg(argIndex); buildFlagsScript = NewString(argv[argIndex + 1]); Swig_mark_arg(argIndex + 1); } else if (strcmp(argv[argIndex], "-gatewayxml") == 0) { Swig_mark_arg(argIndex); createGatewayXML = true; gatewayID = NewString(argv[argIndex + 1]); Swig_mark_arg(argIndex + 1); } else if (strcmp(argv[argIndex], "-gatewayxml6") == 0) { Swig_mark_arg(argIndex); createGatewayXMLV6 = true; } } } if (verboseBuildLevel == NULL) { verboseBuildLevel = NewString("0"); } /* Set language-specific subdirectory in SWIG library */ SWIG_library_directory("scilab"); /* Add a symbol to the parser for conditional compilation */ Preprocessor_define("SWIGSCILAB 1", 0); /* Set scilab configuration file */ SWIG_config_file("scilab.swg"); allow_overloading(); } /* ------------------------------------------------------------------------ * top() * ----------------------------------------------------------------------*/ virtual int top(Node *node) { /* Get the module name */ String *gatewayName = Getattr(node, "name"); // Set library name String *gatewayLibraryName = NewStringf("lib%s", gatewayName); /* Get the output file name */ String *outputFilename = Getattr(node, "outfile"); /* Initialize I/O */ beginSection = NewFile(outputFilename, "w", SWIG_output_files()); if (!beginSection) { FileErrorDisplay(outputFilename); Exit(EXIT_FAILURE); } runtimeSection = NewString(""); initSection = NewString(""); headerSection = NewString(""); wrappersSection = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("begin", beginSection); Swig_register_filebyname("header", headerSection); Swig_register_filebyname("wrapper", wrappersSection); Swig_register_filebyname("runtime", runtimeSection); Swig_register_filebyname("init", initSection); /* Output module initialization code */ Swig_banner(beginSection); Swig_obligatory_macros(runtimeSection, "SCILAB"); // Gateway header source merged with wrapper source in nobuilder mode if (!generateBuilder) startGatewayHeader(gatewayLibraryName); // Create builder file if required if (generateBuilder) { createBuilderFile(outputFilename); } // Create gateway XML if required if (createGatewayXML) { createGatewayXMLFile(gatewayName); } // Create gateway XML V6 if required if (createGatewayXMLV6) { createGatewayXMLFileV6(gatewayName); } // Create loader script if required if (createLoader) { createLoaderFile(gatewayLibraryName); } // Module initialization function String *smallFunctionName = createSmallIdentifierName(gatewayName, SCILAB_IDENTIFIER_NAME_CHAR_MAX - 5); String *gatewayInitFunctionName = NewStringf("%s_Init", gatewayName); String *gatewayInitSmallFunctionName = NewStringf("%s_Init", smallFunctionName); String *wrapperFunctionName = NewStringf("SWIG_%s_Init", gatewayName); /* Add initialization function to builder table */ addFunctionToScilab(gatewayInitFunctionName, gatewayInitSmallFunctionName, wrapperFunctionName); // Add helper functions to builder table addHelperFunctions(); // Open Scilab wrapper variables creation function variablesCode = NewString(""); Printf(variablesCode, "int SWIG_CreateScilabVariables(void *_pvApiCtx) {"); /* Emit code for children */ if (CPlusPlus) { Printf(wrappersSection, "extern \"C\" {\n"); } Language::top(node); if (CPlusPlus) { Printf(wrappersSection, "}\n"); } // Close Scilab wrapper variables creation function Printf(variablesCode, " return SWIG_OK;\n}\n"); // Add Builder footer code and save if (generateBuilder) { saveBuilderFile(gatewayLibraryName); } /* Close the init function and rename with module name */ Printf(initSection, "return 0;\n}\n"); Replaceall(initSection, "", gatewayName); /* Write all to the wrapper file */ SwigType_emit_type_table(runtimeSection, wrappersSection); // Declare pointer types, ... (Ex: SWIGTYPE_p_p_double) // Gateway header source merged with wrapper source in nobuilder mode if (!generateBuilder) { terminateGatewayHeader(gatewayLibraryName); Printv(initSection, gatewayHeader, NIL); } Dump(runtimeSection, beginSection); Dump(headerSection, beginSection); Dump(wrappersSection, beginSection); Dump(variablesCode, beginSection); Wrapper_pretty_print(initSection, beginSection); if (createGatewayXML) { saveGatewayXMLFile(); } if (createGatewayXMLV6) { saveGatewayXMLFileV6(); } if (createLoader) { saveLoaderFile(gatewayLibraryName); } /* Cleanup files */ Delete(runtimeSection); Delete(headerSection); Delete(wrappersSection); Delete(initSection); Delete(beginSection); Delete(sourceFileList); Delete(cflags); Delete(ldflags); return SWIG_OK; } /* ------------------------------------------------------------------------ * emitBanner() * ----------------------------------------------------------------------*/ void emitBanner(File *f) { Printf(f, "// ----------------------------------------------------------------------------\n"); Swig_banner_target_lang(f, "// "); Printf(f, "// ----------------------------------------------------------------------------- */\n\n"); } /* ------------------------------------------------------------------------ * functionWrapper() * ----------------------------------------------------------------------*/ virtual int functionWrapper(Node *node) { /* Get some useful attributes of this function */ String *functionName = Getattr(node, "sym:name"); String *smallFunctionName = createSmallIdentifierName(functionName); SwigType *functionReturnType = Getattr(node, "type"); ParmList *functionParamsList = Getattr(node, "parms"); int paramIndex = 0; // Used for loops over ParmsList Parm *param = NULL; // Used for loops over ParamsList /* Create the wrapper object */ Wrapper *wrapper = NewWrapper(); /* Create the function wrapper name */ String *wrapperName = Swig_name_wrapper(functionName); /* Deal with overloading */ String *overloadedName = Copy(wrapperName); /* Determine whether the function is overloaded or not */ bool isOverloaded = ! !Getattr(node, "sym:overloaded"); /* Determine whether the function is the last overloaded */ bool isLastOverloaded = isOverloaded && !Getattr(node, "sym:nextSibling"); if (!isOverloaded && !addSymbol(functionName, node)) { DelWrapper(wrapper); return SWIG_ERROR; } if (isOverloaded) { Append(overloadedName, Getattr(node, "sym:overname")); } /* Write the wrapper function definition (standard Scilab gateway function prototype) */ Printv(wrapper->def, "SWIGEXPORT int ", overloadedName, "(SWIG_GatewayParameters) {", NIL); /* Emit all of the local variables for holding arguments */ // E.g.: double arg1; emit_parameter_variables(functionParamsList, wrapper); /* Attach typemaps to the parameter list */ // Add local variables used in typemaps (iRows, iCols, ...) emit_attach_parmmaps(functionParamsList, wrapper); Setattr(node, "wrap:parms", functionParamsList); /* Check input/output arguments count */ int maxInputArguments = emit_num_arguments(functionParamsList); int minInputArguments = emit_num_required(functionParamsList); int minOutputArguments = 0; int maxOutputArguments = 0; if (!emit_isvarargs(functionParamsList)) { Printf(wrapper->code, "SWIG_CheckInputArgument(pvApiCtx, $mininputarguments, $maxinputarguments);\n"); } else { Printf(wrapper->code, "SWIG_CheckInputArgumentAtLeast(pvApiCtx, $mininputarguments-1);\n"); } Printf(wrapper->code, "SWIG_CheckOutputArgument(pvApiCtx, $minoutputarguments, $maxoutputarguments);\n"); /* Set context */ Printf(wrapper->code, "SWIG_Scilab_SetFuncName(fname);\n"); Printf(wrapper->code, "SWIG_Scilab_SetApiContext(pvApiCtx);\n"); /* Write typemaps(in) */ for (paramIndex = 0, param = functionParamsList; paramIndex < maxInputArguments; ++paramIndex) { // Ignore parameter if the typemap specifies numinputs=0 while (checkAttribute(param, "tmap:in:numinputs", "0")) { param = Getattr(param, "tmap:in:next"); } SwigType *paramType = Getattr(param, "type"); String *paramTypemap = Getattr(param, "tmap:in"); if (paramTypemap) { // Replace $input by the position on Scilab stack String *source = NewString(""); Printf(source, "%d", paramIndex + 1); Setattr(param, "emit:input", source); Replaceall(paramTypemap, "$input", Getattr(param, "emit:input")); if (Getattr(param, "wrap:disown") || (Getattr(param, "tmap:in:disown"))) { Replaceall(paramTypemap, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(paramTypemap, "$disown", "0"); } if (paramIndex >= minInputArguments) { /* Optional input argument management */ Printf(wrapper->code, "if (SWIG_NbInputArgument(pvApiCtx) > %d) {\n%s\n}\n", paramIndex, paramTypemap); } else { Printf(wrapper->code, "%s\n", paramTypemap); } param = Getattr(param, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(paramType, 0)); break; } } /* TODO write constraints */ Setattr(node, "wrap:name", overloadedName); /* Emit the function call */ Swig_director_emit_dynamic_cast(node, wrapper); String *functionActionCode = emit_action(node); /* Insert the return variable */ emit_return_variable(node, functionReturnType, wrapper); /* Return the function value if necessary */ String *functionReturnTypemap = Swig_typemap_lookup_out("out", node, Swig_cresult_name(), wrapper, functionActionCode); if (functionReturnTypemap) { // Result is actually the position of output value on stack if (Len(functionReturnTypemap) > 0) { Printf(wrapper->code, "SWIG_Scilab_SetOutputPosition(%d);\n", 1); } Replaceall(functionReturnTypemap, "$result", "1"); if (GetFlag(node, "feature:new")) { Replaceall(functionReturnTypemap, "$owner", "1"); } else { Replaceall(functionReturnTypemap, "$owner", "0"); } Printf(wrapper->code, "%s\n", functionReturnTypemap); /* If the typemap is not empty, the function return one more argument than the typemaps gives */ if (Len(functionReturnTypemap) > 0) { minOutputArguments++; maxOutputArguments++; } Delete(functionReturnTypemap); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(functionReturnType, 0), functionName); } /* Write typemaps(out) */ for (param = functionParamsList; param;) { String *paramTypemap = Getattr(param, "tmap:argout"); if (paramTypemap) { minOutputArguments++; maxOutputArguments++; Printf(wrapper->code, "SWIG_Scilab_SetOutputPosition(%d);\n", minOutputArguments); String *result = NewString(""); Printf(result, "%d", minOutputArguments); Replaceall(paramTypemap, "$result", result); Printf(wrapper->code, "%s\n", paramTypemap); Delete(paramTypemap); param = Getattr(param, "tmap:argout:next"); } else { param = nextSibling(param); } } /* Add cleanup code */ for (param = functionParamsList; param;) { String *tm; if ((tm = Getattr(param, "tmap:freearg"))) { if (tm && (Len(tm) != 0)) { Printf(wrapper->code, "%s\n", tm); } param = Getattr(param, "tmap:freearg:next"); } else { param = nextSibling(param); } } /* See if there is any return cleanup code */ String *tm; if ((tm = Swig_typemap_lookup("ret", node, Swig_cresult_name(), 0))) { Printf(wrapper->code, "%s\n", tm); Delete(tm); } /* Close the function(ok) */ Printv(wrapper->code, "return SWIG_OK;\n", NIL); Printv(wrapper->code, "}\n", NIL); /* Add the failure cleanup code */ /* TODO */ /* Final substitutions if applicable */ bool isvoid = !Cmp(functionReturnType, "void"); Replaceall(wrapper->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(wrapper->code, "$symname", functionName); /* Set CheckInputArgument and CheckOutputArgument input arguments */ if (maxOutputArguments < 1) { maxOutputArguments = 1; } if (minOutputArguments == 1) { minOutputArguments = 0; } String *argnumber = NewString(""); Printf(argnumber, "%d", minInputArguments); Replaceall(wrapper->code, "$mininputarguments", argnumber); argnumber = NewString(""); Printf(argnumber, "%d", maxInputArguments); Replaceall(wrapper->code, "$maxinputarguments", argnumber); argnumber = NewString(""); Printf(argnumber, "%d", minOutputArguments); Replaceall(wrapper->code, "$minoutputarguments", argnumber); argnumber = NewString(""); Printf(argnumber, "%d", maxOutputArguments); Replaceall(wrapper->code, "$maxoutputarguments", argnumber); /* Dump the function out */ Wrapper_print(wrapper, wrappersSection); /* Update builder.sce contents */ if (isLastOverloaded) { addFunctionToScilab(functionName, smallFunctionName, wrapperName); dispatchFunction(node); } if (!isOverloaded) { addFunctionToScilab(functionName, smallFunctionName, wrapperName); } /* tidy up */ Delete(overloadedName); Delete(wrapperName); DelWrapper(wrapper); return SWIG_OK; } /* ----------------------------------------------------------------------- * dispatchFunction() * ----------------------------------------------------------------------- */ void dispatchFunction(Node *node) { Wrapper *wrapper = NewWrapper(); String *functionName = Getattr(node, "sym:name"); String *wrapperName = Swig_name_wrapper(functionName); int maxargs = 0; bool check_emitted = false; /* Generate the dispatch function */ String *dispatch = Swig_overload_dispatch(node, "return %s(SWIG_GatewayArguments);", &maxargs, &check_emitted); String *tmp = NewString(""); Printv(wrapper->def, "SWIGEXPORT int ", wrapperName, "(SWIG_GatewayParameters) {\n", NIL); /* Get the number of the parameters */ Wrapper_add_local(wrapper, "argc", "int argc = SWIG_NbInputArgument(pvApiCtx)"); Printf(tmp, "int argv[%d] = {", maxargs); for (int j = 0; j < maxargs; ++j) { Printf(tmp, "%s%d", j ? "," : " ", j + 1); } Printf(tmp, "}"); Wrapper_add_local(wrapper, "argv", tmp); Printf(wrapper->code, "SWIG_Scilab_SetApiContext(pvApiCtx);\n"); /* Dump the dispatch function */ Printv(wrapper->code, dispatch, "\n", NIL); Printf(wrapper->code, "Scierror(999, _(\"No matching function for overload\"));\n"); Printf(wrapper->code, "return SWIG_ERROR;\n"); Printv(wrapper->code, "}\n", NIL); Wrapper_print(wrapper, wrappersSection); Delete(tmp); DelWrapper(wrapper); Delete(dispatch); Delete(wrapperName); } /* ----------------------------------------------------------------------- * variableWrapper() * ----------------------------------------------------------------------- */ virtual int variableWrapper(Node *node) { /* Get information about variable */ String *origVariableName = Getattr(node, "name"); // Ex: Shape::nshapes String *variableName = Getattr(node, "sym:name"); // Ex; Shape_nshapes (can be used for function names, ...) String *smallVariableName = createSmallIdentifierName(variableName, SCILAB_IDENTIFIER_NAME_CHAR_MAX - 4); /* Manage GET function */ Wrapper *getFunctionWrapper = NewWrapper(); String *getFunctionName = Swig_name_get(NSPACE_TODO, variableName); String *scilabGetFunctionName = Swig_name_get(NSPACE_TODO, variableName); String *scilabGetSmallFunctionName = Swig_name_get(NSPACE_TODO, smallVariableName); Setattr(node, "wrap:name", getFunctionName); Printv(getFunctionWrapper->def, "SWIGEXPORT int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL); /* Check the number of input and output */ Printf(getFunctionWrapper->def, "SWIG_CheckInputArgument(pvApiCtx, 0, 0);\n"); Printf(getFunctionWrapper->def, "SWIG_CheckOutputArgument(pvApiCtx, 0, 1);\n"); Printf(getFunctionWrapper->def, "SWIG_Scilab_SetApiContext(pvApiCtx);\n"); String *varoutTypemap = Swig_typemap_lookup("varout", node, origVariableName, 0); if (varoutTypemap != NULL) { Printf(getFunctionWrapper->code, "SWIG_Scilab_SetOutputPosition(%d);\n", 1); Replaceall(varoutTypemap, "$value", origVariableName); Replaceall(varoutTypemap, "$result", "1"); emit_action_code(node, getFunctionWrapper->code, varoutTypemap); Delete(varoutTypemap); } Append(getFunctionWrapper->code, "return SWIG_OK;\n"); Append(getFunctionWrapper->code, "}\n"); Wrapper_print(getFunctionWrapper, wrappersSection); /* Add function to builder table */ addFunctionToScilab(scilabGetFunctionName, scilabGetSmallFunctionName, getFunctionName); /* Manage SET function */ if (!is_immutable(node)) { Wrapper *setFunctionWrapper = NewWrapper(); String *setFunctionName = Swig_name_set(NSPACE_TODO, variableName); String *scilabSetFunctionName = Swig_name_set(NSPACE_TODO, variableName); String *scilabSetSmallFunctionName = Swig_name_set(NSPACE_TODO, smallVariableName); Setattr(node, "wrap:name", setFunctionName); Printv(setFunctionWrapper->def, "SWIGEXPORT int ", setFunctionName, "(SWIG_GatewayParameters) {\n", NIL); /* Check the number of input and output */ Printf(setFunctionWrapper->def, "SWIG_CheckInputArgument(pvApiCtx, 1, 1);\n"); Printf(setFunctionWrapper->def, "SWIG_CheckOutputArgument(pvApiCtx, 0, 1);\n"); Printf(setFunctionWrapper->def, "SWIG_Scilab_SetApiContext(pvApiCtx);\n"); String *varinTypemap = Swig_typemap_lookup("varin", node, origVariableName, 0); if (varinTypemap != NULL) { Replaceall(varinTypemap, "$input", "1"); emit_action_code(node, setFunctionWrapper->code, varinTypemap); Delete(varinTypemap); } Append(setFunctionWrapper->code, "return SWIG_OK;\n"); Append(setFunctionWrapper->code, "}\n"); Wrapper_print(setFunctionWrapper, wrappersSection); /* Add function to builder table */ addFunctionToScilab(scilabSetFunctionName, scilabSetSmallFunctionName, setFunctionName); DelWrapper(setFunctionWrapper); } DelWrapper(getFunctionWrapper); return SWIG_OK; } /* ----------------------------------------------------------------------- * constantWrapper() * ----------------------------------------------------------------------- */ virtual int constantWrapper(Node *node) { /* Get the useful information from the node */ String *nodeName = Getattr(node, "name"); SwigType *type = Getattr(node, "type"); String *constantName = Getattr(node, "sym:name"); String *constantValue = Getattr(node, "value"); String *constantTypemap = NULL; // If feature scilab:const enabled, constants & enums are wrapped to Scilab variables if (GetFlag(node, "feature:scilab:const")) { bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)); bool isEnum = (Cmp(nodeType(node), "enumitem") == 0); if (isConstant || isEnum) { if (isEnum) { Setattr(node, "type", "double"); constantValue = Getattr(node, "value"); } constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0); if (constantTypemap != NULL) { Setattr(node, "wrap:name", constantName); Replaceall(constantTypemap, "$result", constantName); Replaceall(constantTypemap, "$value", constantValue); emit_action_code(node, variablesCode, constantTypemap); Delete(constantTypemap); return SWIG_OK; } } } /* Create variables for member pointer constants, not supported by typemaps (like Python wrapper does) */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(constantName); String *str = SwigType_str(type, wname); Printf(headerSection, "static %s = %s;\n", str, constantValue); Delete(str); constantValue = wname; } // Constant names can have SCILAB_VARIABLE_NAME_CHAR_MAX because of suffixes "_get" added to function String *smallConstantName = createSmallIdentifierName(constantName, SCILAB_IDENTIFIER_NAME_CHAR_MAX - 4); /* Create GET function to get the constant value */ Wrapper *getFunctionWrapper = NewWrapper(); String *getFunctionName = Swig_name_get(NSPACE_TODO, constantName); String *scilabGetSmallFunctionName = Swig_name_get(NSPACE_TODO, smallConstantName); Setattr(node, "wrap:name", getFunctionName); Setattr(node, "wrap:name", getFunctionName); Printv(getFunctionWrapper->def, "SWIGEXPORT int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL); /* Check the number of input and output */ Printf(getFunctionWrapper->def, "SWIG_CheckInputArgument(pvApiCtx, 0, 0);\n"); Printf(getFunctionWrapper->def, "SWIG_CheckOutputArgument(pvApiCtx, 0, 1);\n"); Printf(getFunctionWrapper->def, "SWIG_Scilab_SetApiContext(pvApiCtx);\n"); constantTypemap = Swig_typemap_lookup("constcode", node, nodeName, 0); if (constantTypemap != NULL) { Printf(getFunctionWrapper->code, "SWIG_Scilab_SetOutputPosition(%d);\n", 1); Replaceall(constantTypemap, "$value", constantValue); Replaceall(constantTypemap, "$result", "1"); emit_action_code(node, getFunctionWrapper->code, constantTypemap); Delete(constantTypemap); } /* Dump the wrapper function */ Append(getFunctionWrapper->code, "return SWIG_OK;\n"); Append(getFunctionWrapper->code, "}\n"); Wrapper_print(getFunctionWrapper, wrappersSection); /* Add the function to Scilab */ addFunctionToScilab(getFunctionName, scilabGetSmallFunctionName, getFunctionName); DelWrapper(getFunctionWrapper); return SWIG_OK; } /* --------------------------------------------------------------------- * enumvalueDeclaration() * --------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *node) { static int iPreviousEnumValue = 0; if (GetFlag(node, "feature:scilab:const")) { // Compute the "absolute" value of enum if needed // (most of time enum values are a linked list of relative values) String *enumValue = Getattr(node, "enumvalue"); String *enumValueEx = Getattr(node, "enumvalueex"); // First enum value ? String *firstenumitem = Getattr(node, "firstenumitem"); if (firstenumitem) { if (enumValue) { // Value is in 'enumvalue' iPreviousEnumValue = atoi(Char(enumValue)); } else if (enumValueEx) { // Or value is in 'enumValueEx' iPreviousEnumValue = atoi(Char(enumValueEx)); enumValue = NewString(""); Printf(enumValue, "%d", iPreviousEnumValue); Setattr(node, "enumvalue", enumValue); } } else if (!enumValue && enumValueEx) { // Value is not specified, set it by incrementing last value enumValue = NewString(""); Printf(enumValue, "%d", ++iPreviousEnumValue); Setattr(node, "enumvalue", enumValue); } // Enums in Scilab are mapped to double Setattr(node, "type", "double"); } return Language::enumvalueDeclaration(node); } /* ----------------------------------------------------------------------- * addHelperFunctions() * ----------------------------------------------------------------------- */ void addHelperFunctions() { addFunctionToScilab("SWIG_this", "SWIG_this", "SWIG_this"); addFunctionToScilab("SWIG_ptr", "SWIG_ptr", "SWIG_ptr"); } /* ----------------------------------------------------------------------- * addFunctionToScilab() * Declare a wrapped function in Scilab (builder, gateway, XML, ...) * ----------------------------------------------------------------------- */ void addFunctionToScilab(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr scilabSmallFunctionName, const_String_or_char_ptr wrapperFunctionName) { if (!generateBuilder) addFunctionInGatewayHeader(scilabFunctionName, scilabSmallFunctionName, wrapperFunctionName); if (generateBuilder) { addFunctionInScriptTable(scilabFunctionName, scilabSmallFunctionName, wrapperFunctionName, builderCode5, builderCode6); } if (createLoader) { addFunctionInLoader(scilabFunctionName, scilabSmallFunctionName); } if (gatewayXMLFile) { Printf(gatewayXML, "\n", gatewayID, primitiveID++, scilabSmallFunctionName); } if (gatewayXMLFileV6) { Printf(gatewayXMLV6, "\n", scilabFunctionName, scilabFunctionName); } } /* ----------------------------------------------------------------------- * createBuilderCode() * ----------------------------------------------------------------------- */ void createBuilderFile(String *outputFilename) { String *builderFilename = NewStringf("builder.sce"); builderFile = NewFile(builderFilename, "w", SWIG_output_files()); if (!builderFile) { FileErrorDisplay(builderFilename); Exit(EXIT_FAILURE); } emitBanner(builderFile); builderFunctionCount = 0; builderCode = NewString(""); builderCode5 = NewString(""); builderCode6 = NewString(""); Printf(builderCode, "mode(-1);\n"); Printf(builderCode, "lines(0);\n"); /* Useful for automatic tests */ // Scilab needs to be in the build directory Printf(builderCode, "originaldir = pwd();\n"); Printf(builderCode, "builddir = get_absolute_file_path('builder.sce');\n"); Printf(builderCode, "cd(builddir);\n"); Printf(builderCode, "ilib_verbose(%s);\n", verboseBuildLevel); Printf(builderCode, "libs = [];\n"); // Flags from command line arguments Printf(builderCode, "cflags = \"\";\n"); for (int i = 0; i < Len(cflags); i++) { String *cflag = Getitem(cflags, i); Printf(builderCode, "cflags = cflags + \" %s\";\n", cflag); } if (Len(ldflags) > 0) { for (int i = 0; i < Len(ldflags); i++) { String *ldflag = Getitem(ldflags, i); if (i == 0) { Printf(builderCode, "ldflags = \"%s\";\n", ldflag); } else { Printf(builderCode, "ldflags = ldflags + \" %s\";\n", ldflag); } } } else { Printf(builderCode, "ldflags = \"\";\n"); } // External script to set flags if (buildFlagsScript) { Printf(builderCode, "exec(\"%s\");\n", buildFlagsScript); Printf(builderCode, "cflags = cflags + getCompilationFlags();\n"); Printf(builderCode, "ldflags = ldflags + getLinkFlags();\n"); } // Additional sources Insert(sourceFileList, 0, outputFilename); for (int i = 0; i < Len(sourceFileList); i++) { String *sourceFile = Getitem(sourceFileList, i); if (i == 0) { Printf(builderCode, "files = \"%s\";\n", sourceFile); } else { Printf(builderCode, "files($ + 1) = \"%s\";\n", sourceFile); } } Printf(builderCode5, "table = [ ..\n"); Printf(builderCode6, "table = [ ..\n"); } /* ----------------------------------------------------------------------- * addFunctionInBuilderCode() * Add a function wrapper in the function table of generated builder script * ----------------------------------------------------------------------- */ void addFunctionInScriptTable(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr scilabSmallFunctionName, const_String_or_char_ptr wrapperFunctionName, String *scriptCode5, String *scriptCode6) { if (++builderFunctionCount % 10 == 0) { Printf(scriptCode5, "];\ntable = [table; ..\n"); Printf(scriptCode6, "];\ntable = [table; ..\n"); } Printf(scriptCode5, "\"%s\",\"%s\"; ..\n", scilabSmallFunctionName, wrapperFunctionName); Printf(scriptCode6, "\"%s\",\"%s\"; ..\n", scilabFunctionName, wrapperFunctionName); } /* ----------------------------------------------------------------------- * saveBuilderFile() * ----------------------------------------------------------------------- */ void saveBuilderFile(String *gatewayName) { Printf(builderCode5, "];\n"); Printf(builderCode6, "];\n"); if (Equal(builderCode5, builderCode6)) { Append(builderCode, builderCode6); } else { Printf(builderCode, "ver = getversion('scilab');\n"); Printf(builderCode, "if ver(1) < 6 then\n"); Printf(builderCode, " // version is less or equal to 5.5.2\n"); Printf(builderCode, " \n"); Append(builderCode, builderCode5); Printf(builderCode, " \n"); Printf(builderCode, "else\n"); Printf(builderCode, " // version is 6.0.0 or more\n"); Printf(builderCode, " \n"); Append(builderCode, builderCode6); Printf(builderCode, " \n"); Printf(builderCode, "end\n"); } Printf(builderCode, "ierr = 0;\n"); Printf(builderCode, "if ~isempty(table) then\n"); Printf(builderCode, " ierr = execstr(\"ilib_build(''%s'', table, files, libs, [], ldflags, cflags);\", 'errcatch');\n", gatewayName); Printf(builderCode, " if ierr <> 0 then\n"); Printf(builderCode, " err_msg = lasterror();\n"); Printf(builderCode, " end\n"); Printf(builderCode, "end\n"); Printf(builderCode, "cd(originaldir);\n"); Printf(builderCode, "if ierr <> 0 then\n"); Printf(builderCode, " error(ierr, err_msg);\n"); Printf(builderCode, "end\n"); Printv(builderFile, builderCode, NIL); Delete(builderCode); Delete(builderFile); } /* ----------------------------------------------------------------------- * createGatewayXMLFileV6() * This XML file is used by Scilab 6 in the context of internal modules or * to get the function list. * ----------------------------------------------------------------------- */ void createGatewayXMLFileV6(String *gatewayName) { String *gatewayXMLFilename = NewStringf("%s_gateway.xml", gatewayName); gatewayXMLFileV6 = NewFile(gatewayXMLFilename, "w", SWIG_output_files()); if (!gatewayXMLFileV6) { FileErrorDisplay(gatewayXMLFilename); Exit(EXIT_FAILURE); } // Add a slightly modified SWIG banner to the gateway XML ("--modify" is illegal in XML) gatewayXMLV6 = NewString(""); Printf(gatewayXMLV6, "\n"); Printf(gatewayXMLV6, "\n"); Printf(gatewayXMLV6, "\n"); Printf(gatewayXMLV6, "\n", gatewayName); } /* ----------------------------------------------------------------------- * saveGatewayXMLFileV6() * ----------------------------------------------------------------------- */ void saveGatewayXMLFileV6() { Printf(gatewayXMLV6, "\n"); Printv(gatewayXMLFileV6, gatewayXMLV6, NIL); Delete(gatewayXMLFileV6); } /* ----------------------------------------------------------------------- * createGatewayXMLFile() * This XML file is used by Scilab in the context of internal modules * ----------------------------------------------------------------------- */ void createGatewayXMLFile(String *gatewayName) { String *gatewayXMLFilename = NewStringf("%s_gateway.xml", gatewayName); gatewayXMLFile = NewFile(gatewayXMLFilename, "w", SWIG_output_files()); if (!gatewayXMLFile) { FileErrorDisplay(gatewayXMLFilename); Exit(EXIT_FAILURE); } // Add a slightly modified SWIG banner to the gateway XML ("--modify" is illegal in XML) gatewayXML = NewString(""); Printf(gatewayXML, "\n"); Printf(gatewayXML, "\n"); Printf(gatewayXML, "\n", gatewayName); primitiveID = 1; } /* ----------------------------------------------------------------------- * saveGatewayXMLFile() * ----------------------------------------------------------------------- */ void saveGatewayXMLFile() { Printf(gatewayXML, "\n"); Printv(gatewayXMLFile, gatewayXML, NIL); Delete(gatewayXMLFile); } /* ----------------------------------------------------------------------- * startGatewayHeader() * Start the gateway header * ----------------------------------------------------------------------- */ void startGatewayHeader(String *gatewayLibraryName) { gatewayHeader = NewString(""); Printf(gatewayHeader, "\n"); gatewayHeaderV6 = NewString(""); Printf(gatewayHeaderV6, "#ifdef __cplusplus\n"); Printf(gatewayHeaderV6, "extern \"C\" {\n"); Printf(gatewayHeaderV6, "#endif\n"); Printf(gatewayHeaderV6, "#include \"c_gateway_prototype.h\"\n"); Printf(gatewayHeaderV6, "#include \"addfunction.h\"\n"); Printf(gatewayHeaderV6, "#ifdef __cplusplus\n"); Printf(gatewayHeaderV6, "}\n"); Printf(gatewayHeaderV6, "#endif\n"); Printf(gatewayHeaderV6, "\n"); Printf(gatewayHeaderV6, "#define MODULE_NAME L\"%s\"\n", gatewayLibraryName); Printf(gatewayHeaderV6, "#ifdef __cplusplus\n"); Printf(gatewayHeaderV6, "extern \"C\"\n"); Printf(gatewayHeaderV6, "#endif\n"); Printf(gatewayHeaderV6, "SWIGEXPORT int %s(wchar_t *pwstFuncName) {\n", gatewayLibraryName); Printf(gatewayHeaderV6, "\n"); } /* ----------------------------------------------------------------------- * addFunctionInGatewayHeader() * Add a function in the gateway header * ----------------------------------------------------------------------- */ void addFunctionInGatewayHeader(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr scilabSmallFunctionName, const_String_or_char_ptr wrapperFunctionName) { if (gatewayHeaderV5 == NULL) { gatewayHeaderV5 = NewString(""); Printf(gatewayHeaderV5, "static GenericTable Tab[] = {\n"); } else Printf(gatewayHeaderV5, ",\n"); Printf(gatewayHeaderV5, " {(Myinterfun)sci_gateway, (GT)%s, (char *)\"%s\"}", wrapperFunctionName, scilabSmallFunctionName); Printf(gatewayHeaderV6, "if (wcscmp(pwstFuncName, L\"%s\") == 0) { addCStackFunction((wchar_t *)L\"%s\", &%s, (wchar_t *)MODULE_NAME); }\n", scilabFunctionName, scilabFunctionName, wrapperFunctionName); } /* ----------------------------------------------------------------------- * terminateGatewayHeader() * Terminates the gateway header * ----------------------------------------------------------------------- */ void terminateGatewayHeader(String *gatewayLibraryName) { Printf(gatewayHeaderV5, "};\n"); Printf(gatewayHeaderV5, "\n"); Printf(gatewayHeaderV5, "#ifdef __cplusplus\n"); Printf(gatewayHeaderV5, "extern \"C\" {\n"); Printf(gatewayHeaderV5, "#endif\n"); Printf(gatewayHeaderV5, "SWIGEXPORT int C2F(%s)() {\n", gatewayLibraryName); Printf(gatewayHeaderV5, " Rhs = Max(0, Rhs);\n"); Printf(gatewayHeaderV5, " if (*(Tab[Fin-1].f) != NULL) {\n"); Printf(gatewayHeaderV5, " if(pvApiCtx == NULL) {\n"); Printf(gatewayHeaderV5, " pvApiCtx = (StrCtx *)MALLOC(sizeof(StrCtx));\n"); Printf(gatewayHeaderV5, " }\n"); Printf(gatewayHeaderV5, " pvApiCtx->pstName = (char *)Tab[Fin-1].name;\n"); Printf(gatewayHeaderV5, " (*(Tab[Fin-1].f))(Tab[Fin-1].name,(GatefuncH)Tab[Fin-1].F);\n"); Printf(gatewayHeaderV5, " }\n"); Printf(gatewayHeaderV5, " return 0;\n"); Printf(gatewayHeaderV5, "}\n"); Printf(gatewayHeaderV5, "\n"); Printf(gatewayHeaderV5, "#ifdef __cplusplus\n"); Printf(gatewayHeaderV5, "}\n"); Printf(gatewayHeaderV5, "#endif\n"); Printf(gatewayHeaderV6, "return 1;\n"); Printf(gatewayHeaderV6, "};\n"); Printf(gatewayHeader, "#if SCI_VERSION_MAJOR < 6\n"); Printv(gatewayHeader, gatewayHeaderV5, NIL); Printf(gatewayHeader, "#else\n"); Printv(gatewayHeader, gatewayHeaderV6, NIL); Printf(gatewayHeader, "#endif\n"); } /* ----------------------------------------------------------------------- * createLoaderScriptFile() * Creates the loader script file (loader.sce) * ----------------------------------------------------------------------- */ void createLoaderFile(String *gatewayLibraryName) { String *loaderFilename = NewString("loader.sce"); loaderFile = NewFile(loaderFilename, "w", SWIG_output_files()); if (!loaderFile) { FileErrorDisplay(loaderFilename); Exit(EXIT_FAILURE); } emitBanner(loaderFile); loaderFunctionCount = 0; loaderScript = NewString("function loader_function()\n"); Printf(loaderScript, " p = get_absolute_file_path('loader.sce');\n", gatewayLibraryName); Printf(loaderScript, " [bOK, ilib] = c_link('%s');\n", gatewayLibraryName); Printf(loaderScript, " if bOK then\n"); Printf(loaderScript, " ulink(ilib);\n"); Printf(loaderScript, " end\n"); loaderScript5 = NewString(" list_functions = [ ..\n"); loaderScript6 = NewString(" list_functions = [ ..\n"); } /* ----------------------------------------------------------------------- * addFunctionInLoaderScript() * Add a function in the loader script table * ----------------------------------------------------------------------- */ void addFunctionInLoader(const_String_or_char_ptr scilabFunctionName, const_String_or_char_ptr scilabSmallFunctionName) { if (++loaderFunctionCount % 10 == 0) { Printf(loaderScript5, " ];\n list_functions = [list_functions; ..\n"); Printf(loaderScript6, " ];\n list_functions = [list_functions; ..\n"); } Printf(loaderScript5, " '%s'; ..\n", scilabSmallFunctionName); Printf(loaderScript6, " '%s'; ..\n", scilabFunctionName); } /* ----------------------------------------------------------------------- * saveLoaderScriptFile() * Terminates and saves the loader script * ----------------------------------------------------------------------- */ void saveLoaderFile(String *gatewayLibraryName) { Printf(loaderScript5, " ];\n"); Printf(loaderScript6, " ];\n"); if (Equal(loaderScript5, loaderScript6)) { Append(loaderScript, loaderScript6); } else { Printf(loaderScript, " ver = getversion('scilab');\n"); Printf(loaderScript, " if ver(1) < 6 then\n"); Printf(loaderScript, " // version is less or equal to 5.5.2\n"); Printf(loaderScript, " \n"); Append(loaderScript, loaderScript5); Delete(loaderScript5); Printf(loaderScript, " \n"); Printf(loaderScript, " else\n"); Printf(loaderScript, " // version is 6.0.0 or more\n"); Printf(loaderScript, " \n"); Append(loaderScript, loaderScript6); Delete(loaderScript6); Printf(loaderScript, " \n"); Printf(loaderScript, " end\n"); } Printf(loaderScript, " addinter(p + '%s' + getdynlibext(), '%s', list_functions);\n", gatewayLibraryName, gatewayLibraryName); Printf(loaderScript, "endfunction\n"); Printf(loaderScript, "loader_function();\n"); Printf(loaderScript, "clear loader_function;\n"); Printv(loaderFile, loaderScript, NIL); Delete(loaderScript); Delete(loaderFile); } /* ----------------------------------------------------------------------- * createSmallIdentifierName() * Create a Scilab small identifier to be used by Scilab 5 * ----------------------------------------------------------------------- */ String* createSmallIdentifierName(String* name, int outputLen = SCILAB_IDENTIFIER_NAME_CHAR_MAX) { char* s = Char(name); int nameLen = Len(s); // truncate and preserve common suffix if (outputLen > 4 && nameLen > outputLen) { String* smallName = NewStringWithSize(name, outputLen); char* smallNameStr = (char*) Data(smallName); if (s[nameLen-4] == '_' && s[nameLen - 3] == 'g' && s[nameLen - 2] == 'e' && s[nameLen - 1] == 't') { // get memcpy(&smallNameStr[outputLen - 4], &s[nameLen - 4], 4); } else if (s[nameLen-4] == '_' && s[nameLen - 3] == 's' && s[nameLen - 2] == 'e' && s[nameLen - 1] == 't') { // set memcpy(&smallNameStr[outputLen - 4], &s[nameLen - 4], 4); } return smallName; } return name; } }; extern "C" Language *swig_scilab(void) { return new SCILAB(); } swig-4.4.0/Source/Modules/ocaml.cxx0000664000175000017500000017167715075443613017072 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * ocaml.cxx * * Ocaml language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include static const char *usage = "\ Ocaml Options (available with -ocaml)\n\ -oldvarnames - Old intermediary method names for variable wrappers\n\ -prefix - Set a prefix to be prepended to all names\n\ -where - Emit library location\n\ \n"; static int classmode = 0; static int in_constructor = 0, in_destructor = 0, in_copyconst = 0; static int const_enum = 0; static int static_member_function = 0; static int generate_sizeof = 0; static String *prefix = 0; static const char *ocaml_path = "ocaml"; static bool old_variable_names = false; static String *classname = 0; static String *module = 0; static String *init_func_def = 0; static String *f_classtemplate = 0; static SwigType *name_qualifier_type = 0; static Hash *seen_enums = 0; static Hash *seen_enumvalues = 0; static Hash *seen_constructors = 0; static File *f_header = 0; static File *f_begin = 0; static File *f_runtime = 0; static File *f_wrappers = 0; static File *f_directors = 0; static File *f_directors_h = 0; static File *f_init = 0; static File *f_mlout = 0; static File *f_mliout = 0; static File *f_mlbody = 0; static File *f_mlibody = 0; static File *f_mltail = 0; static File *f_mlitail = 0; static File *f_enumtypes_type = 0; static File *f_enumtypes_value = 0; static File *f_class_ctors = 0; static File *f_class_ctors_end = 0; static File *f_enum_to_int = 0; static File *f_int_to_enum = 0; class OCAML:public Language { public: OCAML() { director_prot_ctor_code = NewString(""); Printv(director_prot_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " caml_failwith(\"accessing abstract class or protected constructor\"); \n", "}\n", NIL); director_multiple_inheritance = 1; directorLanguage(); } String *Swig_class_name(Node *n) { String *name; name = Copy(Getattr(n, "sym:name")); return name; } void PrintIncludeArg() { Printv(stdout, SWIG_LIB, SWIG_FILE_DELIMITER, ocaml_path, "\n", NIL); } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { int i; prefix = 0; SWIG_library_directory(ocaml_path); // Look for certain command line options for (i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); Exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-where") == 0) { PrintIncludeArg(); Exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-oldvarnames") == 0) { Swig_mark_arg(i); old_variable_names = true; } } } // If a prefix has been specified make sure it ends in a '_' (not actually used!) if (prefix) { const char *px = Char(prefix); if (px[Len(prefix) - 1] != '_') Printf(prefix, "_"); } else prefix = NewString("swig_"); // Add a symbol for this module Preprocessor_define("SWIGOCAML 1", 0); // Read in default typemaps */ SWIG_config_file("ocaml.i"); allow_overloading(); } /* Swig_director_declaration() * * Generate the full director class declaration, complete with base classes. * e.g. "class SwigDirector_myclass : public myclass, public Swig::Director {" * */ String *Swig_director_declaration(Node *n) { String *classname = Swig_class_name(n); String *directorname = NewStringf("SwigDirector_%s", classname); String *base = Getattr(n, "classtype"); String *declaration = Swig_class_declaration(n, directorname); Printf(declaration, " : public %s, public Swig::Director {\n", base); Delete(classname); Delete(directorname); return declaration; } void emitBanner(File *f) { Printf(f, "(* ----------------------------------------------------------------------------\n"); Swig_banner_target_lang(f, " *"); Printf(f, " * ---------------------------------------------------------------------------- *)\n\n"); } /* ------------------------------------------------------------ * top() * * Recognize the %module, and capture the module name. * Create the default enum cases. * Set up the named outputs: * * init * ml * mli * wrapper * header * runtime * directors * directors_h * ------------------------------------------------------------ */ virtual int top(Node *n) { /* Set comparison with none for ConstructorToFunction */ setSubclassInstanceCheck(NewString("caml_list_nth(args,0) != Val_unit")); /* check if directors are enabled for this module. note: this * is a "master" switch, without which no director code will be * emitted. %feature("director") statements are also required * to enable directors for individual classes or methods. * * use %module(directors="1") modulename at the start of the * interface file to enable director generation. */ String *mod_docstring = NULL; { Node *module = Getattr(n, "module"); if (module) { Node *options = Getattr(module, "options"); if (options) { if (Getattr(options, "directors")) { allow_directors(); } if (Getattr(options, "dirprot")) { allow_dirprot(); } if (Getattr(options, "sizeof")) { generate_sizeof = 1; } mod_docstring = Getattr(options, "docstring"); } } } /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors = NewString(""); f_directors_h = NewString(""); f_enumtypes_type = NewString(""); f_enumtypes_value = NewString(""); init_func_def = NewString(""); f_mlbody = NewString(""); f_mlibody = NewString(""); f_mltail = NewString(""); f_mlitail = NewString(""); f_class_ctors = NewString(""); f_class_ctors_end = NewString(""); f_enum_to_int = NewString(""); f_int_to_enum = NewString(""); f_classtemplate = NewString(""); module = Getattr(n, "name"); seen_constructors = NewHash(); seen_enums = NewHash(); seen_enumvalues = NewHash(); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("init", init_func_def); Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("mli", f_mlibody); Swig_register_filebyname("ml", f_mlbody); Swig_register_filebyname("mlitail", f_mlitail); Swig_register_filebyname("mltail", f_mltail); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); Swig_register_filebyname("classtemplate", f_classtemplate); Swig_register_filebyname("class_ctors", f_class_ctors); if (old_variable_names) { Swig_name_register("set", "%n%v__set__"); Swig_name_register("get", "%n%v__get__"); } Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "OCAML"); Printf(f_runtime, "#define SWIG_MODULE \"%s\"\n", module); /* Module name */ Printf(f_mlbody, "let module_name = \"%s\"\n", module); Printf(f_mlibody, "val module_name : string\n"); Printf(f_enum_to_int, "let enum_to_int x (v : c_obj) =\n" " match v with\n" " C_enum _y ->\n" " (let y = _y in match (x : c_enum_type) with\n" " `unknown -> " " (match y with\n" " `Int x -> (Swig.C_int x)\n" " | _ -> raise (LabelNotFromThisEnum v))\n"); Printf(f_int_to_enum, "let int_to_enum x y =\n" " match (x : c_enum_type) with\n" " `unknown -> C_enum (`Int y)\n"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); } Printf(f_runtime, "\n"); /* Produce the enum_to_int and int_to_enum functions */ Printf(f_enumtypes_type, "open Swig\n" "type c_enum_type = [ \n `unknown\n"); Printf(f_enumtypes_value, "type c_enum_value = [ \n `Int of int\n"); String *mlfile = NewString(""); String *mlifile = NewString(""); Printv(mlfile, module, ".ml", NIL); Printv(mlifile, module, ".mli", NIL); String *mlfilen = NewStringf("%s%s", SWIG_output_directory(), mlfile); if ((f_mlout = NewFile(mlfilen, "w", SWIG_output_files())) == 0) { FileErrorDisplay(mlfilen); Exit(EXIT_FAILURE); } String *mlifilen = NewStringf("%s%s", SWIG_output_directory(), mlifile); if ((f_mliout = NewFile(mlifilen, "w", SWIG_output_files())) == 0) { FileErrorDisplay(mlifilen); Exit(EXIT_FAILURE); } emitBanner(f_mlout); emitBanner(f_mliout); Language::top(n); if (mod_docstring) { if (Len(mod_docstring)) { Printv(f_mliout, "(** ", mod_docstring, " *)\n", NIL); } Delete(mod_docstring); mod_docstring = NULL; } Printf(f_enum_to_int, ") | _ -> (C_int (get_int v))\n" "let _ = Callback.register \"%s_enum_to_int\" enum_to_int\n", module); Printf(f_mlibody, "val enum_to_int : c_enum_type -> c_obj -> Swig.c_obj\n"); Printf(f_int_to_enum, "let _ = Callback.register \"%s_int_to_enum\" int_to_enum\n", module); Printf(f_mlibody, "val int_to_enum : c_enum_type -> int -> c_obj\n"); Printf(f_init, "#define SWIG_init f_%s_init\n" "%s" "}\n", module, init_func_def); Printf(f_mlbody, "external f_init : unit -> unit = \"f_%s_init\" ;;\n" "let _ = f_init ()\n", module); Printf(f_enumtypes_type, "]\n"); Printf(f_enumtypes_value, "]\n\n" "type c_obj = c_enum_value c_obj_t\n"); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (make it occur before %header section) Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } SwigType_emit_type_table(f_runtime, f_wrappers); /* Close all of the files */ Dump(f_runtime, f_begin); Dump(f_directors_h, f_header); Dump(f_header, f_begin); Dump(f_directors, f_wrappers); Dump(f_wrappers, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_runtime); Delete(f_begin); Dump(f_enumtypes_type, f_mlout); Dump(f_enumtypes_value, f_mlout); Dump(f_mlbody, f_mlout); Dump(f_enum_to_int, f_mlout); Dump(f_int_to_enum, f_mlout); Delete(f_int_to_enum); Delete(f_enum_to_int); Dump(f_class_ctors, f_mlout); Dump(f_class_ctors_end, f_mlout); Dump(f_mltail, f_mlout); Delete(f_mlout); Dump(f_enumtypes_type, f_mliout); Dump(f_enumtypes_value, f_mliout); Dump(f_mlibody, f_mliout); Dump(f_mlitail, f_mliout); Delete(f_mliout); return SWIG_OK; } /* Produce an error for the given type */ void throw_unhandled_ocaml_type_error(SwigType *d, const char *types) { Swig_warning(WARN_TYPEMAP_UNDEF, input_file, line_number, "Unable to handle type %s (%s).\n", SwigType_str(d, 0), types); } /* Return true iff T is a pointer type */ int is_a_pointer(SwigType *t) { return SwigType_ispointer(SwigType_typedef_resolve_all(t)); } /* * Delete one reference from a given type. */ void oc_SwigType_del_reference(SwigType *t) { if (SwigType_isqualifier(t)) { SwigType_del_qualifier(t); } SwigType_del_reference(t); } void oc_SwigType_del_array(SwigType *t) { if (SwigType_isqualifier(t)) { SwigType_del_qualifier(t); } if (SwigType_isarray(t)) { SwigType_del_array(t); } } /* * Return true iff T is a reference type */ int is_a_reference(SwigType *t) { return SwigType_isreference(SwigType_typedef_resolve_all(t)); } int is_an_array(SwigType *t) { return SwigType_isarray(SwigType_typedef_resolve_all(t)); } virtual int membervariableHandler(Node *n) { String *symname = Getattr(n, "sym:name"); Language::membervariableHandler(n); String *mname = Swig_name_member(NSPACE_TODO, classname, symname); String *getname = Swig_name_get(NSPACE_TODO, mname); String *mangled_getname = mangleNameForCaml(getname); Delete(getname); if (!GetFlag(n, "feature:immutable")) { String *setname = Swig_name_set(NSPACE_TODO, mname); String *mangled_setname = mangleNameForCaml(setname); Delete(setname); Printf(f_class_ctors, " \"[%s]\", (fun args -> " "if args = (C_list [ raw_ptr ]) then _%s args else _%s args) ;\n", symname, mangled_getname, mangled_setname); Delete(mangled_setname); } else { Printf(f_class_ctors, " \"[%s]\", (fun args -> " "if args = (C_list [ raw_ptr ]) then _%s args else C_void) ;\n", symname, mangled_getname); } Delete(mangled_getname); Delete(mname); return SWIG_OK; } /* ------------------------------------------------------------ * functionWrapper() * Create a function declaration and register it with the interpreter. * ------------------------------------------------------------ */ virtual int functionWrapper(Node *n) { char *iname = GetChar(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); String *return_type_normalized = normalizeTemplatedClassName(returntype); ParmList *l = Getattr(n, "parms"); int director_method = 0; Parm *p; Wrapper *f = NewWrapper(); String *proc_name = NewString(""); String *target = NewString(""); String *arg = NewString(""); String *cleanup = NewString(""); String *outarg = NewString(""); String *build = NewString(""); String *tm; int i = 0; int numargs; int numreq; int newobj = GetFlag(n, "feature:new"); String *nodeType = Getattr(n, "nodeType"); int destructor = (!Cmp(nodeType, "destructor")); String *overname = 0; bool isOverloaded = Getattr(n, "sym:overloaded") ? true : false; // For overloaded functions, only the dispatch function needs to be exposed in the ml and mli files. bool expose_func = !isOverloaded || !Getattr(n, "sym:nextSibling"); // Make a wrapper name for this String *wname = Swig_name_wrapper(iname); if (isOverloaded) { overname = Getattr(n, "sym:overname"); } else { if (!addSymbol(iname, n)) { DelWrapper(f); return SWIG_ERROR; } } if (overname) { Append(wname, overname); } /* Do this to disambiguate functions emitted from different modules */ Append(wname, module); Setattr(n, "wrap:name", wname); // Build the name for Scheme. Printv(proc_name, "_", iname, NIL); String *mangled_name = mangleNameForCaml(proc_name); if (classmode && in_constructor && expose_func) { // Emit constructor for object String *mangled_name_nounder = NewString((char *) (Char(mangled_name)) + 1); Printf(f_class_ctors_end, "let %s clst = _%s clst\n", mangled_name_nounder, mangled_name_nounder); Printf(f_mlibody, "val %s : c_obj -> c_obj\n", mangled_name_nounder); Delete(mangled_name_nounder); } else if (classmode && in_destructor) { Printf(f_class_ctors, " \"~\", %s ;\n", mangled_name); } else if (classmode && !in_constructor && !in_destructor && !static_member_function && !Getattr(n, "membervariableHandler:sym:name") && expose_func) { String *opname = Copy(Getattr(n, "memberfunctionHandler:sym:name")); Replaceall(opname, "operator ", ""); Printf(f_class_ctors, " \"%s\", %s ;\n", opname, mangled_name); Delete(opname); } if (classmode && in_constructor) { Setattr(seen_constructors, mangled_name, "true"); } // writing the function wrapper function Printv(f->def, "SWIGEXT value ", wname, " (", NIL); Printv(f->def, "value args", NIL); Printv(f->def, ")\n{", NIL); /* Define the scheme name in C. This define is used by several macros. */ //Printv(f->def, "#define FUNC_NAME \"", mangled_name, "\"", NIL); // adds local variables Wrapper_add_local(f, "args", "CAMLparam1(args)"); Wrapper_add_local(f, "ret", "CAMLlocal2(swig_result,rv)"); returntype = SwigType_typedef_qualified(returntype); emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); Setattr(n, "wrap:parms", l); numargs = emit_num_arguments(l); numreq = emit_num_required(l); if (!isOverloaded) { if (numargs > 0) { if (numreq > 0) { Printf(f->code, "if (caml_list_length(args) < %d || caml_list_length(args) > %d) {\n", numreq, numargs); } else { Printf(f->code, "if (caml_list_length(args) > %d) {\n", numargs); } Printf(f->code, "caml_invalid_argument(\"Incorrect number of arguments passed to '%s'\");\n}\n", iname); } else { Printf(f->code, "if (caml_list_length(args) > 0) caml_invalid_argument(\"'%s' takes no arguments\");\n", iname); } } Printf(f->code, "swig_result = Val_unit;\n"); // Now write code to extract the parameters (this is super ugly) for (i = 0, p = l; i < numargs; i++) { /* Skip ignored arguments */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); pt = SwigType_typedef_qualified(pt); // Produce names of source and target Clear(target); Clear(arg); String *source = NewStringf("caml_list_nth(args,%d)", i); Printf(target, "%s", ln); Printv(arg, Getattr(p, "name"), NIL); if (i >= numreq) { Printf(f->code, "if (caml_list_length(args) > %d) {\n", i); } // Handle parameter types. if ((tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:in:next"); } else { // no typemap found // check if typedef and resolve throw_unhandled_ocaml_type_error(pt, "in"); p = nextSibling(p); } if (i >= numreq) { Printf(f->code, "}\n"); } Delete(source); } /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } // Pass output arguments back to the caller. for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Replaceall(tm, "$ntype", normalizeTemplatedClassName(Getattr(p, "type"))); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } // Free up any memory allocated for the arguments. /* Insert cleanup code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* if the object is a director, and the method call originated from its * underlying ocaml object, resolve the call by going up the c++ * inheritance chain. otherwise try to resolve the method in ocaml. * without this check an infinite loop is set up between the director and * shadow class method calls. */ // NOTE: this code should only be inserted if this class is the // base class of a director class. however, in general we haven't // yet analyzed all classes derived from this one to see if they are // directors. furthermore, this class may be used as the base of // a director class defined in a completely different module at a // later time, so this test must be included whether or not directorbase // is true. we do skip this code if directors have not been enabled // at the command line to preserve source-level compatibility with // non-polymorphic swig. also, if this wrapper is for a smart-pointer // method, there is no need to perform the test since the calling object // (the smart-pointer) and the director object (the "pointee") are // distinct. director_method = is_member_director(n) && !is_smart_pointer() && !destructor; if (director_method) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Printf(f->code, "director = dynamic_cast(arg1);\n"); Wrapper_add_local(f, "upcall", "bool upcall = false"); Append(f->code, "upcall = (director);\n"); } // Now write code to make the function call Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { Replaceall(tm, "$result", "rv"); Replaceall(tm, "$ntype", return_type_normalized); Printv(f->code, tm, "\n", NIL); } else { throw_unhandled_ocaml_type_error(returntype, "out"); } emit_return_variable(n, returntype, f); // Dump the argument output code Printv(f->code, Char(outarg), NIL); // Dump the argument cleanup code Printv(f->code, Char(cleanup), NIL); // Look for any remaining cleanup if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printv(f->code, tm, "\n", NIL); } } /* See if there is any return cleanup code */ if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } // Free any memory allocated by the function being wrapped.. if ((tm = Swig_typemap_lookup("swig_result", n, Swig_cresult_name(), 0))) { Printv(f->code, tm, "\n", NIL); } // Wrap things up (in a manner of speaking) Printv(f->code, tab4, "swig_result = caml_list_append(swig_result,rv);\n", NIL); Printv(f->code, tab4, "CAMLreturn(swig_result);\n", NIL); Printv(f->code, "}\n", NIL); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", iname); Wrapper_print(f, f_wrappers); if (isOverloaded) { if (!Getattr(n, "sym:nextSibling")) { int maxargs; bool check_emitted = false; Wrapper *df = NewWrapper(); String *dispatch = Swig_overload_dispatch(n, "free(argv);\n" "CAMLreturn(%s(args));\n", &maxargs, &check_emitted); Wrapper_add_local(df, "argv", "value *argv"); /* Undifferentiate name .. this is the dispatch function */ wname = Swig_name_wrapper(iname); /* Do this to disambiguate functions emitted from different * modules */ Append(wname, module); Printv(df->def, "SWIGEXT value ", wname, "(value args) {\n" " CAMLparam1(args);\n" " int i;\n" " int argc = caml_list_length(args);\n", NIL); Printv(df->code, "argv = (value *)malloc( argc * sizeof( value ) );\n" "for( i = 0; i < argc; i++ ) {\n" " argv[i] = caml_list_nth(args,i);\n" "}\n", NIL); Printv(df->code, dispatch, "\nfree(argv);\n", NIL); Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) sibl = Getattr(sibl, "sym:previousSibling"); String *protoTypes = NewString(""); do { String *fulldecl = Swig_name_decl(sibl); Printf(protoTypes, "\n\" %s\\n\"", fulldecl); Delete(fulldecl); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Printf(df->code, "caml_failwith(\"Wrong number or type of arguments for overloaded function '%s'.\\n\"" "\n\" Possible C/C++ prototypes are:\\n\"%s);\n", iname, protoTypes); Delete(protoTypes); Printv(df->code, "}\n", NIL); Wrapper_print(df, f_wrappers); DelWrapper(df); Delete(dispatch); } } if (expose_func) { Printf(f_mlbody, "external %s_f : c_obj list -> c_obj list = \"%s\" ;;\n", mangled_name, wname); Printf(f_mlbody, "let %s arg = match %s_f (%s(fnhelper arg)) with\n", mangled_name, mangled_name, in_constructor && Swig_directorclass(getCurrentClass()) ? "director_core_helper " : ""); Printf(f_mlbody, " [] -> C_void\n" "| [x] -> (if %s then Gc.finalise \n" " (fun x -> ignore ((invoke x) \"~\" C_void)) x) ; x\n" "| lst -> C_list lst ;;\n", newobj ? "true" : "false"); } if ((!classmode || in_constructor || in_destructor || static_member_function) && expose_func) Printf(f_mlibody, "val %s : c_obj -> c_obj\n", mangled_name); Delete(proc_name); Delete(target); Delete(arg); Delete(outarg); Delete(cleanup); Delete(build); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------ * variableWrapper() * * Create a link to a C variable. * This creates a single function _wrap_varname(). * This function takes a single optional argument. If supplied, it means * we are setting this variable to some value. If omitted, it means we are * simply evaluating this variable. We return the value of the variable * in both cases. * * symname is the name of the variable with respect to C. This * may need to differ from the original name in the case of enums. * enumvname is the name of the variable with respect to ocaml. This * will vary if the variable has been renamed. * ------------------------------------------------------------ */ virtual int variableWrapper(Node *n) { char *name = GetChar(n, "feature:symname"); String *iname = Getattr(n, "feature:enumvname"); String *mname = mangleNameForCaml(iname); SwigType *t = Getattr(n, "type"); String *proc_name = NewString(""); String *tm; Wrapper *f; if (!name) { name = GetChar(n, "name"); } if (!iname) { iname = Getattr(n, "sym:name"); mname = mangleNameForCaml(NewString(iname)); } if (!iname || !addSymbol(iname, n)) return SWIG_ERROR; f = NewWrapper(); // evaluation function names String *var_name = Swig_name_wrapper(iname); // Build the name for OCaml. Printv(proc_name, iname, NIL); Setattr(n, "wrap:name", proc_name); Printf(f->def, "SWIGEXT value %s(value args) {\n", var_name); // Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL); Wrapper_add_local(f, "args", "CAMLparam1(args)"); Wrapper_add_local(f, "swig_result", "CAMLlocal1(swig_result)"); Printf(f->code, "swig_result = Val_unit;\n"); int assignable = !is_immutable(n); if (assignable) { /* Check for a setting of the variable value */ Printf(f->code, "if (args != Val_int(0)) {\n"); if ((tm = Swig_typemap_lookup("varin", n, name, 0))) { Replaceall(tm, "$input", "args"); emit_action_code(n, f->code, tm); } else if ((tm = Swig_typemap_lookup("in", n, name, 0))) { Replaceall(tm, "$input", "args"); emit_action_code(n, f->code, tm); } else { throw_unhandled_ocaml_type_error(t, "varin/in"); } Printf(f->code, "}\n"); } // Now return the value of the variable (regardless // of evaluating or setting) if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { Replaceall(tm, "$result", "swig_result"); emit_action_code(n, f->code, tm); } else if ((tm = Swig_typemap_lookup("out", n, name, 0))) { Replaceall(tm, "$result", "swig_result"); emit_action_code(n, f->code, tm); } else { throw_unhandled_ocaml_type_error(t, "varout/out"); } Printf(f->code, "\nCAMLreturn(swig_result);\n"); Printf(f->code, "}\n"); Wrapper_print(f, f_wrappers); // Now add symbol to the Ocaml interpreter if (!assignable) { Printf(f_mlbody, "external _%s : c_obj -> Swig.c_obj = \"%s\" \n", mname, var_name); Printf(f_mlibody, "val _%s : c_obj -> Swig.c_obj\n", iname); if (const_enum) { Printf(f_enum_to_int, " | `%s -> _%s C_void\n", mname, mname); Printf(f_int_to_enum, " if y = (get_int (_%s C_void)) then `%s else\n", mname, mname); } } else { Printf(f_mlbody, "external _%s : c_obj -> c_obj = \"%s\"\n", mname, var_name); Printf(f_mlibody, "external _%s : c_obj -> c_obj = \"%s\"\n", mname, var_name); } Delete(var_name); Delete(proc_name); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------ * staticmemberfunctionHandler -- * Overridden to set static_member_function * ------------------------------------------------------------ */ virtual int staticmemberfunctionHandler(Node *n) { static_member_function = 1; Language::staticmemberfunctionHandler(n); static_member_function = 0; return SWIG_OK; } /* ------------------------------------------------------------ * constantWrapper() * * The one trick here is that we have to make sure we rename the * constant to something useful that doesn't collide with the * original if any exists. * ------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { String *name = Getattr(n, "feature:symname"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); SwigType *qname = Getattr(n, "qualified:name"); if (qname) value = qname; if (!name) { name = mangleNameForCaml(Getattr(n, "name")); Insert(name, 0, "_swig_wrap_"); Setattr(n, "feature:symname", name); } // See if there's a typemap // Create variable and assign it a value Printf(f_header, "static %s = %s;\n", SwigType_str(type, name), value); SetFlag(n, "feature:immutable"); variableWrapper(n); return SWIG_OK; } int constructorHandler(Node *n) { int ret; in_constructor = 1; ret = Language::constructorHandler(n); in_constructor = 0; return ret; } /* destructorHandler: * Turn on destructor flag to inform decisions in functionWrapper */ int destructorHandler(Node *n) { int ret; in_destructor = 1; ret = Language::destructorHandler(n); in_destructor = 0; return ret; } /* copyconstructorHandler: * Turn on constructor and copyconstructor flags for functionWrapper */ int copyconstructorHandler(Node *n) { int ret; in_copyconst = 1; in_constructor = 1; ret = Language::copyconstructorHandler(n); in_constructor = 0; in_copyconst = 0; return ret; } /** * A simple, somewhat general purpose function for writing to multiple * streams from a source template. This allows the user to define the * class definition in ways different from the one I have here if they * want to. It will also make the class definition system easier to * fiddle with when I want to change methods, etc. */ void Multiwrite(String *s) { char *find_marker = strstr(Char(s), "(*Stream:"); while (find_marker) { char *next = strstr(find_marker, "*)"); find_marker += strlen("(*Stream:"); if (next) { int num_chars = (int)(next - find_marker); String *stream_name = NewString(find_marker); Delslice(stream_name, num_chars, Len(stream_name)); File *fout = Swig_filebyname(stream_name); if (fout) { next += strlen("*)"); char *following = strstr(next, "(*Stream:"); find_marker = following; if (!following) following = next + strlen(next); String *chunk = NewString(next); Delslice(chunk, (int)(following - next), Len(chunk)); Printv(fout, chunk, NIL); } } } } bool isSimpleType(String *name) { char *ch = Char(name); return !(strchr(ch, '(') || strchr(ch, '<') || strchr(ch, ')') || strchr(ch, '>')); } /* We accept all chars in identifiers because we use strings to index * them. */ int validIdentifier(String *name) { return Len(name) > 0 ? 1 : 0; } /* classHandler * * Create a "class" definition for ocaml. I thought quite a bit about * how I should do this part of it, and arrived here, using a function * invocation to select a method, and dispatch. This can obviously be * done better, but I can't see how, given that I want to support * overloaded methods, out parameters, and operators. * * I needed a system that would do this: * * a Be able to call these methods: * int foo( int x ); * float foo( int x, int &out ); * * b Be typeable, even in the presence of mutually dependent classes. * * c Support some form of operator invocation. * * (c) I chose strings for the method names so that "+=" would be a * valid method name, and the somewhat natural << (invoke x) "+=" y >> * would work. * * (a) (b) Since the c_obj type exists, it's easy to return C_int in one * case and C_list [ C_float ; C_int ] in the other. This makes tricky * problems with out parameters disappear; they're simply appended to the * return list. * * (b) Since every item that comes from C++ is the same type, there is no * problem with the following: * * class Foo; * class Bar { Foo *toFoo(); } * class Foo { Bar *toBar(); } * * Since the Objective caml types of Foo and Bar are the same. Now that * I correctly incorporate SWIG's typechecking, this isn't a big deal. * * The class is in the form of a function returning a c_obj. The c_obj * is a C_obj containing a function which invokes a method on the * underlying object given its type. * * The name emitted here is normalized before being sent to * Callback.register, because we need this string to look up properly * when the typemap passes the descriptor string. I've been considering * some, possibly more forgiving method that would do some transformations * on the $descriptor in order to find a potential match. This is for * later. * * Important things to note: * * We rely on exception handling (BadMethodName) in order to call an * ancestor. This can be improved. * * The method used to get :classof could be improved to look at the type * info that the base pointer contains. It's really an error to have a * SWIG-generated object that does not contain type info, since the * existence of the object means that SWIG knows the type. * * :parents could use :classof to tell what class it is and make a better * decision. This could be nice, (i.e. provide a run-time graph of C++ * classes represented);. * * I can't think of a more elegant way of converting a C_obj fun to a * pointer than "operator &"... * * Added a 'sizeof' that will allow you to do the expected thing. * This should help users to fill buffer structs and the like (as is * typical in windows-styled code). It's only enabled if you give * %feature(sizeof) and then, only for simple types. * * Overall, carrying the list of methods and base classes has worked well. * It allows me to give the Ocaml user introspection over their objects. */ int classHandler(Node *n) { String *name = Getattr(n, "name"); classname = Getattr(n, "sym:name"); if (!name) return SWIG_OK; String *mangled_name = mangleNameForCaml(name); String *this_class_def = NewString(f_classtemplate); String *name_normalized = normalizeTemplatedClassName(name); String *old_class_ctors = f_class_ctors; String *base_classes = NewString(""); f_class_ctors = NewString(""); bool sizeof_feature = generate_sizeof && isSimpleType(name); classmode = true; int rv = Language::classHandler(n); classmode = false; if (sizeof_feature) { Printf(f_wrappers, "SWIGEXT value _wrap_%s_sizeof( value args ) {\n" " CAMLparam1(args);\n" " CAMLreturn(Val_int(sizeof(%s)));\n" "}\n", mangled_name, name_normalized); Printf(f_mlbody, "external __%s_sizeof : unit -> int = " "\"_wrap_%s_sizeof\"\n", mangled_name, mangled_name); } /* Insert sizeof operator for concrete classes */ if (sizeof_feature) { Printv(f_class_ctors, "\"sizeof\" , (fun args -> C_int (__", mangled_name, "_sizeof ())) ;\n", NIL); } /* Handle up-casts in a nice way */ List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator b; b = First(baselist); while (b.item) { String *bname = Getattr(b.item, "name"); if (bname) { String *base_create = NewString(""); Printv(base_create, "(create_class \"", bname, "\")", NIL); Printv(f_class_ctors, " \"::", bname, "\", (fun args -> ", base_create, " args) ;\n", NIL); Printv(base_classes, base_create, " ;\n", NIL); } b = Next(b); } } Replaceall(this_class_def, "$classname", mangled_name); Replaceall(this_class_def, "$normalized", name_normalized); Replaceall(this_class_def, "$realname", name); Replaceall(this_class_def, "$baselist", base_classes); Replaceall(this_class_def, "$classbody", f_class_ctors); Delete(f_class_ctors); f_class_ctors = old_class_ctors; // Actually write out the class definition Multiwrite(this_class_def); Setattr(n, "ocaml:ctor", mangled_name); return rv; } String *normalizeTemplatedClassName(String *name) { String *name_normalized = SwigType_typedef_resolve_all(name); bool took_action; do { took_action = false; if (is_a_pointer(name_normalized)) { SwigType_del_pointer(name_normalized); took_action = true; } if (is_a_reference(name_normalized)) { oc_SwigType_del_reference(name_normalized); took_action = true; } if (is_an_array(name_normalized)) { oc_SwigType_del_array(name_normalized); took_action = true; } } while (took_action); return SwigType_str(name_normalized, 0); } /* * Produce the symbol name that ocaml will use when referring to the * target item. I wonder if there's a better way to do this: * (WF - use Swig_name_mangle_string/Swig_name_mangle_type) * * I shudder to think about doing it with a hash lookup, but that would * make a couple of things easier: */ String *mangleNameForCaml(String *s) { String *out = Copy(s); Replaceall(out, " ", "_xx"); Replaceall(out, "::", "_xx"); Replaceall(out, ",", "_x"); Replaceall(out, "+", "_xx_plus"); Replaceall(out, "-", "_xx_minus"); Replaceall(out, "<", "_xx_ldbrace"); Replaceall(out, ">", "_xx_rdbrace"); Replaceall(out, "!", "_xx_not"); Replaceall(out, "%", "_xx_mod"); Replaceall(out, "^", "_xx_xor"); Replaceall(out, "*", "_xx_star"); Replaceall(out, "&", "_xx_amp"); Replaceall(out, "|", "_xx_or"); Replaceall(out, "(", "_xx_lparen"); Replaceall(out, ")", "_xx_rparen"); Replaceall(out, "[", "_xx_lbrace"); Replaceall(out, "]", "_xx_rbrace"); Replaceall(out, "~", "_xx_bnot"); Replaceall(out, "=", "_xx_equals"); Replaceall(out, "/", "_xx_slash"); Replaceall(out, ".", "_xx_dot"); Replaceall(out, "?", "_xx_question"); Replaceall(out, ":", "_xx_colon"); return out; } SwigType *fully_qualified_enum_type(Node *n) { Node *parent = 0; String *fully_qualified_name = NewString(""); String *parent_type = 0; parent = parentNode(n); while (parent) { parent_type = nodeType(parent); if (Getattr(parent, "name")) { String *parent_copy = NewStringf("%s::", Getattr(parent, "name")); if (Cmp(parent_type, "class") == 0 || Cmp(parent_type, "namespace") == 0) Insert(fully_qualified_name, 0, parent_copy); Delete(parent_copy); } if (!Cmp(parent_type, "class")) break; parent = parentNode(parent); } return fully_qualified_name; } /* Benedikt Grundmann inspired --> Enum wrap styles */ int enumvalueDeclaration(Node *n) { String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *qtype = 0; if (name_qualifier_type) { qtype = Copy(name_qualifier_type); Printv(qtype, name, NIL); } if (const_enum && qtype && symname && !Getattr(seen_enumvalues, symname)) { Setattr(seen_enumvalues, symname, "true"); SetFlag(n, "feature:immutable"); Setattr(n, "feature:enumvalue", "1"); // this does not appear to be used Setattr(n, "qualified:name", SwigType_namestr(qtype)); String *evname = SwigType_manglestr(qtype); Insert(evname, 0, "SWIG_ENUM_"); Setattr(n, "feature:enumvname", symname); Setattr(n, "feature:symname", evname); Delete(evname); Printf(f_enumtypes_value, "| `%s\n", symname); return Language::enumvalueDeclaration(n); } else return SWIG_OK; } /* ------------------------------------------------------------------- * This function is a bit uglier than it deserves. * * I used to direct lookup the name of the enum. Now that certain fixes * have been made in other places, the names of enums are now fully * qualified, which is a good thing, overall, but requires me to do * some legwork. * * The other thing that uglifies this function is the varying way that * typedef enum and enum are handled. I need to produce consistent names, * which means looking up and registering by typedef and enum name. */ int enumDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; String *name = Getattr(n, "name"); if (name) { String *oname = NewString(name); /* name is now fully qualified */ String *fully_qualified_name = NewString(name); bool seen_enum = false; if (name_qualifier_type) Delete(name_qualifier_type); char *strip_position; name_qualifier_type = fully_qualified_enum_type(n); strip_position = strstr(Char(oname), "::"); while (strip_position) { strip_position += 2; oname = NewString(strip_position); strip_position = strstr(Char(oname), "::"); } seen_enum = (Getattr(seen_enums, fully_qualified_name) ? true : false); if (!seen_enum) { const_enum = true; Printf(f_enum_to_int, "| `%s -> (match y with\n", oname); Printf(f_int_to_enum, "| `%s -> C_enum (\n", oname); /* * * * A note about enum name resolution * * * * * This code should now work, but I think we can do a bit better. * The problem I'm having is that swig isn't very precise about * typedef name resolution. My opinion is that SwigType_typedef * resolve_all should *always* return the enum tag if one exists, * rather than the admittedly friendlier enclosing typedef. * * This would make one of the cases below unnecessary. * * * */ Printf(f_mlbody, "let _ = Callback.register \"%s_marker\" (`%s)\n", fully_qualified_name, oname); if (!strncmp(Char(fully_qualified_name), "enum ", 5)) { String *fq_noenum = NewString(Char(fully_qualified_name) + 5); Printf(f_mlbody, "let _ = Callback.register \"%s_marker\" (`%s)\n" "let _ = Callback.register \"%s_marker\" (`%s)\n", fq_noenum, oname, fq_noenum, name); } Printf(f_enumtypes_type, "| `%s\n", oname); Insert(fully_qualified_name, 0, "enum "); Setattr(seen_enums, fully_qualified_name, n); } } int ret = Language::enumDeclaration(n); if (const_enum) { Printf(f_int_to_enum, "`Int y)\n"); Printf(f_enum_to_int, "| `Int x -> Swig.C_int x\n" "| _ -> raise (LabelNotFromThisEnum v))\n"); } const_enum = false; return ret; } /* ---------------------------------------------------------------------------- * BEGIN C++ Director Class modifications * ------------------------------------------------------------------------- */ /* * Modified polymorphism code for Ocaml language module. * * TODO * * Move some boilerplate code generation to Swig_...() functions. * */ /* --------------------------------------------------------------- * classDirectorMethod() * * Emit a virtual director method to pass a method call on to the * underlying Python object. * * --------------------------------------------------------------- */ int classDirectorMethod(Node *n, Node *parent, String *super) { int is_void = 0; int is_pointer = 0; String *storage = Getattr(n, "storage"); String *value = Getattr(n, "value"); String *decl = Getattr(n, "decl"); SwigType *returntype = Getattr(n, "type"); String *name = Getattr(n, "name"); String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *symname = Getattr(n, "sym:name"); String *declaration = NewString(""); ParmList *l = Getattr(n, "parms"); Wrapper *w = NewWrapper(); String *tm; String *wrap_args = NewString(""); int status = SWIG_OK; int idx; bool pure_virtual = false; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { pure_virtual = true; } } Printf(w->locals, "CAMLparam0();\n"); /* determine if the method returns a pointer */ is_pointer = SwigType_ispointer_return(decl); is_void = (!Cmp(returntype, "void") && !is_pointer); /* virtual method definition */ String *target; String *pclassname = NewStringf("SwigDirector_%s", classname); String *qualified_name = NewStringf("%s::%s", pclassname, name); SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); /* header declaration */ target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Get any exception classes in the throws typemap if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = 0; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { Parm *p; int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } String *str = SwigType_str(Getattr(p, "type"), 0); Append(w->def, str); Append(declaration, str); Delete(str); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); /* declare method return value * if the return value is a reference or const reference, a specialized typemap must * handle it, including declaration of c_result ($result). */ if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (ignored_method) { if (!pure_virtual) { String *super_call = Swig_method_call(super, l); if (is_void) Printf(w->code, "%s;\n", super_call); else Printf(w->code, "CAMLreturnT(%s, %s);\n", SwigType_str(returntype, 0), super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); } } else { Wrapper_add_local(w, "swig_result", "CAMLlocal2(swig_result, args)"); /* attach typemaps to arguments (C/C++ -> Ocaml) */ String *arglist = NewString(""); Swig_director_parms_fixup(l); Swig_typemap_attach_parms("in", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("directorargout", l, w); Parm *p; int num_arguments = emit_num_arguments(l); int i; char source[256]; /* build argument list and type conversion string */ for (i = 0, idx = 0, p = l; i < num_arguments; i++) { String *pname = Getattr(p, "name"); String *ptype = Getattr(p, "type"); Putc(',', arglist); if ((tm = Getattr(p, "tmap:directorin")) != 0) { Setattr(p, "emit:directorinput", pname); Replaceall(tm, "$input", pname); Replaceall(tm, "$owner", "0"); if (Len(tm) == 0) Append(tm, pname); Printv(wrap_args, tm, "\n", NIL); p = Getattr(p, "tmap:directorin:next"); continue; } else if (Cmp(ptype, "void")) { /* special handling for pointers to other C++ director classes. * ideally this would be left to a typemap, but there is currently no * way to selectively apply the dynamic_cast<> to classes that have * directors. in other words, the type "SwigDirector_$1_lname" only exists * for classes with directors. we avoid the problem here by checking * module.wrap::directormap, but it's not clear how to get a typemap to * do something similar. perhaps a new default typemap (in addition * to SWIGTYPE) called DIRECTORTYPE? */ if (SwigType_ispointer(ptype) || SwigType_isreference(ptype)) { Node *module = Getattr(parent, "module"); Node *target = Swig_directormap(module, ptype); sprintf(source, "obj%d", idx++); String *nonconst = 0; /* strip pointer/reference --- should move to Swig/stype.c */ String *nptype = NewString(Char(ptype) + 2); /* name as pointer */ String *ppname = Copy(pname); if (SwigType_isreference(ptype)) { Insert(ppname, 0, "&"); } /* if necessary, cast away const since Python doesn't support it! */ if (SwigType_isconst(nptype)) { nonconst = NewStringf("nc_tmp_%s", pname); String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(ptype, 0), ppname); Wrapper_add_localv(w, nonconst, SwigType_lstr(ptype, 0), nonconst, nonconst_i, NIL); Delete(nonconst_i); Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number, "Target language argument '%s' discards const in director method %s::%s.\n", SwigType_str(ptype, pname), SwigType_namestr(c_classname), SwigType_namestr(name)); } else { nonconst = Copy(ppname); } Delete(nptype); Delete(ppname); String *mangle = SwigType_manglestr(ptype); if (target) { String *director = NewStringf("director_%s", mangle); Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); Wrapper_add_localv(w, source, "value", source, "= Val_unit", NIL); Printf(wrap_args, "%s = dynamic_cast(%s);\n", director, nonconst); Printf(wrap_args, "if (!%s) {\n", director); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); Printf(wrap_args, "} else {\n"); Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director); Printf(wrap_args, "}\n"); Delete(director); Printv(arglist, source, NIL); } else { Wrapper_add_localv(w, source, "value", source, "= Val_unit", NIL); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); //Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE_p_%s, 0);\n", // source, nonconst, base); Printv(arglist, source, NIL); } Delete(mangle); Delete(nonconst); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_NOWRAP; break; } } p = nextSibling(p); } Printv(w->code, "swig_result = Val_unit;\n", 0); Printf(w->code, "args = Val_unit;\n"); /* wrap complex arguments to values */ Printv(w->code, wrap_args, NIL); /* pass the method call on to the OCaml object */ Printv(w->code, "swig_result = caml_swig_alloc(1,C_list);\n" "Store_field(swig_result,0,args);\n" "args = swig_result;\n" "swig_result = Val_unit;\n", 0); Printf(w->code, "static const value *swig_ocaml_func_val = NULL;\n" "if (!swig_ocaml_func_val) {\n"); Printf(w->code, " swig_ocaml_func_val = caml_named_value(\"swig_runmethod\");\n }\n"); Printf(w->code, "swig_result = caml_callback3(*swig_ocaml_func_val,swig_get_self(),caml_copy_string(\"%s\"),args);\n", Getattr(n, "name")); /* exception handling */ tm = Swig_typemap_lookup("director:except", n, Swig_cresult_name(), 0); if (!tm) { tm = Getattr(n, "feature:director:except"); } if ((tm) && Len(tm) && (Strcmp(tm, "1") != 0)) { Printf(w->code, "if (!%s) {\n", Swig_cresult_name()); Printf(w->code, " value error = *caml_named_value(\"director_except\");\n"); Replaceall(tm, "$error", "error"); Printv(w->code, Str(tm), "\n", NIL); Printf(w->code, "}\n"); } /* * Python method may return a simple object, or a tuple. * for in/out arguments, we have to extract the appropriate values from the * argument list, then marshal everything back to C/C++ (return value and * output arguments). */ /* marshal return value and other outputs (if any) from value to C/C++ * type */ String *cleanup = NewString(""); String *outarg = NewString(""); tm = Swig_typemap_lookup("directorout", n, "c_result", w); if (tm != 0) { Replaceall(tm, "$input", "swig_result"); /* TODO check this */ if (Getattr(n, "wrap:disown")) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Replaceall(tm, "$result", "c_result"); Printv(w->code, tm, "\n", NIL); } /* marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { Replaceall(tm, "$result", "swig_result"); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } Delete(arglist); Delete(cleanup); Delete(outarg); } /* any existing helper functions to handle this? */ if (!is_void) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { Printf(w->code, "CAMLreturnT(%s, (%s)c_result);\n", rettype, rettype); } else { Printf(w->code, "CAMLreturnT(%s, (%s)*c_result);\n", rettype, rettype); } Delete(rettype); } } else { Printf(w->code, "CAMLreturn0;\n"); } Printf(w->code, "}\n"); // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK) { Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } /* clean up */ Delete(wrap_args); Delete(pclassname); DelWrapper(w); return status; } /* ------------------------------------------------------------ * classDirectorConstructor() * ------------------------------------------------------------ */ int classDirectorConstructor(Node *n) { Node *parent = Getattr(n, "parentNode"); String *sub = NewString(""); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *classname = NewString(""); Printf(classname, "SwigDirector_%s", supername); /* insert self parameter */ Parm *p, *q; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("value"); p = NewParm(type, NewString("self"), n); q = Copy(p); set_nextSibling(q, superparms); set_nextSibling(p, parms); parms = p; if (!Getattr(n, "defaultargs")) { /* constructor */ { Wrapper *w = NewWrapper(); String *call; String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, classname, parms, 0); call = Swig_csuperclass_call(0, basetype, superparms); Printf(w->def, "%s::%s: %s, Swig::Director(self) { }", classname, target, call); Delete(target); Wrapper_print(w, f_directors); Delete(call); DelWrapper(w); } /* constructor header */ { String *target = Swig_method_decl(0, decl, classname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Setattr(n, "parms", q); Language::classDirectorConstructor(n); Delete(sub); Delete(classname); Delete(supername); //Delete(parms); return SWIG_OK; } /* ------------------------------------------------------------ * classDirectorDefaultConstructor() * ------------------------------------------------------------ */ int classDirectorDefaultConstructor(Node *n) { String *classname; classname = Swig_class_name(n); /* insert self parameter */ Parm *p, *q; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("value"); p = NewParm(type, NewString("self"), n); q = Copy(p); set_nextSibling(p, parms); { Wrapper *w = NewWrapper(); Printf(w->def, "SwigDirector_%s::SwigDirector_%s(value self) : Swig::Director(self) { }", classname, classname); Wrapper_print(w, f_directors); DelWrapper(w); } Printf(f_directors_h, " SwigDirector_%s(value self);\n", classname); Delete(classname); Setattr(n, "parms", q); return Language::classDirectorDefaultConstructor(n); } int classDirectorInit(Node *n) { String *declaration = Swig_director_declaration(n); Printf(f_directors_h, "\n" "%s\n" "public:\n", declaration); Delete(declaration); return Language::classDirectorInit(n); } int classDirectorEnd(Node *n) { Printf(f_directors_h, "};\n\n"); return Language::classDirectorEnd(n); } /* --------------------------------------------------------------------- * typedefHandler * * This is here in order to maintain the correct association between * typedef names and enum names. * * Since I implement enums as polymorphic variant tags, I need to call * back into ocaml to evaluate them. This requires a string that can * be generated in the typemaps, and also at SWIG time to be the same * string. The problem that arises is that SWIG variously generates * enum e_name_tag * e_name_tag * e_typedef_name * for * typedef enum e_name_tag { ... } e_typedef_name; * * Since I need these strings to be consistent, I must maintain a correct * association list between typedef and enum names. * --------------------------------------------------------------------- */ int typedefHandler(Node *n) { String *type = Getattr(n, "type"); Node *enum_node = type ? Getattr(seen_enums, type) : 0; if (enum_node) { String *name = Getattr(enum_node, "name"); Printf(f_mlbody, "let _ = Callback.register \"%s_marker\" (`%s)\n", Getattr(n, "name"), name); } return SWIG_OK; } String *runtimeCode() { String *s = Swig_include_sys("ocamlrun.swg"); if (!s) { Printf(stderr, "*** Unable to open 'ocamlrun.swg'\n"); s = NewString(""); } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigocamlrun.h"); } }; /* ------------------------------------------------------------------------- * swig_ocaml() - Instantiate module * ------------------------------------------------------------------------- */ static Language *new_swig_ocaml() { return new OCAML(); } extern "C" Language *swig_ocaml(void) { return new_swig_ocaml(); } swig-4.4.0/Source/Modules/c.cxx0000664000175000017500000033017615075443613016210 0ustar williamwilliam/* ----------------------------------------------------------------------------- * 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. * * c.cxx * * C language module for SWIG. * ----------------------------------------------------------------------------- */ #include #include "swigmod.h" extern "C" { extern int UseWrapperSuffix; } int SwigType_isbuiltin(SwigType *t) { const char* builtins[] = { "void", "short", "int", "long", "char", "float", "double", "bool", 0 }; int i = 0; char *c = Char(t); if (!t) return 0; while (builtins[i]) { if (strcmp(c, builtins[i]) == 0) return 1; i++; } return 0; } // Private helpers, could be made public and reused from other language modules in the future. namespace { enum exceptions_support { exceptions_support_enabled, // Default value in C++ mode. exceptions_support_disabled, // Not needed at all. exceptions_support_imported // Needed, but already defined in an imported module. }; // When using scoped_dohptr, it's very simple to accidentally pass it to a vararg function, such as Printv() or Printf(), resulting in catastrophic results // during run-time (crash or, worse, junk in the generated output), so make sure gcc warning about this, which is not enabled by default for some reason (see // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64867 for more information), is enabled. #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic error "-Wconditionally-supported" #endif // __GNUC__ // Delete a DOH object on scope exit. class scoped_dohptr { public: scoped_dohptr() : obj_(NULL) {} explicit scoped_dohptr(DOH* obj) : obj_(obj) {} ~scoped_dohptr() { Delete(obj_); } // This is an std::auto_ptr<>-like "destructive" copy ctor which allows to return objects of this type from functions. scoped_dohptr(scoped_dohptr const& other) : obj_(other.release()) {} // Same for the assignment operator. scoped_dohptr& operator=(scoped_dohptr const& other) { reset(other.release()); return *this; } // Assignment operator takes ownership of the pointer, just as the ctor does. scoped_dohptr& operator=(DOH* obj) { reset(obj); return *this; } DOH* get() const { return obj_; } DOH* release() const /* not really */ { DOH* obj = obj_; const_cast(const_cast(this)->obj_) = NULL; return obj; } void reset(DOH* obj = NULL) { if (obj != obj_) { Delete(obj_); obj_ = obj; } } operator DOH*() const { return obj_; } protected: DOH* obj_; }; // Wrapper for a DOH object which can be owned or not. class maybe_owned_dohptr : public scoped_dohptr { public: explicit maybe_owned_dohptr(DOH* obj = NULL) : scoped_dohptr(obj), owned_(true) {} maybe_owned_dohptr(maybe_owned_dohptr const& other) : scoped_dohptr(other) { owned_ = other.owned_; // We can live other.owned_ unchanged, as its pointer is null now anyhow. } maybe_owned_dohptr& operator=(maybe_owned_dohptr const& other) { reset(other.release()); owned_ = other.owned_; return *this; } ~maybe_owned_dohptr() { if (!owned_) obj_ = NULL; // Prevent it from being deleted by the base class dtor. } void assign_owned(DOH* obj) { reset(obj); } void assign_non_owned(DOH* obj) { reset(obj); owned_ = false; } private: bool owned_; }; // Helper class setting the given pointer to the given value in its ctor and resetting it in the dtor. // // Used to non-intrusively set a pointer to some object only during this object life-time. template class temp_ptr_setter { public: // Pointer must be non-null, its current value is restored when this object is destroyed. temp_ptr_setter(T* ptr, T value) : ptr_(ptr), value_orig_(*ptr) { *ptr_ = value; } ~temp_ptr_setter() { *ptr_ = value_orig_; } private: T* const ptr_; T const value_orig_; // Non copyable. temp_ptr_setter(const temp_ptr_setter&); temp_ptr_setter& operator=(const temp_ptr_setter&); }; // Helper class to output "begin" fragment in the ctor and "end" in the dtor. class begin_end_output_guard { public: begin_end_output_guard(File* f, const_String_or_char_ptr begin, const_String_or_char_ptr end) : f_(f), end_(NewString(end)) { String* const s = NewString(begin); Dump(s, f_); Delete(s); } ~begin_end_output_guard() { Dump(end_, f_); Delete(end_); } private: // Non copyable. begin_end_output_guard(const begin_end_output_guard&); begin_end_output_guard& operator=(const begin_end_output_guard&); File* const f_; String* const end_; }; // Subclass to output extern "C" guards when compiling as C++. class cplusplus_output_guard : private begin_end_output_guard { public: explicit cplusplus_output_guard(File* f) : begin_end_output_guard( f, "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n\n", "#ifdef __cplusplus\n" "}\n" "#endif\n\n" ) { } }; // String containing one indentation level for the generated code. const char* const cindent = " "; // Returns the non-owned string to the name of the class or enum to use in C wrappers. String* get_c_proxy_name(Node* n) { String *proxyname = Getattr(n, "proxyname"); if (!proxyname) { String *symname = Getattr(n, "sym:name"); String *nspace = Getattr(n, "sym:nspace"); if (nspace) { scoped_dohptr nspace_mangled(Swig_name_mangle_string(nspace)); proxyname = NewStringf("%s_%s", (DOH*)nspace_mangled, symname); } else { proxyname = Swig_name_type(symname); } Setattr(n, "proxyname", proxyname); Delete(proxyname); // It stays alive because it's referenced by the hash. } return proxyname; } // Returns the first named "import" node under the given one (which must be non-NULL). May return NULL. Node* find_first_named_import(Node* parent) { for (Node* n = firstChild(parent); n; n = nextSibling(n)) { if (Cmp(nodeType(n), "import") == 0) { // We've almost succeeded, but there are sometimes some weird unnamed import modules that don't really count for our purposes, so skip them. if (Getattr(n, "module")) return n; } else if (Cmp(nodeType(n), "include") == 0) { // Recurse into this node as included files may contain imports too. if (Node* const import = find_first_named_import(n)) return import; } else { // We consider that import nodes can only occur in the global scope, some don't bother recursing here. If this turns out to be false, we'd just need to // start doing it. } } return NULL; } /* Information about the function return type. */ class cxx_rtype_desc { public: // Default ctor creates a "void" return type. cxx_rtype_desc() {} // If this returns true, get_return_code() can't be called. bool is_void() const { return !type_; } // This function must be called before calling get_return_code(). void set_type(String* type) { type_ = Copy(type); } // This function must also be called before calling get_return_code(). // // NB: It takes ownership of the string, the intended use is to pass it NewStringf(...). void set_return_value(String* new_string) { value_ = new_string; } // This function applies the given typemap, which must set $result variable from $cresult containing the result of C wrapper function call. // // If it is not called, the trivial "$result = $cresult" typemap is used and, in fact, the extra variables are optimized away and just "return $cresult" is // generated directly for brevity. // // If the string doesn't start with "$result = ", it is prepended to it implicitly, for convenience. // // NB: It takes ownership of the string, which is typically returned from Swig_typemap_lookup(). void apply_out_typemap(String* new_out_tm_string) { out_tm_ = new_out_tm_string; } // Return the function return type: can always be called, even for void functions (for which it just returns "void"). String* type() const { return type_ ? type_ : get_void_type(); } // Return the string containing the code for returning the value, previously set by set_return_value(). // // The returned string ends with a semicolon, i.e. is a complete statement (or possibly more than one). // // Asserts unless both set_type() and set_return_value() had been called. scoped_dohptr get_return_code() const { assert(type_); assert(value_); if (!out_tm_) { // Trivial case when we return the same value, just do it. // // Add extra spaces after/before opening/closing braces because we keep everything on the same line in this case. return scoped_dohptr(NewStringf(" return %s; ", value_.get())); } // We need to start by introducing a temporary variable for the C call result because if $cresult is used twice by the typemap, we don't want to call the // function twice. Note that just "auto" is enough because C functions can't return references, but we need "auto&&" for C++ result which can be anything // (defined by the user in their typemaps). scoped_dohptr code(NewStringf( "\n" "%sauto swig_cres = %s;\n", cindent, value_.get() )); // We support 2 cases: either typemap is a statement, or multiple statements, containing assignment to $result, in which case this assignment must occur at // its beginning. bool const has_result = strstr(Char(out_tm_), "$result = ") != NULL; if (has_result) { Printv(code, cindent, "auto&& ", NIL); } else { // Or the typemap is just an expression, which can be returned directly, without defining $result at all. Note that this is more than an optimization as // it allows the generated code to work even with non-copyable classes. Printv(code, cindent, "return ", NIL); } // Skip leading whitespace and chop the trailing whitespace from the typemap to keep indentation consistent. const char* tm = Char(out_tm_); while (isspace(*tm)) ++tm; Append(code, tm); Chop(code); if ((Char(code))[Len(code) - 1] != ';') Append(code, ";"); Replaceall(code, "$cresult", "swig_cres"); if (has_result) { Printf(code, "\n%sreturn $result;\n", cindent); Replaceall(code, "$result", "swig_cxxres"); } else { Append(code, "\n"); } return code; } private: static String* get_void_type() { static String* const void_type = NewString("void"); return void_type; } scoped_dohptr type_; scoped_dohptr value_; scoped_dohptr out_tm_; }; /* Information about a function parameter. This is similar to cxx_rtype_desc, but is used for the parameters and not the return type. */ class cxx_ptype_desc { public: // Ctor initializes the object to an empty/unknown state, call set_type() later to finish initialization. cxx_ptype_desc() {} // This function must be called (with a non-null string) before calling get_param_code(). void set_type(String* type) { type_ = Copy(type); } // If this one returns NULL, it means that we don't have any type information at all. String* type() const { return type_; } // This may be called before calling get_param_code() if a translation from C++ to C type is necessary. By default the parameter is passed "as is". void apply_in_typemap(String* new_in_tm_string) { in_tm_ = new_in_tm_string; } // Return the full expression needed to pass the given value as parameter to C wrapper function. scoped_dohptr get_param_code(String* value) const { assert(type_); if (!in_tm_) { return scoped_dohptr(Copy(value)); } // There doesn't seem to be any simple way to use the full SWIG typemap expansion machinery here, so just do it manually. scoped_dohptr code(Copy(in_tm_)); Replace(code, "$1", value, DOH_REPLACE_NUMBER_END); return code; } private: scoped_dohptr type_; scoped_dohptr in_tm_; }; /* Struct containing information needed only for generating C++ wrappers. */ struct cxx_wrappers { // Default ctor doesn't do anything, use initialize() if C++ wrappers really need to be generated. cxx_wrappers() : except_check_start(NULL), except_check_end(NULL), sect_cxx_h(NULL), sect_types(NULL), sect_decls(NULL), sect_impls(NULL) { node_func_ = NULL; rtype_desc_ = NULL; ptype_desc_ = NULL; } void initialize() { sect_cxx_h = NewStringEmpty(); sect_types = NewStringEmpty(); sect_decls = NewStringEmpty(); sect_impls = NewStringEmpty(); // Allow using SWIG directive to inject code here. Swig_register_filebyname("cxxheader", sect_cxx_h); Swig_register_filebyname("cxxcode", sect_impls); } // This function must be called after initialize(). The two can't be combined because we don't yet know if we're going to use exceptions or not when we // initialize the object of this class in C::main(), so this one is called later from C::top(). void initialize_exceptions(exceptions_support support) { switch (support) { case exceptions_support_enabled: case exceptions_support_imported: except_check_start = "swig_check("; except_check_end = ")"; break; case exceptions_support_disabled: except_check_start = except_check_end = ""; break; } } bool is_initialized() const { return sect_types != NULL; } bool is_exception_support_enabled() const { return *except_check_start != '\0'; } // All the functions below are only used when is_initialized() returns true. // Fill the provided rtype_desc with the type information for the given function node. // // Returns false in case of error, i.e. if function wrapper can't be generated at all. bool lookup_cxx_ret_type(cxx_rtype_desc& rtype_desc, Node* n) { String* const func_type = Getattr(n, "type"); if (SwigType_type(func_type) == T_VOID) { // Nothing to do, rtype_desc is void by default. return true; } // As above, ensure our replaceSpecialVariables() is used. temp_ptr_setter set(&rtype_desc_, &rtype_desc); bool use_cxxout = true; String* type(Swig_typemap_lookup("cxxouttype", n, "", NULL)); if (!type) { use_cxxout = false; type = Swig_typemap_lookup("ctype", n, "", NULL); } if (!type) { Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, Getfile(n), Getline(n), "No ctype typemap defined for the return type \"%s\" of %s\n", SwigType_str(func_type, NULL), Getattr(n, "sym:name") ); return false; } if (!do_resolve_type(n, func_type, type, NULL, &rtype_desc)) return false; if (use_cxxout) { if (String* out_tm = Swig_typemap_lookup("cxxout", n, "", NULL)) rtype_desc.apply_out_typemap(out_tm); } return true; } // Return the type description for the given parameter of the function. bool lookup_cxx_parm_type(cxx_ptype_desc& ptype_desc, Node* n, Parm* p) { // Ensure our own replaceSpecialVariables() is used for $typemap() expansion. temp_ptr_setter set(&ptype_desc_, &ptype_desc); bool use_cxxin = true; String* type = Swig_typemap_lookup("cxxintype", p, "", NULL); if (!type) { use_cxxin = false; type = Swig_typemap_lookup("ctype", p, "", NULL); } if (!type) { Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, Getfile(p), Getline(p), "No ctype typemap defined for the parameter \"%s\" of %s\n", Getattr(p, "name"), Getattr(n, "sym:name") ); return false; } if (!do_resolve_type(n, Getattr(p, "type"), type, &ptype_desc, NULL)) return false; if (use_cxxin) { if (String* in_tm = Getattr(p, "tmap:cxxin")) ptype_desc.apply_in_typemap(Copy(in_tm)); } return true; } // This function is called from C::replaceSpecialVariables() but only does something non-trivial when it's called by our own lookup_cxx_xxx_type() functions. bool replaceSpecialVariables(String *method, String *tm, Parm *parm) { if (!ptype_desc_ && !rtype_desc_) return false; if (Cmp(method, "ctype") != 0) { Swig_warning(WARN_C_UNSUPPORTTED, input_file, line_number, "Unsupported %s typemap %s\n", method, tm); return false; } if (SwigType *type = Getattr(parm, "type")) { if (ptype_desc_) ptype_desc_->set_type(type); if (rtype_desc_) rtype_desc_->set_type(type); if (!do_resolve_type(node_func_, type, tm, ptype_desc_, rtype_desc_)) return false; } return true; } // Used for generating exception checks around the calls, see initialize_exceptions(). const char* except_check_start; const char* except_check_end; // The order of the members here is the same as the order in which they appear in the output file. // This section doesn't contain anything by default but can be used by typemaps etc. It is the only section outside of the namespace in which all the other // declaration live. String* sect_cxx_h; // This section contains forward declarations of the classes. String* sect_types; // Full declarations of the classes. String* sect_decls; // Implementation of the classes. String* sect_impls; private: // Replace "resolved_type" occurrences in the string with the value corresponding to the given type. // // Note that the node here is the function itself, but type may be either its return type or the type of one of its parameters, so it's passed as a different // parameter. // // Also fills in the start/end wrapper parts of the provided type descriptions if they're not null, with the casts needed to translate from C type to C++ type // (this is used for the parameters of C++ functions, hence the name) and from C types to C++ types (which is used for the function return values). static bool do_resolve_type(Node* n, String* type, String* s, cxx_ptype_desc* ptype_desc, cxx_rtype_desc* rtype_desc) { enum TypeKind { Type_Ptr, Type_Ref, Type_Obj, Type_Enm, Type_Max } typeKind = Type_Max; // These correspond to the typemaps for SWIGTYPE*, SWIGTYPE&, SWIGTYPE and enum SWIGTYPE, respectively, defined in c.swg. static const char* typemaps[Type_Max] = { "$resolved_type*", "$*resolved_type*", "$&resolved_type*", "$resolved_type", }; for (int i = 0; i < Type_Max; ++i) { if (Strstr(s, typemaps[i])) { typeKind = static_cast(i); break; } } if (typeKind == Type_Max) { if (Strstr(s, "resolved_type")) { Swig_warning(WARN_C_UNSUPPORTTED, input_file, line_number, "Unsupported typemap \"%s\" used for type \"%s\" of \"%s\"\n", s, type, Getattr(n, "name") ); return false; } // Nothing else needed. if (rtype_desc) rtype_desc->set_type(s); if (ptype_desc) ptype_desc->set_type(s); return true; } // The logic here is somewhat messy because we use the same "$resolved_type*" typemap for pointers/references to both enums and classes, but we actually // need to do quite different things for them. It could probably be simplified by changing the typemaps to be distinct, but this would require also updating // the code for C wrappers generation in substituteResolvedTypeSpecialVariable(). // // An even better idea might be to try to define this using cxx{in,out} typemaps for the various types and let the generic SWIG machinery do all the // matching instead of doing it in the code here. scoped_dohptr resolved_type(SwigType_typedef_resolve_all(type)); scoped_dohptr base_resolved_type(SwigType_base(resolved_type)); scoped_dohptr typestr; if (SwigType_isenum(base_resolved_type)) { String* enumname = NULL; if (Node* const enum_node = Language::instance()->enumLookup(base_resolved_type)) { // This is the name of the enum in C wrappers, it should be already set by getEnumName(). enumname = Getattr(enum_node, "enumname"); if (enumname) { String* const enum_symname = Getattr(enum_node, "sym:name"); if (Checkattr(enum_node, "ismember", "1")) { Node* const parent_class = parentNode(enum_node); typestr = NewStringf("%s::%s", Getattr(parent_class, "sym:name"), enum_symname); } else { typestr = Copy(enum_symname); } } } if (!enumname) { // Unknown enums are mapped to int and no casts are necessary in this case. typestr = NewString("int"); } if (SwigType_ispointer(type)) Append(typestr, " *"); else if (SwigType_isreference(type)) Append(typestr, " &"); if (enumname) { switch (typeKind) { case Type_Ptr: if (rtype_desc) { rtype_desc->apply_out_typemap(NewStringf("(%s)$cresult", typestr.get())); } if (ptype_desc) { ptype_desc->apply_in_typemap(NewStringf("(%s*)$1", enumname)); } break; case Type_Ref: if (rtype_desc) { rtype_desc->apply_out_typemap(NewStringf("(%s)(*($cresult))", typestr.get())); } if (ptype_desc) { ptype_desc->apply_in_typemap(NewStringf("(%s*)&($1)", enumname)); } break; case Type_Enm: if (rtype_desc) { rtype_desc->apply_out_typemap(NewStringf("(%s)$cresult", typestr.get())); } if (ptype_desc) { ptype_desc->apply_in_typemap(NewStringf("(%s)$1", enumname)); } break; case Type_Obj: case Type_Max: // Unreachable, but keep here to avoid -Wswitch warnings. assert(0); } } else { // This is the only thing we need to do even when we don't have the enum name. if (typeKind == Type_Ref && ptype_desc) ptype_desc->apply_in_typemap(NewString("&($1)")); } } else { String* classname; if (Node* const class_node = Language::instance()->classLookup(type)) { // Deal with some special cases: switch (typeKind) { case Type_Ptr: // If this is a pointer passed by const reference, we return just the pointer directly because we don't have any pointer-valued variable to give out // a reference to. if (strncmp(Char(resolved_type), "r.q(const).", 11) == 0) { scoped_dohptr deref_type(Copy(resolved_type)); Delslice(deref_type, 0, 11); typestr = SwigType_str(deref_type, 0); } break; case Type_Obj: // Const objects are just objects for our purposes here, remove the const from them to avoid having "const const" in the output. if (SwigType_isconst(resolved_type)) SwigType_del_qualifier(resolved_type); break; case Type_Ref: case Type_Enm: case Type_Max: // Nothing special to do. break; } if (!typestr) typestr = SwigType_str(resolved_type, 0); classname = Getattr(class_node, "sym:name"); // We don't use namespaces, but the type may contain them, so get rid of them by replacing the base type name, which is fully qualified, with just the // class name, which is not. scoped_dohptr basetype(SwigType_base(resolved_type)); scoped_dohptr basetypestr(SwigType_str(basetype, 0)); if (Cmp(basetypestr, classname) != 0) { Replaceall(typestr, basetypestr, classname); } } else { classname = NULL; } if (!classname) { Swig_warning(WARN_C_UNSUPPORTTED, input_file, line_number, "Unsupported C++ wrapper function %s type \"%s\"\n", ptype_desc ? "parameter" : "return", SwigType_str(type, 0) ); return false; } const char* const owns = GetFlag(n, "feature:new") ? "true" : "false"; switch (typeKind) { case Type_Ptr: if (ptype_desc) { ptype_desc->apply_in_typemap(NewString("$1->swig_self()")); } if (rtype_desc) { rtype_desc->apply_out_typemap(NewStringf( "$cresult ? new %s($cresult, %s) : nullptr;", classname, owns )); } break; case Type_Ref: if (rtype_desc) { // We can't return a reference, as this requires an existing object and we don't have any, so we have to return an object instead, and this object // must be constructed using the special ctor not taking the pointer ownership. typestr = Copy(classname); rtype_desc->apply_out_typemap(NewStringf("%s{$cresult, false}", classname)); } if (ptype_desc) { ptype_desc->apply_in_typemap(NewString("$1.swig_self()")); } break; case Type_Obj: if (rtype_desc) { // The pointer returned by C function wrapping a function returning an object should never be null unless an exception happened, so we don't test // for it here, unlike in Type_Ptr case. // // Also, normally all returned objects should be owned by their wrappers, but there is a special case of objects not being returned by value: this // seems not to make sense, but can actually happen when typemaps map references or pointers to objects, like they do for e.g. shared_ptr<>. // // Note that we must use the type of the function, retrieved from its node, here and not the type passed to us which is the result of typemap // expansion and so may not be a reference any more. rtype_desc->apply_out_typemap(NewStringf("%s{$cresult, %s}", typestr.get(), SwigType_isreference(Getattr(n, "type")) ? owns : "true" )); } if (ptype_desc) { // It doesn't seem like it can ever be useful to pass an object by value to a wrapper function and it can fail if it doesn't have a copy ctor (see // code related to has_copy_ctor_ in our dtor above), so always pass it by const reference instead. Append(typestr, " const&"); ptype_desc->apply_in_typemap(NewString("$1.swig_self()")); } break; case Type_Enm: case Type_Max: // Unreachable, but keep here to avoid -Wswitch warnings. assert(0); } } Replaceall(s, typemaps[typeKind], typestr); if (rtype_desc) rtype_desc->set_type(s); if (ptype_desc) ptype_desc->set_type(s); return true; } // These pointers are temporarily set to non-null value only while expanding a typemap for C++ wrappers, see replaceSpecialVariables(). cxx_ptype_desc* ptype_desc_; cxx_rtype_desc* rtype_desc_; // This one is set from the outside, so make it public for simplicity. public: Node* node_func_; }; /* cxx_function_wrapper Outputs the C++ wrapper function. It's different from the C function because it is declared inside the namespace and so doesn't need the usual prefix and may also have different parameter and return types when objects and/or cxx{in,out}type typemaps are involved. */ class cxx_function_wrapper { public: // Call can_wrap() to check if this wrapper can be emitted later. explicit cxx_function_wrapper(cxx_wrappers& cxx_wrappers, Node* n, Parm* p) : cxx_wrappers_(cxx_wrappers) { func_node = NULL; except_check_start = except_check_end = ""; if (Checkattr(n, "feature:cxxignore", "1")) return; // Usually generating wrappers for overloaded methods is fine, but sometimes their types can clash after applying typemaps and in this case we have no // choice but to avoid generating them, as otherwise we'd just generate uncompilable code. if (Getattr(n, "sym:overloaded")) { Swig_overload_check(n); if (Getattr(n, "overload:ignore")) return; } if (!cxx_wrappers_.lookup_cxx_ret_type(rtype_desc, n)) return; parms_cxx = NewStringEmpty(); parms_call = NewStringEmpty(); if (p) { // We want to use readable parameter names in our wrappers instead of the autogenerated arg$N if possible, so do it, and do it before calling // Swig_typemap_attach_parms(), as this uses the parameter names for typemap expansion. for (Parm* p2 = p; p2; p2 = nextSibling(p2)) { String* name = Getattr(p, "name"); if (!name) { // Can't do anything for unnamed parameters. continue; } // Static variables use fully qualified names, so we need to strip the scope from them. scoped_dohptr name_ptr; if (Strstr(name, "::")) { name_ptr = Swig_scopename_last(name); name = name_ptr.get(); } Setattr(p, "lname", name); } Swig_typemap_attach_parms("cxxin", p, NULL); for (; p; p = Getattr(p, "tmap:in:next")) { if (Checkattr(p, "tmap:in:numinputs", "0")) continue; String* const name = Getattr(p, "lname"); cxx_ptype_desc ptype_desc; if (!cxx_wrappers_.lookup_cxx_parm_type(ptype_desc, n, p)) return; if (Len(parms_cxx)) Append(parms_cxx, ", "); Printv(parms_cxx, ptype_desc.type(), " ", name, NIL); if (Len(parms_call)) Append(parms_call, ", "); Append(parms_call, ptype_desc.get_param_code(name)); } } // Avoid checking for exceptions unnecessarily. Note that this is more than an optimization: we'd get into infinite recursion if we checked for exceptions // thrown by members of SWIG_CException itself if we didn't do it. if (cxx_wrappers_.is_exception_support_enabled() && !Checkattr(n, "noexcept", "true") && (!Checkattr(n, "throw", "1") || Getattr(n, "throws"))) { except_check_start = cxx_wrappers_.except_check_start; except_check_end = cxx_wrappers_.except_check_end; } // Everything is fine, so set func_node to indicate success. func_node = n; } bool can_wrap() const { return func_node != NULL; } // Emit just the function body, including the braces around it. // // This helper is used both by our emit() and emit_member_function(). void emit_body(String* wparms) { String* const wname = Getattr(func_node, "wrap:name"); Append(cxx_wrappers_.sect_impls, "{"); if (rtype_desc.is_void()) { Printv(cxx_wrappers_.sect_impls, " ", wname, "(", wparms, "); ", NIL ); if (*except_check_start != '\0') { Printv(cxx_wrappers_.sect_impls, except_check_start, except_check_end, "; ", NIL ); } } else { rtype_desc.set_return_value(NewStringf("%s%s(%s)%s", except_check_start, wname, wparms, except_check_end)); Append(cxx_wrappers_.sect_impls, rtype_desc.get_return_code()); } Append(cxx_wrappers_.sect_impls, "}\n"); } // Do emit the function wrapper. void emit() { // The wrapper function name should be sym:name, but we change it to include the namespace prefix in our own globalvariableHandler(), so now we have to undo // this by using the value saved there, if available. This is definitely clumsy and it would be better to avoid it, but this would probably need to be done // by separating C and C++ wrapper generation in two different passes and so would require significantly more changes than this hack. String* name = Getattr(func_node, "c:globalvariableHandler:sym:name"); if (!name) name = Getattr(func_node, "sym:name"); Printv(cxx_wrappers_.sect_impls, "inline ", rtype_desc.type(), " ", name, "(", parms_cxx.get(), ") ", NIL ); emit_body(parms_call); } cxx_wrappers& cxx_wrappers_; Node* func_node; cxx_rtype_desc rtype_desc; scoped_dohptr parms_cxx; scoped_dohptr parms_call; const char* except_check_start; const char* except_check_end; private: // Non copyable. cxx_function_wrapper(const cxx_function_wrapper&); cxx_function_wrapper& operator=(const cxx_function_wrapper&); }; /* Return true if the class, or one of its base classes, uses multiple inheritance, i.e. has more than one base class. The output first_base parameter is optional and is filled with the first base class (if any). */ bool uses_multiple_inheritance(Node* n, scoped_dohptr* first_base_out = NULL) { if (first_base_out) first_base_out->reset(); List* const baselist = Getattr(n, "bases"); if (!baselist) return false; scoped_dohptr first_base; for (Iterator i = First(baselist); i.item; i = Next(i)) { if (Checkattr(i.item, "feature:ignore", "1")) continue; if (first_base) return true; if (uses_multiple_inheritance(i.item)) return true; first_base = Copy(i.item); } if (first_base_out) *first_base_out = first_base; return false; } /* cxx_class_wrapper Outputs the declaration of the class wrapping the given one if we're generating C++ wrappers, i.e. if the provided cxx_wrappers object is initialized. */ class cxx_class_wrapper { public: // If the provided cxx_wrappers object is not initialized, this class doesn't do anything. // // The node pointer must be valid, point to a class and remain valid for the lifetime of this object. cxx_class_wrapper(cxx_wrappers& cxx_wrappers, Node* n) : cxx_wrappers_(cxx_wrappers) { class_node_ = NULL; if (!cxx_wrappers_.is_initialized()) return; if (Checkattr(n, "feature:cxxignore", "1")) return; String* const classname = Getattr(n, "sym:name"); scoped_dohptr base_classes(NewStringEmpty()); if (uses_multiple_inheritance(n, &first_base_)) { Swig_warning(WARN_C_UNSUPPORTTED, Getfile(n), Getline(n), "Multiple inheritance not supported yet, skipping C++ wrapper generation for %s\n", classname ); // Return before initializing class_node_, so that the dtor won't output anything neither. return; } if (first_base_) Printv(base_classes, " : public ", Getattr(first_base_, "sym:name"), NIL); Printv(cxx_wrappers_.sect_types, "class ", classname, ";\n", NIL ); Printv(cxx_wrappers_.sect_decls, "class ", classname, base_classes.get(), " {\n" "public:", NIL ); // If we have any extra code, inject it. Note that we need a hack with an artificial extra node to use Swig_typemap_lookup(), as it needs a "type" attribute // which the class node doesn't have. scoped_dohptr dummy(NewHash()); Setattr(dummy, "type", Getattr(n, "name")); Setfile(dummy, Getfile(n)); Setline(dummy, Getline(n)); scoped_dohptr cxxcode(Swig_typemap_lookup("cxxcode", dummy, "", NULL)); if (!cxxcode || *Char(cxxcode) != '\n') Append(cxx_wrappers_.sect_decls, "\n"); if (cxxcode) { Replaceall(cxxcode, "$cxxclassname", classname); Replaceall(cxxcode, "$cclassptrname", get_c_class_ptr(n)); Append(cxx_wrappers_.sect_decls, cxxcode); } class_node_ = n; dtor_wname_ = NULL; has_copy_ctor_ = false; } // Get indentation used inside this class declaration. const char* get_indent() const { // Currently we always use a single level of indent, but this would need to change if/when nested classes are supported. // // As the first step, we should probably change all occurrences of "cindent" in this class itself to use get_indent() instead. return cindent; } // Emit wrapper of a member function. void emit_member_function(Node* n) { if (!class_node_) return; // We don't need to redeclare functions inherited from the base class, as we use real inheritance. if (Getattr(n, "c:inherited_from")) return; // And we even don't need to redeclare virtual functions actually overridden in the derived class, as their implementation is the same as in the base class // anyhow, so don't bother generating needless extra code. if (Getattr(n, "override")) return; // Also ignore friend function declarations: they appear inside the class, but we shouldn't generate any wrappers for them. if (Checkattr(n, "storage", "friend")) return; // As mentioned elsewhere, we can't use Swig_storage_isstatic() here because the "storage" attribute is temporarily saved in another view when this // function is being executed, so rely on another attribute to determine if it's a static function instead. const bool is_member = Checkattr(n, "ismember", "1"); const bool is_static = is_member && Getattr(n, "cplus:staticbase"); const bool is_ctor = Checkattr(n, "nodeType", "constructor"); Parm* p = Getattr(n, "parms"); if (p && is_member && !is_ctor && !is_static) { // We should have "this" as the first parameter and we need to just skip it, as we handle it specially in C++ wrappers. if (Checkattr(p, "name", "self")) { p = nextSibling(p); } else { // This is not supposed to happen, so warn if it does. Swig_warning(WARN_C_UNSUPPORTTED, Getfile(n), Getline(n), "Unexpected first parameter \"%s\" in %s\n", Getattr(p, "name"), Getattr(n, "sym:name")); } } cxx_function_wrapper func_wrapper(cxx_wrappers_, n, p); if (!func_wrapper.can_wrap()) return; // Define aliases for the stuff actually stored in the function wrapper object. cxx_rtype_desc& rtype_desc = func_wrapper.rtype_desc; String* const parms_cxx = func_wrapper.parms_cxx; String* const parms_call = func_wrapper.parms_call; // For some reason overloaded functions use fully-qualified name, so we can't just use the name directly. scoped_dohptr name_ptr(Swig_scopename_last(Getattr(n, "name"))); String* const name = name_ptr; String* const wname = Getattr(n, "wrap:name"); String* const classname = Getattr(class_node_, "sym:name"); if (Checkattr(n, "kind", "variable")) { if (Checkattr(n, "memberget", "1")) { Printv(cxx_wrappers_.sect_decls, cindent, rtype_desc.type(), " ", name, "() const;\n", NIL ); rtype_desc.set_return_value(NewStringf("%s(swig_self())", Getattr(n, "sym:name"))); Printv(cxx_wrappers_.sect_impls, "inline ", rtype_desc.type(), " ", classname, "::", name, "() const " "{", rtype_desc.get_return_code().get(), "}\n", NIL ); } else if (Checkattr(n, "memberset", "1")) { Printv(cxx_wrappers_.sect_decls, cindent, "void ", name, "(", parms_cxx, ");\n", NIL ); Printv(cxx_wrappers_.sect_impls, "inline void ", classname, "::", name, "(", parms_cxx, ") " "{ ", Getattr(n, "sym:name"), "(swig_self(), ", parms_call, "); }\n", NIL ); } else if (Checkattr(n, "varget", "1")) { Printv(cxx_wrappers_.sect_decls, cindent, "static ", rtype_desc.type(), " ", name, "();\n", NIL ); rtype_desc.set_return_value(NewStringf("%s()", Getattr(n, "sym:name"))); Printv(cxx_wrappers_.sect_impls, "inline ", rtype_desc.type(), " ", classname, "::", name, "() " "{", rtype_desc.get_return_code().get(), "}\n", NIL ); } else if (Checkattr(n, "varset", "1")) { Printv(cxx_wrappers_.sect_decls, cindent, "static void ", name, "(", parms_cxx, ");\n", NIL ); Printv(cxx_wrappers_.sect_impls, "inline void ", classname, "::", name, "(", parms_cxx, ") " "{ ", Getattr(n, "sym:name"), "(", parms_call, "); }\n", NIL ); } else { Swig_warning(WARN_C_UNSUPPORTTED, Getfile(n), Getline(n), "Not generating C++ wrappers for variable %s\n", Getattr(n, "sym:name") ); } } else if (is_ctor) { // Delegate to the ctor from opaque C pointer taking ownership of the object. Printv(cxx_wrappers_.sect_decls, cindent, classname, "(", parms_cxx, ");\n", NIL ); Printv(cxx_wrappers_.sect_impls, "inline ", classname, "::", classname, "(", parms_cxx, ") : ", classname, "{", func_wrapper.except_check_start, wname, "(", parms_call, ")", func_wrapper.except_check_end, "} {}\n", NIL ); // Remember that we had a copy ctor. if (Checkattr(n, "copy_constructor", "1")) has_copy_ctor_ = true; } else if (Checkattr(n, "nodeType", "destructor")) { if (first_base_) { // Delete the pointer and reset the ownership flag to ensure that the base class doesn't do it again. Printv(cxx_wrappers_.sect_decls, cindent, get_virtual_prefix(n), "~", classname, "() {\n", cindent, cindent, "if (swig_owns_self_) {\n", cindent, cindent, cindent, wname, "(swig_self());\n", cindent, cindent, cindent, "swig_owns_self_ = false;\n", cindent, cindent, "}\n", cindent, "}\n", NIL ); } else { // Slightly simplified version for classes without base classes, as we don't need to reset swig_self_ then. Printv(cxx_wrappers_.sect_decls, cindent, get_virtual_prefix(n), "~", classname, "() {\n", cindent, cindent, "if (swig_owns_self_)\n", cindent, cindent, cindent, wname, "(swig_self_);\n", cindent, "}\n", NIL ); // We're also going to need this in move assignment operator. dtor_wname_ = wname; } } else if (is_member) { // Wrapper parameters list may or not include "this" pointer and may or not have other parameters, so construct it piecewise for simplicity. scoped_dohptr wparms(NewStringEmpty()); if (!is_static) Append(wparms, "swig_self()"); if (Len(parms_call)) { if (Len(wparms)) Append(wparms, ", "); Append(wparms, parms_call); } Printv(cxx_wrappers_.sect_decls, cindent, is_static ? "static " : get_virtual_prefix(n), rtype_desc.type(), " ", name, "(", parms_cxx, ")", get_const_suffix(n), ";\n", NIL ); Printv(cxx_wrappers_.sect_impls, "inline ", rtype_desc.type(), " ", classname, "::", name, "(", parms_cxx, ")", get_const_suffix(n), " ", NIL ); func_wrapper.emit_body(wparms); } else { // This is something we don't know about Swig_warning(WARN_C_UNSUPPORTTED, Getfile(n), Getline(n), "Not generating C++ wrappers for %s\n", Getattr(n, "sym:name") ); } } ~cxx_class_wrapper() { // Don't do anything if generation of the wrapper for this class was disabled in ctor. if (!class_node_) return; // This is the name used for the class pointers in C wrappers. scoped_dohptr c_class_ptr = get_c_class_ptr(class_node_); String* const classname = Getattr(class_node_, "sym:name"); // We need to generate a ctor from the C object pointer, which is required to be able to create objects of this class from pointers created by C wrappers // and also by any derived classes. Printv(cxx_wrappers_.sect_decls, "\n", cindent, "explicit ", classname, "(", c_class_ptr.get(), " swig_self, " "bool swig_owns_self = true) noexcept : ", NIL ); if (first_base_) { // In this case we delegate to the base class ctor, but need a cast because it expects a different pointer type (as these types are opaque, there is no // relationship between them). Printv(cxx_wrappers_.sect_decls, Getattr(first_base_, "sym:name"), "{(", get_c_class_ptr(first_base_).get(), ")swig_self, swig_owns_self}", NIL ); } else { // Just initialize our own field. Printv(cxx_wrappers_.sect_decls, "swig_self_{swig_self}, swig_owns_self_{swig_owns_self}", NIL ); } Append(cxx_wrappers_.sect_decls, " {}\n"); // If the class doesn't have a copy ctor, forbid copying it: we currently must do it even if the original class has a perfectly cromulent implicit copy ctor // because we don't wrap it and copying would use the trivial ctor that would just copy the swig_self_ pointer resulting in double destruction of it later. // To fix this, we would need to always provide our own C wrapper for the copy ctor, which is not something we do currently. if (!has_copy_ctor_) { Printv(cxx_wrappers_.sect_decls, cindent, classname, "(", classname, " const&) = delete;\n", NIL ); } // We currently never wrap the assignment operator, so we have to always disable it for the same reason we disable the copy ctor above. // It would definitely be nice to provide the assignment, if possible. Printv(cxx_wrappers_.sect_decls, cindent, classname, "& operator=(", classname, " const&) = delete;\n", NIL ); // OTOH we may always provide move ctor and assignment, as we can always implement them trivially ourselves. if (first_base_) { Printv(cxx_wrappers_.sect_decls, cindent, classname, "(", classname, "&& obj) = default;\n", cindent, classname, "& operator=(", classname, "&& obj) = default;\n", NIL ); } else { Printv(cxx_wrappers_.sect_decls, cindent, classname, "(", classname, "&& obj) noexcept : " "swig_self_{obj.swig_self_}, swig_owns_self_{obj.swig_owns_self_} { " "obj.swig_owns_self_ = false; " "}\n", cindent, classname, "& operator=(", classname, "&& obj) noexcept {\n", NIL ); if (dtor_wname_) { Printv(cxx_wrappers_.sect_decls, cindent, cindent, "if (swig_owns_self_)\n", cindent, cindent, cindent, dtor_wname_, "(swig_self_);\n", NIL ); } Printv(cxx_wrappers_.sect_decls, cindent, cindent, "swig_self_ = obj.swig_self_;\n", cindent, cindent, "swig_owns_self_ = obj.swig_owns_self_;\n", cindent, cindent, "obj.swig_owns_self_ = false;\n", cindent, cindent, "return *this;\n", cindent, "}\n", NIL ); } // We also need a swig_self() method for accessing the C object pointer. Printv(cxx_wrappers_.sect_decls, cindent, c_class_ptr.get(), " swig_self() const noexcept ", NIL ); if (first_base_) { // If we have a base class, we reuse its existing "self" pointer. Printv(cxx_wrappers_.sect_decls, "{ return (", c_class_ptr.get(), ")", Getattr(first_base_, "sym:name"), "::swig_self(); }\n", NIL ); } else { // We use our own pointer, which we also have to declare, together with the ownership flag. // // Perhaps we could avoid having a separate bool flag by reusing the low-order bit of the pointer itself as the indicator of ownership and masking it when // retrieving it here in the future. If we decide to implement this optimization, the code generated here should be the only thing that would need to // change. Printv(cxx_wrappers_.sect_decls, "{ return swig_self_; }\n", cindent, c_class_ptr.get(), " swig_self_;\n", cindent, "bool swig_owns_self_;\n", NIL ); } Printv(cxx_wrappers_.sect_decls, "};\n" "\n", NIL ); } private: // Various helpers. // Return the string containing the pointer type used for representing the objects of the given class in the C wrappers. // // Returned value includes "*" at the end. static scoped_dohptr get_c_class_ptr(Node* class_node) { return scoped_dohptr(NewStringf("SwigObj_%s*", get_c_proxy_name(class_node))); } // Return "virtual " if this is a virtual function, empty string otherwise. static const char* get_virtual_prefix(Node* n) { return Checkattr(n, "storage", "virtual") ? "virtual " : ""; } // Return " const" if this is a const function, empty string otherwise. static const char* get_const_suffix(Node* n) { String* const qualifier = Getattr(n, "qualifier"); return qualifier && strncmp(Char(qualifier), "q(const)", 8) == 0 ? " const" : ""; } cxx_wrappers& cxx_wrappers_; // The class node itself, left null only if we skip generating wrappers for it for whatever reason. Node* class_node_; // We currently don't support generating C++ wrappers for classes using multiple inheritance. This could be implemented, with some tweaks to allow // initializing the other base classes after creating the most-derived object, but hasn't been done yet. Until then we store just the first base class (if // any, this member can also be null). scoped_dohptr first_base_; // Name of the C function used for deleting the owned object, if any. String* dtor_wname_; // True if the class defines an explicit copy ctor. bool has_copy_ctor_; // Non copyable. cxx_class_wrapper(const cxx_class_wrapper&); cxx_class_wrapper& operator=(const cxx_class_wrapper&); }; } // anonymous namespace class C:public Language { static const char *usage; // These files contain types used by the wrappers declarations and the declarations themselves and end up in the output header file. String *sect_wrappers_types; String *sect_wrappers_decl; // This one contains wrapper functions definitions and end up in the output C++ file. String *sect_wrappers; String *empty_string; // Namespace used for the C++ wrappers, set from -namespace command-line option if specified or from the module name otherwise. String *ns_cxx; // Prefix used for all symbols, if non-null. If ns_cxx was specified, it is a mangled version of it. String *ns_prefix; // Name of the module, used as a prefix for module-level symbols if ns_prefix is null. String *module_name; // Name of the output header, set in top(). String *outfile_h; // Used only while generating wrappers for an enum and contains the prefix, ending with underscore, to use for enum elements or is empty. scoped_dohptr enum_prefix_; // Used only while generating wrappers for an enum, as we don't know if enum will have any elements or not in advance and we must not generate an empty enum, // so we accumulate the full declaration here and then write it to sect_wrappers_types at once only if there are any elements. String *enum_decl; // Selects between the wrappers (public) declarations and (private) definitions. enum { output_wrapper_decl, output_wrapper_def } current_output; // Selects between various kinds of needed support for exception-related code. exceptions_support exceptions_support_; // This object contains information necessary only for C++ wrappers generation, use its is_initialized() to check if this is being done. cxx_wrappers cxx_wrappers_; // Non-owning pointer to the current C++ class wrapper if we're currently generating one or NULL. cxx_class_wrapper* cxx_class_wrapper_; // This is parallel to enum_prefix_ but for C++ enum elements. scoped_dohptr cxx_enum_prefix_; // This is parallel to enum_decl but for C++ enum declaration. String *cxx_enum_decl; // An extra indent level needed for nested C++ enums. const char* cxx_enum_indent; public: /* ----------------------------------------------------------------------------- * C() * ----------------------------------------------------------------------------- */ C() : empty_string(NewString("")), ns_cxx(NULL), ns_prefix(NULL), module_name(NULL), outfile_h(NULL), cxx_class_wrapper_(NULL) { UseWrapperSuffix = 1; } ~C() { Delete(ns_cxx); Delete(ns_prefix); } // Construct the name to be used for a function with the given name in C wrappers. // // The returned string must be freed by caller. maybe_owned_dohptr getFunctionWrapperName(Node *n, String *name) const { maybe_owned_dohptr wname; // The basic idea here is that for class members we don't need to use any prefix at all, as they're already prefixed by the class name, which has the // appropriate prefix, but we need to use a prefix for the other symbols. // // However there are a couple of special cases complicating this: // // - Friend functions are declared inside the class, but are not member functions, so we have to check for both the current class and "ismember" property. // - Destructors and implicitly generated constructors don't have "ismember" for some reason, so we need to check for them specifically. // - Variable getters and setters don't need to use the prefix as they don't clash with anything. if ((getCurrentClass() && (Checkattr(n, "ismember", "1") || Checkattr(n, "nodeType", "constructor") || Checkattr(n, "nodeType", "destructor"))) || - Checkattr(n, "varget", "1") || Checkattr(n, "varset", "1")) { wname.assign_non_owned(name); return wname; } // Use namespace as the prefix if feature:nspace is in use. scoped_dohptr scopename_prefix; if (GetFlag(parentNode(n), "feature:nspace")) { scopename_prefix = Swig_scopename_prefix(Getattr(n, "name")); if (scopename_prefix) { scoped_dohptr mangled_prefix(Swig_name_mangle_string(scopename_prefix)); scopename_prefix = mangled_prefix; } } // Fall back to the module name if we don't use feature:nspace and don't have the global prefix neither. // // Note that we really, really need to use some prefix, as wrapper function can't have the same name as the original function being wrapped. String* const prefix = scopename_prefix ? scopename_prefix : ns_prefix ? ns_prefix : module_name; wname.assign_owned(NewStringf("%s_%s", prefix, name)); return wname; } /* ----------------------------------------------------------------------------- * getClassProxyName() * * Test to see if a type corresponds to something wrapped with a proxy class. * Return NULL if not, otherwise the proxy class name to be freed by the caller. * ----------------------------------------------------------------------------- */ String *getClassProxyName(SwigType *t) { Node *n = classLookup(t); return n ? Copy(get_c_proxy_name(n)) : NULL; } /* ----------------------------------------------------------------------------- * getEnumName() * * Return the name to use for the enum in the generated code. * Also caches it in the node for subsequent access. * Returns NULL if the node doesn't correspond to an enum. * ----------------------------------------------------------------------------- */ String *getEnumName(Node *n) { String *enumname = Getattr(n, "enumname"); if (!enumname) { // We can't use forward-declared enums because we can't define them for C wrappers (we could forward declare them in C++ if their underlying type, // available as "inherit" node attribute, is specified, but not in C), so we have no choice but to use "int" for them. if (Checkattr(n, "sym:weak", "1")) return NULL; String *symname = Getattr(n, "sym:name"); if (symname) { // Add in class scope when referencing enum if not a global enum String *proxyname = 0; if (String *name = Getattr(n, "name")) { if (String *scopename_prefix = Swig_scopename_prefix(name)) { proxyname = getClassProxyName(scopename_prefix); Delete(scopename_prefix); } } if (proxyname) { enumname = NewStringf("%s_%s", proxyname, symname); Delete(proxyname); } else { // global enum or enum in a namespace enumname = Copy(get_c_proxy_name(n)); } Setattr(n, "enumname", enumname); Delete(enumname); } } return enumname; } /* ----------------------------------------------------------------------------- * substituteResolvedTypeSpecialVariable() * ----------------------------------------------------------------------------- */ void substituteResolvedTypeSpecialVariable(SwigType *classnametype, String *tm, const char *classnamespecialvariable) { scoped_dohptr btype(SwigType_base(classnametype)); if (SwigType_isenum(btype)) { Node* const enum_node = enumLookup(btype); String* const enumname = enum_node ? getEnumName(enum_node) : NULL; // We use the enum name in the wrapper declaration if it's available, as this makes it more type safe, but we always use just int for the function // definition because we don't have the enum declaration in scope there. This obviously only actually works if the actual enum underlying type is int (or // smaller). maybe_owned_dohptr c_enumname; if (current_output == output_wrapper_decl && enumname) { // We need to add "enum" iff this is not already a typedef for the enum. if (Checkattr(enum_node, "allows_typedef", "1")) c_enumname.assign_non_owned(enumname); else c_enumname.assign_owned(NewStringf("enum %s", enumname)); } else { c_enumname.assign_owned(NewString("int")); } Replaceall(tm, classnamespecialvariable, c_enumname); } else { if (!CPlusPlus) { // Just use the original C type when not using C++, we know that this type can be used in the wrappers. Clear(tm); String* const s = SwigType_str(classnametype, 0); Append(tm, s); Delete(s); return; } String* typestr = NIL; if (current_output == output_wrapper_def || Cmp(btype, "SwigObj") == 0) { // Special case, just leave it unchanged. typestr = NewString("SwigObj"); } else { typestr = getClassProxyName(btype); if (!typestr) { if (SwigType_isbuiltin(btype)) { // This should work just as well in C without any changes. typestr = SwigType_str(classnametype, 0); } else { // Swig doesn't know anything about this type, use descriptor for it. typestr = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype)); // And make sure it is declared before it is used. Printf(sect_wrappers_types, "typedef struct %s %s;\n\n", typestr, typestr); } } } Replaceall(tm, classnamespecialvariable, typestr); Delete(typestr); } } /* ----------------------------------------------------------------------------- * substituteResolvedType() * * Substitute the special variable $csclassname with the proxy class name for classes/structs/unions * that SWIG knows about. Also substitutes enums with enum name. * Otherwise use the $descriptor name for the C# class name. Note that the $&csclassname substitution * is the same as a $&descriptor substitution, ie one pointer added to descriptor name. * Inputs: * pt - parameter type * tm - typemap contents that might contain the special variable to be replaced * Outputs: * tm - typemap contents complete with the special variable substitution * ----------------------------------------------------------------------------- */ void substituteResolvedType(SwigType *pt, String *tm) { SwigType *type = SwigType_typedef_resolve_all(pt); SwigType *strippedtype = SwigType_strip_qualifiers(type); if (Strstr(tm, "$resolved_type")) { SwigType *classnametype = Copy(strippedtype); substituteResolvedTypeSpecialVariable(classnametype, tm, "$resolved_type"); Delete(classnametype); } if (Strstr(tm, "$*resolved_type")) { SwigType *classnametype = Copy(strippedtype); Delete(SwigType_pop(classnametype)); if (Len(classnametype) > 0) { substituteResolvedTypeSpecialVariable(classnametype, tm, "$*resolved_type"); } Delete(classnametype); } if (Strstr(tm, "$&resolved_type")) { SwigType *classnametype = Copy(strippedtype); SwigType_add_pointer(classnametype); substituteResolvedTypeSpecialVariable(classnametype, tm, "$&resolved_type"); Delete(classnametype); } Delete(strippedtype); Delete(type); } /*---------------------------------------------------------------------- * replaceSpecialVariables() * * Override the base class method to ensure that $resolved_type is expanded correctly inside $typemap(). *--------------------------------------------------------------------*/ virtual void replaceSpecialVariables(String *method, String *tm, Parm *parm) { // This function is called by Swig_typemap_lookup(), which may be called when generating C or C++ wrappers, so delegate to the latter one if necessary. if (cxx_wrappers_.is_initialized() && cxx_wrappers_.replaceSpecialVariables(method, tm, parm)) return; SwigType *type = Getattr(parm, "type"); substituteResolvedType(type, tm); } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { bool except_flag = CPlusPlus; bool use_cxx_wrappers = CPlusPlus; // look for certain command line options for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-help") == 0) { Printf(stdout, "%s\n", usage); } else if (strcmp(argv[i], "-namespace") == 0) { if (argv[i + 1]) { ns_cxx = NewString(argv[i + 1]); ns_prefix = Swig_name_mangle_string(ns_cxx); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-nocxx") == 0) { use_cxx_wrappers = false; Swig_mark_arg(i); } else if (strcmp(argv[i], "-noexcept") == 0) { except_flag = false; Swig_mark_arg(i); } } } // add a symbol to the parser for conditional compilation Preprocessor_define("SWIGC 1", 0); if (except_flag) Preprocessor_define("SWIG_C_EXCEPT 1", 0); if (CPlusPlus) Preprocessor_define("SWIG_CPPMODE 1", 0); if (use_cxx_wrappers) Preprocessor_define("SWIG_CXX_WRAPPERS 1", 0); SWIG_library_directory("c"); SWIG_config_file("c.swg"); String* const ns_prefix_ = ns_prefix ? NewStringf("%s_", ns_prefix) : NewString(""); // The default naming convention is to use new_Foo(), copy_Foo() and delete_Foo() for the default/copy ctor and dtor of the class Foo, but we prefer to // start all Foo methods with the same prefix, so change this. Notice that new/delete are chosen to ensure that we avoid conflicts with the existing class // methods, more natural create/destroy, for example, could result in errors if the class already had a method with the same name, but this is impossible // for the chosen names as they're keywords in C++ ("copy" is still a problem but we'll just have to live with it). Swig_name_register("construct", NewStringf("%s%%n%%c_new", ns_prefix_)); Swig_name_register("copy", NewStringf("%s%%n%%c_copy", ns_prefix_)); Swig_name_register("destroy", NewStringf("%s%%n%%c_delete", ns_prefix_)); // These ones are only needed when using a global prefix, as otherwise the defaults are fine. if (ns_prefix) { Swig_name_register("member", NewStringf("%s%%n%%c_%%m", ns_prefix_)); Swig_name_register("type", NewStringf("%s%%c", ns_prefix_)); } Delete(ns_prefix_); exceptions_support_ = except_flag ? exceptions_support_enabled : exceptions_support_disabled; if (use_cxx_wrappers) cxx_wrappers_.initialize(); allow_overloading(); } /* --------------------------------------------------------------------- * top() * --------------------------------------------------------------------- */ virtual int top(Node *n) { module_name = Getattr(n, "name"); String *outfile = Getattr(n, "outfile"); // initialize I/O const scoped_dohptr f_wrappers_cxx(NewFile(outfile, "w", SWIG_output_files())); if (!f_wrappers_cxx) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } Swig_banner(f_wrappers_cxx); Swig_obligatory_macros(f_wrappers_cxx, "C"); // Open the file where all wrapper declarations will be written to in the end. outfile_h = Getattr(n, "outfile_h"); const scoped_dohptr f_wrappers_h(NewFile(outfile_h, "w", SWIG_output_files())); if (!f_wrappers_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } Swig_banner(f_wrappers_h); // Associate file with the SWIG sections with the same name, so that e.g. "%header" contents end up in sect_header etc. const scoped_dohptr sect_begin(NewStringEmpty()); const scoped_dohptr sect_header(NewStringEmpty()); const scoped_dohptr sect_runtime(NewStringEmpty()); const scoped_dohptr sect_init(NewStringEmpty()); // This one is used outside of this function, so it's a member variable rather than a local one. sect_wrappers = NewStringEmpty(); Swig_register_filebyname("begin", sect_begin); Swig_register_filebyname("header", sect_header); Swig_register_filebyname("wrapper", sect_wrappers); Swig_register_filebyname("runtime", sect_runtime); Swig_register_filebyname("init", sect_init); // This one is C-specific and goes directly to the output header file. Swig_register_filebyname("cheader", f_wrappers_h); // Deal with exceptions support. if (exceptions_support_ == exceptions_support_enabled) { // Redefine SWIG_CException_Raise() to have a unique prefix in the shared library built from SWIG-generated sources to allow using more than one extension // in the same process without conflicts. This has to be done in this hackish way because we really need to change the name of the function itself, not // its wrapper (which is not even generated). Printv(sect_runtime, "#define SWIG_CException_Raise ", (ns_prefix ? ns_prefix : module_name), "_SWIG_CException_Raise\n", NIL ); // We need to check if we have any %imported modules, as they would already define the exception support code and we want to have exactly one copy of it // in the generated shared library, so check for "import" nodes. if (find_first_named_import(n)) { // We import another module, which will have already defined SWIG_CException, so set the flag indicating that we shouldn't do it again in this one and // define the symbol to skip compiling its implementation. Printv(sect_runtime, "#define SWIG_CException_DEFINED 1\n", NIL); // Also set a flag telling classDeclaration() to skip creating SWIG_CException wrappers. exceptions_support_ = exceptions_support_imported; } } if (cxx_wrappers_.is_initialized()) cxx_wrappers_.initialize_exceptions(exceptions_support_); { String* const include_guard_name = NewStringf("SWIG_%s_WRAP_H_", module_name); String* const include_guard_begin = NewStringf( "#ifndef %s\n" "#define %s\n\n", include_guard_name, include_guard_name ); String* const include_guard_end = NewStringf( "\n" "#endif /* %s */\n", include_guard_name ); begin_end_output_guard include_guard_wrappers_h(f_wrappers_h, include_guard_begin, include_guard_end); // All the struct types used by the functions go to f_wrappers_types so that they're certain to be defined before they're used by any functions. All the // functions declarations go directly to f_wrappers_decl we write both of them to f_wrappers_h at the end. sect_wrappers_types = NewString(""); sect_wrappers_decl = NewString(""); { cplusplus_output_guard cplusplus_guard_wrappers(sect_wrappers), cplusplus_guard_wrappers_h(sect_wrappers_decl); // emit code for children Language::top(n); } // close extern "C" guards Dump(sect_wrappers_types, f_wrappers_h); Delete(sect_wrappers_types); Dump(sect_wrappers_decl, f_wrappers_h); Delete(sect_wrappers_decl); if (cxx_wrappers_.is_initialized()) { if (!ns_cxx) { // We need some namespace for the C++ wrappers as otherwise their names could conflict with the C functions, so use the module name if nothing was // explicitly specified. ns_cxx = Copy(module_name); } Printv(f_wrappers_h, "#ifdef __cplusplus\n\n", NIL); Dump(cxx_wrappers_.sect_cxx_h, f_wrappers_h); // Generate possibly nested namespace declarations, as unfortunately we can't rely on C++17 nested namespace definitions being always available. scoped_dohptr cxx_ns_end(NewStringEmpty()); for (const char* c = Char(ns_cxx);;) { const char* const next = strstr(c, "::"); maybe_owned_dohptr ns_component; if (next) { ns_component.assign_owned(NewStringWithSize(c, (int)(next - c))); } else { ns_component.assign_non_owned((DOH*)c); } Printf(f_wrappers_h, "namespace %s {\n", ns_component.get()); Printf(cxx_ns_end, "}\n"); if (!next) break; c = next + 2; } Printv(f_wrappers_h, "\n", NIL); Dump(cxx_wrappers_.sect_types, f_wrappers_h); Printv(f_wrappers_h, "\n", NIL); Dump(cxx_wrappers_.sect_decls, f_wrappers_h); Printv(f_wrappers_h, "\n", NIL); Dump(cxx_wrappers_.sect_impls, f_wrappers_h); Printv(f_wrappers_h, "\n", cxx_ns_end.get(), "\n#endif /* __cplusplus */\n", NIL); } } // close wrapper header guard // write all to the file Dump(sect_begin, f_wrappers_cxx); Dump(sect_runtime, f_wrappers_cxx); Dump(sect_header, f_wrappers_cxx); Dump(sect_wrappers, f_wrappers_cxx); Dump(sect_init, f_wrappers_cxx); return SWIG_OK; } /* ----------------------------------------------------------------------- * importDirective() * ------------------------------------------------------------------------ */ virtual int importDirective(Node *n) { // When we import another module, we need access to its declarations in our header, so we must include the header generated for that module. Unfortunately // there doesn't seem to be any good way to get the name of that header, so we try to guess it from the header name of this module. This is obviously not // completely reliable, but works reasonably well in practice and it's not clear what else could we do, short of requiring some C-specific %import attribute // specifying the name of the header explicitly. // We can only do something if we have a module name. if (String* const imported_module_name = Getattr(n, "module")) { // Start with our header name. scoped_dohptr header_name(Copy(outfile_h)); // Strip the output directory from the file name, as it should be common to all generated headers. Replace(header_name, SWIG_output_directory(), "", DOH_REPLACE_FIRST); // And replace our module name with the name of the one being imported. Replace(header_name, module_name, imported_module_name, DOH_REPLACE_FIRST); // Finally inject inclusion of this header. Printv(Swig_filebyname("cheader"), "#include \"", header_name.get(), "\"\n", NIL); } return Language::importDirective(n); } /* ----------------------------------------------------------------------- * globalvariableHandler() * ------------------------------------------------------------------------ */ virtual int globalvariableHandler(Node *n) { // Don't export static globals, they won't be accessible when using a shared library, for example. if (Checkattr(n, "storage", "static")) return SWIG_NOWRAP; // We can't export variables defined inside namespaces to C directly, whatever their type, and we can only export them under their original name, so we // can't do it when using a global namespace prefix neither. if (!ns_prefix && !scoped_dohptr(Swig_scopename_prefix(Getattr(n, "name")))) { // If we can export the variable directly, do it, this will be more convenient to use from C code than accessor functions. if (String* const var_decl = make_c_var_decl(n)) { Printv(sect_wrappers_decl, "SWIGIMPORT ", var_decl, ";\n\n", NIL); Delete(var_decl); return SWIG_OK; } } // We have to prepend the global prefix to the names of the accessors for this variable, if we use one. // // Note that we can't just register the name format using the prefix for "get" and "set", as we do it for "member", and using it for both would result in // the prefix being used twice for the member variables getters and setters, so we have to work around it here instead. if (ns_prefix && !getCurrentClass()) { Swig_require("c:globalvariableHandler", n, "*sym:name", NIL); Setattr(n, "sym:name", NewStringf("%s_%s", ns_prefix, Getattr(n, "sym:name"))); } // Otherwise, e.g. if it's of a C++-only type, or a reference, generate accessor functions for it. int const rc = Language::globalvariableHandler(n); if (Getattr(n, "view")) Swig_restore(n); return rc; } /* ---------------------------------------------------------------------- * prepend_feature() * ---------------------------------------------------------------------- */ String* prepend_feature(Node *n) { String *prepend_str = Getattr(n, "feature:prepend"); if (prepend_str) { char *t = Char(prepend_str); if (*t == '{') { Delitem(prepend_str, 0); Delitem(prepend_str, DOH_END); } } return (prepend_str ? prepend_str : empty_string); } /* ---------------------------------------------------------------------- * append_feature() * ---------------------------------------------------------------------- */ String* append_feature(Node *n) { String *append_str = Getattr(n, "feature:append"); if (append_str) { char *t = Char(append_str); if (*t == '{') { Delitem(append_str, 0); Delitem(append_str, DOH_END); } } return (append_str ? append_str : empty_string); } /* ---------------------------------------------------------------------- * get_mangled_type() * ---------------------------------------------------------------------- */ String *get_mangled_type(SwigType *type_arg) { String *result = NewString(""); SwigType *type = 0; SwigType *tdtype = SwigType_typedef_resolve_all(type_arg); if (tdtype) type = tdtype; else type = Copy(type_arg); // special cases for ptr to function as an argument if (SwigType_ismemberpointer(type)) { SwigType_del_memberpointer(type); SwigType_add_pointer(type); } if (SwigType_ispointer(type)) { SwigType_del_pointer(type); if (SwigType_isfunction(type)) { Printf(result, "f"); Delete(type); return result; } Delete(type); type = Copy(type_arg); } SwigType *prefix = SwigType_prefix(type); if (Len(prefix)) { Replaceall(prefix, ".", ""); Replaceall(prefix, "const", "c"); Replaceall(prefix, "volatile", "v"); Replaceall(prefix, "a(", "a"); Replaceall(prefix, "m(", "m"); Replaceall(prefix, "q(", ""); Replaceall(prefix, ")", ""); Replaceall(prefix, " ", ""); Printf(result, "%s", prefix); } type = SwigType_base(type); if (SwigType_isbuiltin(type)) { Printf(result, "%c", *Char(SwigType_base(type))); } else if (SwigType_isenum(type)) { String* enumname = Swig_scopename_last(type); const char* s = Char(enumname); static const size_t len_enum_prefix = strlen("enum "); if (strncmp(s, "enum ", len_enum_prefix) == 0) s += len_enum_prefix; Printf(result, "e%s", s); } else { // Use Swig_name_mangle_type() here and not Swig_name_mangle_string() to get slightly simpler mangled name for templates (notably avoiding "_Sp_" and // "_SP_" fragments for the parentheses used by SWIG internally around template parameters). Printf(result, "%s", Char(Swig_name_mangle_type(SwigType_base(type)))); } Delete(prefix); Delete(type); return result; } void functionWrapperCSpecific(Node *n) { // this is C function, we don't apply typemaps to it String *name = Getattr(n, "sym:name"); maybe_owned_dohptr wname = getFunctionWrapperName(n, name); SwigType *type = Getattr(n, "type"); SwigType *return_type = NULL; String *arg_names = NULL; ParmList *parms = Getattr(n, "parms"); Parm *p; String *proto = NewString(""); int gencomma = 0; bool is_void_return = (SwigType_type(type) == T_VOID); // create new function wrapper object Wrapper *wrapper = NewWrapper(); // create new wrapper name Setattr(n, "wrap:name", wname); //Necessary to set this attribute? Apparently, it's never read! // create function call arg_names = Swig_cfunction_call(empty_string, parms); if (arg_names) { Delitem(arg_names, 0); Delitem(arg_names, DOH_END); } return_type = SwigType_str(type, 0); // emit wrapper prototype and code for (p = parms, gencomma = 0; p; p = nextSibling(p)) { Printv(proto, gencomma ? ", " : "", SwigType_str(Getattr(p, "type"), 0), " ", Getattr(p, "lname"), NIL); gencomma = 1; } Printv(wrapper->def, return_type, " ", wname.get(), "(", proto, ") {\n", NIL); if (!is_void_return) { Printv(wrapper->code, return_type, " result;\n", NIL); } // attach 'check' typemaps Swig_typemap_attach_parms("check", parms, wrapper); // insert constraint checking for (p = parms; p; ) { String *tm; if ((tm = Getattr(p, "tmap:check"))) { Replaceall(tm, "$target", Getattr(p, "lname")); Replaceall(tm, "$name", name); Printv(wrapper->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } Append(wrapper->code, prepend_feature(n)); if (!is_void_return) { Printf(wrapper->code, "result = "); } Printv(wrapper->code, Getattr(n, "name"), "(", arg_names, ");\n", NIL); Append(wrapper->code, append_feature(n)); if (!is_void_return) Printf(wrapper->code, "return result;\n"); Printf(wrapper->code, "}"); Wrapper_print(wrapper, sect_wrappers); emit_wrapper_func_decl(n, wname); // cleanup Delete(proto); Delete(arg_names); Delete(return_type); DelWrapper(wrapper); } void functionWrapperAppendOverloaded(String *name, Parm* first_param) { String *over_suffix = NewString(""); Parm *p; String *mangled; for (p = first_param; p; p = nextSibling(p)) { mangled = get_mangled_type(Getattr(p, "type")); Printv(over_suffix, "_", mangled, NIL); } Append(name, over_suffix); Delete(over_suffix); } scoped_dohptr get_wrapper_func_return_type(Node *n) { SwigType *type = Getattr(n, "type"); String *return_type; if ((return_type = Swig_typemap_lookup("ctype", n, "", 0))) { substituteResolvedType(type, return_type); } else { Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(type, 0)); return_type = NewString(""); } Replaceall(return_type, "::", "_"); return scoped_dohptr(return_type); } /* ---------------------------------------------------------------------- * get_wrapper_func_proto() * * Return the function signature, i.e. the comma-separated list of argument types and names surrounded by parentheses. * If a non-null wrapper is specified, it is used to emit typemap-defined code in it and it also determines whether we're generating the prototype for the * declarations or the definitions, which changes the type used for the C++ objects. * ---------------------------------------------------------------------- */ scoped_dohptr get_wrapper_func_proto(Node *n, Wrapper* wrapper = NULL) { ParmList *parms = Getattr(n, "parms"); Parm *p; String *proto = NewString("("); int gencomma = 0; // attach the standard typemaps if (wrapper) { emit_attach_parmmaps(parms, wrapper); } else { // We can't call emit_attach_parmmaps() without a wrapper, it would just crash. // Attach "in" manually, we need it for tmap:in:numinputs below. Swig_typemap_attach_parms("in", parms, 0); } Setattr(n, "wrap:parms", parms); //never read again?! // attach 'ctype' typemaps Swig_typemap_attach_parms("ctype", parms, 0); // prepare function definition for (p = parms, gencomma = 0; p; ) { String *tm; SwigType *type = NULL; while (p && checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } if (!p) break; type = Getattr(p, "type"); if (SwigType_type(type) == T_VOID) { p = nextSibling(p); continue; } if (SwigType_type(type) == T_VARARGS) { Swig_error(Getfile(n), Getline(n), "Vararg function %s not supported.\n", Getattr(n, "name")); return scoped_dohptr(NULL); } String *lname = Getattr(p, "lname"); String *c_parm_type = 0; String *arg_name = NewString(""); Printf(arg_name, "c%s", lname); if ((tm = Getattr(p, "tmap:ctype"))) { // set the appropriate type for parameter c_parm_type = Copy(tm); substituteResolvedType(type, c_parm_type); // We prefer to keep typedefs in the wrapper functions signatures as it makes them more readable, but we can't do it for nested typedefs as // they're not valid in C, so resolve them in this case. if (strstr(Char(c_parm_type), "::")) { SwigType* const tdtype = SwigType_typedef_resolve_all(c_parm_type); Delete(c_parm_type); c_parm_type = tdtype; } // template handling Replaceall(c_parm_type, "$tt", SwigType_lstr(type, 0)); } else { Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(type, 0)); } Printv(proto, gencomma ? ", " : "", c_parm_type, " ", arg_name, NIL); gencomma = 1; // apply typemaps for input parameter if ((tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", arg_name); if (wrapper) { Setattr(p, "emit:input", arg_name); Printf(wrapper->code, "%s\n", tm); } p = Getattr(p, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(type, 0)); p = nextSibling(p); } Delete(arg_name); Delete(c_parm_type); } Printv(proto, ")", NIL); return scoped_dohptr(proto); } /* ---------------------------------------------------------------------- * emit_wrapper_func_decl() * * Declares the wrapper function, using the C types used for it, in the header. * The node here is a function declaration. * ---------------------------------------------------------------------- */ void emit_wrapper_func_decl(Node *n, String *wname) { current_output = output_wrapper_decl; // add function declaration to the proxy header file Printv(sect_wrappers_decl, "SWIGIMPORT ", get_wrapper_func_return_type(n).get(), " ", wname, get_wrapper_func_proto(n).get(), ";\n\n", NIL); } void functionWrapperCPPSpecific(Node *n) { ParmList *parms = Getattr(n, "parms"); String *name = Copy(Getattr(n, "sym:name")); // mangle name if function is overloaded if (Getattr(n, "sym:overloaded")) { if (!Getattr(n, "copy_constructor")) { Parm* first_param = (Parm*)parms; if (first_param) { // Skip the first "this" parameter of the wrapped methods, it doesn't participate in overload resolution and would just result in extra long // and ugly names. // // We need to avoid dropping the first argument of static methods which don't have "this" pointer, in spite of being members (and we have to // use "cplus:staticbase" for this instead of just using Swig_storage_isstatic() because "storage" is reset in staticmemberfunctionHandler() // and so is not available here. // // Of course, the constructors don't have the extra first parameter neither. if (!Checkattr(n, "nodeType", "constructor") && Checkattr(n, "ismember", "1") && !Getattr(n, "cplus:staticbase")) { first_param = nextSibling(first_param); // A special case of overloading on const/non-const "this" pointer only, we still need to distinguish between those. if (SwigType_isconst(Getattr(n, "decl"))) { const char * const nonconst = Char(Getattr(n, "decl")) + 9 /* strlen("q(const).") */; for (Node* nover = Getattr(n, "sym:overloaded"); nover; nover = Getattr(nover, "sym:nextSibling")) { if (nover == n) continue; if (Cmp(Getattr(nover, "decl"), nonconst) == 0) { // We have an overload differing by const only, disambiguate. Append(name, "_const"); break; } } } } functionWrapperAppendOverloaded(name, first_param); } } } // make sure lnames are set Parm *p; int index = 1; String *lname = 0; for (p = (Parm*)parms, index = 1; p; (p = nextSibling(p)), index++) { if(!(lname = Getattr(p, "lname"))) { lname = NewStringf("arg%d", index); Setattr(p, "lname", lname); } } // C++ function wrapper current_output = output_wrapper_def; SwigType *type = Getattr(n, "type"); scoped_dohptr return_type = get_wrapper_func_return_type(n); maybe_owned_dohptr wname = getFunctionWrapperName(n, name); bool is_void_return = (SwigType_type(type) == T_VOID); // create new function wrapper object Wrapper *wrapper = NewWrapper(); // create new wrapper name Setattr(n, "wrap:name", wname); // add variable for holding result of original function 'cppresult' if (!is_void_return) { SwigType *value_type = cplus_value_type(type); SwigType* cppresult_type = value_type ? value_type : type; SwigType* ltype = SwigType_ltype(cppresult_type); Wrapper_add_local(wrapper, "cppresult", SwigType_str(ltype, "cppresult")); Delete(ltype); Delete(value_type); } // create wrapper function prototype Printv(wrapper->def, "SWIGEXPORTC ", return_type.get(), " ", wname.get(), NIL); Printv(wrapper->def, get_wrapper_func_proto(n, wrapper).get(), NIL); Printv(wrapper->def, " {", NIL); // emit variables for holding parameters emit_parameter_variables(parms, wrapper); // emit variable for holding function return value emit_return_variable(n, return_type, wrapper); // insert constraint checking for (p = parms; p; ) { String *tm; if ((tm = Getattr(p, "tmap:check"))) { Replaceall(tm, "$target", Getattr(p, "lname")); Replaceall(tm, "$name", name); Printv(wrapper->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } // create action code String *action = Getattr(n, "wrap:action"); if (!action) action = NewString(""); String *cbase_name = Getattr(n, "c:base_name"); if (cbase_name) { Replaceall(action, "arg1)->", NewStringf("(%s*)arg1)->", Getattr(n, "c:inherited_from"))); Replaceall(action, Getattr(n, "name"), cbase_name); } Replaceall(action, "result =", "cppresult ="); // prepare action code to use, e.g. insert try-catch blocks action = emit_action(n); // emit output typemap if needed if (!is_void_return) { String *tm; if ((tm = Swig_typemap_lookup_out("out", n, "cppresult", wrapper, action))) { // This is ugly, but the type of our result variable is not always the same as the actual return type currently because // get_wrapper_func_return_type() applies ctype typemap to it. These types are more or less compatible though, so we should be able to cast // between them explicitly. const char* start = Char(tm); const char* p = strstr(start, "$result = "); if (p == start || (p && p[-1] == ' ')) { p += strlen("$result = "); scoped_dohptr result_cast(NewStringf("(%s)", return_type.get())); // However don't add a cast which is already there. if (strncmp(p, Char(result_cast), strlen(Char(result_cast))) != 0) Insert(tm, (int)(p - start), result_cast); } Replaceall(tm, "$result", "result"); Replaceall(tm, "$owner", GetFlag(n, "feature:new") ? "1" : "0"); Printf(wrapper->code, "%s", tm); if (Len(tm)) Printf(wrapper->code, "\n"); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(type, 0), Getattr(n, "name")); } } else { Append(wrapper->code, action); } // insert cleanup code for (p = parms; p; ) { String *tm; if ((tm = Getattr(p, "tmap:freearg"))) { if (tm && (Len(tm) != 0)) { String *input = NewStringf("c%s", Getattr(p, "lname")); Replaceall(tm, "$source", Getattr(p, "lname")); Replaceall(tm, "$input", input); Delete(input); Printv(wrapper->code, tm, "\n", NIL); } p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } if (is_void_return) { Replaceall(wrapper->code, "$null", ""); Replaceall(wrapper->code, "$isvoid", "1"); } else { Replaceall(wrapper->code, "$null", "0"); Replaceall(wrapper->code, "$isvoid", "0"); Append(wrapper->code, "return result;\n"); } Append(wrapper->code, "}\n"); Wrapper_print(wrapper, sect_wrappers); // cleanup DelWrapper(wrapper); emit_wrapper_func_decl(n, wname); if (cxx_wrappers_.is_initialized()) { temp_ptr_setter set(&cxx_wrappers_.node_func_, n); if (cxx_class_wrapper_) { cxx_class_wrapper_->emit_member_function(n); } else { cxx_function_wrapper w(cxx_wrappers_, n, Getattr(n, "parms")); if (w.can_wrap()) w.emit(); } } Delete(name); } /* ---------------------------------------------------------------------- * functionWrapper() * ---------------------------------------------------------------------- */ virtual int functionWrapper(Node *n) { if (!Getattr(n, "sym:overloaded")) { if (!addSymbol(Getattr(n, "sym:name"), n)) return SWIG_ERROR; } if (CPlusPlus) { functionWrapperCPPSpecific(n); } else { functionWrapperCSpecific(n); } return SWIG_OK; } /* --------------------------------------------------------------------- * copy_node() * * This is not a general-purpose node copying function, but just a helper of classHandler(). * --------------------------------------------------------------------- */ Node *copy_node(Node *node) { Node *new_node = NewHash(); Setattr(new_node, "name", Copy(Getattr(node, "name"))); Setattr(new_node, "ismember", Copy(Getattr(node, "ismember"))); Setattr(new_node, "view", Copy(Getattr(node, "view"))); Setattr(new_node, "kind", Copy(Getattr(node, "kind"))); Setattr(new_node, "access", Copy(Getattr(node, "access"))); Setattr(new_node, "parms", Copy(Getattr(node, "parms"))); Setattr(new_node, "type", Copy(Getattr(node, "type"))); Setattr(new_node, "decl", Copy(Getattr(node, "decl"))); Node* const parent = parentNode(node); Setattr(new_node, "c:inherited_from", Getattr(parent, "name")); Setattr(new_node, "sym:name", Getattr(node, "sym:name")); Setattr(new_node, "sym:symtab", Getattr(parent, "symtab")); set_nodeType(new_node, "cdecl"); return new_node; } /* --------------------------------------------------------------------- * is_in() * * tests if given name already exists in one of child nodes of n * --------------------------------------------------------------------- */ Hash *is_in(String *name, Node *n) { Hash *h; for (h = firstChild(n); h; h = nextSibling(h)) { if (Cmp(name, Getattr(h, "name")) == 0) return h; } return 0; } /* --------------------------------------------------------------------- * make_c_var_decl() * * Return the C declaration for the given node of "variable" kind. * * If the variable has a type not representable in C, returns NULL, the caller must check for this! * * This function accounts for two special cases: * 1. If the type is an anonymous enum, "int" is used instead. * 2. If the type is an array, its bounds are stripped. * --------------------------------------------------------------------- */ String *make_c_var_decl(Node *n) { String *name = Getattr(n, "name"); SwigType *type = Getattr(n, "type"); String *type_str = SwigType_str(type, 0); if (Getattr(n, "unnamedinstance")) { // If this is an anonymous enum, we can declare the variable as int even though we can't reference this type. if (Strncmp(type_str, "enum $", 6) != 0) { // Otherwise we're out of luck, with the current approach of exposing the variables directly we simply can't do it, we would need to use accessor // functions instead to support this. Swig_error(Getfile(n), Getline(n), "Variables of anonymous non-enum types are not supported.\n"); return SWIG_ERROR; } const char * const unnamed_end = strchr(Char(type_str) + 6, '$'); if (!unnamed_end) { Swig_error(Getfile(n), Getline(n), "Unsupported anonymous enum type \"%s\".\n", type_str); return SWIG_ERROR; } String* const int_type_str = NewStringf("int%s", unnamed_end + 1); Delete(type_str); type_str = int_type_str; } else { scoped_dohptr btype(SwigType_base(type)); if (SwigType_isenum(btype)) { // Enums are special as they can be unknown, i.e. not wrapped by SWIG. In this case we just use int instead. if (!enumLookup(btype)) { Replaceall(type_str, btype, "int"); } } else { // Don't bother with checking if type is representable in C if we're wrapping C and not C++ anyhow: of course it is. if (CPlusPlus) { if (SwigType_isreference(type)) return NIL; if (!SwigType_isbuiltin(btype)) return NIL; // Final complication: define bool if it is used here. if (Cmp(btype, "bool") == 0) { Printv(sect_wrappers_types, "#include \n\n", NIL); } } } } String* const var_decl = NewStringEmpty(); if (SwigType_isarray(type)) { String *dims = Strchr(type_str, '['); char *c = Char(type_str); c[Len(type_str) - Len(dims) - 1] = '\0'; Printv(var_decl, c, " ", name, "[]", NIL); } else { Printv(var_decl, type_str, " ", name, NIL); } Delete(type_str); return var_decl; } /* --------------------------------------------------------------------- * emit_c_struct_def() * * Append the declarations of C struct members to the given string. * Notice that this function has a side effect of outputting all enum declarations inside the struct into sect_wrappers_types directly. * This is done to avoid gcc warnings "declaration does not declare anything" given for the anonymous enums inside the structs. * --------------------------------------------------------------------- */ void emit_c_struct_def(String* out, Node *n) { for ( Node* node = firstChild(n); node; node = nextSibling(node)) { String* const ntype = nodeType(node); if (Cmp(ntype, "cdecl") == 0) { SwigType* t = NewString(Getattr(node, "type")); SwigType_push(t, Getattr(node, "decl")); t = SwigType_typedef_resolve_all(t); if (SwigType_isfunction(t)) { Swig_warning(WARN_C_UNSUPPORTTED, input_file, line_number, "Extending C struct with %s is not currently supported, ignored.\n", SwigType_str(t, 0)); } else { String* const var_decl = make_c_var_decl(node); Printv(out, cindent, var_decl, ";\n", NIL); Delete(var_decl); } } else if (Cmp(ntype, "enum") == 0) { // This goes directly into sect_wrappers_types, before this struct declaration. emit_one(node); } else { // WARNING: proxy declaration can be different than original code if (Cmp(nodeType(node), "extend") == 0) emit_c_struct_def(out, node); } } } /* --------------------------------------------------------------------- * classDeclaration() * --------------------------------------------------------------------- */ virtual int classDeclaration(Node *n) { if (Cmp(Getattr(n, "name"), "SWIG_CException") == 0) { // Ignore this class only if it was already wrapped in another module, imported from this one (if exceptions are disabled, we shouldn't be even parsing // SWIG_CException in the first place and if they're enabled, we handle it normally). if (exceptions_support_ == exceptions_support_imported) return SWIG_NOWRAP; } return Language::classDeclaration(n); } /* --------------------------------------------------------------------- * classHandler() * --------------------------------------------------------------------- */ virtual int classHandler(Node *n) { String* const name = get_c_proxy_name(n); if (CPlusPlus) { cxx_class_wrapper cxx_class_wrapper_obj(cxx_wrappers_, n); temp_ptr_setter set_cxx_class_wrapper(&cxx_class_wrapper_, &cxx_class_wrapper_obj); // inheritance support: attach all members from base classes to this class if (List *baselist = Getattr(n, "bases")) { Iterator i; for (i = First(baselist); i.item; i = Next(i)) { // look for member variables and functions Node *node; for (node = firstChild(i.item); node; node = nextSibling(node)) { if ((Cmp(Getattr(node, "kind"), "variable") == 0) || (Cmp(Getattr(node, "kind"), "function") == 0)) { if ((Cmp(Getattr(node, "access"), "public") == 0) && (Cmp(Getattr(node, "storage"), "static") != 0)) { // Assignment operators are not inherited in C++ and symbols without sym:name should be ignored, not copied into the derived class. if (Getattr(node, "sym:name") && Cmp(Getattr(node, "name"), "operator =") != 0) { String *parent_name = Getattr(parentNode(node), "name"); Hash *dupl_name_node = is_in(Getattr(node, "name"), n); // if there's a duplicate inherited name, due to the C++ multiple // inheritance, change both names to avoid ambiguity if (dupl_name_node) { String *cif = Getattr(dupl_name_node, "c:inherited_from"); String *old_name = Getattr(dupl_name_node, "sym:name"); if (cif && parent_name && (Cmp(cif, parent_name) != 0)) { Setattr(dupl_name_node, "sym:name", NewStringf("%s%s", cif ? cif : "", old_name)); Setattr(dupl_name_node, "c:base_name", old_name); Node *new_node = copy_node(node); Setattr(new_node, "name", NewStringf("%s%s", parent_name, old_name)); Setattr(new_node, "c:base_name", old_name); appendChild(n, new_node); } } else { appendChild(n, copy_node(node)); } } } } } } } // declare type for specific class in the proxy header Printv(sect_wrappers_types, "typedef struct SwigObj_", name, " ", name, ";\n\n", NIL); return Language::classHandler(n); } else { // this is C struct, just declare it in the proxy String* struct_def = NewStringEmpty(); String* const tdname = Getattr(n, "tdname"); if (tdname) Append(struct_def, "typedef struct {\n"); else Printv(struct_def, "struct ", name, " {\n", NIL); emit_c_struct_def(struct_def, n); if (tdname) Printv(struct_def, "} ", tdname, ";\n\n", NIL); else Append(struct_def, "};\n\n"); Printv(sect_wrappers_types, struct_def, NIL); Delete(struct_def); } return SWIG_OK; } /* --------------------------------------------------------------------- * staticmembervariableHandler() * --------------------------------------------------------------------- */ virtual int staticmembervariableHandler(Node *n) { SwigType *type = Getattr(n, "type"); SwigType *tdtype = SwigType_typedef_resolve_all(type); if (tdtype) { type = tdtype; Setattr(n, "type", type); } SwigType *btype = SwigType_base(type); if (SwigType_isarray(type) && !SwigType_isbuiltin(btype)) { // this hack applies to member objects array (not ptrs.) SwigType_add_pointer(btype); SwigType_add_array(btype, NewStringf("%s", SwigType_array_getdim(type, 0))); Setattr(n, "type", btype); } Delete(type); Delete(btype); return Language::staticmembervariableHandler(n); } /* --------------------------------------------------------------------- * membervariableHandler() * --------------------------------------------------------------------- */ virtual int membervariableHandler(Node *n) { SwigType *type = Getattr(n, "type"); SwigType *tdtype = SwigType_typedef_resolve_all(type); if (tdtype) { type = tdtype; Setattr(n, "type", type); } SwigType *btype = SwigType_base(type); if (SwigType_isarray(type) && !SwigType_isbuiltin(btype)) { // this hack applies to member objects array (not ptrs.) SwigType_add_pointer(btype); SwigType_add_array(btype, NewStringf("%s", SwigType_array_getdim(type, 0))); Setattr(n, "type", btype); } Delete(type); Delete(btype); return Language::membervariableHandler(n); } /* --------------------------------------------------------------------- * constructorHandler() * --------------------------------------------------------------------- */ virtual int constructorHandler(Node *n) { // For some reason, the base class implementation of constructorDeclaration() only takes care of the copy ctor automatically for the languages not // supporting overloading (i.e. not calling allow_overloading(), as we do). So duplicate the relevant part of its code here, if (!Abstract && Getattr(n, "copy_constructor")) { return Language::copyconstructorHandler(n); } if (GetFlag(n, "feature:extend")) { // Pretend that all ctors added via %extend are overloaded to avoid clash between the functions created for them and the actual exported function, that // could have the same "Foo_new" name otherwise. SetFlag(n, "sym:overloaded"); } return Language::constructorHandler(n); } /* ---------------------------------------------------------------------- * Language::enumforwardDeclaration() * ---------------------------------------------------------------------- */ virtual int enumforwardDeclaration(Node *n) { // Base implementation of this function calls enumDeclaration() for "missing" enums, i.e. those without any definition at all. This results in invalid (at // least in C++) enum declarations in the output, so simply don't do this here. (void) n; return SWIG_OK; } /* --------------------------------------------------------------------- * enumDeclaration() * --------------------------------------------------------------------- */ virtual int enumDeclaration(Node *n) { if (ImportMode) return SWIG_OK; if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; // We don't know here if we're going to have any non-ignored enum elements, so generate enum declaration in a temporary string. enum_decl = NewStringEmpty(); // Another string for C++ enum declaration, which differs from the C one because it never uses the prefix, as C++ enums are declared in the correct scope. cxx_enum_decl = cxx_wrappers_.is_initialized() ? NewStringEmpty() : NULL; // If we're currently generating a wrapper class, we need an extra level of indent. if (cxx_enum_decl) { if (cxx_class_wrapper_) { cxx_enum_indent = cxx_class_wrapper_->get_indent(); Append(cxx_enum_decl, cxx_enum_indent); } else { cxx_enum_indent = ""; } } String* const symname = Getattr(n, "sym:name"); // Preserve the typedef if we have it in the input. bool const is_typedef = Checkattr(n, "allows_typedef", "1"); if (is_typedef) { Printv(enum_decl, "typedef ", NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, "typedef ", NIL); } Printv(enum_decl, "enum", NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, "enum", NIL); String* enum_prefix; if (Node* const klass = getCurrentClass()) { enum_prefix = get_c_proxy_name(klass); } else { enum_prefix = ns_prefix; // Possibly NULL, but that's fine. } // C++ enum names don't use the prefix, as they're defined in namespace or class scope. String* cxx_enum_prefix = NULL; scoped_dohptr enumname; scoped_dohptr cxx_enumname; // Unnamed enums may just have no name at all or have a synthesized invalid name of the form "$unnamedN$ which is indicated by "unnamed" attribute. if (String* const name = Getattr(n, "unnamed") ? NULL : symname) { // If it's a typedef, its sym:name is the typedef name, but we don't want to use it here (we already use it for the typedef we generate), so use the // actual C++ name instead. if (is_typedef) { // But the name may include the containing class, so get rid of it. enumname = Swig_scopename_last(Getattr(n, "name")); } else { enumname = Copy(name); } const bool scoped_enum = Checkattr(n, "scopedenum", "1"); if (cxx_enum_decl) { // In C++ we can use actual scoped enums instead of emulating them with element prefixes. if (scoped_enum) Printv(cxx_enum_decl, " class", NIL); // And enum name itself shouldn't include the prefix neither, as this enum is either inside a namespace or inside a class, so use enumname before it // gets updated below. Printv(cxx_enum_decl, " ", enumname.get(), NIL); } if (enum_prefix) { enumname = NewStringf("%s_%s", enum_prefix, enumname.get()); } Printv(enum_decl, " ", enumname.get(), NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, " ", cxx_enumname.get(), NIL); // For scoped enums, their name should be prefixed to their elements in addition to any other prefix we use. if (scoped_enum) { enum_prefix = enumname.get(); cxx_enum_prefix = cxx_enumname.get(); } } enum_prefix_ = enum_prefix ? NewStringf("%s_", enum_prefix) : NewStringEmpty(); cxx_enum_prefix_ = cxx_enum_prefix ? NewStringf("%s_", cxx_enum_prefix) : NewStringEmpty(); Printv(enum_decl, " {\n", NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, " {\n", NIL); int const len_orig = Len(enum_decl); // Emit each enum item. Language::enumDeclaration(n); // Only emit the enum declaration if there were actually any items. if (Len(enum_decl) > len_orig) { Printv(enum_decl, "\n}", NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, "\n", cxx_enum_indent, "}", NIL); if (is_typedef) { Printv(enum_decl, " ", enum_prefix_.get(), symname, NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, " ", symname, NIL); } Printv(enum_decl, ";\n\n", NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, ";\n\n", NIL); Append(sect_wrappers_types, enum_decl); if (cxx_enum_decl) { // Enums declared in global scopes can be just defined before everything else, but nested enums have to be defined inside the declaration of the class, // which we must be in process of creating, so output them in the appropriate section. Append(cxx_class_wrapper_ ? cxx_wrappers_.sect_decls : cxx_wrappers_.sect_types, cxx_enum_decl); } } Delete(enum_decl); if (cxx_enum_decl) Delete(cxx_enum_decl); return SWIG_OK; } /* --------------------------------------------------------------------- * enumvalueDeclaration() * --------------------------------------------------------------------- */ virtual int enumvalueDeclaration(Node *n) { if (Cmp(Getattr(n, "ismember"), "1") == 0 && Cmp(Getattr(n, "access"), "public") != 0) return SWIG_NOWRAP; Swig_require("enumvalueDeclaration", n, "?enumvalueex", "?enumvalue", NIL); if (!GetFlag(n, "firstenumitem")) { Printv(enum_decl, ",\n", NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, ",\n", NIL); } String* const symname = Getattr(n, "sym:name"); Printv(enum_decl, cindent, enum_prefix_.get(), symname, NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, cxx_enum_indent, cindent, cxx_enum_prefix_.get(), symname, NIL); // We only use "enumvalue", which comes from the input, and not "enumvalueex" synthesized by SWIG itself because C should use the correct value for the enum // items without an explicit one anyhow (and "enumvalueex" can't be always used as is in C code for enum elements inside a class or even a namespace). String *value = Getattr(n, "enumvalue"); if (value) { // We can't always use the raw value, check its type to see if we need to transform it. maybe_owned_dohptr cvalue; switch (SwigType_type(Getattr(n, "type"))) { case T_BOOL: // Boolean constants can't appear in C code, so replace them with their values in the simplest possible case. This is not exhaustive, of course, // but better than nothing and doing the right thing is not simple at all as we'd need to really parse the expression, just textual substitution wouldn't // be enough (consider e.g. an enum element called "very_true" and another one using it as its value). if (Cmp(value, "true") == 0) { cvalue.assign_owned(NewString("1")); } else if (Cmp(value, "false") == 0) { cvalue.assign_owned(NewString("0")); } else { Swig_error(Getfile(n), Getline(n), "Unsupported boolean enum value \"%s\".\n", value); } break; default: cvalue.assign_non_owned(value); } Printv(enum_decl, " = ", cvalue.get(), NIL); if (cxx_enum_decl) Printv(cxx_enum_decl, " = ", cvalue.get(), NIL); } Swig_restore(n); return SWIG_OK; } /* --------------------------------------------------------------------- * constantWrapper() * --------------------------------------------------------------------- */ virtual int constantWrapper(Node *n) { String *name = Getattr(n, "sym:name"); // We use the "value" and hope that it will work in C as well as in C++. String *value = Getattr(n, "value"); Printv(sect_wrappers_decl, "#define ", name, " ", value, "\n", NIL); return SWIG_OK; } }; /* class C */ /* ----------------------------------------------------------------------------- * swig_c() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_c() { return new C(); } extern "C" Language *swig_c(void) { return new_swig_c(); } /* ----------------------------------------------------------------------------- * Static member variables * ----------------------------------------------------------------------------- */ const char *C::usage = (char *) "\ C Options (available with -c)\n\ -namespace ns - use prefix based on the provided namespace\n\ -nocxx - do not generate C++ wrappers\n\ -noexcept - do not generate exception handling code\n\ \n"; swig-4.4.0/Source/Modules/tcl8.cxx0000664000175000017500000011762715075443613016644 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * tcl8.cxx * * Tcl8 language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" static const char *usage = "\ Tcl 8 Options (available with -tcl8)\n\ -itcl - Enable ITcl support\n\ -nosafe - Leave out SafeInit module function.\n\ -prefix - Set a prefix to be prepended to all names\n\ -namespace - Build module into a Tcl 8 namespace\n\ -pkgversion - Set package version\n\n"; static String *cmd_tab = 0; /* Table of command names */ static String *var_tab = 0; /* Table of global variables */ static String *const_tab = 0; /* Constant table */ static String *methods_tab = 0; /* Methods table */ static String *attr_tab = 0; /* Attribute table */ static String *prefix = 0; static String *module = 0; static int namespace_option = 0; static String *init_name = 0; static String *ns_name = 0; static int have_constructor; static String *constructor_name; static int have_destructor; static int have_base_classes; static String *destructor_action = 0; static String *version = (String *) "0.0"; static String *class_name = 0; static int have_attributes; static int have_methods; static int nosafe = 0; static File *f_header = 0; static File *f_wrappers = 0; static File *f_init = 0; static File *f_begin = 0; static File *f_runtime = 0; // Itcl support static int itcl = 0; static File *f_shadow = 0; static File *f_shadow_stubs = 0; static String *constructor = 0; static String *destructor = 0; static String *base_classes = 0; static String *base_class_init = 0; static String *methods = 0; static String *imethods = 0; static String *attributes = 0; static String *attribute_traces = 0; static String *iattribute_traces = 0; class TCL8:public Language { public: /* ------------------------------------------------------------ * TCL8::main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { SWIG_library_directory("tcl"); for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else Swig_arg_error(); } else if (strcmp(argv[i], "-pkgversion") == 0) { if (argv[i + 1]) { version = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } } else if (strcmp(argv[i], "-namespace") == 0) { namespace_option = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-itcl") == 0) { itcl = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-nosafe") == 0) { nosafe = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); } else if (strcmp(argv[i], "-cppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is now always on.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is no longer supported.\n", argv[i]); Swig_mark_arg(i); Exit(EXIT_FAILURE); } } } Preprocessor_define("SWIGTCL 1", 0); // SWIGTCL8 is deprecated, and no longer documented. Preprocessor_define("SWIGTCL8 1", 0); SWIG_config_file("tcl8.swg"); allow_overloading(); } /* ------------------------------------------------------------ * top() * ------------------------------------------------------------ */ virtual int top(Node *n) { /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); /* Initialize some variables for the object interface */ cmd_tab = NewString(""); var_tab = NewString(""); methods_tab = NewString(""); const_tab = NewString(""); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "TCL"); /* Set the module name, namespace, and prefix */ module = NewStringf("%(lower)s", Getattr(n, "name")); init_name = NewStringf("%(title)s_Init", module); ns_name = prefix ? Copy(prefix) : Copy(module); if (prefix) Append(prefix, "_"); /* If shadow classing is enabled, we're going to change the module name to "_module" */ if (itcl) { String *filen; filen = NewStringf("%s%s.itcl", SWIG_output_directory(), module); Insert(module, 0, "_"); if ((f_shadow = NewFile(filen, "w", SWIG_output_files())) == 0) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } f_shadow_stubs = NewString(""); Swig_register_filebyname("shadow", f_shadow); Swig_register_filebyname("itcl", f_shadow); Swig_banner_target_lang(f_shadow, "#"); Printv(f_shadow, "\npackage require Itcl\n\n", NIL); Delete(filen); } /* Generate some macros used throughout code generation */ Printf(f_header, "#define SWIG_init %s\n", init_name); Printf(f_header, "#define SWIG_name \"%s\"\n", module); if (namespace_option) { Printf(f_header, "#define SWIG_prefix \"%s::\"\n", ns_name); Printf(f_header, "#define SWIG_namespace \"%s\"\n\n", ns_name); } else { Printf(f_header, "#define SWIG_prefix \"%s\"\n", prefix); } Printf(f_header, "#define SWIG_version \"%s\"\n", version); Printf(cmd_tab, "\nstatic swig_command_info swig_commands[] = {\n"); Printf(var_tab, "\nstatic swig_var_info swig_variables[] = {\n"); Printf(const_tab, "\nstatic swig_const_info swig_constants[] = {\n"); Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); /* Start emitting code */ Language::top(n); /* Done. Close up the module */ Printv(cmd_tab, tab4, "{0, 0, 0}\n", "};\n", NIL); Printv(var_tab, tab4, "{0,0,0,0}\n", "};\n", NIL); Printv(const_tab, tab4, "{0,0,0,0,0,0}\n", "};\n", NIL); Printv(f_wrappers, cmd_tab, var_tab, const_tab, NIL); /* Dump the pointer equivalency table */ SwigType_emit_type_table(f_runtime, f_wrappers); Printf(f_wrappers, "#ifdef __cplusplus\n}\n#endif\n"); /* Close the init function and quit */ Printf(f_init, "return TCL_OK;\n}\n"); if (!nosafe) { Printf(f_init, "SWIGEXPORT int %(title)s_SafeInit(Tcl_Interp *interp) {\n", module); Printf(f_init, " return SWIG_init(interp);\n"); Printf(f_init, "}\n"); } if (itcl) { Printv(f_shadow, f_shadow_stubs, "\n", NIL); Delete(f_shadow); } /* Close all of the files */ Dump(f_runtime, f_begin); Printv(f_begin, f_header, f_wrappers, NIL); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* ------------------------------------------------------------ * functionWrapper() * ------------------------------------------------------------ */ virtual int functionWrapper(Node *n) { String *name = Getattr(n, "name"); /* Like to get rid of this */ String *iname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *parms = Getattr(n, "parms"); String *overname = 0; Parm *p; int i; String *tm; Wrapper *f; String *incode, *cleanup, *outarg, *argstr, *args; int num_arguments = 0; int num_required = 0; int varargs = 0; char source[64]; if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } else { if (!addSymbol(iname, n)) return SWIG_ERROR; } incode = NewString(""); cleanup = NewString(""); outarg = NewString(""); argstr = NewString("\""); args = NewString(""); f = NewWrapper(); #ifdef SWIG_USE_RESULTOBJ Wrapper_add_local(f, "resultobj", "Tcl_Obj *resultobj = NULL"); #endif String *wname = Swig_name_wrapper(iname); if (overname) { Append(wname, overname); } Setattr(n, "wrap:name", wname); Printv(f->def, "SWIGINTERN int\n ", wname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {", NIL); // Emit all of the local variables for holding arguments. emit_parameter_variables(parms, f); /* Attach standard typemaps */ emit_attach_parmmaps(parms, f); Setattr(n, "wrap:parms", parms); /* Get number of require and total arguments */ num_arguments = emit_num_arguments(parms); num_required = emit_num_required(parms); varargs = emit_isvarargs(parms); /* Unmarshal parameters */ for (i = 0, p = parms; i < num_arguments; i++) { /* Skip ignored arguments */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); /* Produce string representations of the source and target arguments */ sprintf(source, "objv[%d]", i + 1); if (i == num_required) Putc('|', argstr); if ((tm = Getattr(p, "tmap:in"))) { String *parse = Getattr(p, "tmap:in:parse"); if (!parse) { Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Putc('o', argstr); Printf(args, ",(void *)0"); if (i >= num_required) { Printf(incode, "if (objc > %d) {\n", i + 1); } Printf(incode, "%s\n", tm); if (i >= num_required) { Printf(incode, "}\n"); } } else { Printf(argstr, "%s", parse); Printf(args, ",&%s", ln); if (Strcmp(parse, "p") == 0) { SwigType *lt = SwigType_ltype(pt); SwigType_remember(pt); if (Cmp(lt, "p.void") == 0) { Printf(args, ",(void *)0"); } else { Printf(args, ",SWIGTYPE%s", SwigType_manglestr(pt)); } Delete(lt); } } p = Getattr(p, "tmap:in:next"); continue; } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); } p = nextSibling(p); } if (!varargs) { Putc(':', argstr); } else { Putc(';', argstr); /* If variable length arguments we need to emit the in typemap here */ if (p && (tm = Getattr(p, "tmap:in"))) { sprintf(source, "objv[%d]", i + 1); Printf(incode, "if (objc > %d) {\n", i); Replaceall(tm, "$input", source); Printv(incode, tm, "\n", NIL); Printf(incode, "}\n"); } } Printf(argstr, "%s\"", usage_string(Char(iname), returntype, parms)); Printv(f->code, "if (SWIG_GetArgs(interp, objc, objv,", argstr, args, ") == TCL_ERROR) SWIG_fail;\n", NIL); Printv(f->code, incode, NIL); /* Insert constraint checking code */ for (p = parms; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (i = 0, p = parms; p; i++) { if (!checkAttribute(p, "tmap:in:numinputs", "0") && !Getattr(p, "tmap:in:parse") && (tm = Getattr(p, "tmap:freearg"))) { if (Len(tm) != 0) { Printv(cleanup, tm, "\n", NIL); } p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ for (i = 0, p = parms; p; i++) { if ((tm = Getattr(p, "tmap:argout"))) { #ifdef SWIG_USE_RESULTOBJ Replaceall(tm, "$result", "resultobj"); #else Replaceall(tm, "$result", "(Tcl_GetObjResult(interp))"); #endif Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } /* Now write code to make the function call */ String *actioncode = emit_action(n); /* Need to redo all of this code (eventually) */ /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { #ifdef SWIG_USE_RESULTOBJ Replaceall(tm, "$result", "resultobj"); #else Replaceall(tm, "$result", "(Tcl_GetObjResult(interp))"); #endif if (GetFlag(n, "feature:new")) { Replaceall(tm, "$owner", "SWIG_POINTER_OWN"); } else { Replaceall(tm, "$owner", "0"); } Printf(f->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), name); } emit_return_variable(n, returntype, f); /* Dump output argument code */ Printv(f->code, outarg, NIL); /* Dump the argument cleanup code */ Printv(f->code, cleanup, NIL); /* Look for any remaining cleanup */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } } if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } #ifdef SWIG_USE_RESULTOBJ Printv(f->code, "if (resultobj) Tcl_SetObjResult(interp, resultobj);\n", NIL); #endif Printv(f->code, "return TCL_OK;\n", NIL); Printv(f->code, "fail:\n", cleanup, "return TCL_ERROR;\n", NIL); Printv(f->code, "}\n", NIL); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(f->code, "$symname", iname); /* Dump out the function */ Wrapper_print(f, f_wrappers); if (!Getattr(n, "sym:overloaded")) { /* Register the function with Tcl */ Printv(cmd_tab, tab4, "{ SWIG_prefix \"", iname, "\", (swig_wrapper_func) ", Swig_name_wrapper(iname), ", NULL},\n", NIL); } else { if (!Getattr(n, "sym:nextSibling")) { /* Emit overloading dispatch function */ int maxargs; bool check_emitted = false; String *dispatch = Swig_overload_dispatch(n, "return %s(clientData, interp, objc, argv - 1);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *df = NewWrapper(); String *dname = Swig_name_wrapper(iname); Printv(df->def, "SWIGINTERN int\n", dname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {", NIL); Printf(df->code, "Tcl_Obj *const *argv = objv+1;\n"); Printf(df->code, "int argc = objc-1;\n"); Printv(df->code, dispatch, "\n", NIL); Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up String *protoTypes = NewString(""); do { String *fulldecl = Swig_name_decl(sibl); Printf(protoTypes, "\n\" %s\\n\"", fulldecl); Delete(fulldecl); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Printf(df->code, "Tcl_SetResult(interp,(char *) " "\"Wrong number or type of arguments for overloaded function '%s'.\\n\"" "\n\" Possible C/C++ prototypes are:\\n\"%s, TCL_STATIC);\n", iname, protoTypes); Delete(protoTypes); Printf(df->code, "return TCL_ERROR;\n"); Printv(df->code, "}\n", NIL); Wrapper_print(df, f_wrappers); Printv(cmd_tab, tab4, "{ SWIG_prefix \"", iname, "\", (swig_wrapper_func) ", dname, ", NULL},\n", NIL); DelWrapper(df); Delete(dispatch); Delete(dname); } } Delete(incode); Delete(cleanup); Delete(outarg); Delete(argstr); Delete(args); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------ * variableWrapper() * ------------------------------------------------------------ */ virtual int variableWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); String *setname = 0; String *setfname = 0; Wrapper *setf = 0, *getf = 0; int readonly = 0; String *tm; if (!addSymbol(iname, n)) return SWIG_ERROR; /* Create a function for getting a variable */ int addfail = 0; getf = NewWrapper(); String *getname = Swig_name_get(NSPACE_TODO, iname); String *getfname = Swig_name_wrapper(getname); Setattr(n, "wrap:name", getfname); Printv(getf->def, "SWIGINTERN const char *", getfname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, char *name1, char *name2, int flags) {", NIL); Wrapper_add_local(getf, "value", "Tcl_Obj *value = 0"); if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { Replaceall(tm, "$result", "value"); /* Printf(getf->code, "%s\n",tm); */ addfail = emit_action_code(n, getf->code, tm); Printf(getf->code, "if (value) {\n"); Printf(getf->code, "Tcl_SetVar2(interp,name1,name2,Tcl_GetString(value), flags);\n"); Printf(getf->code, "Tcl_DecrRefCount(value);\n"); Printf(getf->code, "}\n"); Printf(getf->code, "return NULL;\n"); if (addfail) { Append(getf->code, "fail:\n"); Printf(getf->code, "return \"%s\";\n", iname); } Printf(getf->code, "}\n"); Wrapper_print(getf, f_wrappers); } else { Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); DelWrapper(getf); return SWIG_NOWRAP; } DelWrapper(getf); /* Try to create a function setting a variable */ if (!is_immutable(n)) { setf = NewWrapper(); setname = Swig_name_set(NSPACE_TODO, iname); setfname = Swig_name_wrapper(setname); Setattr(n, "wrap:name", setfname); if (setf) { Printv(setf->def, "SWIGINTERN const char *", setfname, "(ClientData clientData SWIGUNUSED, Tcl_Interp *interp, char *name1, char *name2 SWIGUNUSED, int flags) {", NIL); Wrapper_add_local(setf, "value", "Tcl_Obj *value = 0"); Wrapper_add_local(setf, "name1o", "Tcl_Obj *name1o = 0"); if ((tm = Swig_typemap_lookup("varin", n, name, 0))) { Replaceall(tm, "$input", "value"); Printf(setf->code, "name1o = Tcl_NewStringObj(name1,-1);\n"); Printf(setf->code, "value = Tcl_ObjGetVar2(interp, name1o, 0, flags);\n"); Printf(setf->code, "Tcl_DecrRefCount(name1o);\n"); Printf(setf->code, "if (!value) SWIG_fail;\n"); /* Printf(setf->code,"%s\n", tm); */ emit_action_code(n, setf->code, tm); Printf(setf->code, "return NULL;\n"); Printf(setf->code, "fail:\n"); Printf(setf->code, "return \"%s\";\n", iname); Printf(setf->code, "}\n"); Wrapper_print(setf, f_wrappers); } else { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0)); readonly = 1; } } DelWrapper(setf); } else { readonly = 1; } Printv(var_tab, tab4, "{ SWIG_prefix \"", iname, "\", 0, (swig_variable_func) ", getfname, ",", NIL); if (readonly) { static int readonlywrap = 0; if (!readonlywrap) { Wrapper *ro = NewWrapper(); Printf(ro->def, "SWIGINTERN const char *swig_readonly(ClientData clientData SWIGUNUSED, Tcl_Interp *interp SWIGUNUSED, char *name1 SWIGUNUSED, char *name2 SWIGUNUSED, int flags SWIGUNUSED) {"); Printv(ro->code, "return \"Variable is read-only\";\n", "}\n", NIL); Wrapper_print(ro, f_wrappers); readonlywrap = 1; DelWrapper(ro); } Printf(var_tab, "(swig_variable_func) swig_readonly},\n"); } else { Printv(var_tab, "(swig_variable_func) ", setfname, "},\n", NIL); } Delete(getfname); Delete(setfname); Delete(setname); Delete(getname); return SWIG_OK; } /* ------------------------------------------------------------ * constantWrapper() * ------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); String *nsname = !namespace_option ? Copy(iname) : NewStringf("%s::%s", ns_name, iname); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *tm; if (!addSymbol(iname, n)) return SWIG_ERROR; if (namespace_option) Setattr(n, "sym:name", nsname); /* Special hook for member pointer */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(iname); Printf(f_wrappers, "static %s = %s;\n", SwigType_str(type, wname), value); value = Char(wname); } if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) { Replaceall(tm, "$value", value); Replaceall(tm, "$nsname", nsname); Printf(const_tab, "%s,\n", tm); } else if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) { Replaceall(tm, "$value", value); Replaceall(tm, "$nsname", nsname); Printf(f_init, "%s\n", tm); } else { Delete(nsname); Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); return SWIG_NOWRAP; } Delete(nsname); return SWIG_OK; } /* ------------------------------------------------------------ * nativeWrapper() * ------------------------------------------------------------ */ virtual int nativeWrapper(Node *n) { String *name = Getattr(n, "sym:name"); String *funcname = Getattr(n, "wrap:name"); if (!addSymbol(funcname, n)) return SWIG_ERROR; Printf(f_init, "\t Tcl_CreateObjCommand(interp, SWIG_prefix \"%s\", (swig_wrapper_func) %s, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);\n", name, funcname); return SWIG_OK; } /* ------------------------------------------------------------ * classHandler() * ------------------------------------------------------------ */ virtual int classHandler(Node *n) { static Hash *emitted = NewHash(); String *mangled_classname = 0; SwigType *real_classname = 0; have_constructor = 0; have_destructor = 0; destructor_action = 0; constructor_name = 0; if (itcl) { constructor = NewString(""); destructor = NewString(""); base_classes = NewString(""); base_class_init = NewString(""); methods = NewString(""); imethods = NewString(""); attributes = NewString(""); attribute_traces = NewString(""); iattribute_traces = NewString(""); have_base_classes = 0; have_methods = 0; have_attributes = 0; } class_name = Getattr(n, "sym:name"); if (!addSymbol(class_name, n)) return SWIG_ERROR; real_classname = Getattr(n, "name"); mangled_classname = Swig_name_mangle_type(real_classname); if (Getattr(emitted, mangled_classname)) return SWIG_NOWRAP; Setattr(emitted, mangled_classname, "1"); attr_tab = NewString(""); Printf(attr_tab, "static swig_attribute swig_"); Printv(attr_tab, mangled_classname, "_attributes[] = {\n", NIL); methods_tab = NewStringf(""); Printf(methods_tab, "static swig_method swig_"); Printv(methods_tab, mangled_classname, "_methods[] = {\n", NIL); /* Generate normal wrappers */ Language::classHandler(n); SwigType *t = Copy(Getattr(n, "name")); SwigType_add_pointer(t); // Catch all: eg. a class with only static functions and/or variables will not have 'remembered' // SwigType_remember(t); String *wrap_class = NewStringf("&_wrap_class_%s", mangled_classname); SwigType_remember_clientdata(t, wrap_class); String *rt = Copy(getClassType()); SwigType_add_pointer(rt); // Register the class structure with the type checker /* Printf(f_init,"SWIG_TypeClientData(SWIGTYPE%s, (void *) &_wrap_class_%s);\n", SwigType_manglestr(t), mangled_classname); */ if (have_destructor) { Printv(f_wrappers, "SWIGINTERN void swig_delete_", class_name, "(void *obj) {\n", NIL); if (destructor_action) { Printv(f_wrappers, SwigType_str(rt, "arg1"), " = (", SwigType_str(rt, 0), ") obj;\n", NIL); Printv(f_wrappers, destructor_action, "\n", NIL); } else { if (CPlusPlus) { Printv(f_wrappers, " delete (", SwigType_str(rt, 0), ") obj;\n", NIL); } else { Printv(f_wrappers, " free((char *) obj);\n", NIL); } } Printf(f_wrappers, "}\n"); } Printf(methods_tab, " {0,0}\n};\n"); Printv(f_wrappers, methods_tab, NIL); Printf(attr_tab, " {0,0,0}\n};\n"); Printv(f_wrappers, attr_tab, NIL); /* Handle inheritance */ String *base_class = NewString(""); String *base_class_names = NewString(""); if (itcl) { base_classes = NewString(""); } List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator b; int index = 0; b = First(baselist); while (b.item) { SwigType *bname = Getattr(b.item, "name"); if ((!bname) || GetFlag(b.item, "feature:ignore") || (!Getattr(b.item, "module"))) { b = Next(b); continue; } if (itcl) { have_base_classes = 1; Printv(base_classes, bname, " ", NIL); Printv(base_class_init, " ", bname, "Ptr::constructor $ptr\n", NIL); } String *bmangle = Swig_name_mangle_type(bname); // Printv(f_wrappers,"extern swig_class _wrap_class_", bmangle, ";\n", NIL); // Printf(base_class,"&_wrap_class_%s",bmangle); Printf(base_class, "0"); Printf(base_class_names, "\"%s *\",", SwigType_namestr(bname)); /* Put code to register base classes in init function */ //Printf(f_init,"/* Register base : %s */\n", bmangle); //Printf(f_init,"swig_%s_bases[%d] = (swig_class *) SWIG_TypeQuery(\"%s *\")->clientdata;\n", mangled_classname, index, SwigType_namestr(bname)); (void)index; b = Next(b); index++; Putc(',', base_class); Delete(bmangle); } } if (itcl) { String *ptrclass = NewString(""); // First, build the pointer base class Printv(ptrclass, "itcl::class ", class_name, "Ptr {\n", NIL); if (have_base_classes) Printv(ptrclass, " inherit ", base_classes, "\n", NIL); // Define protected variables for SWIG object pointer Printv(ptrclass, " protected variable swigobj\n", " protected variable thisown\n", NIL); // Define public variables if (have_attributes) { Printv(ptrclass, attributes, NIL); // base class swig_getset was being called for complex inheritance trees if (namespace_option) { Printv(ptrclass, " protected method ", class_name, "_swig_getset {var name1 name2 op} {\n", NIL); Printv(ptrclass, " switch -exact -- $op {\n", " r {set $var [", ns_name, "::", class_name, "_[set var]_get $swigobj]}\n", " w {", ns_name, "::", class_name, "_${var}_set $swigobj [set $var]}\n", " }\n", " }\n", NIL); } else { Printv(ptrclass, " protected method ", class_name, "_swig_getset {var name1 name2 op} {\n", " switch -exact -- $op {\n", " r {set $var [", class_name, "_[set var]_get $swigobj]}\n", " w {", class_name, "_${var}_set $swigobj [set $var]}\n", " }\n", " }\n", NIL); } } // Add the constructor, which may include // calls to base class class constructors Printv(ptrclass, " constructor { ptr } {\n", NIL); if (have_base_classes) { Printv(ptrclass, base_class_init, NIL); Printv(ptrclass, " } {\n", NIL); } Printv(ptrclass, " set swigobj $ptr\n", " set thisown 0\n", NIL); if (have_attributes) { Printv(ptrclass, attribute_traces, NIL); } Printv(ptrclass, " }\n", NIL); // Add destructor Printv(ptrclass, " destructor {\n", " set d_func delete_", class_name, "\n", " if { $thisown && ([info command $d_func] != \"\") } {\n" " $d_func $swigobj\n", " }\n", " }\n", NIL); // Add methods if (have_methods) { Printv(ptrclass, imethods, NIL); } // Close out the pointer class Printv(ptrclass, "}\n\n", NIL); Printv(f_shadow, ptrclass, NIL); // pointer class end // Create the "real" class. Printv(f_shadow, "itcl::class ", class_name, " {\n", NIL); Printv(f_shadow, " inherit ", class_name, "Ptr\n", NIL); // If we have a constructor, then use it. // If not, then we must have an abstract class without // any constructor. So we create a class constructor // which will fail for this class (but not for inherited // classes). Note that the constructor must fail before // calling the ptrclass constructor. if (have_constructor) { Printv(f_shadow, constructor, NIL); } else { Printv(f_shadow, " constructor { } {\n", NIL); Printv(f_shadow, " # This constructor will fail if called directly\n", NIL); Printv(f_shadow, " if { [info class] == \"::", class_name, "\" } {\n", NIL); Printv(f_shadow, " error \"No constructor for class ", class_name, (Getattr(n, "abstracts") ? " - class is abstract" : ""), "\"\n", NIL); Printv(f_shadow, " }\n", NIL); Printv(f_shadow, " }\n", NIL); } Printv(f_shadow, "}\n\n", NIL); } Printv(f_wrappers, "static swig_class *swig_", mangled_classname, "_bases[] = {", base_class, "0};\n", NIL); Printv(f_wrappers, "static const char * swig_", mangled_classname, "_base_names[] = {", base_class_names, "0};\n", NIL); Delete(base_class); Delete(base_class_names); Printv(f_wrappers, "static swig_class _wrap_class_", mangled_classname, " = { \"", class_name, "\", &SWIGTYPE", SwigType_manglestr(t), ",", NIL); if (have_constructor) { Printf(f_wrappers, "%s", Swig_name_wrapper(Swig_name_construct(NSPACE_TODO, constructor_name))); Delete(constructor_name); constructor_name = 0; } else { Printf(f_wrappers, "0"); } if (have_destructor) { Printv(f_wrappers, ", swig_delete_", class_name, NIL); } else { Printf(f_wrappers, ",0"); } Printv(f_wrappers, ", swig_", mangled_classname, "_methods, swig_", mangled_classname, "_attributes, swig_", mangled_classname, "_bases,", "swig_", mangled_classname, "_base_names, &swig_module, SWIG_TCL_HASHTABLE_INIT };\n", NIL); if (!itcl) { Printv(cmd_tab, tab4, "{ SWIG_prefix \"", class_name, "\", (swig_wrapper_func) SWIG_ObjectConstructor, (ClientData)&_wrap_class_", mangled_classname, "},\n", NIL); } Delete(t); Delete(mangled_classname); return SWIG_OK; } /* ------------------------------------------------------------ * memberfunctionHandler() * ------------------------------------------------------------ */ virtual int memberfunctionHandler(Node *n) { String *name = Getattr(n, "name"); String *iname = GetChar(n, "sym:name"); String *realname, *rname; Language::memberfunctionHandler(n); realname = iname ? iname : name; rname = Swig_name_wrapper(Swig_name_member(NSPACE_TODO, class_name, realname)); if (!Getattr(n, "sym:nextSibling")) { Printv(methods_tab, tab4, "{\"", realname, "\", ", rname, "}, \n", NIL); } if (itcl) { ParmList *l = Getattr(n, "parms"); Parm *p = 0; String *pname = NewString(""); // Add this member to our class handler function Printv(imethods, tab2, "method ", realname, " [list ", NIL); int pnum = 0; for (p = l; p; p = nextSibling(p)) { String *pn = Getattr(p, "name"); String *dv = Getattr(p, "value"); SwigType *pt = Getattr(p, "type"); Printv(pname, ",(", pt, ")", NIL); Clear(pname); /* Only print an argument if not void */ if (Cmp(pt, "void") != 0) { if (Len(pn) > 0) { Printv(pname, pn, NIL); } else { Printf(pname, "p%d", pnum); } if (Len(dv) > 0) { String *defval = NewString(dv); if (namespace_option) { Insert(defval, 0, "::"); Insert(defval, 0, ns_name); } if (Strncmp(dv, "(", 1) == 0) { Insert(defval, 0, "$"); Replaceall(defval, "(", ""); Replaceall(defval, ")", ""); } Printv(imethods, "[list ", pname, " ", defval, "] ", NIL); } else { Printv(imethods, pname, " ", NIL); } } ++pnum; } Printv(imethods, "] ", NIL); if (namespace_option) { Printv(imethods, "{ ", ns_name, "::", class_name, "_", realname, " $swigobj", NIL); } else { Printv(imethods, "{ ", class_name, "_", realname, " $swigobj", NIL); } pnum = 0; for (p = l; p; p = nextSibling(p)) { String *pn = Getattr(p, "name"); SwigType *pt = Getattr(p, "type"); Clear(pname); /* Only print an argument if not void */ if (Cmp(pt, "void") != 0) { if (Len(pn) > 0) { Printv(pname, pn, NIL); } else { Printf(pname, "p%d", pnum); } Printv(imethods, " $", pname, NIL); } ++pnum; } Printv(imethods, " }\n", NIL); have_methods = 1; } Delete(rname); return SWIG_OK; } /* ------------------------------------------------------------ * membervariableHandler() * ------------------------------------------------------------ */ virtual int membervariableHandler(Node *n) { String *symname = Getattr(n, "sym:name"); String *rname; Language::membervariableHandler(n); Printv(attr_tab, tab4, "{ \"-", symname, "\",", NIL); rname = Swig_name_wrapper(Swig_name_get(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname))); Printv(attr_tab, rname, ", ", NIL); Delete(rname); if (!GetFlag(n, "feature:immutable")) { rname = Swig_name_wrapper(Swig_name_set(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname))); Printv(attr_tab, rname, "},\n", NIL); Delete(rname); } else { Printf(attr_tab, "0 },\n"); } if (itcl) { Printv(attributes, " public variable ", symname, "\n", NIL); Printv(attribute_traces, " trace variable ", symname, " rw [list ", class_name, "_swig_getset ", symname, "]\n", NIL); Printv(attribute_traces, " set ", symname, "\n", NIL); have_attributes = 1; } return SWIG_OK; } /* ------------------------------------------------------------ * constructorHandler() * ------------------------------------------------------------ */ virtual int constructorHandler(Node *n) { Language::constructorHandler(n); if (itcl) { String *name = Getattr(n, "name"); String *iname = GetChar(n, "sym:name"); String *realname; ParmList *l = Getattr(n, "parms"); Parm *p = 0; String *pname = NewString(""); realname = iname ? iname : name; if (!have_constructor) { // Add this member to our class handler function Printf(constructor, " constructor { "); // Add parameter list int pnum = 0; for (p = l; p; p = nextSibling(p)) { SwigType *pt = Getattr(p, "type"); String *pn = Getattr(p, "name"); String *dv = Getattr(p, "value"); Clear(pname); /* Only print an argument if not void */ if (Cmp(pt, "void") != 0) { if (Len(pn) > 0) { Printv(pname, pn, NIL); } else { Printf(pname, "p%d", pnum); } if (Len(dv) > 0) { Printv(constructor, "{", pname, " {", dv, "} } ", NIL); } else { Printv(constructor, pname, " ", NIL); } } ++pnum; } Printf(constructor, "} { \n"); // [BRE] 08/17/00 Added test to see if we are instantiating this object // type, or, if this constructor is being called as part of the itcl // inheritance hierarchy. // In the former case, we need to call the C++ constructor, in the // latter we don't, or we end up with two C++ objects. // Check to see if we are instantiating a 'realname' or something // derived from it. // Printv(constructor, " if { [string equal -nocase \"", realname, "\" \"[namespace tail [info class]]\" ] } {\n", NIL); // Call to constructor wrapper and parent Ptr class // [BRE] add -namespace/-prefix support if (namespace_option) { Printv(constructor, " ", realname, "Ptr::constructor [", ns_name, "::new_", realname, NIL); } else { Printv(constructor, " ", realname, "Ptr::constructor [new_", realname, NIL); } pnum = 0; for (p = l; p; p = nextSibling(p)) { SwigType *pt = Getattr(p, "type"); String *pn = Getattr(p, "name"); Clear(pname); /* Only print an argument if not void */ if (Cmp(pt, "void") != 0) { if (Len(pn) > 0) { Printv(pname, pn, NIL); } else { Printf(pname, "p%d", pnum); } Printv(constructor, " $", pname, NIL); } ++pnum; } Printv(constructor, "]\n", " }\n", " } {\n", " set thisown 1\n", " }\n", NIL); } } if (!have_constructor) constructor_name = NewString(Getattr(n, "sym:name")); have_constructor = 1; return SWIG_OK; } /* ------------------------------------------------------------ * destructorHandler() * ------------------------------------------------------------ */ virtual int destructorHandler(Node *n) { Language::destructorHandler(n); have_destructor = 1; destructor_action = Getattr(n, "wrap:action"); return SWIG_OK; } /* ------------------------------------------------------------ * validIdentifier() * ------------------------------------------------------------ */ virtual int validIdentifier(String *s) { if (Strchr(s, ' ')) return 0; return 1; } /* ------------------------------------------------------------ * usage_string() * ------------------------------------------------------------ */ char *usage_string(char *iname, SwigType *, ParmList *l) { static String *temp = 0; Parm *p; int i, numopt, pcount; if (!temp) temp = NewString(""); Clear(temp); if (namespace_option) { Printf(temp, "%s::%s ", ns_name, iname); } else { Printf(temp, "%s ", iname); } /* Now go through and print parameters */ i = 0; pcount = emit_num_arguments(l); numopt = pcount - emit_num_required(l); for (p = l; p; p = nextSibling(p)) { SwigType *pt = Getattr(p, "type"); String *pn = Getattr(p, "name"); /* Only print an argument if not ignored */ if (!checkAttribute(p, "tmap:in:numinputs", "0")) { if (i >= (pcount - numopt)) Putc('?', temp); if (Len(pn) > 0) { Printf(temp, "%s", pn); } else { Printf(temp, "%s", SwigType_str(pt, 0)); } if (i >= (pcount - numopt)) Putc('?', temp); Putc(' ', temp); i++; } } return Char(temp); } String *runtimeCode() { String *s = NewString(""); String *sincludes = Swig_include_sys("tclincludes.swg"); if (!sincludes) { Printf(stderr, "*** Unable to open 'tclincludes.swg'\n"); } else { Append(s, sincludes); Delete(sincludes); } String *serrors = Swig_include_sys("tclerrors.swg"); if (!serrors) { Printf(stderr, "*** Unable to open 'tclerrors.swg'\n"); } else { Append(s, serrors); Delete(serrors); } String *sapi = Swig_include_sys("tclapi.swg"); if (!sapi) { Printf(stderr, "*** Unable to open 'tclapi.swg'\n"); } else { Append(s, sapi); Delete(sapi); } String *srun = Swig_include_sys("tclrun.swg"); if (!srun) { Printf(stderr, "*** Unable to open 'tclrun.swg'\n"); } else { Append(s, srun); Delete(srun); } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigtclrun.h"); } }; /* ---------------------------------------------------------------------- * swig_tcl() - Instantiate module * ---------------------------------------------------------------------- */ static Language *new_swig_tcl() { return new TCL8(); } extern "C" Language *swig_tcl(void) { return new_swig_tcl(); } swig-4.4.0/Source/Modules/typepass.cxx0000664000175000017500000011410315075443613017624 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * typepass.cxx * * This module builds all of the internal type information by collecting * typedef declarations as well as registering classes, structures, and unions. * This information is needed to correctly handle shadow classes and other * advanced features. This phase of compilation is also used to perform * type-expansion. All types are fully qualified with namespace prefixes * and other information needed for compilation. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" struct normal_node { Symtab *symtab; Hash *typescope; List *normallist; normal_node *next; }; static normal_node *patch_list = 0; /* Singleton class - all non-static methods in this class are private */ class TypePass:private Dispatcher { Node *inclass; Node *module; int importmode; String *nsname; String *nssymname; Hash *classhash; List *normalize; TypePass() : inclass(0), module(0), importmode(0), nsname(0), nssymname(0), classhash(0), normalize(0) { } /* Normalize a type. Replaces type with fully qualified version */ void normalize_type(SwigType *ty) { SwigType *qty; if (CPlusPlus) { Replaceall(ty, "struct ", ""); Replaceall(ty, "union ", ""); Replaceall(ty, "class ", ""); } qty = SwigType_typedef_qualified(ty); /* Printf(stdout,"%s --> %s\n", ty, qty); */ Clear(ty); Append(ty, qty); Delete(qty); } /* Normalize a parameter list */ void normalize_parms(ParmList *p) { while (p) { SwigType *ty = Getattr(p, "type"); normalize_type(ty); /* This is a check for a function type */ { SwigType *qty = SwigType_typedef_resolve_all(ty); if (SwigType_isfunction(qty)) { SwigType_add_pointer(ty); } Delete(qty); } String *value = Getattr(p, "value"); if (value) { Node *n = Swig_symbol_clookup(value, 0); if (n) { String *q = Swig_symbol_qualified(n); if (q && Len(q)) { String *vb = Swig_scopename_last(value); Clear(value); Printf(value, "%s::%s", SwigType_namestr(q), vb); Delete(q); } } } if (value && SwigType_istemplate(value)) { String *nv = SwigType_namestr(value); Setattr(p, "value", nv); } p = nextSibling(p); } } void normalize_later(ParmList *p) { while (p) { SwigType *ty = Getattr(p, "type"); Append(normalize, ty); p = nextSibling(p); } } /* Walk through entries in normalize list and patch them up */ void normalize_list() { Hash *currentsym = Swig_symbol_current(); normal_node *nn = patch_list; normal_node *np; while (nn) { Swig_symbol_setscope(nn->symtab); SwigType_set_scope(nn->typescope); Iterator t; for (t = First(nn->normallist); t.item; t = Next(t)) { normalize_type(t.item); } Delete(nn->normallist); np = nn->next; delete(nn); nn = np; } Swig_symbol_setscope(currentsym); } /* generate C++ inheritance type-relationships */ void cplus_inherit_types_impl(Node *first, Node *cls, String *clsname, const char *bases, const char *baselist, int ispublic, String *cast = 0) { if (first == cls) return; /* The Marcelo check */ if (!cls) cls = first; List *alist = 0; List *ilist = Getattr(cls, bases); if (!ilist) { List *nlist = Getattr(cls, baselist); if (nlist) { int len = Len(nlist); int i; for (i = 0; i < len; i++) { Node *bcls = 0; int clsforward = 0; String *bname = Getitem(nlist, i); String *sname = bname; String *tname = 0; /* Try to locate the base class. We look in the symbol table and we chase typedef declarations to get to the base class if necessary */ Symtab *st = Getattr(cls, "sym:symtab"); if (SwigType_istemplate(bname)) { tname = SwigType_typedef_resolve_all(bname); sname = tname; } while (1) { String *qsname = SwigType_typedef_qualified(sname); bcls = Swig_symbol_clookup(qsname, st); Delete(qsname); if (bcls) { if (Strcmp(nodeType(bcls), "class") != 0) { /* Not a class. The symbol could be a typedef. */ if (checkAttribute(bcls, "storage", "typedef")) { SwigType *decl = Getattr(bcls, "decl"); if (!decl || !(Len(decl))) { sname = Getattr(bcls, "type"); st = Getattr(bcls, "sym:symtab"); if (SwigType_istemplate(sname)) { if (tname) Delete(tname); tname = SwigType_typedef_resolve_all(sname); sname = tname; } continue; } // A case when both outer and nested classes inherit from the same parent. Constructor may be found instead of the class itself. } else if (GetFlag(cls, "nested") && checkAttribute(bcls, "nodeType", "constructor")) { bcls = Getattr(bcls, "parentNode"); if (Getattr(bcls, "typepass:visit")) { if (!Getattr(bcls, "feature:onlychildren")) { if (!ilist) ilist = alist = NewList(); Append(ilist, bcls); } else { if (!GetFlag(bcls, "feature:ignore")) { Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bname), Getline(bname), "Base class '%s' has no name as it is an empty template instantiated with '%%template()'. Ignored.\n", SwigType_namestr(bname)); Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bcls), Getline(bcls), "The %%template directive must be written before '%s' is used as a base class and be declared with a name.\n", SwigType_namestr(bname)); } } } break; } if (Strcmp(nodeType(bcls), "classforward") != 0) { Swig_error(Getfile(bname), Getline(bname), "'%s' is not a valid base class.\n", SwigType_namestr(bname)); Swig_error(Getfile(bcls), Getline(bcls), "See definition of '%s'.\n", SwigType_namestr(bname)); } else { Swig_warning(WARN_TYPE_INCOMPLETE, Getfile(bname), Getline(bname), "Base class '%s' is incomplete.\n", SwigType_namestr(bname)); Swig_warning(WARN_TYPE_INCOMPLETE, Getfile(bcls), Getline(bcls), "Only forward declaration '%s' was found.\n", SwigType_namestr(bname)); clsforward = 1; } bcls = 0; } else { if (Getattr(bcls, "typepass:visit")) { if (!Getattr(bcls, "feature:onlychildren")) { if (!ilist) ilist = alist = NewList(); Append(ilist, bcls); } else { if (!GetFlag(bcls, "feature:ignore")) { Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bname), Getline(bname), "Base class '%s' has no name as it is an empty template instantiated with '%%template()'. Ignored.\n", SwigType_namestr(bname)); Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bcls), Getline(bcls), "The %%template directive must be written before '%s' is used as a base class and be declared with a name.\n", SwigType_namestr(bname)); } } } else { Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bname), Getline(bname), "Base class '%s' undefined.\n", SwigType_namestr(bname)); Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bcls), Getline(bcls), "'%s' must be defined before it is used as a base class.\n", SwigType_namestr(bname)); } } } break; } if (tname) Delete(tname); if (!bcls) { if (!clsforward && !GetFlag(cls, "feature:ignore")) { if (ispublic && !Getmeta(bname, "already_warned")) { Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bname), Getline(bname), "Nothing known about base class '%s'. Ignored.\n", SwigType_namestr(bname)); if (Strchr(bname, '<')) { Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(bname), Getline(bname), "Maybe you forgot to instantiate '%s' using %%template.\n", SwigType_namestr(bname)); } Setmeta(bname, "already_warned", "1"); } } SwigType_inherit(clsname, bname, cast, 0); } } } if (ilist) { Setattr(cls, bases, ilist); } } if (alist) Delete(alist); if (!ilist) return; int len = Len(ilist); int i; for (i = 0; i < len; i++) { Node *bclass = Getitem(ilist, i); SwigType *bname = Getattr(bclass, "name"); Hash *scopes = Getattr(bclass, "typescope"); SwigType_inherit(clsname, bname, cast, 0); if (ispublic && !GetFlag(bclass, "feature:ignore")) { String *smart = Getattr(first, "smart"); if (smart) { /* Record a (fake) inheritance relationship between smart pointer and smart pointer to base class, so that smart pointer upcasts are automatically generated. */ SwigType *bsmart = Getattr(bclass, "smart"); if (bsmart) { String *smartnamestr = SwigType_namestr(smart); String *bsmartnamestr = SwigType_namestr(bsmart); /* construct casting code */ String *convcode = NewStringf("\n *newmemory = SWIG_CAST_NEW_MEMORY;\n return (void *) new %s(*(%s *)$from);\n", bsmartnamestr, smartnamestr); /* setup inheritance relationship between smart pointer templates */ SwigType_inherit(smart, bsmart, 0, convcode); Delete(bsmartnamestr); Delete(smartnamestr); Delete(convcode); } else { Swig_warning(WARN_LANG_SMARTPTR_MISSING, Getfile(first), Getline(first), "Base class '%s' of '%s' is not similarly marked as a smart pointer.\n", SwigType_namestr(Getattr(bclass, "name")), SwigType_namestr(Getattr(first, "name"))); } } else { if (GetFlag(bclass, "smart")) if (!GetFlag(first, "feature:ignore")) Swig_warning(WARN_LANG_SMARTPTR_MISSING, Getfile(first), Getline(first), "Derived class '%s' of '%s' is not similarly marked as a smart pointer.\n", SwigType_namestr(Getattr(first, "name")), SwigType_namestr(Getattr(bclass, "name"))); } } if (!importmode) { String *btype = Copy(bname); SwigType_add_pointer(btype); SwigType_remember(btype); Delete(btype); } if (scopes) { SwigType_inherit_scope(scopes); } /* Set up inheritance in the symbol table */ Symtab *st = Getattr(cls, "symtab"); Symtab *bst = Getattr(bclass, "symtab"); if (st == bst) { Swig_warning(WARN_PARSE_REC_INHERITANCE, Getfile(cls), Getline(cls), "Recursive scope inheritance of '%s'.\n", SwigType_namestr(Getattr(cls, "name"))); continue; } Symtab *s = Swig_symbol_current(); Swig_symbol_setscope(st); Swig_symbol_inherit(bst); Swig_symbol_setscope(s); /* Recursively hit base classes */ String *namestr = SwigType_namestr(Getattr(bclass, "name")); String *newcast = NewStringf("(%s *)%s", namestr, cast); Delete(namestr); cplus_inherit_types_impl(first, bclass, clsname, bases, baselist, ispublic, newcast); Delete(newcast); } } void append_list(List *lb, List *la) { if (la && lb) { for (Iterator bi = First(la); bi.item; bi = Next(bi)) { Append(lb, bi.item); } } } void cplus_inherit_types(Node *first, Node *cls, String *clsname, String *cast = 0) { cplus_inherit_types_impl(first, cls, clsname, "bases", "baselist", 1, cast); cplus_inherit_types_impl(first, cls, clsname, "protectedbases", "protectedbaselist", 0, cast); cplus_inherit_types_impl(first, cls, clsname, "privatebases", "privatebaselist", 0, cast); if (!cls) cls = first; List *allbases = NewList(); append_list(allbases, Getattr(cls, "bases")); append_list(allbases, Getattr(cls, "protectedbases")); append_list(allbases, Getattr(cls, "privatebases")); if (Len(allbases)) { Setattr(cls, "allbases", allbases); } Delete(allbases); } /* ------------------------------------------------------------ * nspace_setting() * * Configures "sym:nspace" on the node and returns an overridden * nspace value when %nspacemove is used. * outer should point to parent class. * ------------------------------------------------------------ */ String *nspace_setting(Node *n, Node *outer) { String *nssymname_new = nssymname; String *feature_nspace = GetFlagAttr(n, "feature:nspace"); String *nspace = Copy(feature_nspace); // Check validity - single colons not allowed const char *c = Char(feature_nspace); int valid = 1; while(c && *c) { if (*(c++) == ':' && *(c++) != ':') { valid = 0; break; } } // Remove whitespace Replaceall(nspace, " ", ""); Replaceall(nspace, "\t", ""); valid = valid && (feature_nspace ? Equal(feature_nspace, "1") || Swig_scopename_isvalid(nspace) : 1); Replaceall(nspace, "::", "."); if (valid) { if (outer) { // Nested class or enum in a class String *outer_nspace = Getattr(outer, "sym:nspace"); String *nspace_attribute = Getattr(n, "feature:nspace"); bool warn = false; if (outer_nspace) { if (Equal(nspace_attribute, "0")) { warn = true; } else if (nspace && !(Equal(nspace, "1") || Equal(nspace, outer_nspace))) { warn = true; } } else if (nspace) { warn = true; } if (warn) { String *outer_nspace_feature = Copy(outer_nspace); Replaceall(outer_nspace_feature, ".", "::"); Swig_warning(WARN_TYPE_NSPACE_SETTING, Getfile(n), Getline(n), "Ignoring nspace setting (%s) for '%s',\n", nspace_attribute, Swig_name_decl(n)); Swig_warning(WARN_TYPE_NSPACE_SETTING, Getfile(outer), Getline(outer), "as it conflicts with the nspace setting (%s) for outer class '%s'.\n", outer_nspace_feature, Swig_name_decl(outer)); } Setattr(n, "sym:nspace", outer_nspace); } else { if (nspace) { if (Equal(nspace, "1")) { if (nssymname) Setattr(n, "sym:nspace", nssymname); } else { Setattr(n, "sym:nspace", nspace); nssymname_new = nspace; } } } } else { Swig_error(Getfile(n), Getline(n), "'%s' is not a valid identifier for nspace.\n", feature_nspace); } Delete(nspace); return nssymname_new; } /* ------------------------------------------------------------ * top() * ------------------------------------------------------------ */ virtual int top(Node *n) { importmode = 0; module = Getattr(n, "module"); inclass = 0; normalize = 0; nsname = 0; nssymname = 0; classhash = Getattr(n, "classes"); emit_children(n); normalize_list(); SwigType_set_scope(0); return SWIG_OK; } /* ------------------------------------------------------------ * moduleDirective() * ------------------------------------------------------------ */ virtual int moduleDirective(Node *n) { if (!module) { module = n; } return SWIG_OK; } /* ------------------------------------------------------------ * importDirective() * ------------------------------------------------------------ */ virtual int importDirective(Node *n) { String *oldmodule = module; int oldimport = importmode; importmode = 1; module = 0; emit_children(n); importmode = oldimport; module = oldmodule; return SWIG_OK; } /* ------------------------------------------------------------ * includeDirective() * externDirective() * extendDirective() * ------------------------------------------------------------ */ virtual int includeDirective(Node *n) { return emit_children(n); } virtual int externDeclaration(Node *n) { return emit_children(n); } virtual int extendDirective(Node *n) { return emit_children(n); } /* ------------------------------------------------------------ * classDeclaration() * ------------------------------------------------------------ */ virtual int classDeclaration(Node *n) { String *name = Getattr(n, "name"); String *tdname = Getattr(n, "tdname"); String *unnamed = Getattr(n, "unnamed"); String *storage = Getattr(n, "storage"); String *kind = Getattr(n, "kind"); save_value oldinclass(inclass); List *olist = normalize; Symtab *symtab; String *nname = 0; String *fname = 0; String *scopename = 0; String *template_default_expanded = 0; normalize = NewList(); if (name) { if (SwigType_istemplate(name)) { // We need to fully resolve the name and expand default template parameters to make templates work correctly */ Node *cn; SwigType *resolved_name = SwigType_typedef_resolve_all(name); SwigType *deftype_name = Swig_symbol_template_deftype(resolved_name, 0); fname = Copy(resolved_name); if (!Equal(resolved_name, deftype_name)) template_default_expanded = Copy(deftype_name); if (!Equal(fname, name) && (cn = Swig_symbol_clookup_local(fname, 0))) { if ((n == cn) || (Strcmp(nodeType(cn), "template") == 0) || (Getattr(cn, "feature:onlychildren") != 0) || (Getattr(n, "feature:onlychildren") != 0)) { Swig_symbol_cadd(fname, n); if (template_default_expanded) Swig_symbol_cadd(template_default_expanded, n); SwigType_typedef_class(fname); scopename = Copy(fname); } else { // Arguably the parser should instead ignore these duplicate template instantiations, in particular for ensuring the first parsed instantiation is used SetFlag(n, "feature:ignore"); Swig_warning(WARN_TYPE_REDEFINED, Getfile(n), Getline(n), "Duplicate template instantiation of '%s' with name '%s' ignored,\n", SwigType_namestr(name), Getattr(n, "sym:name")); Swig_warning(WARN_TYPE_REDEFINED, Getfile(cn), Getline(cn), "previous instantiation of '%s' with name '%s'.\n", SwigType_namestr(Getattr(cn, "name")), Getattr(cn, "sym:name")); scopename = 0; } } else { Swig_symbol_cadd(fname, n); SwigType_typedef_class(fname); scopename = Copy(fname); } Delete(deftype_name); Delete(resolved_name); } else { if ((CPlusPlus) || (unnamed)) { SwigType_typedef_class(name); } else { SwigType_typedef_class(NewStringf("%s %s", kind, name)); } scopename = Copy(name); } } else { scopename = 0; } Setattr(n, "typepass:visit", "1"); /* Need to set up a typedef if unnamed */ if (unnamed && tdname && (Cmp(storage, "typedef") == 0)) { SwigType_typedef(unnamed, tdname); } // name of the outer class should already be patched to contain its outer classes names, but not to contain namespaces // namespace name (if present) is added after processing child nodes if (Getattr(n, "nested:outer") && name) { String *outerName = Getattr(Getattr(n, "nested:outer"), "name"); name = NewStringf("%s::%s", outerName, name); Setattr(n, "name", name); if (tdname) { tdname = NewStringf("%s::%s", outerName, tdname); Setattr(n, "tdname", tdname); } } if (nsname && name) { nname = NewStringf("%s::%s", nsname, name); String *tdname = Getattr(n, "tdname"); if (tdname) { tdname = NewStringf("%s::%s", nsname, tdname); Setattr(n, "tdname", tdname); } } String *oldnssymname = nssymname; nssymname = nspace_setting(n, Getattr(n, "nested:outer")); SwigType_new_scope(scopename); SwigType_attach_symtab(Getattr(n, "symtab")); if (!GetFlag(n, "feature:ignore")) { SwigType *smart = Swig_cparse_smartptr(n); if (smart) { // Resolve the type in 'feature:smartptr' in the scope of the class it is attached to normalize_type(smart); Setattr(n, "smart", smart); Delete(smart); } } /* Inherit type definitions into the class */ if (name && !(GetFlag(n, "nested") && !checkAttribute(n, "access", "public") && (GetFlag(n, "feature:flatnested") || Language::instance()->nestedClassesSupport() == Language::NCS_None))) { cplus_inherit_types(n, 0, nname ? nname : (fname ? fname : name)); } inclass = n; symtab = Swig_symbol_setscope(Getattr(n, "symtab")); emit_children(n); Swig_symbol_setscope(symtab); Hash *ts = SwigType_pop_scope(); Setattr(n, "typescope", ts); Delete(ts); Setattr(n, "module", module); // When a fully qualified templated type with default parameters is used in the parsed code, // the following additional symbols and scopes are needed for successful lookups if (template_default_expanded) { Swig_symbol_alias(template_default_expanded, Getattr(n, "symtab")); SwigType_scope_alias(template_default_expanded, Getattr(n, "typescope")); } nssymname = oldnssymname; /* Normalize deferred types */ { normal_node *nn = new normal_node(); nn->normallist = normalize; nn->symtab = Getattr(n, "symtab"); nn->next = patch_list; nn->typescope = Getattr(n, "typescope"); patch_list = nn; } normalize = olist; /* If in a namespace, patch the class name */ if (nname) { Setattr(n, "name", nname); Delete(nname); } Delete(fname); return SWIG_OK; } /* ------------------------------------------------------------ * templateDeclaration() * ------------------------------------------------------------ */ virtual int templateDeclaration(Node *n) { String *name = Getattr(n, "name"); String *ttype = Getattr(n, "templatetype"); if (Strcmp(ttype, "class") == 0) { String *rname = SwigType_typedef_resolve_all(name); SwigType_typedef_class(rname); Delete(rname); } else if (Strcmp(ttype, "classforward") == 0) { String *rname = SwigType_typedef_resolve_all(name); SwigType_typedef_class(rname); Delete(rname); /* SwigType_typedef_class(name); */ } else if (Strcmp(ttype, "cdecl") == 0) { String *rname = SwigType_typedef_resolve_all(name); SwigType_typedef_class(rname); Delete(rname); } return SWIG_OK; } /* ------------------------------------------------------------ * lambdaDeclaration() * ------------------------------------------------------------ */ virtual int lambdaDeclaration(Node *) { return SWIG_OK; } /* ------------------------------------------------------------ * classforwardDeclaration() * ------------------------------------------------------------ */ virtual int classforwardDeclaration(Node *n) { /* Can't do inside a C struct because it breaks C nested structure wrapping */ if ((!inclass) || (CPlusPlus)) { String *name = Getattr(n, "name"); SwigType_typedef_class(name); } return SWIG_OK; } /* ------------------------------------------------------------ * namespaceDeclaration() * ------------------------------------------------------------ */ virtual int namespaceDeclaration(Node *n) { Symtab *symtab; String *name = Getattr(n, "name"); String *alias = Getattr(n, "alias"); List *olist = normalize; normalize = NewList(); if (alias) { Typetab *ts = Getattr(n, "typescope"); if (!ts) { /* Create an empty scope for the alias */ Node *ns = Getattr(n, "namespace"); SwigType_scope_alias(name, Getattr(ns, "typescope")); ts = Getattr(ns, "typescope"); Setattr(n, "typescope", ts); } /* Namespace alias */ return SWIG_OK; } else { if (name) { Node *nn = Swig_symbol_clookup(name, n); Hash *ts = 0; if (nn) ts = Getattr(nn, "typescope"); if (!ts) { SwigType_new_scope(name); SwigType_attach_symtab(Getattr(n, "symtab")); } else { SwigType_set_scope(ts); } } String *oldnsname = nsname; String *oldnssymname = nssymname; nsname = Swig_symbol_qualified(Getattr(n, "symtab")); nssymname = Swig_symbol_qualified_language_scopename(Getattr(n, "symtab")); symtab = Swig_symbol_setscope(Getattr(n, "symtab")); emit_children(n); Swig_symbol_setscope(symtab); if (name) { Hash *ts = SwigType_pop_scope(); Setattr(n, "typescope", ts); Delete(ts); } /* Normalize deferred types */ { normal_node *nn = new normal_node(); nn->normallist = normalize; nn->symtab = Getattr(n, "symtab"); nn->next = patch_list; nn->typescope = Getattr(n, "typescope"); patch_list = nn; } normalize = olist; Delete(nssymname); nssymname = oldnssymname; Delete(nsname); nsname = oldnsname; return SWIG_OK; } } /* ------------------------------------------------------------ * cDeclaration() * ------------------------------------------------------------ */ virtual int cDeclaration(Node *n) { if (NoExcept) { Delattr(n, "throws"); } /* Normalize types. */ SwigType *ty = Getattr(n, "type"); if (!ty) { return SWIG_OK; } normalize_type(ty); SwigType *decl = Getattr(n, "decl"); if (decl) { normalize_type(decl); } normalize_parms(Getattr(n, "parms")); normalize_parms(Getattr(n, "throws")); if (GetFlag(n, "conversion_operator")) { /* The call to the operator in the generated wrapper must be fully qualified in order to compile */ SwigType *name = Getattr(n, "name"); SwigType *qualifiedname = Swig_symbol_string_qualify(name, 0); Clear(name); Append(name, qualifiedname); Delete(qualifiedname); } if (checkAttribute(n, "storage", "typedef")) { String *name = Getattr(n, "name"); ty = Getattr(n, "type"); decl = Getattr(n, "decl"); SwigType *t = Copy(ty); { /* If the typename is qualified, make sure the scopename is fully qualified when making a typedef */ if (Swig_scopename_check(t) && strncmp(Char(t), "::", 2)) { String *base, *prefix, *qprefix; base = Swig_scopename_last(t); prefix = Swig_scopename_prefix(t); qprefix = SwigType_typedef_qualified(prefix); Delete(t); t = NewStringf("%s::%s", qprefix, base); Delete(base); Delete(prefix); Delete(qprefix); } } SwigType_push(t, decl); if (CPlusPlus) { Replaceall(t, "struct ", ""); Replaceall(t, "union ", ""); Replaceall(t, "class ", ""); } SwigType_typedef(t, name); } /* If namespaces are active. We need to patch the name with a namespace prefix */ if (nsname && !inclass) { String *name = Getattr(n, "name"); if (name) { String *nname = NewStringf("%s::%s", nsname, name); Setattr(n, "name", nname); Delete(nname); } } clean_overloaded(n); return SWIG_OK; } /* ------------------------------------------------------------ * constructorDeclaration() * ------------------------------------------------------------ */ virtual int constructorDeclaration(Node *n) { if (NoExcept) { Delattr(n, "throws"); } normalize_parms(Getattr(n, "parms")); normalize_parms(Getattr(n, "throws")); clean_overloaded(n); return SWIG_OK; } /* ------------------------------------------------------------ * destructorDeclaration() * ------------------------------------------------------------ */ virtual int destructorDeclaration(Node *) { return SWIG_OK; } /* ------------------------------------------------------------ * constantDirective() * ------------------------------------------------------------ */ virtual int constantDirective(Node *n) { SwigType *ty = Getattr(n, "type"); if (ty) { Setattr(n, "type", SwigType_typedef_qualified(ty)); } return SWIG_OK; } /* ------------------------------------------------------------ * enumDeclaration() * ------------------------------------------------------------ */ virtual int enumDeclaration(Node *n) { String *name = Getattr(n, "name"); if (name) { String *scope = 0; // Add a typedef to the type table so that we can use 'enum Name' as well as just 'Name' if (nsname || inclass) { // But first correct the name and tdname to contain the fully qualified scopename if (nsname && inclass) { scope = NewStringf("%s::%s", nsname, Getattr(inclass, "name")); } else if (nsname) { scope = NewStringf("%s", nsname); } else if (inclass) { scope = NewStringf("%s", Getattr(inclass, "name")); } String *nname = NewStringf("%s::%s", scope, name); Setattr(n, "name", nname); String *tdname = Getattr(n, "tdname"); if (tdname) { tdname = NewStringf("%s::%s", scope, tdname); Setattr(n, "tdname", tdname); } SwigType *t = NewStringf("enum %s", nname); SwigType_typedef(t, name); } else { SwigType *t = NewStringf("enum %s", name); SwigType_typedef(t, name); } Delete(scope); } String *tdname = Getattr(n, "tdname"); String *unnamed = Getattr(n, "unnamed"); String *storage = Getattr(n, "storage"); // Construct enumtype - for declaring an enum of this type with SwigType_ltype() etc String *enumtype = 0; if (unnamed && tdname && (Cmp(storage, "typedef") == 0)) { enumtype = Copy(Getattr(n, "tdname")); } else if (name) { enumtype = NewStringf("%s%s", CPlusPlus ? "" : "enum ", Getattr(n, "name")); } else { // anonymous enums enumtype = Copy(Getattr(n, "type")); } Setattr(n, "enumtype", enumtype); String *oldnssymname = nssymname; Node *parent = parentNode(n); nssymname = nspace_setting(n, parent && Equal(nodeType(parent), "class") ? parent : NULL); // This block of code is for dealing with %ignore on an enum item where the target language // attempts to use the C enum value in the target language itself and expects the previous enum value // to be one more than the previous value... the previous enum item might not exist if it is ignored! // - It sets the first non-ignored enum item with the "firstenumitem" attribute. // - It adds an enumvalue attribute if the previous enum item is ignored { Node *c; int count = 0; String *previous = 0; bool previous_ignored = false; bool firstenumitem = false; for (c = firstChild(n); c; c = nextSibling(c)) { assert(strcmp(Char(nodeType(c)), "enumitem") == 0); bool reset; String *enumvalue = Getattr(c, "enumvalue"); if (GetFlag(c, "feature:ignore") || !Getattr(c, "sym:name")) { reset = enumvalue ? true : false; previous_ignored = true; } else { if (!enumvalue && previous_ignored) { if (previous) Setattr(c, "enumvalue", NewStringf("(%s) + %d", previous, count+1)); else Setattr(c, "enumvalue", NewStringf("%d", count)); SetFlag(c, "virtenumvalue"); // identify enumvalue as virtual, ie not from the parsed source } if (!firstenumitem) { SetFlag(c, "firstenumitem"); firstenumitem = true; } reset = true; previous_ignored = false; } if (reset) { previous = enumvalue ? enumvalue : Getattr(c, "name"); count = 0; } else { count++; } } } emit_children(n); nssymname = oldnssymname; return SWIG_OK; } /* ------------------------------------------------------------ * enumvalueDeclaration() * ------------------------------------------------------------ */ virtual int enumvalueDeclaration(Node *n) { String *name = Getattr(n, "name"); String *value = Getattr(n, "value"); String *scopedenum = Getattr(parentNode(n), "scopedenum"); if (!value) value = name; if (Strcmp(value, name) == 0) { String *new_value; if ((nsname || inclass || scopedenum) && cparse_cplusplus) { new_value = NewStringf("%s::%s", SwigType_namestr(Swig_symbol_qualified(n)), value); } else { new_value = NewString(value); } if ((nsname || inclass || scopedenum) && !cparse_cplusplus) { String *cppvalue = NewStringf("%s::%s", SwigType_namestr(Swig_symbol_qualified(n)), value); Setattr(n, "cppvalue", cppvalue); /* for target languages that always generate C++ code even when wrapping C code */ } Setattr(n, "value", new_value); Delete(new_value); } Node *next = nextSibling(n); // Make up an enumvalue if one was not specified in the parsed code (not designed to be used on enum items and %ignore - enumvalue will be set instead) if (!GetFlag(n, "feature:ignore")) { if (Getattr(n, "_last") && !Getattr(n, "enumvalue")) { // Only the first enum item has _last set (Note: first non-ignored enum item has firstenumitem set) Setattr(n, "enumvalueex", "0"); } if (next && !Getattr(next, "enumvalue")) { Setattr(next, "enumvalueex", NewStringf("%s + 1", Getattr(n, "sym:name"))); } } return SWIG_OK; } /* ------------------------------------------------------------ * enumforwardDeclaration() * ------------------------------------------------------------ */ virtual int enumforwardDeclaration(Node *n) { // Use enumDeclaration() to do all the hard work. // Note that no children can be emitted in a forward declaration as there aren't any. int result = enumDeclaration(n); if (result == SWIG_OK) { // Detect when the real enum matching the forward enum declaration has not been parsed/declared SwigType *ty = SwigType_typedef_resolve_all(Getattr(n, "type")); Replaceall(ty, "enum ", ""); Node *nn = Swig_symbol_clookup(ty, 0); String *nodetype = nn ? nodeType(nn) : 0; if (nodetype) { if (Equal(nodetype, "enumforward")) { SetFlag(nn, "enumMissing"); } // if a real enum was declared this would be an "enum" node type } Delete(ty); } return result; } #ifdef DEBUG_OVERLOADED static void show_overloaded(Node *n) { Node *c = Getattr(n, "sym:overloaded"); Node *checkoverloaded = c; Printf(stdout, "-------------------- overloaded start %s sym:overloaded():%p -------------------------------\n", Getattr(n, "name"), c); while (c) { if (Getattr(c, "error")) { c = Getattr(c, "sym:nextSibling"); continue; } if (Getattr(c, "sym:overloaded") != checkoverloaded) { Printf(stdout, "sym:overloaded error c:%p checkoverloaded:%p\n", c, checkoverloaded); Swig_print_node(c); Exit(EXIT_FAILURE); } String *decl = Strcmp(nodeType(c), "using") == 0 ? NewString("------") : Getattr(c, "decl"); Printf(stdout, " show_overloaded %s::%s(%s) [%s] nodeType:%s\n", parentNode(c) ? Getattr(parentNode(c), "name") : "NOPARENT", Getattr(c, "name"), decl, Getattr(c, "sym:overname"), nodeType(c)); if (!Getattr(c, "sym:overloaded")) { Printf(stdout, "sym:overloaded error.....%p\n", c); Swig_print_node(c); Exit(EXIT_FAILURE); } c = Getattr(c, "sym:nextSibling"); } Printf(stdout, "-------------------- overloaded end %s -------------------------------\n", Getattr(n, "name")); } #endif /* ------------------------------------------------------------ * usingDeclaration() * ------------------------------------------------------------ */ virtual int usingDeclaration(Node *n) { if (Getattr(n, "namespace")) { /* using namespace id */ /* For a namespace import. We set up inheritance in the type system */ Node *ns = Getattr(n, "node"); if (ns) { Typetab *ts = Getattr(ns, "typescope"); if (ts) { SwigType_using_scope(ts); } } return SWIG_OK; } else { Node *ns; /* using id */ Symtab *stab = Getattr(n, "sym:symtab"); if (stab) { String *uname = Getattr(n, "uname"); ns = Swig_symbol_clookup(uname, stab); if (!ns && SwigType_istemplate(uname)) { String *tmp = Swig_symbol_template_deftype(uname, 0); if (!Equal(tmp, uname)) { ns = Swig_symbol_clookup(tmp, stab); } Delete(tmp); } } else { ns = 0; } // Note that Allocate::usingDeclaration will warn when using member is not found (when ns is zero) if (ns) { /* Only a single symbol is being used. There are only a few symbols that we actually care about. These are typedef, class declarations, and enum */ String *ntype = nodeType(ns); if (Equal(ntype, "cdecl") || Equal(ntype, "constructor")) { if (checkAttribute(ns, "storage", "typedef")) { /* A typedef declaration */ String *uname = Getattr(n, "uname"); SwigType_typedef_using(uname); } } else if ((Strcmp(ntype, "class") == 0) || ((Strcmp(ntype, "classforward") == 0))) { /* We install the using class name as kind of a typedef back to the original class */ String *uname = Getattr(n, "uname"); /* Import into current type scope */ SwigType_typedef_using(uname); } else if (Strcmp(ntype, "enum") == 0) { SwigType_typedef_using(Getattr(n, "uname")); } else if (Strcmp(ntype, "template") == 0) { SwigType_typedef_using(Getattr(n, "uname")); } } } return SWIG_OK; } /* ------------------------------------------------------------ * typemapDirective() * ------------------------------------------------------------ */ virtual int typemapDirective(Node *n) { if (inclass || nsname) { Node *items = firstChild(n); while (items) { Parm *pattern = Getattr(items, "pattern"); Parm *parms = Getattr(items, "parms"); normalize_later(pattern); normalize_later(parms); items = nextSibling(items); } } return SWIG_OK; } /* ------------------------------------------------------------ * typemapcopyDirective() * ------------------------------------------------------------ */ virtual int typemapcopyDirective(Node *n) { if (inclass || nsname) { Node *items = firstChild(n); ParmList *pattern = Getattr(n, "pattern"); normalize_later(pattern); while (items) { ParmList *npattern = Getattr(items, "pattern"); normalize_later(npattern); items = nextSibling(items); } } return SWIG_OK; } /* ------------------------------------------------------------ * applyDirective() * ------------------------------------------------------------ */ virtual int applyDirective(Node *n) { if (inclass || nsname) { ParmList *pattern = Getattr(n, "pattern"); normalize_later(pattern); Node *items = firstChild(n); while (items) { Parm *apattern = Getattr(items, "pattern"); normalize_later(apattern); items = nextSibling(items); } } return SWIG_OK; } /* ------------------------------------------------------------ * clearDirective() * ------------------------------------------------------------ */ virtual int clearDirective(Node *n) { if (inclass || nsname) { Node *p; for (p = firstChild(n); p; p = nextSibling(p)) { ParmList *pattern = Getattr(p, "pattern"); normalize_later(pattern); } } return SWIG_OK; } public: static void pass(Node *n) { TypePass t; t.top(n); } }; void Swig_process_types(Node *n) { if (!n) return; TypePass::pass(n); } swig-4.4.0/Source/Modules/python.cxx0000664000175000017500000067005615075443613017313 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * python.cxx * * Python language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include #include #include #include #include "pydoc.h" #define PYSHADOW_MEMBER 0x2 #define WARN_PYTHON_MULTIPLE_INH 405 #define PYTHON_INT_MAX (2147483647) #define PYTHON_INT_MIN (-2147483647-1) static String *const_code = 0; static String *module = 0; static String *package = 0; static String *mainmodule = 0; static String *interface = 0; static String *global_name = 0; static int shadow = 1; static int use_kw = 0; static int director_method_index = 0; static int builtin = 0; static File *f_begin = 0; static File *f_runtime = 0; static File *f_runtime_h = 0; static File *f_header = 0; static File *f_wrappers = 0; static File *f_directors = 0; static File *f_directors_h = 0; static File *f_init = 0; static File *f_shadow_py = 0; static String *f_shadow = 0; static String *f_shadow_begin = 0; static Hash *f_shadow_imports = 0; static String *f_shadow_after_begin = 0; static String *f_shadow_stubs = 0; static Hash *builtin_getset = 0; static Hash *builtin_closures = 0; static Hash *class_members = 0; static File *f_builtins = 0; static String *builtin_tp_init = 0; static String *builtin_methods = 0; static String *builtin_default_unref = 0; static String *builtin_closures_code = 0; static String *f_varlinks = 0; static String *methods; static String *methods_proxydocs; static String *class_name; static String *shadow_indent = 0; static int in_class = 0; static int no_header_file = 0; static int max_bases = 0; static int builtin_bases_needed = 0; /* C++ Support + Shadow Classes */ static int have_constructor = 0; static int have_repr = 0; static bool have_builtin_static_member_method_callback = false; static bool have_fast_proxy_static_member_method_callback = false; static String *real_classname; /* Thread Support */ static int threads = 0; static int nothreads = 0; /* Other options */ static int dirvtable = 0; static int doxygen = 0; static int fastunpack = 1; static int fastproxy = 0; static int olddefs = 0; static int castmode = 0; static int extranative = 0; static int nortti = 0; static int relativeimport = 0; static int flat_static_method = 0; static int nogil = 0; /* flags for the make_autodoc function */ namespace { enum autodoc_t { AUTODOC_CLASS, AUTODOC_CTOR, AUTODOC_DTOR, AUTODOC_STATICFUNC, AUTODOC_FUNC, AUTODOC_METHOD, AUTODOC_CONST, AUTODOC_VAR }; } static const char *usage1 = "\ Python Options (available with -python)\n\ -builtin - Create Python built-in types rather than proxy classes, for better performance\n\ -castmode - Enable the casting mode, which allows implicit cast between types in Python\n\ -debug-doxygen-parser - Display doxygen parser module debugging information\n\ -debug-doxygen-translator - Display doxygen translator module debugging information\n\ -dirvtable - Generate a pseudo virtual table for directors for faster dispatch\n\ -doxygen - Convert C++ doxygen comments to pydoc comments in proxy classes\n\ -extranative - Return extra native wrappers for C++ std containers wherever possible\n\ -fastproxy - Use fast proxy mechanism for member methods\n\ -flatstaticmethod - Generate additional flattened Python methods for C++ static methods\n\ -globals - Set used to access C global variable (default: 'cvar')\n\ -interface - Set low-level C/C++ module name to (default: module name prefixed by '_')\n\ -keyword - Use keyword arguments\n"; static const char *usage2 = "\ -nofastunpack - Use traditional UnpackTuple method to parse the argument functions\n\ -nogil - Enable free-threading if supported by the Python interpreter\n\ -noh - Don't generate the output header file\n"; static const char *usage3 = "\ -noproxy - Don't generate proxy classes\n\ -nortti - Disable the use of the native C++ RTTI with directors\n\ -nothreads - Disable thread support for the entire interface\n\ -olddefs - Keep the old method definitions when using -fastproxy\n\ -relativeimport - Use relative Python imports\n\ -threads - Add thread support for all the interface\n\ -O - Enable the following optimization options:\n\ -fastdispatch -fastproxy -fvirtual\n\ \n"; static String *getSlot(Node *n = NULL, const char *key = NULL, String *default_slot = NULL) { static String *zero = NewString("0"); String *val = n && key && *key ? Getattr(n, key) : NULL; return val ? val : default_slot ? default_slot : zero; } static String *getHeapTypesSlot(Node *n = NULL, const char *key = NULL, String *default_slot = NULL) { String *val = n && key && *key ? Getattr(n, key) : NULL; return val ? val : default_slot; } static void printSlot(File *f, String *slotval, const char *slotname, const char *functype = NULL) { String *slotval_override = 0; if (functype && Strcmp(slotval, "0") == 0) slotval = slotval_override = NewStringf("(%s) %s", functype, slotval); int len = Len(slotval); int fieldwidth = len >= 39 ? 1 : 39 - len; Printf(f, " %s,%*s/* %s */\n", slotval, fieldwidth, "", slotname); Delete(slotval_override); } static void printHeapTypesSlot(File *f, String *slotval, const char *slotname, const char *functype = NULL) { if (slotval) { String *slotval_override = 0; if (functype) slotval = slotval_override = NewStringf("(%s) %s", functype, slotval); int len = Len(slotname); int fieldwidth = len >= 30 ? 1 : 30 - len; Printf(f, " { Py_%s,%*s(void *)%s },\n", slotname, fieldwidth, "", slotval); Delete(slotval_override); } } static String *getClosure(String *functype, String *wrapper, int funpack = 0) { static const char *functypes[] = { "unaryfunc", "SWIGPY_UNARYFUNC_CLOSURE", "destructor", "SWIGPY_DESTRUCTOR_CLOSURE", "inquiry", "SWIGPY_INQUIRY_CLOSURE", "getiterfunc", "SWIGPY_GETITERFUNC_CLOSURE", "binaryfunc", "SWIGPY_BINARYFUNC_CLOSURE", "ternaryfunc", "SWIGPY_TERNARYFUNC_CLOSURE", "ternarycallfunc", "SWIGPY_TERNARYCALLFUNC_CLOSURE", "lenfunc", "SWIGPY_LENFUNC_CLOSURE", "ssizeargfunc", "SWIGPY_SSIZEARGFUNC_CLOSURE", "ssizessizeargfunc", "SWIGPY_SSIZESSIZEARGFUNC_CLOSURE", "ssizeobjargproc", "SWIGPY_SSIZEOBJARGPROC_CLOSURE", "ssizessizeobjargproc", "SWIGPY_SSIZESSIZEOBJARGPROC_CLOSURE", "objobjproc", "SWIGPY_OBJOBJPROC_CLOSURE", "objobjargproc", "SWIGPY_OBJOBJARGPROC_CLOSURE", "reprfunc", "SWIGPY_REPRFUNC_CLOSURE", "hashfunc", "SWIGPY_HASHFUNC_CLOSURE", "iternextfunc", "SWIGPY_ITERNEXTFUNC_CLOSURE", NULL }; static const char *funpack_functypes[] = { "unaryfunc", "SWIGPY_UNARYFUNC_CLOSURE", "destructor", "SWIGPY_DESTRUCTOR_CLOSURE", "inquiry", "SWIGPY_INQUIRY_CLOSURE", "getiterfunc", "SWIGPY_GETITERFUNC_CLOSURE", "ternaryfunc", "SWIGPY_TERNARYFUNC_CLOSURE", "ternarycallfunc", "SWIGPY_TERNARYCALLFUNC_CLOSURE", "lenfunc", "SWIGPY_LENFUNC_CLOSURE", "ssizeargfunc", "SWIGPY_FUNPACK_SSIZEARGFUNC_CLOSURE", "ssizessizeargfunc", "SWIGPY_SSIZESSIZEARGFUNC_CLOSURE", "ssizeobjargproc", "SWIGPY_SSIZEOBJARGPROC_CLOSURE", "ssizessizeobjargproc", "SWIGPY_SSIZESSIZEOBJARGPROC_CLOSURE", "objobjproc", "SWIGPY_FUNPACK_OBJOBJPROC_CLOSURE", "objobjargproc", "SWIGPY_OBJOBJARGPROC_CLOSURE", "reprfunc", "SWIGPY_REPRFUNC_CLOSURE", "hashfunc", "SWIGPY_HASHFUNC_CLOSURE", "iternextfunc", "SWIGPY_ITERNEXTFUNC_CLOSURE", NULL }; if (!functype) return NULL; char *c = Char(functype); int i; if (funpack) { for (i = 0; funpack_functypes[i] != NULL; i += 2) { if (!strcmp(c, funpack_functypes[i])) return NewStringf("%s(%s)", funpack_functypes[i + 1], wrapper); } } else { for (i = 0; functypes[i] != NULL; i += 2) { if (!strcmp(c, functypes[i])) return NewStringf("%s(%s)", functypes[i + 1], wrapper); } } return NULL; } class PYTHON:public Language { public: PYTHON() { /* Add code to manage protected constructors and directors */ director_prot_ctor_code = NewString(""); Printv(director_prot_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " SWIG_SetErrorMsg(PyExc_RuntimeError,\"accessing abstract class or protected constructor\"); \n", " SWIG_fail;\n", "}\n", NIL); director_multiple_inheritance = 1; directorLanguage(); } ~PYTHON() { delete doxygenTranslator; } /* ------------------------------------------------------------ * Thread Implementation * ------------------------------------------------------------ */ int threads_enable(Node *n) const { return threads && !GetFlagAttr(n, "feature:nothread"); } int initialize_threads(String *f_init) { if (!threads) { return SWIG_OK; } Printf(f_init, "\n"); Printf(f_init, "/* Initialize threading */\n"); Printf(f_init, "SWIG_PYTHON_INITIALIZE_THREADS;\n"); return SWIG_OK; } virtual void thread_begin_block(Node *n, String *f) { if (!GetFlag(n, "feature:nothreadblock")) { String *bb = Getattr(n, "feature:threadbeginblock"); if (bb) { Append(f, bb); } else { Append(f, "SWIG_PYTHON_THREAD_BEGIN_BLOCK;\n"); } } } virtual void thread_end_block(Node *n, String *f) { if (!GetFlag(n, "feature:nothreadblock")) { String *eb = Getattr(n, "feature:threadendblock"); if (eb) { Append(f, eb); } else { Append(f, "SWIG_PYTHON_THREAD_END_BLOCK;\n"); } } } virtual void thread_begin_allow(Node *n, String *f) { if (!GetFlag(n, "feature:nothreadallow")) { String *bb = Getattr(n, "feature:threadbeginallow"); Append(f, "{\n"); if (bb) { Append(f, bb); } else { Append(f, "SWIG_PYTHON_THREAD_BEGIN_ALLOW;\n"); } } } virtual void thread_end_allow(Node *n, String *f) { if (!GetFlag(n, "feature:nothreadallow")) { String *eb = Getattr(n, "feature:threadendallow"); Append(f, "\n"); if (eb) { Append(f, eb); } else { Append(f, "SWIG_PYTHON_THREAD_END_ALLOW;"); } Append(f, "\n}"); } } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { SWIG_library_directory("python"); int doxygen_translator_flags = 0; for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-interface") == 0) { if (argv[i + 1]) { interface = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-globals") == 0) { if (argv[i + 1]) { global_name = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if ((strcmp(argv[i], "-shadow") == 0) || ((strcmp(argv[i], "-proxy") == 0))) { shadow = 1; Swig_mark_arg(i); } else if ((strcmp(argv[i], "-noproxy") == 0)) { shadow = 0; Swig_mark_arg(i); } else if (strcmp(argv[i], "-keyword") == 0) { use_kw = 1; SWIG_cparse_set_compact_default_args(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nortti") == 0) { nortti = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-threads") == 0) { threads = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-nothreads") == 0) { /* Turn off thread support mode */ nothreads = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-dirvtable") == 0) { dirvtable = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-doxygen") == 0) { doxygen = 1; scan_doxygen_comments = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-doxygen-translator") == 0) { doxygen_translator_flags |= DoxygenTranslator::debug_translator; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-doxygen-parser") == 0) { doxygen_translator_flags |= DoxygenTranslator::debug_parser; Swig_mark_arg(i); } else if (strcmp(argv[i], "-nofastunpack") == 0) { fastunpack = 0; Swig_mark_arg(i); } else if (strcmp(argv[i], "-fastproxy") == 0) { fastproxy = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-olddefs") == 0) { olddefs = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-castmode") == 0) { castmode = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-extranative") == 0) { extranative = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-flatstaticmethod") == 0) { flat_static_method = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-noh") == 0) { no_header_file = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-newvwm") == 0) { /* Turn on new value wrapper mode */ /* Undocumented option, did have -help text: New value wrapper mode, use only when everything else fails */ Swig_value_wrapper_mode(1); no_header_file = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-O") == 0) { fastproxy = 1; Wrapper_fast_dispatch_mode_set(1); Wrapper_virtual_elimination_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-help") == 0) { fputs(usage1, stdout); fputs(usage2, stdout); fputs(usage3, stdout); } else if (strcmp(argv[i], "-builtin") == 0) { builtin = 1; Preprocessor_define("SWIGPYTHON_BUILTIN", 0); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nogil") == 0) { nogil = 1; Preprocessor_define("SWIGPYTHON_NOGIL", 0); Swig_mark_arg(i); } else if (strcmp(argv[i], "-relativeimport") == 0) { relativeimport = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-cppcast") == 0 || strcmp(argv[i], "-fastinit") == 0 || strcmp(argv[i], "-fastquery") == 0 || strcmp(argv[i], "-fastunpack") == 0 || strcmp(argv[i], "-modern") == 0 || strcmp(argv[i], "-modernargs") == 0 || strcmp(argv[i], "-noproxydel") == 0 || strcmp(argv[i], "-safecstrings") == 0) { Printf(stderr, "Deprecated command line option: %s. Ignored, this option is now always on.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-py3") == 0) { Printf(stderr, "Deprecated command line option: %s. Ignored, this option is no longer supported.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-aliasobj0") == 0 || strcmp(argv[i], "-buildnone") == 0 || strcmp(argv[i], "-classic") == 0 || strcmp(argv[i], "-classptr") == 0 || strcmp(argv[i], "-new_repr") == 0 || strcmp(argv[i], "-new_vwm") == 0 || strcmp(argv[i], "-newrepr") == 0 || strcmp(argv[i], "-noaliasobj0") == 0 || strcmp(argv[i], "-nobuildnone") == 0 || strcmp(argv[i], "-nocastmode") == 0 || strcmp(argv[i], "-nocppcast") == 0 || strcmp(argv[i], "-nodirvtable") == 0 || strcmp(argv[i], "-noextranative") == 0 || strcmp(argv[i], "-nofastinit") == 0 || strcmp(argv[i], "-nofastproxy") == 0 || strcmp(argv[i], "-nofastquery") == 0 || strcmp(argv[i], "-nomodern") == 0 || strcmp(argv[i], "-nomodernargs") == 0 || strcmp(argv[i], "-noolddefs") == 0 || strcmp(argv[i], "-nooutputtuple") == 0 || strcmp(argv[i], "-noproxyimport") == 0 || strcmp(argv[i], "-nosafecstrings") == 0 || strcmp(argv[i], "-old_repr") == 0 || strcmp(argv[i], "-oldrepr") == 0 || strcmp(argv[i], "-outputtuple") == 0 || strcmp(argv[i], "-proxydel") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is no longer available.\n", argv[i]); Swig_mark_arg(i); Exit(EXIT_FAILURE); } } } if (builtin && !shadow) { Printf(stderr, "Incompatible options -builtin and -noproxy specified.\n"); Exit(EXIT_FAILURE); } if (fastproxy) { Preprocessor_define("SWIGPYTHON_FASTPROXY", 0); } if (doxygen) doxygenTranslator = new PyDocConverter(doxygen_translator_flags); if (!global_name) global_name = NewString("cvar"); Preprocessor_define("SWIGPYTHON 1", 0); SWIG_config_file("python.swg"); allow_overloading(); } /* ------------------------------------------------------------ * top() * ------------------------------------------------------------ */ virtual int top(Node *n) { /* check if directors are enabled for this module. note: this * is a "master" switch, without which no director code will be * emitted. %feature("director") statements are also required * to enable directors for individual classes or methods. * * use %module(directors="1") modulename at the start of the * interface file to enable director generation. */ String *mod_docstring = NULL; String *moduleimport = NULL; { Node *mod = Getattr(n, "module"); if (mod) { Node *options = Getattr(mod, "options"); if (options) { int dirprot = 0; if (Getattr(options, "dirprot")) { dirprot = 1; } if (Getattr(options, "nodirprot")) { dirprot = 0; } if (Getattr(options, "directors")) { allow_directors(); if (dirprot) allow_dirprot(); } if (Getattr(options, "threads")) { threads = 1; } if (Getattr(options, "castmode")) { castmode = 1; } if (Getattr(options, "nocastmode")) { Printf(stderr, "Deprecated module option: %s. This option is no longer supported.\n", "nocastmode"); Exit(EXIT_FAILURE); } if (Getattr(options, "extranative")) { extranative = 1; } if (Getattr(options, "noextranative")) { Printf(stderr, "Deprecated module option: %s. This option is no longer supported.\n", "noextranative"); Exit(EXIT_FAILURE); } if (Getattr(options, "outputtuple")) { Printf(stderr, "Deprecated module option: %s. This option is no longer supported.\n", "outputtuple"); Exit(EXIT_FAILURE); } if (Getattr(options, "nooutputtuple")) { Printf(stderr, "Deprecated module option: %s. This option is no longer supported.\n", "nooutputtuple"); Exit(EXIT_FAILURE); } mod_docstring = Getattr(options, "docstring"); package = Getattr(options, "package"); moduleimport = Getattr(options, "moduleimport"); } } } /* Set comparison with none for ConstructorToFunction */ setSubclassInstanceCheck(NewString("$arg != Py_None")); /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = !no_header_file ? Getattr(n, "outfile_h") : 0; f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); builtin_getset = NewHash(); builtin_closures = NewHash(); builtin_closures_code = NewString(""); class_members = NewHash(); builtin_methods = NewString(""); builtin_default_unref = NewString("delete $self;"); f_varlinks = NewString(""); if (builtin) { f_builtins = NewString(""); } if (Swig_directors_enabled()) { if (!no_header_file) { f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } else { f_runtime_h = f_runtime; } } /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); const_code = NewString(""); methods = NewString(""); methods_proxydocs = NewString(""); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "PYTHON"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); } if (nothreads) { Printf(f_runtime, "#define SWIG_PYTHON_NO_THREADS\n"); } else if (threads) { Printf(f_runtime, "#define SWIG_PYTHON_THREADS\n"); } if (!dirvtable) { Printf(f_runtime, "#define SWIG_PYTHON_DIRECTOR_NO_VTABLE\n"); } if (nortti) { Printf(f_runtime, "#ifndef SWIG_DIRECTOR_NORTTI\n"); Printf(f_runtime, "#define SWIG_DIRECTOR_NORTTI\n"); Printf(f_runtime, "#endif\n"); } if (castmode) { Printf(f_runtime, "#define SWIG_CASTRANK_MODE\n"); Printf(f_runtime, "#define SWIG_PYTHON_CAST_MODE\n"); } if (extranative) { Printf(f_runtime, "#define SWIG_PYTHON_EXTRA_NATIVE_CONTAINERS\n"); } if (builtin) { Printf(f_runtime, "#define SWIGPYTHON_BUILTIN\n"); } if (fastproxy) { Printf(f_runtime, "#define SWIGPYTHON_FASTPROXY\n"); } if (nogil) { Printf(f_runtime, "#define SWIGPYTHON_NOGIL\n"); } Printf(f_runtime, "\n"); Printf(f_header, "#ifdef SWIG_TypeQuery\n"); Printf(f_header, "# undef SWIG_TypeQuery\n"); Printf(f_header, "#endif\n"); Printf(f_header, "#define SWIG_TypeQuery SWIG_Python_TypeQuery\n"); /* Set module name */ module = Copy(Getattr(n, "name")); mainmodule = Getattr(n, "name"); if (Swig_directors_enabled()) { Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module); if (dirprot_mode()) { Printf(f_directors_h, "#include \n"); Printf(f_directors_h, "#include \n\n"); } Printf(f_directors, "\n\n"); Printf(f_directors, "/* ---------------------------------------------------\n"); Printf(f_directors, " * C++ director class methods\n"); Printf(f_directors, " * --------------------------------------------------- */\n\n"); if (outfile_h) { String *filename = Swig_file_filename(outfile_h); Printf(f_directors, "#include \"%s\"\n\n", filename); Delete(filename); } } /* If shadow classing is enabled, we're going to change the module name to "_module" */ String *default_import_code = NewString(""); if (shadow) { String *filen = NewStringf("%s%s.py", SWIG_output_directory(), Char(module)); // If we don't have an interface then change the module name X to _X if (interface) module = interface; else Insert(module, 0, "_"); if ((f_shadow_py = NewFile(filen, "w", SWIG_output_files())) == 0) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Delete(filen); filen = NULL; f_shadow = NewString(""); f_shadow_begin = NewString(""); f_shadow_imports = NewHash(); f_shadow_after_begin = NewString(""); f_shadow_stubs = NewString(""); Swig_register_filebyname("shadow", f_shadow); Swig_register_filebyname("python", f_shadow); if (!builtin) { /* Import the low-level C/C++ module. This should be a relative import, * since the shadow module may also have been imported by a relative * import, and there is thus no guarantee that the low-level C/C++ module is on * sys.path. Relative imports must be explicitly specified from 2.6.0 * onwards (implicit relative imports raised a DeprecationWarning in 2.6, * and fail in 2.7 onwards). * * First check for __spec__.parent which is available from 3.4 onwards, * see https://docs.python.org/3/reference/import.html#spec. If not, * check for __package__, which is available from 2.6 onwards (see PEP366), * but will no longer be set/used in 3.15. * Next try determine the shadow wrapper's package based on the __name__ it * was given by the importer that loaded it. * If the module is in a package, load the low-level C/C++ module from the * same package, otherwise load it as a global module. */ Printv(default_import_code, "# Import the low-level C/C++ module\n", NULL); Printv(default_import_code, "if getattr(globals().get(\"__spec__\"), \"parent\", None) or __package__ or \".\" in __name__:\n", NULL); Printv(default_import_code, tab4, "from . import ", module, "\n", NULL); Printv(default_import_code, "else:\n", NULL); Printv(default_import_code, tab4, "import ", module, "\n", NULL); } else { Printv(default_import_code, "# Pull in all the attributes from the low-level C/C++ module\n", NULL); Printv(default_import_code, "if getattr(globals().get(\"__spec__\"), \"parent\", None) or __package__ or \".\" in __name__:\n", NULL); Printv(default_import_code, tab4, "from .", module, " import *\n", NULL); Printv(default_import_code, "else:\n", NULL); Printv(default_import_code, tab4, "from ", module, " import *\n", NULL); } if (!builtin) { /* Need builtins to qualify names like Exception that might also be defined in this module (try both Python 3 and Python 2 names) */ Printv(f_shadow, "try:\n", tab4, "import builtins as __builtin__\n", "except ImportError:\n", tab4, "import __builtin__\n", NULL); } if (!builtin && fastproxy) { Printf(f_shadow, "\n"); Printf(f_shadow, "_swig_new_instance_method = %s.SWIG_PyInstanceMethod_New\n", module); Printf(f_shadow, "_swig_new_static_method = %s.SWIG_PyStaticMethod_New\n", module); } if (!builtin) { Printv(f_shadow, "\n", "def _swig_repr(self):\n", tab4, "try:\n", tab4, tab4, "strthis = \"proxy of \" + self.this.__repr__()\n", tab4, "except __builtin__.Exception:\n", tab4, tab4, "strthis = \"\"\n", tab4, "return \"<%s.%s; %s >\" % (self.__class__.__module__, self.__class__.__name__, strthis,)\n\n", NIL); Printv(f_shadow, "\n", "def _swig_setattr_nondynamic_instance_variable(set):\n", tab4, "def set_instance_attr(self, name, value):\n", tab4, tab4, "if name == \"this\":\n", tab4, tab4, tab4, "set(self, name, value)\n", tab4, tab4, "elif name == \"thisown\":\n", tab4, tab4, tab4, "self.this.own(value)\n", tab4, tab4, "elif hasattr(self, name) and isinstance(getattr(type(self), name), property):\n", tab4, tab4, tab4, "set(self, name, value)\n", tab4, tab4, "else:\n", tab4, tab4, tab4, "raise AttributeError(\"You cannot add instance attributes to %s\" % self)\n", tab4, "return set_instance_attr\n\n", NIL); Printv(f_shadow, "\n", "def _swig_setattr_nondynamic_class_variable(set):\n", tab4, "def set_class_attr(cls, name, value):\n", tab4, tab4, "if hasattr(cls, name) and not isinstance(getattr(cls, name), property):\n", tab4, tab4, tab4, "set(cls, name, value)\n", tab4, tab4, "else:\n", tab4, tab4, tab4, "raise AttributeError(\"You cannot add class attributes to %s\" % cls)\n", tab4, "return set_class_attr\n\n", NIL); Printv(f_shadow, "\n", "def _swig_add_metaclass(metaclass):\n", tab4, "\"\"\"Class decorator for adding a metaclass to a SWIG wrapped class - a slimmed down version of six.add_metaclass\"\"\"\n", tab4, "def wrapper(cls):\n", tab4, tab4, "return metaclass(cls.__name__, cls.__bases__, cls.__dict__.copy())\n", tab4, "return wrapper\n\n", NIL); Printv(f_shadow, "\n", "class _SwigNonDynamicMeta(type):\n", tab4, "\"\"\"Meta class to enforce nondynamic attributes (no new attributes) for a class\"\"\"\n", tab4, "__setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__)\n", "\n", NIL); Printv(f_shadow, "\n", NIL); if (Swig_directors_enabled()) Printv(f_shadow, "import weakref\n\n", NIL); } } // Include some information in the code Printf(f_header, "\n/*-----------------------------------------------\n @(target):= %s.so\n\ ------------------------------------------------*/\n", module); Printf(f_header, "#if PY_VERSION_HEX >= 0x03000000\n"); Printf(f_header, "# define SWIG_init PyInit_%s\n\n", module); Printf(f_header, "#else\n"); Printf(f_header, "# define SWIG_init init%s\n\n", module); Printf(f_header, "#endif\n"); Printf(f_runtime, "#define SWIG_name \"%s\"\n", module); Printf(f_wrappers, "#ifdef __cplusplus\n"); Printf(f_wrappers, "extern \"C\" {\n"); Printf(f_wrappers, "#endif\n"); Append(const_code, "static swig_const_info swig_const_table[] = {\n"); Append(methods, "static PyMethodDef SwigMethods[] = {\n"); Append(methods_proxydocs, "static PyMethodDef SwigMethods_proxydocs[] = {\n"); /* the method exported for replacement of new.instancemethod in Python 3 */ add_pyinstancemethod_new(); add_pystaticmethod_new(); if (builtin) { SwigType *s = NewString("SwigPyObject"); SwigType_add_pointer(s); SwigType_remember(s); Delete(s); } /* emit code */ Language::top(n); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (make it occur before %header section) Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director_py_mutex.swg", f_runtime); Swig_insert_file("director_guard.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } /* Close language module */ Append(methods, "\t { NULL, NULL, 0, NULL }\n"); Append(methods, "};\n"); Printf(f_wrappers, "%s\n", methods); Append(methods_proxydocs, "\t { NULL, NULL, 0, NULL }\n"); Append(methods_proxydocs, "};\n"); if ((fastproxy && !builtin) || have_fast_proxy_static_member_method_callback) Printf(f_wrappers, "%s\n", methods_proxydocs); if (builtin) { Dump(f_builtins, f_wrappers); } SwigType_emit_type_table(f_runtime, f_wrappers); Append(const_code, "{0, 0, 0, 0.0, 0, 0}};\n"); Printf(f_wrappers, "%s\n", const_code); if (have_fast_proxy_static_member_method_callback) Printf(f_init, " SWIG_Python_FixMethods(SwigMethods_proxydocs, swig_const_table, swig_types, swig_type_initial);\n\n"); initialize_threads(f_init); Dump(f_varlinks, f_init); Printf(f_init, " return 0;\n"); Printf(f_init, "}\n"); Printf(f_wrappers, "#ifdef __cplusplus\n"); Printf(f_wrappers, "}\n"); Printf(f_wrappers, "#endif\n"); if (shadow) { Swig_banner_target_lang(f_shadow_py, "#"); if (mod_docstring) { if (Len(mod_docstring)) { const char *triple_double = "\"\"\""; // follow PEP257 rules: https://www.python.org/dev/peps/pep-0257/ // reported by pep257: https://github.com/GreenSteam/pep257 bool multi_line_ds = Strchr(mod_docstring, '\n') != 0; Printv(f_shadow_py, "\n", triple_double, multi_line_ds ? "\n":"", mod_docstring, multi_line_ds ? "\n":"", triple_double, "\n", NIL); } Delete(mod_docstring); mod_docstring = NULL; } if (Len(f_shadow_begin) > 0) Printv(f_shadow_py, "\n", f_shadow_begin, "\n", NIL); Printv(f_shadow_py, "\nfrom sys import version_info as _swig_python_version_info\n", NULL); if (Len(f_shadow_after_begin) > 0) Printv(f_shadow_py, f_shadow_after_begin, "\n", NIL); if (moduleimport) { Replaceall(moduleimport, "$module", module); Printv(f_shadow_py, moduleimport, "\n", NIL); } else { Printv(f_shadow_py, default_import_code, NIL); } if (Len(f_shadow) > 0) Printv(f_shadow_py, "\n", f_shadow, "\n", NIL); if (Len(f_shadow_stubs) > 0) Printv(f_shadow_py, f_shadow_stubs, "\n", NIL); Delete(f_shadow_py); } /* Close all of the files */ Dump(f_runtime, f_begin); Dump(f_header, f_begin); if (Swig_directors_enabled()) { Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); if (f_runtime_h != f_begin) Delete(f_runtime_h); Dump(f_directors, f_begin); } Dump(f_wrappers, f_begin); if (builtin && builtin_bases_needed) Printf(f_begin, "static PyTypeObject *builtin_bases[%d];\n\n", max_bases + 2); Wrapper_pretty_print(f_init, f_begin); Delete(default_import_code); Delete(f_shadow_after_begin); Delete(f_shadow_imports); Delete(f_shadow_begin); Delete(f_shadow); Delete(f_header); Delete(f_wrappers); Delete(f_builtins); Delete(f_init); Delete(f_directors); Delete(f_directors_h); Delete(f_runtime); Delete(f_begin); Delete(f_varlinks); return SWIG_OK; } /* ------------------------------------------------------------ * Emit the wrapper for PyInstanceMethod_New to MethodDef array. * This wrapper is used to implement -fastproxy, * as a replacement of new.instancemethod in Python 3. * ------------------------------------------------------------ */ int add_pyinstancemethod_new() { if (!builtin && fastproxy) { String *name = NewString("SWIG_PyInstanceMethod_New"); String *line = NewString(""); Printf(line, "\t { \"%s\", %s, METH_O, NULL},\n", name, name); Append(methods, line); Append(methods_proxydocs, line); Delete(line); Delete(name); } return 0; } /* ------------------------------------------------------------ * Emit the wrapper for PyStaticMethod_New to MethodDef array. * This wrapper is used to ensure the correct documentation is * generated for static methods when using -fastproxy * ------------------------------------------------------------ */ int add_pystaticmethod_new() { if (!builtin && fastproxy) { String *name = NewString("SWIG_PyStaticMethod_New"); String *line = NewString(""); Printf(line, "\t { \"%s\", %s, METH_O, NULL},\n", name, name); Append(methods, line); Append(methods_proxydocs, line); Delete(line); Delete(name); } return 0; } /* ------------------------------------------------------------ * subpkg_tail() * * Return the name of 'other' package relative to 'base'. * * 1. If 'other' is a sub-package of 'base', returns the 'other' relative to * 'base'. * 2. If 'other' and 'base' are equal, returns empty string "". * 3. In any other case, NULL pointer is returned. * * The 'base' and 'other' are expected to be fully qualified names. * * NOTE: none of 'base' nor 'other' can be null. * * Examples: * * # base other tail * -- ---- ----- ---- * 1 "Foo" "Foo.Bar" -> "Bar" * 2 "Foo" "Foo." -> "" * 3 "Foo" "FooB.ar" -> NULL * 4 "Foo.Bar" "Foo.Bar" -> "" * 5 "Foo.Bar" "Foo" -> NULL * 6 "Foo.Bar" "Foo.Gez" -> NULL * * NOTE: the example #2 is actually a syntax error (at input). I believe * swig parser prevents us from this case happening here. * ------------------------------------------------------------ */ static String *subpkg_tail(const String *base, const String *other) { int baselen = Len(base); int otherlen = Len(other); if (Strncmp(other, base, baselen) == 0) { if ((baselen < otherlen) && (Char(other))[baselen] == '.') { return NewString((Char(other)) + baselen + 1); } else if (baselen == otherlen) { return NewString(""); } else { return 0; } } else { return 0; } } /* ------------------------------------------------------------ * abs_import_directive_string() * * Return a string containing python code to import module. * * pkg package name or the module being imported * mod module name of the module being imported * pfx optional prefix to module name * * NOTE: keep this function consistent with abs_import_name_string(). * ------------------------------------------------------------ */ static String *abs_import_directive_string(const String *pkg, const String *mod, const char *pfx = "") { String *out = NewString(""); if (pkg && *Char(pkg)) { Printf(out, "import %s.%s%s\n", pkg, pfx, mod); } else { Printf(out, "import %s%s\n", pfx, mod); } return out; } /* ------------------------------------------------------------ * rel_import_directive_string() * * Return a string containing python code to import module that * is potentially within a package. * * mainpkg package name of the module which imports the other module * pkg package name or the module being imported * mod module name of the module being imported * pfx optional prefix to module name * * NOTE: keep this function consistent with rel_import_name_string(). * ------------------------------------------------------------ */ static String *rel_import_directive_string(const String *mainpkg, const String *pkg, const String *mod, const char *pfx = "") { /* NOTE: things are not so trivial. This is what we do here (by examples): * * 0. To import module 'foo', which is not in any package, we do absolute * import: * * import foo * * 1. To import 'pkg1.pkg2.foo', when mainpkg != "pkg1" and * mainpkg != "pkg1.pkg2" or when mainpkg is not given we do absolute * import: * * import pkg1.pkg2.foo * * 2. To import module pkg1.foo, when mainpkg == "pkg1", we do: * * - for py3 = 0: * * import foo * * - for py3 = 1: * * from . import foo * * 3. To import "pkg1.pkg2.pkg3.foo", when mainpkg = "pkg1", we do: * * - for py3 == 0: * * import pkg2.pkg3.foo * * - for py3 == 1: * * from . import pkg2 # [1] * import pkg1.pkg2.pkg3.foo * * NOTE: [1] is necessary for pkg2.foo to be present in the importing module */ String *apkg = 0; // absolute (FQDN) package name of pkg String *rpkg = 0; // relative package name int py3_rlen1 = 0; // length of 1st level sub-package name, used by py3 String *out = NewString(""); if (pkg && *Char(pkg)) { if (mainpkg) { String *tail = subpkg_tail(mainpkg, pkg); if (tail) { if (*Char(tail)) { rpkg = NewString(tail); const char *py3_end1 = Strchr(rpkg, '.'); if (!py3_end1) py3_end1 = (Char(rpkg)) + Len(rpkg); py3_rlen1 = (int)(py3_end1 - Char(rpkg)); } else { rpkg = NewString(""); } Delete(tail); } else { apkg = NewString(pkg); } } else { apkg = NewString(pkg); } } else { apkg = NewString(""); } if (apkg) { Printf(out, "import %s%s%s%s\n", apkg, *Char(apkg) ? "." : "", pfx, mod); Delete(apkg); } else { if (py3_rlen1) Printf(out, "from . import %.*s\n", py3_rlen1, rpkg); Printf(out, "from .%s import %s%s\n", rpkg, pfx, mod); Delete(rpkg); } return out; } /* ------------------------------------------------------------ * import_directive_string() * ------------------------------------------------------------ */ static String *import_directive_string(const String *mainpkg, const String *pkg, const String *mod, const char *pfx = "") { if (!relativeimport) { return abs_import_directive_string(pkg, mod, pfx); } else { return rel_import_directive_string(mainpkg, pkg, mod, pfx); } } /* ------------------------------------------------------------ * abs_import_name_string() * * Return a string with the name of a symbol (perhaps imported * from external module by absolute import directive). * * mainpkg package name of current module * mainmod module name of current module * pkg package name of (perhaps other) module * mod module name of (perhaps other) module * sym symbol name * * NOTE: mainmod, mod, and sym can't be NULL. * NOTE: keep this function consistent with abs_import_directive_string() * ------------------------------------------------------------ */ static String *abs_import_name_string(const String *mainpkg, const String *mainmod, const String *pkg, const String *mod, const String *sym) { String *out = NewString(""); if (pkg && *Char(pkg)) { if (mainpkg && *Char(mainpkg)) { if (Strcmp(mainpkg,pkg) != 0 || Strcmp(mainmod, mod) != 0) { Printf(out, "%s.%s.", pkg, mod); } } else { Printf(out, "%s.%s.", pkg, mod); } } else if ((mainpkg && *Char(mainpkg)) || Strcmp(mainmod, mod) != 0) { Printf(out, "%s.", mod); } Append(out, sym); return out; } /* ------------------------------------------------------------ * rel_import_name_string() * * Return a string with the name of a symbol (perhaps imported * from external module by relative import directive). * * mainpkg package name of current module * mainmod module name of current module * pkg package name of (perhaps other) module * mod module name of (perhaps other) module * sym symbol name * * NOTE: mainmod, mod, and sym can't be NULL. * NOTE: keep this function consistent with rel_import_directive_string() * ------------------------------------------------------------ */ static String *rel_import_name_string(const String *mainpkg, const String *mainmod, const String *pkg, const String *mod, const String *sym) { String *out = NewString(""); if (pkg && *Char(pkg)) { String *tail = 0; if (mainpkg) tail = subpkg_tail(mainpkg, pkg); if (!tail) tail = NewString(pkg); if (*Char(tail)) { Printf(out, "%s.%s.", tail, mod); } else if (Strcmp(mainmod, mod) != 0) { Printf(out, "%s.", mod); } Delete(tail); } else if ((mainpkg && *Char(mainpkg)) || Strcmp(mainmod, mod) != 0) { Printf(out, "%s.", mod); } Append(out, sym); return out; } /* ------------------------------------------------------------ * import_name_string() * ------------------------------------------------------------ */ static String *import_name_string(const String *mainpkg, const String *mainmod, const String *pkg, const String *mod, const String *sym) { if (!relativeimport) { return abs_import_name_string(mainpkg,mainmod,pkg,mod,sym); } else { return rel_import_name_string(mainpkg,mainmod,pkg,mod,sym); } } /* ------------------------------------------------------------ * importDirective() * ------------------------------------------------------------ */ virtual int importDirective(Node *n) { if (shadow) { String *modname = Getattr(n, "module"); if (modname) { // Find the module node for this imported module. It should be the // first child but search just in case. Node *mod = firstChild(n); while (mod && Strcmp(nodeType(mod), "module") != 0) mod = nextSibling(mod); Node *options = Getattr(mod, "options"); String *pkg = options ? Getattr(options, "package") : 0; if (!options || (!Getattr(options, "noshadow") && !Getattr(options, "noproxy"))) { String *_import = import_directive_string(package, pkg, modname, "_"); if (!GetFlagAttr(f_shadow_imports, _import)) { String *import = import_directive_string(package, pkg, modname); Printf(builtin ? f_shadow_after_begin : f_shadow, "%s", import); Delete(import); SetFlag(f_shadow_imports, _import); } Delete(_import); } } } return Language::importDirective(n); } /* ------------------------------------------------------------ * funcCall() * * Emit shadow code to call a function in the extension * module. Using proper argument and calling style for * given node n. * ------------------------------------------------------------ */ String *funcCall(String *name, String *parms) { String *str = NewString(""); Printv(str, module, ".", name, "(", parms, ")", NIL); return str; } /* ------------------------------------------------------------ * indent_pythoncode() * * Format (indent) Python code. * Remove leading whitespace from 'code' and re-indent using * the indentation string in 'indent'. * ------------------------------------------------------------ */ String *indent_pythoncode(const String *code, const_String_or_char_ptr indent, String *file, int line, const char *directive_name) { String *out = NewString(""); String *temp; char *t; if (!indent) indent = ""; temp = NewString(code); t = Char(temp); if (*t == '{') { Delitem(temp, 0); Delitem(temp, DOH_END); } /* Split the input text into lines */ List *clist = SplitLines(temp); Delete(temp); // Line number within the pythoncode. int py_line = 0; String *initial = 0; Iterator si; /* Get the initial indentation. Skip lines which only contain whitespace * and/or a comment, as the indentation of those doesn't matter: * * A logical line that contains only spaces, tabs, formfeeds and * possibly a comment, is ignored (i.e., no NEWLINE token is * generated). * * see: * https://docs.python.org/2/reference/lexical_analysis.html#blank-lines * https://docs.python.org/3/reference/lexical_analysis.html#blank-lines */ for (si = First(clist); si.item; si = Next(si), ++py_line) { const char *c = Char(si.item); int i; for (i = 0; isspace((unsigned char)c[i]); i++) { // Scan forward until we find a non-space (which may be a null byte). } char ch = c[i]; if (ch && ch != '#') { // Found a line with actual content. initial = NewStringWithSize(c, i); break; } if (ch) { Printv(out, indent, c, NIL); } Putc('\n', out); } // Process remaining lines. for ( ; si.item; si = Next(si), ++py_line) { const char *c = Char(si.item); // If no prefixed line was found, the above loop should have completed. assert(initial); int i; for (i = 0; isspace((unsigned char)c[i]); i++) { // Scan forward until we find a non-space (which may be a null byte). } char ch = c[i]; if (!ch) { // Line is just whitespace - emit an empty line. Putc('\n', out); continue; } if (ch == '#') { // Comment - the indentation doesn't matter to python, but try to // adjust the whitespace for the benefit of human readers (though SWIG // currently seems to always remove any whitespace before a '#' before // we get here, in which case we'll just leave the comment at the start // of the line). if (i >= Len(initial)) { Printv(out, indent, NIL); } Printv(out, c + i, "\n", NIL); continue; } if (i < Len(initial)) { // There's non-whitespace in the initial prefix of this line. Swig_error(file, line, "Line indented less than expected (line %d of %s) as no line should be indented less than the indentation in line 1\n", py_line, directive_name); Printv(out, indent, c, "\n", NIL); } else { if (memcmp(c, Char(initial), Len(initial)) == 0) { // Prefix matches initial, so just remove it. Printv(out, indent, c + Len(initial), "\n", NIL); continue; } Swig_warning(WARN_PYTHON_INDENT_MISMATCH, file, line, "Whitespace indentation is inconsistent compared to earlier lines (line %d of %s)\n", py_line, directive_name); // To avoid gratuitously breaking interface files which worked with // SWIG <= 3.0.5, we remove a prefix of the same number of bytes for // lines which start with different whitespace to the line we got // 'initial' from. Printv(out, indent, c + Len(initial), "\n", NIL); } } Delete(clist); return out; } /* ------------------------------------------------------------ * indent_docstring() * * Format (indent) a Python docstring. * Remove leading whitespace from 'code' and re-indent using * the indentation string in 'indent'. * ------------------------------------------------------------ */ String *indent_docstring(const String *code, const char *indent) { String *out = NewString(""); String *temp; char *t; if (!indent) indent = ""; temp = NewString(code); t = Char(temp); if (*t == '{') { Delitem(temp, 0); Delitem(temp, DOH_END); } /* Split the input text into lines */ List *clist = SplitLines(temp); Delete(temp); Iterator si; int truncate_characters_count = INT_MAX; for (si = First(clist); si.item; si = Next(si)) { const char *c = Char(si.item); int i; for (i = 0; isspace((unsigned char)c[i]); i++) { // Scan forward until we find a non-space (which may be a null byte). } char ch = c[i]; if (ch) { // Found a line which isn't just whitespace if (i < truncate_characters_count) truncate_characters_count = i; } } if (truncate_characters_count == INT_MAX) truncate_characters_count = 0; for (si = First(clist); si.item; si = Next(si)) { const char *c = Char(si.item); int i; for (i = 0; isspace((unsigned char)c[i]); i++) { // Scan forward until we find a non-space (which may be a null byte). } char ch = c[i]; if (!ch) { // Line is just whitespace - emit an empty line. Putc('\n', out); continue; } Printv(out, indent, c + truncate_characters_count, "\n", NIL); } Delete(clist); return out; } /* ------------------------------------------------------------ * autodoc level declarations * ------------------------------------------------------------ */ enum autodoc_l { NO_AUTODOC = -2, // no autodoc STRING_AUTODOC = -1, // use provided string NAMES_AUTODOC = 0, // only parameter names TYPES_AUTODOC = 1, // parameter names and types EXTEND_AUTODOC = 2, // extended documentation and parameter names EXTEND_TYPES_AUTODOC = 3 // extended documentation and parameter types + names }; autodoc_l autodoc_level(String *autodoc) { autodoc_l dlevel = NO_AUTODOC; char *c = Char(autodoc); if (c) { if (isdigit(c[0])) { dlevel = (autodoc_l) atoi(c); } else { if (strcmp(c, "extended") == 0) { dlevel = EXTEND_AUTODOC; } else { dlevel = STRING_AUTODOC; } } } return dlevel; } /* ------------------------------------------------------------ * have_docstring() * * Check if there is a docstring directive and it has text, * or there is an autodoc flag set * ------------------------------------------------------------ */ bool have_docstring(Node *n) { String *str = Getattr(n, "feature:docstring"); return ((str && Len(str) > 0) || (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc")) || (doxygen && doxygenTranslator->hasDocumentation(n)) ); } /* ------------------------------------------------------------ * find_overload_with_docstring() * * This function should be called with the node pointing to the * last element of an overload set and returns an overload with * a docstring or null if there are none. * * The idea is that, because the Python docstring is shared by * all overloads, it's this function return value and not the * node itself which needs to be passes to docstring() later. * ------------------------------------------------------------ */ Node *find_overload_with_docstring(Node *n) { for (Node *node_with_doc = n; node_with_doc; node_with_doc = Getattr(node_with_doc, "sym:previousSibling")) { if (have_docstring(node_with_doc)) return node_with_doc; } return NULL; } /* ------------------------------------------------------------ * build_combined_docstring() * * Build the full docstring: * Use the docstring if there is one present otherwise * use the Doxygen comment if there is one present. * Ignore autodoc if there is a Doxygen comment, otherwise * create the autodoc string and append to any docstring. * * Return new string to be deleted by caller (never NIL but * may be empty if there is no docstring). * ------------------------------------------------------------ */ String *build_combined_docstring(Node *n, autodoc_t ad_type, const char *indent = "", bool low_level = false) { bool add_autodoc = true; String *docstr = Getattr(n, "feature:docstring"); if (docstr) { // Simplify the code below by just ignoring empty docstrings. if (!Len(docstr)) docstr = NULL; else docstr = Copy(docstr); } if (docstr) { char *t = Char(docstr); if (*t == '{') { Delitem(docstr, 0); Delitem(docstr, DOH_END); } } if (!docstr) { if (doxygen && doxygenTranslator->hasDocumentation(n)) { docstr = Getattr(n, "python:docstring"); if (!docstr) { docstr = doxygenTranslator->getDocumentation(n, 0); // Avoid rebuilding it again the next time: notice that we can't do // this for the combined doc string as autodoc part of it depends on // the sym:name of the node and it is changed while handling it, so // the cached results become incorrect. But Doxygen docstring only // depends on the comment which is not going to change, so we can // safely cache it. Setattr(n, "python:docstring", Copy(docstr)); } else { // Must copy here since if the docstring is multi-line, the String* // here will get Deleted below, which is bad if it is a pointer to // the cached object! docstr = Copy(docstr); } add_autodoc = false; } } if (add_autodoc && Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc")) { String *autodoc = make_autodoc(n, ad_type, low_level); if (autodoc && Len(autodoc) > 0) { if (docstr) { Append(autodoc, "\n"); Append(autodoc, docstr); } String *tmp = autodoc; autodoc = docstr; docstr = tmp; } Delete(autodoc); } if (!docstr) docstr = NewString(""); // If there is more than one line then make docstrings like this: // // """ // This is line1 // And here is line2 followed by the rest of them // """ // // otherwise, put it all on a single line if (Strchr(docstr, '\n')) { String *tmp = NewString(""); Append(tmp, "\n"); Append(tmp, indent_docstring(docstr, indent)); Append(tmp, indent); Delete(docstr); docstr = tmp; } else { // Removing leading and trailing whitespace for single line docstrings Chop(docstr); const char *c = Char(docstr); if (isspace((int)*c)) { while(isspace((int)*(++c))) { } String *old_docstr = docstr; docstr = NewString(c); Delete(old_docstr); } } return docstr; } /* ------------------------------------------------------------ * docstring() * * Get the docstring text, stripping off {} if necessary, * and enclose in triple double quotes. If autodoc is also * set then it will build a combined docstring. * ------------------------------------------------------------ */ String *docstring(Node *n, autodoc_t ad_type, const char *indent, bool low_level = false) { String *docstr = build_combined_docstring(n, ad_type, indent, low_level); const int len = Len(docstr); if (!len) return docstr; // Notice that all comments are created as raw strings (prefix "r"), // because '\' is used often in comments, but may break Python module from // loading. For example, in doxy comment one may write path in quotes: // // This is path to file "C:\x\file.txt" // // Python will not load the module with such comment because of illegal // escape '\x'. '\' may additionally appear in verbatim or htmlonly sections // of doxygen doc, Latex expressions, ... String *doc = NewString(""); // Determine which kind of quotes to use as delimiters: for single line // strings we can avoid problems with having a quote as the last character // of the docstring by using different kind of quotes as delimiters. For // multi-line strings this problem doesn't arise, as we always have a new // line or spaces at the end of it, but it still does no harm to do it for // them too. // // Note: we use double quotes by default, i.e. if there is no reason to // prefer using single ones, for consistency with the older SWIG versions. const bool useSingleQuotes = (Char(docstr))[len - 1] == '"'; Append(doc, useSingleQuotes ? "r'''" : "r\"\"\""); // We also need to avoid having triple quotes of whichever type we use, as // this would break Python doc string syntax too. Unfortunately there is no // way to have triple quotes inside of raw-triple-quoted string, so we have // to break the string in parts and rely on concatenation of the adjacent // string literals. if (useSingleQuotes) Replaceall(docstr, "'''", "''' \"'''\" '''"); else Replaceall(docstr, "\"\"\"", "\"\"\" '\"\"\"' \"\"\""); Append(doc, docstr); Append(doc, useSingleQuotes ? "'''" : "\"\"\""); Delete(docstr); return doc; } /* ------------------------------------------------------------ * cdocstring() * * Get the docstring text as it would appear in C-language * source code (but without quotes around it). * ------------------------------------------------------------ */ String *cdocstring(Node *n, autodoc_t ad_type, bool low_level = false) { String *ds = build_combined_docstring(n, ad_type, "", low_level); Replaceall(ds, "\\", "\\\\"); Replaceall(ds, "\"", "\\\""); Replaceall(ds, "\n", "\\n\"\n\t\t\""); return ds; } /* ----------------------------------------------------------------------------- * addMissingParameterNames() * * For functions that have not had nameless parameters set in the Language class. * * Inputs: * plist - entire parameter list * arg_num - the number to start from when naming arguments * Side effects: * The "lname" attribute in each parameter in plist will be contain a parameter name * ----------------------------------------------------------------------------- */ void addMissingParameterNames(Node *n, ParmList *plist, int arg_num) { Parm *p = plist; int i = arg_num; while (p) { if (!Getattr(p, "lname")) { String *name = makeParameterName(n, p, i); Setattr(p, "lname", name); Delete(name); } i++; p = nextSibling(p); } } /* ------------------------------------------------------------ * make_autodocParmList() * * Generate the documentation for the function parameters * Parameters: * arg_num: The number to start assigning unnamed arguments from * func_annotation: Function annotation support * ------------------------------------------------------------ */ String *make_autodocParmList(Node *n, bool showTypes, int arg_num = 1, bool calling = false, bool func_annotation = false) { String *doc = NewString(""); String *pdocs = 0; ParmList *plist = CopyParmList(Getattr(n, "parms")); Parm *p; Parm *pnext; if (calling) func_annotation = false; addMissingParameterNames(n, plist, arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms Swig_typemap_attach_parms("in", plist, 0); Swig_typemap_attach_parms("doc", plist, 0); if (Strcmp(ParmList_protostr(plist), "void") == 0) { //No parameters actually return doc; } for (p = plist; p; p = pnext) { String *tm = Getattr(p, "tmap:in"); if (tm) { pnext = Getattr(p, "tmap:in:next"); if (checkAttribute(p, "tmap:in:numinputs", "0")) { continue; } } else { pnext = nextSibling(p); } String *name = 0; String *type = 0; String *value = 0; String *pdoc = Getattr(p, "tmap:doc"); if (pdoc) { name = Getattr(p, "tmap:doc:name"); type = Getattr(p, "tmap:doc:type"); value = Getattr(p, "tmap:doc:value"); } // Skip the "self" argument - it is added to the parameter list automatically // and shouldn't be included in the Parameters block if (Getattr(p, "self")) { continue; } // Note: the generated name should be consistent with that in kwnames[] String *made_name = 0; if (!name) { name = made_name = makeParameterName(n, p, arg_num); } // Increment the argument number once we are sure this is a real argument to count arg_num++; type = type ? type : Getattr(p, "type"); value = value ? value : Getattr(p, "value"); if (SwigType_isvarargs(type)) { Delete(made_name); break; } if (Len(doc)) { // add a comma to the previous one if any Append(doc, ", "); } // Do the param type too? Node *nn = classLookup(Getattr(p, "type")); String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0); if (showTypes) Printf(doc, "%s ", type_str); Append(doc, name); if (pdoc) { if (!pdocs) // numpydoc style: https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt pdocs = NewString("\nParameters\n----------\n"); Printf(pdocs, "%s\n", pdoc); } // Write the function annotation if (func_annotation) Printf(doc, ": \"%s\"", type_str); // Write default value if (value && !calling) { String *new_value = convertValue(value, Getattr(p, "numval"), Getattr(p, "stringval"), Getattr(p, "type")); if (new_value) { value = new_value; } else { // Even if the value is not representable in the target language, still use it in the documentation, for compatibility with the previous SWIG versions // and because it can still be useful to see the C++ expression there. Node *lookup = Swig_symbol_clookup(value, 0); if (lookup) value = Getattr(lookup, "sym:name"); } Printf(doc, "=%s", value); if (new_value) Delete(new_value); } Delete(type_str); Delete(made_name); } if (pdocs) Setattr(n, "feature:pdocs", pdocs); Delete(plist); return doc; } /* ------------------------------------------------------------ * make_autodoc() * * Build a docstring for the node, using parameter and other * info in the parse tree. If the value of the autodoc * attribute is "0" then do not include parameter types, if * it is "1" (the default) then do. If it has some other * value then assume it is supplied by the extension writer * and use it directly. * ------------------------------------------------------------ */ String *make_autodoc(Node *n, autodoc_t ad_type, bool low_level = false) { int extended = 0; bool first_func = true; // If the function is overloaded then this function is called // for the last one. Rewind to the first so the docstrings are // in order. while (Getattr(n, "sym:previousSibling")) n = Getattr(n, "sym:previousSibling"); String *doc = NewString(""); while (n) { bool showTypes = false; bool skipAuto = false; String *autodoc = Getattr(n, "feature:autodoc"); autodoc_l dlevel = autodoc_level(autodoc); switch (dlevel) { case NO_AUTODOC: break; case NAMES_AUTODOC: showTypes = false; break; case TYPES_AUTODOC: showTypes = true; break; case EXTEND_AUTODOC: extended = 1; showTypes = false; break; case EXTEND_TYPES_AUTODOC: extended = 1; showTypes = true; break; case STRING_AUTODOC: Append(doc, autodoc); skipAuto = true; break; } if (!skipAuto) { /* Check if a documentation name was given for either the low-level C API or high-level Python shadow API */ String *symname = Getattr(n, low_level ? "doc:low:name" : "doc:high:name"); if (!symname) { symname = Getattr(n, "sym:name"); } SwigType *type = Getattr(n, "type"); String *type_str = NULL; // If the function has default arguments, then that documentation covers this version too if (Getattr(n, "defaultargs") != NULL) { n = Getattr(n, "sym:nextSibling"); continue; } if (!first_func) Append(doc, "\n"); if (type) { if (Strcmp(type, "void") == 0) { type_str = NULL; } else { Node *nn = classLookup(type); type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0); } } /* Treat the low-level C API functions for getting/setting variables as methods for documentation purposes */ String *kind = Getattr(n, "kind"); if (kind && Strcmp(kind, "variable") == 0) { if (ad_type == AUTODOC_FUNC) { ad_type = AUTODOC_METHOD; } } /* Treat destructors as methods for documentation purposes */ String *nodeType = Getattr(n, "nodeType"); if (nodeType && Strcmp(nodeType, "destructor") == 0) { if (ad_type == AUTODOC_FUNC) { ad_type = AUTODOC_METHOD; } } switch (ad_type) { case AUTODOC_CLASS: { // Only do the autodoc if there isn't a docstring for the class String *str = Getattr(n, "feature:docstring"); if (!str || Len(str) == 0) { if (builtin) { SwigType *name = Getattr(n, "name"); SwigType *sname = add_explicit_scope(name); String *rname = SwigType_namestr(sname); Printf(doc, "%s", rname); Delete(sname); Delete(rname); } else { String *classname_str = SwigType_namestr(real_classname); if (CPlusPlus) { Printf(doc, "Proxy of C++ %s class.", classname_str); } else { Printf(doc, "Proxy of C %s struct.", classname_str); } Delete(classname_str); } } } break; case AUTODOC_CTOR: if (Strcmp(class_name, symname) == 0) { String *paramList = make_autodocParmList(n, showTypes, 2); Printf(doc, "__init__("); if (showTypes) Printf(doc, "%s ", class_name); if (Len(paramList)) Printf(doc, "self, %s) -> %s", paramList, class_name); else Printf(doc, "self) -> %s", class_name); } else Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name); break; case AUTODOC_DTOR: if (showTypes) Printf(doc, "__del__(%s self)", class_name); else Printf(doc, "__del__(self)"); break; case AUTODOC_STATICFUNC: Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes)); if (type_str) Printf(doc, " -> %s", type_str); break; case AUTODOC_FUNC: Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes)); if (type_str) Printf(doc, " -> %s", type_str); break; case AUTODOC_METHOD: { String *paramList = make_autodocParmList(n, showTypes, 2); Printf(doc, "%s(", symname); if (showTypes) Printf(doc, "%s ", class_name); if (Len(paramList)) Printf(doc, "self, %s)", paramList); else Printf(doc, "self)"); if (type_str) Printf(doc, " -> %s", type_str); } break; case AUTODOC_CONST: // There is no autodoc support for constants currently, this enum // element only exists to allow calling docstring() with it. return NULL; case AUTODOC_VAR: // Variables can also be documented (e.g. through the property() function in python) Printf(doc, "%s", symname); if (showTypes) { String *type = Getattr(n, "tmap:doc:type"); if (!type) type = Getattr(n, "membervariableHandler:type"); if (!type) type = Getattr(n, "type"); Printf(doc, " : %s", type); } break; } Delete(type_str); // Special case: wrapper functions to get a variable should have no parameters. // Because the node is re-used for the setter and getter, the feature:pdocs field will // exist for the getter function, so explicitly avoid printing parameters in this case. bool variable_getter = kind && Strcmp(kind, "variable") == 0 && Getattr(n, "memberget"); if (extended && ad_type != AUTODOC_VAR && !variable_getter) { String *pdocs = Getattr(n, "feature:pdocs"); if (pdocs) { Printv(doc, "\n", pdocs, NULL); } } } // if it's overloaded then get the next decl and loop around again n = Getattr(n, "sym:nextSibling"); if (n) first_func = false; } return doc; } /* ------------------------------------------------------------ * convertIntegerValue() * * Check if string v is an integer and can be represented in * Python. If so, return an appropriate Python representation, * otherwise (or if we are unsure), return NIL. * ------------------------------------------------------------ */ String *convertIntegerValue(String *v, SwigType *resolved_type) { const char *const s = Char(v); char *end; // Check if this is an integer number in any base. errno = 0; long value = strtol(s, &end, 0); if (errno == ERANGE || end == s || *end != '\0') { return NIL; } // So now we are certain that we are indeed dealing with an integer // that has a representation as long given by value. // Restrict to guaranteed supported range in Python, see maxint docs: https://docs.python.org/2/library/sys.html#sys.maxint // Don't do this pointless check when long is 32 bits or smaller as strtol will have already failed with ERANGE #if LONG_MAX > PYTHON_INT_MAX || LONG_MIN < PYTHON_INT_MIN if (value > PYTHON_INT_MAX || value < PYTHON_INT_MIN) { return NIL; } #endif if (Equal(resolved_type, "bool")) // Allow integers as the default value for a bool parameter. return NewString(value ? "True" : "False"); if (value == 0) return NewString(SwigType_ispointer(resolved_type) ? "None" : "0"); return Copy(v); } /* ------------------------------------------------------------ * convertDoubleValue() * * Check if the given string looks like a decimal floating point constant * and return it if it does, otherwise return NIL. * ------------------------------------------------------------ */ String *convertDoubleValue(String *v) { const char *const s = Char(v); char *end; errno = 0; double value = strtod(s, &end); (void) value; if (errno != ERANGE && end != s) { // An added complication: at least some versions of strtod() recognize // hexadecimal floating point numbers which don't exist in Python, so // detect them ourselves and refuse to convert them (this can't be done // without loss of precision in general). // // Also don't accept neither "NAN" nor "INFINITY" (both of which // conveniently contain "n"). if (strpbrk(s, "xXnN")) return NIL; // Disregard optional "f" suffix, it can be just dropped in Python as it // uses doubles for everything anyhow. for (char * p = end; *p != '\0'; ++p) { switch (*p) { case 'f': case 'F': break; default: return NIL; } } // Avoid unnecessary string allocation in the common case when we don't // need to remove any suffix. return *end == '\0' ? Copy(v) : NewStringWithSize(s, (int)(end - s)); } return NIL; } /* ------------------------------------------------------------ * convertValue() * * Check if string v can be a Python value literal or a * constant. Return an equivalent Python representation, * or NIL if it isn't, or we are unsure. * ------------------------------------------------------------ */ String *convertValue(String *v, String *numval, String *stringval, SwigType *type) { if (stringval) { return NIL; // FIXME: This needs more careful testing. // return NewStringf("'%(escape)s'", stringval); } SwigType *resolved_type = SwigType_typedef_resolve_all(type); SwigType *unqualified_type = NIL; if (SwigType_isreference(resolved_type)) { SwigType *t = Copy(resolved_type); t = SwigType_del_reference(t); unqualified_type = SwigType_strip_qualifiers(t); Delete(t); } else { unqualified_type = SwigType_strip_qualifiers(resolved_type); } if (numval) { if (Equal(unqualified_type, "bool")) { Delete(resolved_type); Delete(unqualified_type); return NewString(*Char(numval) == '0' ? "False" : "True"); } String *result = convertIntegerValue(numval, unqualified_type); Delete(resolved_type); Delete(unqualified_type); return result; } String *result = convertDoubleValue(v); if (!result) { if (Strcmp(v, "NULL") == 0 || Strcmp(v, "nullptr") == 0) result = SwigType_ispointer(unqualified_type) ? NewString("None") : NewString("0"); // This could also be an enum type, default value of which could be // representable in Python if it doesn't include any scope (which could, // but currently is not, translated). else if (!Strchr(v, ':')) { Node *lookup = Swig_symbol_clookup(v, 0); if (lookup) { if (Cmp(Getattr(lookup, "nodeType"), "enumitem") == 0) result = Copy(Getattr(lookup, "sym:name")); } } } Delete(resolved_type); Delete(unqualified_type); return result; } /* ------------------------------------------------------------ * is_representable_as_pyargs() * * Check if the function parameters default argument values * can be represented in Python. * * If this method returns false, the parameters will be translated * to a generic "*args" which allows us to deal with default values * at C++ code level where they can always be handled. * ------------------------------------------------------------ */ bool is_representable_as_pyargs(Node *n) { ParmList *plist = CopyParmList(Getattr(n, "parms")); Swig_typemap_attach_parms("default", plist, NULL); Parm *p; Parm *pnext; for (p = plist; p; p = pnext) { pnext = nextSibling(p); String *tm = Getattr(p, "tmap:in"); if (tm) { Parm *in_next = Getattr(p, "tmap:in:next"); if (in_next) pnext = in_next; if (checkAttribute(p, "tmap:in:numinputs", "0")) { continue; } } // "default" typemap can contain arbitrary C++ code, so while it could, in // principle, be possible to examine it and check if it's just something // simple of the form "$1 = expression" and then use convertValue() to // check if expression can be used in Python, but for now we just // pessimistically give up and prefer to handle this at C++ level only. if (Getattr(p, "tmap:default")) return false; String *value = Getattr(p, "value"); if (value) { String *convertedValue = convertValue(value, Getattr(p, "numval"), Getattr(p, "stringval"), Getattr(p, "type")); if (!convertedValue) return false; Delete(convertedValue); } } return true; } /* ------------------------------------------------------------ * is_real_overloaded() * * Check if the function is overloaded, but not just have some * siblings generated due to the original function having * default arguments. * ------------------------------------------------------------ */ bool is_real_overloaded(Node *n) { Node *h = Getattr(n, "sym:overloaded"); Node *i; if (!h) return false; i = Getattr(h, "sym:nextSibling"); while (i) { Node *nn = Getattr(i, "defaultargs"); if (nn != h) { /* Check if overloaded function has defaultargs and * pointed to the first overloaded. */ return true; } i = Getattr(i, "sym:nextSibling"); } return false; } /* ------------------------------------------------------------ * make_pyParmList() * * Generate parameter list for Python functions or methods, * reuse make_autodocParmList() to do so. * ------------------------------------------------------------ */ String *make_pyParmList(Node *n, bool in_class, bool is_calling, int kw, bool has_self_for_count = false) { /* Get the original function for a defaultargs copy, * see default_arguments() in parser.y. */ Node *nn = Getattr(n, "defaultargs"); if (nn) n = nn; Parm *parms = Getattr(n, "parms"); int varargs = parms ? emit_isvarargs(parms) : 0; /* We prefer to explicitly list all parameters of the C function in the generated Python code as this makes the function more convenient to use, however in some cases we must replace the real parameters list with just the catch all "*args". This happens when: 1. The function is overloaded as Python doesn't support this. 2. We were explicitly asked to use the "compact" arguments form. 3. We were explicitly asked to use default args from C via the "python:cdefaultargs" feature. 4. One of the default argument values can't be represented in Python. 5. Varargs that haven't been forced to use a fixed number of arguments with %varargs. */ if (is_real_overloaded(n) || GetFlag(n, "feature:compactdefaultargs") || GetFlag(n, "feature:python:cdefaultargs") || !is_representable_as_pyargs(n) || varargs) { String *parms = NewString(""); if (in_class) Printf(parms, "self, "); Printf(parms, "*args"); if (kw) Printf(parms, ", **kwargs"); return parms; } bool funcanno = Equal(Getattr(n, "feature:python:annotations"), "c") ? true : false; String *params = NewString(""); String *_params = make_autodocParmList(n, false, ((in_class || has_self_for_count)? 2 : 1), is_calling, funcanno); if (in_class) { Printf(params, "self"); if (Len(_params) > 0) Printf(params, ", "); } Printv(params, _params, NULL); return params; } /* ------------------------------------------------------------ * have_pythonprepend() * * Check if there is a %pythonprepend directive and it has text * ------------------------------------------------------------ */ bool have_pythonprepend(Node *n) { String *str = Getattr(n, "feature:pythonprepend"); return (str && Len(str) > 0); } /* ------------------------------------------------------------ * pythonprepend() * * Get the %pythonprepend code, stripping off {} if necessary * ------------------------------------------------------------ */ String *pythonprepend(Node *n) { String *str = Getattr(n, "feature:pythonprepend"); char *t = Char(str); if (*t == '{') { Delitem(str, 0); Delitem(str, DOH_END); } return str; } /* ------------------------------------------------------------ * have_pythonappend() * * Check if there is a %pythonappend directive and it has text * ------------------------------------------------------------ */ bool have_pythonappend(Node *n) { String *str = Getattr(n, "feature:pythonappend"); if (!str) str = Getattr(n, "feature:addtofunc"); return (str && Len(str) > 0); } /* ------------------------------------------------------------ * pythonappend() * * Get the %pythonappend code, stripping off {} if necessary * ------------------------------------------------------------ */ String *pythonappend(Node *n) { String *str = Getattr(n, "feature:pythonappend"); if (!str) str = Getattr(n, "feature:addtofunc"); char *t = Char(str); if (*t == '{') { Delitem(str, 0); Delitem(str, DOH_END); } return str; } /* ------------------------------------------------------------ * have_addtofunc() * * Check if there is a %addtofunc directive and it has text * ------------------------------------------------------------ */ bool have_addtofunc(Node *n) { return have_pythonappend(n) || have_pythonprepend(n); } /* ------------------------------------------------------------ * returnTypeAnnotation() * * Helper function for constructing the function annotation * of the returning type, return a empty string for Python 2.x * ------------------------------------------------------------ */ String *returnTypeAnnotation(Node *n) { String *ret = 0; Parm *p = Getattr(n, "parms"); String *tm; /* Try to guess the returning type by argout typemap, * however the result may not accurate. */ while (p) { if ((tm = Getattr(p, "tmap:argout:match_type"))) { tm = SwigType_str(tm, 0); if (ret) Printv(ret, ", ", tm, NULL); else ret = tm; p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } /* If no argout typemap, then get the returning type from * the function prototype. */ if (!ret) { ret = Getattr(n, "type"); if (ret) ret = SwigType_str(ret, 0); } bool funcanno = Equal(Getattr(n, "feature:python:annotations"), "c") ? true : false; return (ret && funcanno) ? NewStringf(" -> \"%s\"", ret) : NewString(""); } /* ------------------------------------------------------------ * variableAnnotation() * * Helper function for constructing a variable annotation * ------------------------------------------------------------ */ String *variableAnnotation(Node *n) { String *type = Getattr(n, "type"); if (type) type = SwigType_str(type, 0); bool anno = Equal(Getattr(n, "feature:python:annotations"), "c") ? true : false; anno = GetFlag(n, "feature:python:annotations:novar") ? false : anno; String *annotation = (type && anno) ? NewStringf(": \"%s\"", type) : NewString(""); Delete(type); return annotation; } /* ------------------------------------------------------------ * emitFunctionShadowHelper() * * Refactoring some common code out of functionWrapper and * dispatchFunction that writes the proxy code for non-member * functions. * ------------------------------------------------------------ */ void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) { String *parms = make_pyParmList(n, false, false, kw); String *callParms = make_pyParmList(n, false, true, kw); // Callbacks need the C function in order to extract the pointer from the swig_ptr: string bool fast = (fastproxy && !have_addtofunc(n)) || Getattr(n, "feature:callback"); if (!fast || olddefs) { /* Make a wrapper function to insert the code into */ Printv(f_dest, "\n", "def ", name, "(", parms, ")", returnTypeAnnotation(n), ":\n", NIL); // When handling the last overloaded function in an overload set (and we're only called for the last one if the function is overloaded at all), we need to // output the docstring if any of the overloads has any documentation, not just this last one. if (Node *node_with_doc = find_overload_with_docstring(n)) Printv(f_dest, tab4, docstring(node_with_doc, AUTODOC_FUNC, tab4, true), "\n", NIL); if (have_pythonprepend(n)) Printv(f_dest, indent_pythoncode(pythonprepend(n), tab4, Getfile(n), Getline(n), "%pythonprepend or %feature(\"pythonprepend\")"), "\n", NIL); if (have_pythonappend(n)) { Printv(f_dest, tab4 "val = ", funcCall(name, callParms), "\n", NIL); Printv(f_dest, indent_pythoncode(pythonappend(n), tab4, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n", NIL); Printv(f_dest, tab4 "return val\n", NIL); } else { Printv(f_dest, tab4 "return ", funcCall(name, callParms), "\n", NIL); } } // Below may result in a 2nd definition of the method when -olddefs is used. The Python interpreter will use the second definition as it overwrites the first. if (fast) { /* If there is no addtofunc directive then just assign from the extension module (for speed up) */ Printv(f_dest, name, " = ", module, ".", name, "\n", NIL); } } /* ------------------------------------------------------------ * check_kwargs() * * check if using kwargs is allowed for this Node * ------------------------------------------------------------ */ int check_kwargs(Node *n) const { return (use_kw || GetFlag(n, "feature:kwargs")) && !GetFlag(n, "memberset") && !GetFlag(n, "memberget"); } /* ------------------------------------------------------------ * add_method() * ------------------------------------------------------------ */ void add_method(String *name, String *function, int kw, Node *n = 0, int funpack = 0, int num_required = -1, int num_arguments = -1) { String * meth_str = NewString(""); if (!kw) { if (funpack) { if (num_required == 0 && num_arguments == 0) { Printf(meth_str, "\t { \"%s\", %s, METH_NOARGS, ", name, function); } else if (num_required == 1 && num_arguments == 1) { Printf(meth_str, "\t { \"%s\", %s, METH_O, ", name, function); } else { Printf(meth_str, "\t { \"%s\", %s, METH_VARARGS, ", name, function); } } else { Printf(meth_str, "\t { \"%s\", %s, METH_VARARGS, ", name, function); } } else { // Cast via void(*)(void) to suppress GCC -Wcast-function-type warning. // Python should always call the function correctly, but the Python C API // requires us to store it in function pointer of a different type. Printf(meth_str, "\t { \"%s\", (PyCFunction)(void(*)(void))%s, METH_VARARGS|METH_KEYWORDS, ", name, function); } Append(methods, meth_str); if (fastproxy) { Append(methods_proxydocs, meth_str); } Delete(meth_str); if (!n) { Append(methods, "NULL"); if (fastproxy) { Append(methods_proxydocs, "NULL"); } } else if (Node *node_with_doc = find_overload_with_docstring(n)) { /* Use the low-level docstring here since this is the docstring that will be used for the C API */ String *ds = cdocstring(node_with_doc, Getattr(n, "memberfunction") ? AUTODOC_METHOD : AUTODOC_FUNC, true); Printf(methods, "\"%s\"", ds); if (fastproxy) { /* In the fastproxy case, we must also record the high-level docstring for use in the Python shadow API */ Delete(ds); ds = cdocstring(node_with_doc, Getattr(n, "memberfunction") ? AUTODOC_METHOD : AUTODOC_FUNC); Printf(methods_proxydocs, "\"%s\"", ds); } Delete(ds); } else if (Getattr(n, "feature:callback")) { Printf(methods, "\"swig_ptr: %s\"", Getattr(n, "feature:callback:name")); if (fastproxy) { Printf(methods_proxydocs, "\"swig_ptr: %s\"", Getattr(n, "feature:callback:name")); have_fast_proxy_static_member_method_callback = true; } } else { Append(methods, "NULL"); if (fastproxy) { Append(methods_proxydocs, "NULL"); } } Append(methods, "},\n"); if (fastproxy) { Append(methods_proxydocs, "},\n"); } } /* ------------------------------------------------------------ * dispatchFunction() * ------------------------------------------------------------ */ void dispatchFunction(Node *n, String *linkage, int funpack = 0, bool builtin_self = false, bool builtin_ctor = false, bool director_class = false, bool use_static_method = false) { /* Last node in overloaded chain */ bool add_self = builtin_self && (!builtin_ctor || director_class); int maxargs; bool check_emitted = false; String *tmp = NewString(""); String *dispatch; const char *dispatch_call = funpack ? "%s(self, argc, argv);" : (builtin_ctor ? "%s(self, args, NULL);" : "%s(self, args);"); String *dispatch_code = NewStringf("return %s", dispatch_call); if (castmode) { dispatch = Swig_overload_dispatch_cast(n, dispatch_code, &maxargs, &check_emitted); } else { String *fastdispatch_code; if (builtin_ctor) fastdispatch_code = NewStringf("int retval = %s\nif (retval == 0 || !SWIG_Python_TypeErrorOccurred(NULL)) return retval;\nSWIG_fail;", dispatch_call); else fastdispatch_code = NewStringf("PyObject *retobj = %s\nif (!SWIG_Python_TypeErrorOccurred(retobj)) return retobj;\nSWIG_fail;", dispatch_call); if (!CPlusPlus) { Insert(fastdispatch_code, 0, "{\n"); Append(fastdispatch_code, "\n}"); } dispatch = Swig_overload_dispatch(n, dispatch_code, &maxargs, &check_emitted, fastdispatch_code); Delete(fastdispatch_code); } /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *f = NewWrapper(); String *symname = Getattr(n, "sym:name"); String *wname = Swig_name_wrapper(symname); const char *builtin_kwargs = builtin_ctor ? ", PyObject *kwargs" : ""; Printv(f->def, linkage, builtin_ctor ? "int " : "PyObject *", wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL); if (builtin) { /* Avoid warning if the self parameter is not used. */ Append(f->code, "(void)self;\n"); } Wrapper_add_local(f, "argc", "Py_ssize_t argc"); Printf(tmp, "PyObject *argv[%d] = {0}", maxargs + 1); Wrapper_add_local(f, "argv", tmp); if (!fastunpack) { Wrapper_add_local(f, "ii", "Py_ssize_t ii"); if (builtin_ctor) Printf(f->code, "if (!SWIG_Python_CheckNoKeywords(kwargs, \"%s\")) SWIG_fail;\n", symname); if (maxargs - (add_self ? 1 : 0) > 0) { Append(f->code, "if (!PyTuple_Check(args)) SWIG_fail;\n"); Append(f->code, "argc = PyObject_Length(args);\n"); } else { Append(f->code, "argc = args ? PyObject_Length(args) : 0;\n"); } if (add_self) Append(f->code, "argv[0] = self;\n"); Printf(f->code, "for (ii = 0; (ii < %d) && (ii < argc); ii++) {\n", add_self ? maxargs - 1 : maxargs); Printf(f->code, "argv[ii%s] = PyTuple_GET_ITEM(args,ii);\n", add_self ? " + 1" : ""); Append(f->code, "}\n"); if (add_self) Append(f->code, "argc++;\n"); } else { if (builtin_ctor) Printf(f->code, "if (!SWIG_Python_CheckNoKeywords(kwargs, \"%s\")) SWIG_fail;\n", symname); Printf(f->code, "if (!(argc = SWIG_Python_UnpackTuple(args, \"%s\", 0, %d, argv%s))) SWIG_fail;\n", symname, maxargs, add_self ? "+1" : ""); if (add_self) Append(f->code, "argv[0] = self;\n"); else Append(f->code, "--argc;\n"); } Replaceall(dispatch, "$args", "self, args"); Printv(f->code, dispatch, "\n", NIL); if (GetFlag(n, "feature:python:maybecall")) { Append(f->code, "fail:\n"); Append(f->code, " SWIG_Py_INCREF(Py_NotImplemented);\n"); Append(f->code, " return Py_NotImplemented;\n"); } else { Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up String *protoTypes = NewString(""); do { String *fulldecl = Swig_name_decl(sibl); Printf(protoTypes, "\n\" %s\\n\"", fulldecl); Delete(fulldecl); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Append(f->code, "fail:\n"); Printf(f->code, " SWIG_Python_RaiseOrModifyTypeError(" "\"Wrong number or type of arguments for overloaded function '%s'.\\n\"" "\n\" Possible C/C++ prototypes are:\\n\"%s);\n", symname, protoTypes); Printf(f->code, "return %s;\n", builtin_ctor ? "-1" : "0"); Delete(protoTypes); } Printv(f->code, "}\n", NIL); Wrapper_print(f, f_wrappers); if (!builtin_self && (use_static_method || !builtin)) add_method(symname, wname, 0, Getattr(n, "sym:previousSibling") ? n : NULL); /* Create a shadow for this function (if enabled and not in a member function) */ if (!builtin && shadow && !(shadow & PYSHADOW_MEMBER) && use_static_method) { emitFunctionShadowHelper(n, in_class ? f_shadow_stubs : f_shadow, symname, 0); } DelWrapper(f); Delete(dispatch); Delete(dispatch_code); Delete(tmp); Delete(wname); } /* ------------------------------------------------------------ * functionWrapper() * ------------------------------------------------------------ */ /* A note about argument marshalling with built-in types. There are three distinct cases for member (non-static) methods: 1) An ordinary member function. In this case, the first param in the param list is 'this'. For builtin types, 'this' is taken from the first argument to the wrapper (usually called 'self); it's not extracted from the second argument (which is usually a tuple). 2) A constructor for a non-director class. In this case, the param list doesn't contain an entry for 'this', but the first ('self') argument to the wrapper *does* contain the newly-allocated, uninitialized object. 3) A constructor for a director class. In this case, the param list contains a 'self' param, which comes from the first argument to the wrapper function. */ const char *get_implicitconv_flag(Node *klass) { int conv = 0; if (klass && GetFlag(klass, "feature:implicitconv")) { conv = 1; } return conv ? "SWIG_POINTER_IMPLICIT_CONV" : "0"; } virtual int functionWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); Node *parent = Swig_methodclass(n); int director_method = 0; Parm *p; int i; char source[64]; Wrapper *f; String *self_parse; String *parse_args; String *arglist; String *get_pointers; String *cleanup; String *outarg; String *kwargs; String *tm; String *overname = 0; int num_required; int num_arguments; int num_fixed_arguments; int tuple_required; int tuple_arguments; int varargs = 0; int allow_kwargs = check_kwargs(n); String *nodeType = Getattr(n, "nodeType"); int constructor = (!Cmp(nodeType, "constructor")); int destructor = (!Cmp(nodeType, "destructor")); String *storage = Getattr(n, "storage"); int isfriend = Strstr(storage, "friend") != NULL; /* Only the first constructor is handled as init method. Others constructor can be emitted via %rename */ int handled_as_init = 0; if (!have_constructor && (constructor || Getattr(n, "handled_as_constructor")) && ((shadow & PYSHADOW_MEMBER))) { String *nname = Getattr(n, "sym:name"); String *sname = Getattr(getCurrentClass(), "sym:name"); String *cname = Swig_name_construct(NSPACE_TODO, sname); handled_as_init = (Strcmp(nname, sname) == 0) || (Strcmp(nname, cname) == 0); Delete(cname); } bool builtin_self = builtin && in_class && (constructor || (l && Getattr(l, "self"))); bool builtin_ctor = false; if (builtin_self && constructor) { String *class_mname = Getattr(getCurrentClass(), "sym:name"); String *mrename = Swig_name_construct(getNSpace(), class_mname); if (Cmp(iname, mrename)) builtin_self = false; else builtin_ctor = true; Delete(mrename); } bool director_class = (getCurrentClass() && Swig_directorclass(getCurrentClass())); bool add_self = builtin_self && (!builtin_ctor || director_class); bool builtin_getter = (builtin && GetFlag(n, "memberget")); bool builtin_setter = (builtin && GetFlag(n, "memberset") && !builtin_getter); char const *wrap_return = builtin_ctor ? "int " : "PyObject *"; String *linkage = NewString("SWIGINTERN "); String *wrapper_name = Swig_name_wrapper(iname); if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } else { if (!addSymbol(iname, n)) return SWIG_ERROR; } f = NewWrapper(); self_parse = NewString(""); parse_args = NewString(""); arglist = NewString(""); get_pointers = NewString(""); cleanup = NewString(""); outarg = NewString(""); kwargs = NewString(""); int allow_thread = threads_enable(n); Wrapper_add_local(f, "resultobj", "PyObject *resultobj = 0"); // Emit all of the local variables for holding arguments. emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); Setattr(n, "wrap:parms", l); /* Get number of required and total arguments */ tuple_arguments = num_arguments = emit_num_arguments(l); tuple_required = num_required = emit_num_required(l); if (add_self) { --tuple_arguments; --tuple_required; } num_fixed_arguments = tuple_required; // builtin handles/checks kwargs by default except in constructor wrappers so we need to explicitly handle them in the C constructor wrapper // The check below is for zero arguments. Sometimes (eg directors) self is the first argument for a method with zero arguments. if (((num_arguments == 0) && (num_required == 0)) || ((num_arguments == 1) && (num_required == 1) && Getattr(l, "self"))) if (!builtin_ctor) allow_kwargs = 0; varargs = emit_isvarargs(l); String *wname = Copy(wrapper_name); if (overname) { Append(wname, overname); } const char *builtin_kwargs = builtin_ctor ? ", PyObject *kwargs" : ""; if (!allow_kwargs || overname) { if (!varargs) { Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL); } else { Printv(f->def, linkage, wrap_return, wname, "__varargs__", "(PyObject *self, PyObject *args, PyObject *varargs", builtin_kwargs, ") {", NIL); } if (allow_kwargs) { Swig_warning(WARN_LANG_OVERLOAD_KEYWORD, input_file, line_number, "Can't use keyword arguments with overloaded functions (%s).\n", Swig_name_decl(n)); allow_kwargs = 0; } } else { if (varargs) { Swig_warning(WARN_LANG_VARARGS_KEYWORD, input_file, line_number, "Can't wrap varargs with keyword arguments enabled\n"); varargs = 0; } Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args, PyObject *kwargs) {", NIL); } if (!builtin) { /* Avoid warning if the self parameter is not used. */ Append(f->code, "(void)self;\n"); } if (!builtin || !in_class || tuple_arguments > 0 || builtin_ctor) { if (!allow_kwargs) { Append(parse_args, " if (!PyArg_ParseTuple(args, \""); } else { Append(parse_args, " if (!PyArg_ParseTupleAndKeywords(args, kwargs, \""); Append(arglist, ", kwnames"); } } bool over_varargs = emit_isvarargs_function(n); int funpack = fastunpack && !varargs && !over_varargs && !allow_kwargs; int noargs = funpack && (tuple_required == 0 && tuple_arguments == 0); int onearg = funpack && (tuple_required == 1 && tuple_arguments == 1); if (builtin && funpack && !overname && !builtin_ctor) { if (!(tuple_arguments > tuple_required || varargs)) { String *argattr = NewStringf("%d", tuple_arguments); Setattr(n, "python:argcount", argattr); Delete(argattr); } } /* Generate code for argument marshalling */ bool swig_obj_added = false; if (constructor && num_arguments == 1 && num_required == 1) { if (Cmp(storage, "explicit") == 0) { if (GetFlag(parent, "feature:implicitconv")) { String *desc = NewStringf("SWIGTYPE%s", SwigType_manglestr(Getattr(n, "type"))); Printf(f->code, "if (SWIG_CheckImplicit(%s)) SWIG_fail;\n", desc); Delete(desc); } } } if (builtin_ctor && checkAttribute(n, "access", "protected")) { String *tmp_none_comparison = Copy(none_comparison); Replaceall(tmp_none_comparison, "$arg", "self"); Printf(self_parse, "if (!(%s)) {\n", tmp_none_comparison); Printv(self_parse, " SWIG_SetErrorMsg(PyExc_RuntimeError, \"accessing abstract class or protected constructor\");\n SWIG_fail;\n}\n", NIL); Delete(tmp_none_comparison); } int use_parse = 0; Append(kwargs, "{"); for (i = 0, p = l; i < num_arguments; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); bool parse_from_tuple = (i > 0 || !add_self); if (SwigType_type(pt) == T_VARARGS) { parse_from_tuple = false; num_fixed_arguments -= atoi(Char(Getattr(p, "tmap:in:numinputs"))); } if (!parse_from_tuple) sprintf(source, "self"); else if (funpack) { if (!swig_obj_added && !overname) { sprintf(source, "PyObject *swig_obj[%d]", num_arguments); Wrapper_add_localv(f, "swig_obj", source, NIL); swig_obj_added = true; } sprintf(source, "swig_obj[%d]", add_self && !overname ? i - 1 : i); } else sprintf(source, "obj%d", builtin_ctor ? i + 1 : i); if (parse_from_tuple) { Printf(arglist, ", "); if (i == num_required) Putc('|', parse_args); /* Optional argument separator */ } /* Keyword argument handling */ if (allow_kwargs && parse_from_tuple) { String *name = makeParameterName(n, p, i + 1); Printf(kwargs, " (char *)\"%s\", ", name); Delete(name); } /* Look for an input typemap */ if ((tm = Getattr(p, "tmap:in"))) { String *parse = Getattr(p, "tmap:in:parse"); if (!parse) { if (builtin_self) { Replaceall(tm, "$self", "self"); } else if (funpack) { Replaceall(tm, "$self", "swig_obj[0]"); } else { Replaceall(tm, "$self", "obj0"); } Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); /* Save the location of the object */ if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } if (Getattr(p, "tmap:in:implicitconv")) { const char *convflag = "0"; if (!Getattr(p, "hidden")) { SwigType *ptype = Getattr(p, "type"); convflag = get_implicitconv_flag(classLookup(ptype)); } Replaceall(tm, "$implicitconv", convflag); Setattr(p, "implicitconv", convflag); } if (parse_from_tuple) Putc('O', parse_args); if (!funpack && parse_from_tuple) { Wrapper_add_localv(f, source, "PyObject *", source, "= 0", NIL); Printf(arglist, "&%s", source); } if (i >= num_required) Printv(get_pointers, "if (", source, ") {\n", NIL); Printv(get_pointers, tm, "\n", NIL); if (i >= num_required) Printv(get_pointers, "}\n", NIL); } else { use_parse = 1; Append(parse_args, parse); if (parse_from_tuple) Printf(arglist, "&%s", ln); } p = Getattr(p, "tmap:in:next"); continue; } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); break; } } /* finish argument marshalling */ Append(kwargs, " NULL }"); if (allow_kwargs) { Printv(f->locals, " char * kwnames[] = ", kwargs, ";\n", NIL); } if (use_parse || allow_kwargs) { Printf(parse_args, ":%s\"", iname); Printv(parse_args, arglist, ")) SWIG_fail;\n", NIL); funpack = 0; } else { Clear(parse_args); if (funpack) { Clear(f->def); if (overname) { if (noargs) { Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, Py_ssize_t nobjs, PyObject **SWIGUNUSEDPARM(swig_obj)) {", NIL); } else { Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, Py_ssize_t nobjs, PyObject **swig_obj) {", NIL); } Printf(parse_args, "if ((nobjs < %d) || (nobjs > %d)) SWIG_fail;\n", num_required, num_arguments); } else { int is_tp_call = Equal(Getattr(n, "feature:python:slot"), "tp_call"); Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL); if (builtin_ctor) Printf(parse_args, "if (!SWIG_Python_CheckNoKeywords(kwargs, \"%s\")) SWIG_fail;\n", iname); if (onearg && !builtin_ctor && !is_tp_call) { Printf(parse_args, "if (!args) SWIG_fail;\n"); Append(parse_args, "swig_obj[0] = args;\n"); } else if (!noargs) { Printf(parse_args, "if (!SWIG_Python_UnpackTuple(args, \"%s\", %d, %d, swig_obj)) SWIG_fail;\n", iname, num_fixed_arguments, tuple_arguments); } else if (noargs) { Printf(parse_args, "if (!SWIG_Python_UnpackTuple(args, \"%s\", %d, %d, 0)) SWIG_fail;\n", iname, num_fixed_arguments, tuple_arguments); } } } else { if (builtin_ctor) Printf(parse_args, "if (!SWIG_Python_CheckNoKeywords(kwargs, \"%s\")) SWIG_fail;\n", iname); if (builtin && in_class && tuple_arguments == 0) { Printf(parse_args, " if (args && PyTuple_Check(args) && PyTuple_GET_SIZE(args) > 0) SWIG_exception_fail(SWIG_TypeError, \"%s takes no arguments\");\n", iname); } else { Printf(parse_args, "if (!PyArg_UnpackTuple(args, \"%s\", %d, %d", iname, num_fixed_arguments, tuple_arguments); Printv(parse_args, arglist, ")) SWIG_fail;\n", NIL); } } } /* Now piece together the first part of the wrapper function */ Printv(f->code, self_parse, parse_args, get_pointers, NIL); /* Check for trailing varargs */ if (varargs) { if (p && (tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", "varargs"); Printv(f->code, tm, "\n", NIL); } } /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (p = l; p;) { if (!Getattr(p, "tmap:in:parse") && (tm = Getattr(p, "tmap:freearg"))) { if (Getattr(p, "tmap:freearg:implicitconv")) { const char *convflag = "0"; if (!Getattr(p, "hidden")) { SwigType *ptype = Getattr(p, "type"); convflag = get_implicitconv_flag(classLookup(ptype)); } if (strcmp(convflag, "0") == 0) { tm = 0; } } if (tm && (Len(tm) != 0)) { Printv(cleanup, tm, "\n", NIL); } p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } /* if the object is a director, and the method call originated from its * underlying python object, resolve the call by going up the c++ * inheritance chain. otherwise try to resolve the method in python. * without this check an infinite loop is set up between the director and * shadow class method calls. */ // NOTE: this code should only be inserted if this class is the // base class of a director class. however, in general we haven't // yet analyzed all classes derived from this one to see if they are // directors. furthermore, this class may be used as the base of // a director class defined in a completely different module at a // later time, so this test must be included whether or not directorbase // is true. we do skip this code if directors have not been enabled // at the command line to preserve source-level compatibility with // non-polymorphic swig. also, if this wrapper is for a smart-pointer // method, there is no need to perform the test since the calling object // (the smart-pointer) and the director object (the "pointee") are // distinct. director_method = is_member_director(n) && !is_smart_pointer() && !destructor; if (director_method) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Append(f->code, "director = SWIG_DIRECTOR_CAST(arg1);\n"); if (dirprot_mode() && !is_public(n)) { Printf(f->code, "if (!director || !(director->swig_get_inner(\"%s\"))) {\n", name); Printf(f->code, "SWIG_SetErrorMsg(PyExc_RuntimeError,\"accessing protected member %s\");\n", name); Append(f->code, "SWIG_fail;\n"); Append(f->code, "}\n"); } Wrapper_add_local(f, "upcall", "bool upcall = false"); if (funpack) { const char *self_parm = builtin_self ? "self" : "swig_obj[0]"; Printf(f->code, "upcall = (director && (director->swig_get_self()==%s));\n", self_parm); } else { const char *self_parm = builtin_self ? "self" : "obj0"; Printf(f->code, "upcall = (director && (director->swig_get_self()==%s));\n", self_parm); } } /* Emit the function call */ if (director_method) { Append(f->code, "try {\n"); } else { if (allow_thread) { String *preaction = NewString(""); thread_begin_allow(n, preaction); Setattr(n, "wrap:preaction", preaction); String *postaction = NewString(""); thread_end_allow(n, postaction); Setattr(n, "wrap:postaction", postaction); } } Setattr(n, "wrap:name", wname); Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); if (director_method) { Append(actioncode, "} catch (Swig::DirectorException&) {\n"); Append(actioncode, " SWIG_fail;\n"); Append(actioncode, "}\n"); } /* This part below still needs cleanup */ /* Return the function value */ tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode); if (tm) { if (builtin_self) { Replaceall(tm, "$self", "self"); } else if (funpack) { Replaceall(tm, "$self", "swig_obj[0]"); } else { Replaceall(tm, "$self", "obj0"); } Replaceall(tm, "$result", "resultobj"); if (builtin_ctor) { Replaceall(tm, "$owner", "SWIG_BUILTIN_INIT"); } else if (handled_as_init) { Replaceall(tm, "$owner", "SWIG_POINTER_NEW"); } else { if (GetFlag(n, "feature:new")) { Replaceall(tm, "$owner", "SWIG_POINTER_OWN"); } else { Replaceall(tm, "$owner", "0"); } } // Unwrap return values that are director classes so that the original Python object is returned instead. if (!constructor && Swig_director_can_unwrap(n)) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Printf(f->code, "director = SWIG_DIRECTOR_CAST(%s);\n", Swig_cresult_name()); Append(f->code, "if (director) {\n"); Append(f->code, " resultobj = director->swig_get_self();\n"); Append(f->code, " SWIG_Py_INCREF(resultobj);\n"); Append(f->code, "} else {\n"); Printf(f->code, "%s\n", tm); Append(f->code, "}\n"); } else { Printf(f->code, "%s\n", tm); } Delete(tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), name); } emit_return_variable(n, returntype, f); /* Output argument output code */ Printv(f->code, outarg, NIL); /* Output cleanup code */ int need_cleanup = Len(cleanup) != 0; if (need_cleanup) { Printv(f->code, cleanup, NIL); } /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } } /* See if there is any return cleanup code */ if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } if (director_method) { if ((tm = Swig_typemap_lookup("directorfree", n, Swig_cresult_name(), 0))) { Replaceall(tm, "$input", Swig_cresult_name()); Replaceall(tm, "$result", "resultobj"); Printf(f->code, "%s\n", tm); Delete(tm); } } if (builtin_ctor) Append(f->code, " return resultobj == Py_None ? -1 : 0;\n"); else Append(f->code, " return resultobj;\n"); /* Error handling code */ Append(f->code, "fail:\n"); if (need_cleanup) { Printv(f->code, cleanup, NIL); } if (builtin_ctor) { Printv(f->code, " return -1;\n", NIL); } else { if (GetFlag(n, "feature:python:maybecall")) { Append(f->code, " if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {\n"); Append(f->code, " return NULL;\n"); Append(f->code, " }\n"); Append(f->code, " PyErr_Clear();\n"); Append(f->code, " SWIG_Py_INCREF(Py_NotImplemented);\n"); Append(f->code, " return Py_NotImplemented;\n"); } else { Printv(f->code, " return NULL;\n", NIL); } } Append(f->code, "}\n"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", iname); Replaceall(f->code, "$result", "resultobj"); if (builtin_self) { Replaceall(f->code, "$self", "self"); } else if (funpack) { Replaceall(f->code, "$self", "swig_obj[0]"); } else { Replaceall(f->code, "$self", "obj0"); } /* Dump the function out */ Wrapper_print(f, f_wrappers); /* If varargs. Need to emit a varargs stub */ if (varargs) { DelWrapper(f); f = NewWrapper(); if (funpack) { // Note: funpack is currently always false for varargs Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, Py_ssize_t nobjs, PyObject **swig_obj) {", NIL); } else { Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL); } Wrapper_add_local(f, "resultobj", builtin_ctor ? "int resultobj" : "PyObject *resultobj"); Wrapper_add_local(f, "varargs", "PyObject *varargs"); Wrapper_add_local(f, "newargs", "PyObject *newargs"); if (funpack) { Wrapper_add_local(f, "i", "int i"); Printf(f->code, "newargs = PyTuple_New(%d);\n", num_fixed_arguments); Printf(f->code, "for (i = 0; i < %d; ++i) {\n", num_fixed_arguments); Printf(f->code, " PyTuple_SET_ITEM(newargs, i, swig_obj[i]);\n"); Printf(f->code, " SWIG_Py_XINCREF(swig_obj[i]);\n"); Printf(f->code, "}\n"); Printf(f->code, "varargs = PyTuple_New(nobjs > %d ? nobjs - %d : 0);\n", num_fixed_arguments, num_fixed_arguments); Printf(f->code, "for (i = 0; i < nobjs - %d; ++i) {\n", num_fixed_arguments); Printf(f->code, " PyTuple_SET_ITEM(newargs, i, swig_obj[i + %d]);\n", num_fixed_arguments); Printf(f->code, " SWIG_Py_XINCREF(swig_obj[i + %d]);\n", num_fixed_arguments); Printf(f->code, "}\n"); } else { Printf(f->code, "newargs = PyTuple_GetSlice(args, 0, %d);\n", num_fixed_arguments); Printf(f->code, "varargs = PyTuple_GetSlice(args, %d, PyTuple_Size(args));\n", num_fixed_arguments); } Printf(f->code, "resultobj = %s__varargs__(%s, newargs, varargs%s);\n", wname, builtin ? "self" : "NULL", strlen(builtin_kwargs) == 0 ? "" : ", kwargs"); Append(f->code, "SWIG_Py_XDECREF(newargs);\n"); Append(f->code, "SWIG_Py_XDECREF(varargs);\n"); Append(f->code, "return resultobj;\n"); Append(f->code, "}\n"); Wrapper_print(f, f_wrappers); } bool use_static_method = flat_static_method || !Swig_storage_isstatic_custom(n, "staticmemberfunctionHandler:storage"); /* Now register the function with the interpreter. */ if (!Getattr(n, "sym:overloaded")) { if (!builtin_self && (use_static_method || !builtin)) add_method(iname, wname, allow_kwargs, n, funpack, num_required, num_arguments); /* Create a shadow for this function (if enabled and not in a member function) */ if (!builtin && shadow && !(shadow & PYSHADOW_MEMBER) && use_static_method) { emitFunctionShadowHelper(n, in_class ? f_shadow_stubs : f_shadow, iname, allow_kwargs); } } else { if (!Getattr(n, "sym:nextSibling")) { dispatchFunction(n, linkage, funpack, builtin_self, builtin_ctor, director_class, use_static_method); } } // Put this in tp_init of the PyTypeObject if (builtin_ctor) { if ((director_method || !is_private(n)) && !Getattr(class_members, iname)) { Setattr(class_members, iname, n); if (!builtin_tp_init) builtin_tp_init = Swig_name_wrapper(iname); } } /* If this is a builtin type, create a PyGetSetDef entry for this member variable. */ if (builtin) { const char *memname = "__dict__"; Hash *h = Getattr(builtin_getset, memname); if (!h) { h = NewHash(); Setattr(builtin_getset, memname, h); Delete(h); } Setattr(h, "getter", "SwigPyObject_get___dict__"); if (!Getattr(h, "doc")) { Setattr(h, "doc", "dictionary for instance variables"); } } if (builtin_getter) { String *memname = Getattr(n, "membervariableHandler:sym:name"); if (!memname) memname = iname; Hash *h = Getattr(builtin_getset, memname); if (!h) { h = NewHash(); Setattr(builtin_getset, memname, h); Delete(h); } Setattr(h, "getter", wrapper_name); Delattr(n, "memberget"); if (!Getattr(h, "doc")) { Setattr(n, "doc:high:name", Getattr(n, "name")); String *ds = cdocstring(n, AUTODOC_VAR); Setattr(h, "doc", ds); Delete(ds); } } if (builtin_setter) { String *memname = Getattr(n, "membervariableHandler:sym:name"); if (!memname) memname = iname; Hash *h = Getattr(builtin_getset, memname); if (!h) { h = NewHash(); Setattr(builtin_getset, memname, h); Delete(h); } Setattr(h, "setter", wrapper_name); Delattr(n, "memberset"); if (!Getattr(h, "doc")) { Setattr(n, "doc:high:name", Getattr(n, "name")); String *ds = cdocstring(n, AUTODOC_VAR); Setattr(h, "doc", ds); Delete(ds); } } if (in_class && builtin) { /* Handle operator overloads for builtin types */ String *slot = Getattr(n, "feature:python:slot"); if (slot && !isfriend) { String *func_type = Getattr(n, "feature:python:slot:functype"); String *closure_decl = getClosure(func_type, wrapper_name, overname ? 0 : funpack); String *feature_name = NewStringf("feature:python:%s", slot); String *closure_name = 0; if (closure_decl) { closure_name = NewStringf("%s_%s_closure", wrapper_name, func_type); if (!GetFlag(builtin_closures, closure_name)) Printf(builtin_closures_code, "%s /* defines %s */\n\n", closure_decl, closure_name); SetFlag(builtin_closures, closure_name); Delete(closure_decl); } else { closure_name = Copy(wrapper_name); } if (func_type) { String *s = NewStringf("%s", closure_name); Delete(closure_name); closure_name = s; } Setattr(parent, feature_name, closure_name); Delete(feature_name); Delete(closure_name); } /* Handle comparison operators for builtin types */ String *compare = Getattr(n, "feature:python:compare"); if (compare) { Hash *richcompare = Getattr(parent, "python:richcompare"); assert(richcompare); Setattr(richcompare, compare, wrapper_name); } } Delete(self_parse); Delete(parse_args); Delete(linkage); Delete(arglist); Delete(get_pointers); Delete(cleanup); Delete(outarg); Delete(kwargs); Delete(wname); DelWrapper(f); Delete(wrapper_name); return SWIG_OK; } /* ------------------------------------------------------------ * variableWrapper() * ------------------------------------------------------------ */ virtual int variableWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); static int have_globals = 0; String *tm; Wrapper *getf, *setf; if (!addSymbol(iname, n)) return SWIG_ERROR; getf = NewWrapper(); setf = NewWrapper(); /* If this is our first call, add the globals variable to the Python dictionary. */ if (!have_globals) { Printf(f_init, "\t globals = SWIG_globals();\n"); Printf(f_init, "\t if (!globals) {\n"); Printf(f_init, " PyErr_SetString(PyExc_TypeError, \"Failure to create SWIG globals.\");\n"); Printf(f_init, "\t return -1;\n"); Printf(f_init, "\t }\n"); Printf(f_init, "\t PyDict_SetItemString(md, \"%s\", globals);\n", global_name); if (builtin) Printf(f_init, "\t SwigPyBuiltin_AddPublicSymbol(public_interface, \"%s\");\n", global_name); have_globals = 1; if (!builtin && shadow && !(shadow & PYSHADOW_MEMBER)) { Printf(f_shadow_stubs, "%s = %s.%s\n", global_name, module, global_name); } } int assignable = !is_immutable(n); if (!builtin && shadow && !assignable && !in_class) Printf(f_shadow_stubs, "%s = %s.%s\n", iname, global_name, iname); String *getname = Swig_name_get(NSPACE_TODO, iname); String *setname = Swig_name_set(NSPACE_TODO, iname); String *vargetname = NewStringf("Swig_var_%s", getname); String *varsetname = NewStringf("Swig_var_%s", setname); /* Create a function for setting the value of the variable */ if (assignable) { Setattr(n, "wrap:name", varsetname); if (builtin && in_class) { String *set_wrapper = Swig_name_wrapper(setname); Setattr(n, "pybuiltin:setter", set_wrapper); Delete(set_wrapper); } Printf(setf->def, "SWIGINTERN int %s(PyObject *_val) {", varsetname); if ((tm = Swig_typemap_lookup("varin", n, name, 0))) { Replaceall(tm, "$input", "_val"); if (Getattr(n, "tmap:varin:implicitconv")) { Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); } emit_action_code(n, setf->code, tm); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0)); } Printv(setf->code, " return 0;\n", NULL); Append(setf->code, "fail:\n"); Printv(setf->code, " return 1;\n", NULL); } else { /* Is a readonly variable. Issue an error */ if (CPlusPlus) { Printf(setf->def, "SWIGINTERN int %s(PyObject *) {", varsetname); } else { Printf(setf->def, "SWIGINTERN int %s(PyObject *_val SWIGUNUSED) {", varsetname); } Printv(setf->code, " SWIG_Error(SWIG_AttributeError,\"Variable ", iname, " is read-only.\");\n", " return 1;\n", NIL); } Append(setf->code, "}\n"); Wrapper_print(setf, f_wrappers); /* Create a function for getting the value of a variable */ Setattr(n, "wrap:name", vargetname); if (builtin && in_class) { String *get_wrapper = Swig_name_wrapper(getname); Setattr(n, "pybuiltin:getter", get_wrapper); Delete(get_wrapper); } int addfail = 0; Printf(getf->def, "SWIGINTERN PyObject *%s(void) {", vargetname); Wrapper_add_local(getf, "pyobj", "PyObject *pyobj = 0"); if (builtin) { Wrapper_add_local(getf, "self", "PyObject *self = 0"); Append(getf->code, " (void)self;\n"); } if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { Replaceall(tm, "$result", "pyobj"); addfail = emit_action_code(n, getf->code, tm); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); } Append(getf->code, " return pyobj;\n"); if (addfail) { Append(getf->code, "fail:\n"); Append(getf->code, " return NULL;\n"); } Append(getf->code, "}\n"); Wrapper_print(getf, f_wrappers); /* Now add this to the variable linking mechanism */ Printf(f_varlinks, "\t SWIG_addvarlink(globals, \"%s\", %s, %s);\n", iname, vargetname, varsetname); if (builtin && shadow && !assignable && !in_class) { Printf(f_varlinks, "\t PyDict_SetItemString(md, \"%s\", PyObject_GetAttrString(globals, \"%s\"));\n", iname, iname); Printf(f_varlinks, "\t SwigPyBuiltin_AddPublicSymbol(public_interface, \"%s\");\n", iname); } Delete(vargetname); Delete(varsetname); Delete(getname); Delete(setname); DelWrapper(setf); DelWrapper(getf); return SWIG_OK; } /* ------------------------------------------------------------ * constantWrapper() * ------------------------------------------------------------ */ /* Determine if the node requires the _swigconstant code to be generated */ bool needs_swigconstant(Node *n) { SwigType *type = Getattr(n, "type"); SwigType *qtype = SwigType_typedef_resolve_all(type); SwigType *uqtype = SwigType_strip_qualifiers(qtype); bool result = false; /* Note, that we need special handling for function pointers, as * SwigType_base(fptr) does not return the underlying pointer-to-function * type but the return-type of function. */ if (!SwigType_isfunction(uqtype) && !SwigType_isfunctionpointer(uqtype)) { SwigType *basetype = SwigType_base(uqtype); result = SwigType_isclass(basetype) != 0; Delete(basetype); } Delete(qtype); Delete(uqtype); return result; } virtual int constantWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *tm; int have_tm = 0; int have_builtin_symname = 0; if (!addSymbol(iname, n)) return SWIG_ERROR; /* Special hook for member pointer */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(iname); String *str = SwigType_str(type, wname); Printf(f_header, "static %s = %s;\n", str, value); Delete(str); value = wname; } if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) { Replaceall(tm, "$value", value); Printf(const_code, "%s,\n", tm); Delete(tm); have_tm = 1; } if (builtin && in_class && Getattr(n, "pybuiltin:symname")) { have_builtin_symname = 1; Swig_require("builtin_constantWrapper", n, "*sym:name", "pybuiltin:symname", NIL); Setattr(n, "sym:name", Getattr(n, "pybuiltin:symname")); } if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) { Replaceall(tm, "$value", value); if (needs_swigconstant(n) && !builtin && shadow && !(shadow & PYSHADOW_MEMBER) && (!in_class || !Getattr(n, "feature:python:callback"))) { // Generate `*_swigconstant()` method which registers the new constant. // // *_swigconstant methods are required for constants of class type. // Class types are registered in shadow file (see *_swigregister). The // instances of class must be created (registered) after the type is // registered, so we can't let SWIG_init() to register constants of // class type (the SWIG_init() is called before shadow classes are // defined and registered). Printf(f_wrappers, "SWIGINTERN PyObject *%s_swigconstant(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", iname); Printf(f_wrappers, tab2 "PyObject *module = NULL;\n"); Printf(f_wrappers, tab2 "PyObject *d;\n"); Printf(f_wrappers, tab2 "if (!SWIG_Python_UnpackTuple(args, \"swigconstant\", 1, 1, &module)) return NULL;\n"); Printf(f_wrappers, tab2 "d = PyModule_GetDict(module);\n"); Printf(f_wrappers, tab2 "if (!d) return NULL;\n"); Printf(f_wrappers, tab2 "%s\n", tm); Printf(f_wrappers, tab2 "return SWIG_Py_Void();\n"); Printf(f_wrappers, "}\n\n\n"); // Register the method in SwigMethods array String *cname = NewStringf("%s_swigconstant", iname); add_method(cname, cname, 0, 0, 1, 1, 1); Delete(cname); } else { Printf(f_init, "%s\n", tm); } Delete(tm); have_tm = 1; } if (have_builtin_symname) Swig_restore(n); if (!have_tm) { Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); return SWIG_NOWRAP; } if (!builtin && shadow && !(shadow & PYSHADOW_MEMBER)) { String *f_s; if (!in_class) { f_s = f_shadow; } else { f_s = Getattr(n, "feature:python:callback") ? NIL : f_shadow_stubs; } if (f_s) { if (needs_swigconstant(n)) { Printv(f_s, "\n",NIL); Printv(f_s, module, ".", iname, "_swigconstant(",module,")\n", NIL); } Printv(f_s, iname, " = ", module, ".", iname, "\n", NIL); if (have_docstring(n)) Printv(f_s, docstring(n, AUTODOC_CONST, tab4), "\n", NIL); } } return SWIG_OK; } /* ------------------------------------------------------------ * nativeWrapper() * ------------------------------------------------------------ */ virtual int nativeWrapper(Node *n) { String *name = Getattr(n, "sym:name"); String *wrapname = Getattr(n, "wrap:name"); if (!addSymbol(wrapname, n)) return SWIG_ERROR; add_method(name, wrapname, 0); if (!builtin && shadow) { Printv(f_shadow_stubs, name, " = ", module, ".", name, "\n", NIL); } return SWIG_OK; } /* ---------------------------------------------------------------------------- * BEGIN C++ Director Class modifications * ------------------------------------------------------------------------- */ /* C++/Python polymorphism demo code * * TODO * * Move some boilerplate code generation to Swig_...() functions. * */ /* --------------------------------------------------------------- * classDirectorMethod() * * Emit a virtual director method to pass a method call on to the * underlying Python object. * ** Moved down due to gcc-2.96 internal error ** * --------------------------------------------------------------- */ int classDirectorMethods(Node *n); int classDirectorMethod(Node *n, Node *parent, String *super); /* ------------------------------------------------------------ * classDirectorConstructor() * ------------------------------------------------------------ */ int classDirectorConstructor(Node *n) { Node *parent = Getattr(n, "parentNode"); String *sub = NewString(""); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *classname = NewString(""); Printf(classname, "SwigDirector_%s", supername); /* insert self parameter */ Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("PyObject"); SwigType_add_pointer(type); p = NewParm(type, NewString("self"), n); set_nextSibling(p, parms); parms = p; if (!Getattr(n, "defaultargs")) { /* constructor */ { Wrapper *w = NewWrapper(); String *call; String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, classname, parms, 0); call = Swig_csuperclass_call(0, basetype, superparms); Printf(w->def, "%s::%s: %s, Swig::Director(self) { \n", classname, target, call); Printf(w->def, " SWIG_DIRECTOR_RGTR((%s *)this, this); \n", basetype); Append(w->def, "}\n"); Delete(target); Wrapper_print(w, f_directors); Delete(call); DelWrapper(w); } /* constructor header */ { String *target = Swig_method_decl(0, decl, classname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(classname); Delete(supername); Delete(parms); return Language::classDirectorConstructor(n); } /* ------------------------------------------------------------ * classDirectorDefaultConstructor() * ------------------------------------------------------------ */ int classDirectorDefaultConstructor(Node *n) { String *classname = Swig_class_name(n); { Node *parent = Swig_methodclass(n); String *basetype = Getattr(parent, "classtype"); Wrapper *w = NewWrapper(); Printf(w->def, "SwigDirector_%s::SwigDirector_%s(PyObject *self) : Swig::Director(self) { \n", classname, classname); Printf(w->def, " SWIG_DIRECTOR_RGTR((%s *)this, this); \n", basetype); Append(w->def, "}\n"); Wrapper_print(w, f_directors); DelWrapper(w); } Printf(f_directors_h, " SwigDirector_%s(PyObject *self);\n", classname); Delete(classname); return Language::classDirectorDefaultConstructor(n); } /* ------------------------------------------------------------ * classDirectorInit() * ------------------------------------------------------------ */ int classDirectorInit(Node *n) { String *declaration = Swig_director_declaration(n); Printf(f_directors_h, "\n"); Printf(f_directors_h, "%s\n", declaration); Printf(f_directors_h, "public:\n"); Delete(declaration); return Language::classDirectorInit(n); } /* ------------------------------------------------------------ * classDirectorEnd() * ------------------------------------------------------------ */ int classDirectorEnd(Node *n) { String *classname = Swig_class_name(n); if (dirprot_mode()) { /* This implementation uses a std::map. It should be possible to rewrite it using a more elegant way, like copying the Java approach for the 'override' array. But for now, this seems to be the least intrusive way. */ Printf(f_directors_h, "\n"); Printf(f_directors_h, "/* Internal director utilities */\n"); Printf(f_directors_h, "public:\n"); Printf(f_directors_h, " bool swig_get_inner(const char *swig_protected_method_name) const {\n"); Printf(f_directors_h, " std::map::const_iterator iv = swig_inner.find(swig_protected_method_name);\n"); Printf(f_directors_h, " return (iv != swig_inner.end() ? iv->second : false);\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, " void swig_set_inner(const char *swig_protected_method_name, bool swig_val) const {\n"); Printf(f_directors_h, " swig_inner[swig_protected_method_name] = swig_val;\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, "private:\n"); Printf(f_directors_h, " mutable std::map swig_inner;\n"); } if (director_method_index) { Printf(f_directors_h, "\n"); Printf(f_directors_h, "#if defined(SWIG_PYTHON_DIRECTOR_VTABLE)\n"); Printf(f_directors_h, "/* VTable implementation */\n"); Printf(f_directors_h, " PyObject *swig_get_method(size_t method_index, const char *method_name) const {\n"); Printf(f_directors_h, " PyObject *method = vtable[method_index];\n"); Printf(f_directors_h, " if (!method) {\n"); Printf(f_directors_h, " swig::SwigVar_PyObject name = SWIG_Python_str_FromChar(method_name);\n"); Printf(f_directors_h, " method = PyObject_GetAttr(swig_get_self(), name);\n"); Printf(f_directors_h, " if (!method) {\n"); Printf(f_directors_h, " std::string msg = \"Method in class %s doesn't exist, undefined \";\n", classname); Printf(f_directors_h, " msg += method_name;\n"); Printf(f_directors_h, " Swig::DirectorMethodException::raise(msg.c_str());\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, " vtable[method_index] = method;\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, " return method;\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, "private:\n"); Printf(f_directors_h, " mutable swig::SwigVar_PyObject vtable[%d];\n", director_method_index); Printf(f_directors_h, "#endif\n\n"); } Printf(f_directors_h, "};\n\n"); return Language::classDirectorEnd(n); } /* ------------------------------------------------------------ * classDirectorDisown() * ------------------------------------------------------------ */ int classDirectorDisown(Node *n) { int result; int oldshadow = shadow; /* disable shadowing */ if (shadow) shadow = shadow | PYSHADOW_MEMBER; result = Language::classDirectorDisown(n); shadow = oldshadow; if (shadow) { if (builtin) { String *rname = SwigType_namestr(real_classname); Printf(builtin_methods, " { \"__disown__\", Swig::Director::swig_pyobj_disown< %s >, METH_NOARGS, \"\" },\n", rname); Delete(rname); } else { String *symname = Getattr(n, "sym:name"); String *mrename = Swig_name_disown(NSPACE_TODO, symname); //Getattr(n, "name")); Printv(f_shadow, tab4, "def __disown__(self):\n", NIL); Printv(f_shadow, tab8, "self.this.disown()\n", NIL); Printv(f_shadow, tab8, module, ".", mrename, "(self)\n", NIL); Printv(f_shadow, tab8, "return weakref.proxy(self)\n", NIL); Delete(mrename); } } return result; } /* ---------------------------------------------------------------------------- * END of C++ Director Class modifications * ------------------------------------------------------------------------- */ /* ------------------------------------------------------------ * classDeclaration() * ------------------------------------------------------------ */ virtual int classDeclaration(Node *n) { if (shadow && !Getattr(n, "feature:onlychildren")) { Node *mod = Getattr(n, "module"); if (mod) { String *modname = Getattr(mod, "name"); Node *options = Getattr(mod, "options"); String *pkg = options ? Getattr(options, "package") : 0; String *sym = Getattr(n, "sym:name"); String *importname = import_name_string(package, mainmodule, pkg, modname, sym); Setattr(n, "python:proxy", importname); Delete(importname); } } int result = Language::classDeclaration(n); return result; } /* ------------------------------------------------------------ * classHandler() * ------------------------------------------------------------ */ SwigType *add_explicit_scope(SwigType *s) { if (!Strstr(s, "::")) { return NewStringf("::%s", s); } return Copy(s); } void builtin_pre_decl(Node *n) { SwigType *name = Getattr(n, "name"); SwigType *sname = add_explicit_scope(name); String *rname = SwigType_namestr(sname); String *mname = SwigType_manglestr(sname); Printf(f_init, "\n/* type '%s' */\n", rname); Printf(f_init, " d = PyDict_New();\n"); Delete(sname); Delete(rname); Delete(mname); } void builtin_post_decl(File *f, Node *n) { SwigType *name = Getattr(n, "name"); SwigType *pname = Copy(name); SwigType_add_pointer(pname); String *symname = Getattr(n, "sym:name"); SwigType *sname = add_explicit_scope(name); String *rname = SwigType_namestr(sname); String *mname = SwigType_manglestr(sname); String *pmname = SwigType_manglestr(pname); String *templ = NewStringf("SwigPyBuiltin_%s", mname); int funpack = fastunpack; if (have_builtin_static_member_method_callback) { Printf(f_init, " SWIG_Python_FixMethods(SwigPyBuiltin_%s_methods, swig_const_table, swig_types, swig_type_initial);\n", mname); } Printv(f_init, " builtin_base_count = 0;\n", NIL); List *baselist = Getattr(n, "bases"); if (baselist) { int base_count = 0; for (Iterator b = First(baselist); b.item; b = Next(b)) { String *bname = Getattr(b.item, "name"); if (!bname || GetFlag(b.item, "feature:ignore")) continue; base_count++; String *base_name = Copy(bname); SwigType_add_pointer(base_name); String *base_mname = SwigType_manglestr(base_name); Printf(f_init, " builtin_basetype = SWIG_MangledTypeQuery(\"%s\");\n", base_mname); Printv(f_init, " if (builtin_basetype && builtin_basetype->clientdata && ((SwigPyClientData *) builtin_basetype->clientdata)->pytype) {\n", NIL); Printv(f_init, " builtin_bases[builtin_base_count++] = ((SwigPyClientData *) builtin_basetype->clientdata)->pytype;\n", NIL); Printv(f_init, " } else {\n", NIL); Printf(f_init, " PyErr_SetString(PyExc_TypeError, \"Could not create type '%s' as base '%s' has not been initialized.\\n\");\n", symname, bname); Printv(f_init, " return -1;\n", NIL); Printv(f_init, " }\n", NIL); Delete(base_name); Delete(base_mname); } if (base_count > max_bases) max_bases = base_count; } Printv(f_init, " builtin_bases[builtin_base_count] = NULL;\n", NIL); builtin_bases_needed = 1; // Check for non-public destructor, in which case tp_dealloc will issue // a warning and allow the memory to leak. Any class that doesn't explicitly // have a private/protected destructor has an implicit public destructor. static String *tp_dealloc_bad = NewString("SwigPyBuiltin_BadDealloc"); String *getset_name = NewStringf("%s_getset", templ); String *methods_name = NewStringf("%s_methods", templ); String *getset_def = NewString(""); Printf(getset_def, "SWIGINTERN PyGetSetDef %s[] = {\n", getset_name); // All objects have 'this' and 'thisown' attributes Printv(f_init, "PyDict_SetItemString(d, \"this\", this_descr);\n", NIL); Printv(f_init, "PyDict_SetItemString(d, \"thisown\", thisown_descr);\n", NIL); // Now, the rest of the attributes for (Iterator member_iter = First(builtin_getset); member_iter.item; member_iter = Next(member_iter)) { String *memname = member_iter.key; Hash *mgetset = member_iter.item; String *getter = Getattr(mgetset, "getter"); String *setter = Getattr(mgetset, "setter"); const char *getter_closure = getter ? funpack ? "SwigPyBuiltin_FunpackGetterClosure" : "SwigPyBuiltin_GetterClosure" : "0"; const char *setter_closure = setter ? funpack ? "SwigPyBuiltin_FunpackSetterClosure" : "SwigPyBuiltin_SetterClosure" : "0"; String *gspair = NewStringf("%s_%s_getset", symname, memname); Printf(f, "static SwigPyGetSet %s = { %s, %s };\n", gspair, getter ? getter : "0", setter ? setter : "0"); String *doc = Getattr(mgetset, "doc"); if (!doc) doc = NewStringf("%s.%s", name, memname); String *entry = NewStringf("{ (char *)\"%s\", %s, %s, (char *)\"%s\", &%s }", memname, getter_closure, setter_closure, doc, gspair); if (GetFlag(mgetset, "static")) { Printf(f, "static PyGetSetDef %s_def = %s;\n", gspair, entry); Printf(f_init, "static_getset = SwigPyStaticVar_new_getset(metatype, &%s_def);\n", gspair); Printf(f_init, "PyDict_SetItemString(d, static_getset->d_getset->name, (PyObject *)static_getset);\n"); Printf(f_init, "SWIG_Py_DECREF((PyObject *)static_getset);\n"); } else { Printf(getset_def, " %s,\n", entry); } Delete(gspair); Delete(entry); } Printv(f, getset_def, " { NULL, NULL, NULL, NULL, NULL } /* Sentinel */\n", "};\n\n", NIL); // Rich compare function Hash *richcompare = Getattr(n, "python:richcompare"); String *richcompare_func = NewStringf("%s_richcompare", templ); assert(richcompare); Printf(f, "SWIGINTERN PyObject *\n"); Printf(f, "%s(PyObject *self, PyObject *other, int op) {\n", richcompare_func); Printf(f, " PyObject *result = NULL;\n"); if (!funpack) { Printf(f, " PyObject *tuple = PyTuple_New(1);\n"); Printf(f, " assert(tuple);\n"); Printf(f, " PyTuple_SET_ITEM(tuple, 0, other);\n"); Printf(f, " SWIG_Py_XINCREF(other);\n"); } List *richcompare_list = SortedKeys(richcompare, 0); Iterator rich_iter = First(richcompare_list); if (rich_iter.item) { Printf(f, " switch (op) {\n"); for (; rich_iter.item; rich_iter = Next(rich_iter)) Printf(f, " case %s : result = %s(self, %s); break;\n", rich_iter.item, Getattr(richcompare, rich_iter.item), funpack ? "other" : "tuple"); Printv(f, " default : break;\n", NIL); Printf(f, " }\n"); } Delete(richcompare_list); Printv(f, " if (!result && !PyErr_Occurred()) {\n", NIL); Printv(f, " if (SwigPyObject_Check(self) && SwigPyObject_Check(other)) {\n", NIL); Printv(f, " result = SwigPyObject_richcompare(self, other, op);\n", NIL); Printv(f, " } else {\n", NIL); Printv(f, " result = Py_NotImplemented;\n", NIL); Printv(f, " SWIG_Py_INCREF(result);\n", NIL); Printv(f, " }\n", NIL); Printv(f, " }\n", NIL); if (!funpack) Printf(f, " SWIG_Py_DECREF(tuple);\n"); Printf(f, " return result;\n"); Printf(f, "}\n\n"); // Methods Printf(f, "SWIGINTERN PyMethodDef %s_methods[] = {\n", templ); Dump(builtin_methods, f); Printf(f, " { NULL, NULL, 0, NULL } /* Sentinel */\n};\n\n"); // No instance dict for nondynamic objects if (GetFlag(n, "feature:python:nondynamic")) Setattr(n, "feature:python:tp_setattro", "SWIG_Python_NonDynamicSetAttr"); Node *mod = Getattr(n, "module"); String *modname = mod ? Getattr(mod, "name") : 0; String *quoted_symname; if (package) { if (modname) quoted_symname = NewStringf("\"%s.%s.%s\"", package, modname, symname); else quoted_symname = NewStringf("\"%s.%s\"", package, symname); } else { if (modname) quoted_symname = NewStringf("\"%s.%s\"", modname, symname); else quoted_symname = NewStringf("\"%s\"", symname); } String *quoted_tp_doc_str = NewStringf("\"%s\"", getSlot(n, "feature:python:tp_doc")); String *tp_init = NewString(builtin_tp_init ? Char(builtin_tp_init) : Swig_directorclass(n) ? "0" : "SwigPyBuiltin_BadInit"); String *tp_flags = NewString("Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES"); String *tp_flags_py3 = NewString("Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE"); static String *tp_basicsize = NewStringf("sizeof(SwigPyObject)"); static String *tp_dictoffset_default = NewString("offsetof(SwigPyObject, swigdict)"); static String *tp_hash = NewString("SwigPyObject_hash"); static String *tp_new = NewString("PyType_GenericNew"); String *tp_as_number = NewStringf("&%s_type.as_number", templ); String *tp_as_sequence = NewStringf("&%s_type.as_sequence", templ); String *tp_as_mapping = NewStringf("&%s_type.as_mapping", templ); String *tp_as_buffer = NewStringf("&%s_type.as_buffer", templ); Printv(f, "#ifndef SWIG_HEAPTYPES\n", NIL); Printf(f, "static PyHeapTypeObject %s_type = {\n", templ); // PyTypeObject ht_type Printf(f, " {\n"); Printv(f, "#if PY_VERSION_HEX >= 0x03000000\n", NIL); Printv(f, " PyVarObject_HEAD_INIT(NULL, 0)\n", NIL); Printv(f, "#else\n", NIL); Printf(f, " PyObject_HEAD_INIT(NULL)\n"); printSlot(f, getSlot(), "ob_size"); Printv(f, "#endif\n", NIL); printSlot(f, quoted_symname, "tp_name"); printSlot(f, getSlot(n, "feature:python:tp_basicsize", tp_basicsize), "tp_basicsize"); printSlot(f, getSlot(n, "feature:python:tp_itemsize"), "tp_itemsize"); printSlot(f, getSlot(n, "feature:python:tp_dealloc", tp_dealloc_bad), "tp_dealloc", "destructor"); Printv(f, "#if PY_VERSION_HEX < 0x030800b4\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_print"), "tp_print", "printfunc"); Printv(f, "#else\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_vectorcall_offset"), "tp_vectorcall_offset", "Py_ssize_t"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_getattr"), "tp_getattr", "getattrfunc"); printSlot(f, getSlot(n, "feature:python:tp_setattr"), "tp_setattr", "setattrfunc"); Printv(f, "#if PY_VERSION_HEX >= 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_compare"), "tp_compare"); Printv(f, "#else\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_compare"), "tp_compare", "cmpfunc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_repr"), "tp_repr", "reprfunc"); printSlot(f, getSlot(n, "feature:python:tp_as_number", tp_as_number), "tp_as_number"); printSlot(f, getSlot(n, "feature:python:tp_as_sequence", tp_as_sequence), "tp_as_sequence"); printSlot(f, getSlot(n, "feature:python:tp_as_mapping", tp_as_mapping), "tp_as_mapping"); printSlot(f, getSlot(n, "feature:python:tp_hash", tp_hash), "tp_hash", "hashfunc"); printSlot(f, getSlot(n, "feature:python:tp_call"), "tp_call", "ternaryfunc"); printSlot(f, getSlot(n, "feature:python:tp_str"), "tp_str", "reprfunc"); printSlot(f, getSlot(n, "feature:python:tp_getattro"), "tp_getattro", "getattrofunc"); printSlot(f, getSlot(n, "feature:python:tp_setattro"), "tp_setattro", "setattrofunc"); printSlot(f, getSlot(n, "feature:python:tp_as_buffer", tp_as_buffer), "tp_as_buffer"); Printv(f, "#if PY_VERSION_HEX >= 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_flags", tp_flags_py3), "tp_flags"); Printv(f, "#else\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_flags", tp_flags), "tp_flags"); Printv(f, "#endif\n", NIL); if (have_docstring(n)) { String *ds = cdocstring(n, AUTODOC_CLASS); String *tp_doc = NewString(""); Printf(tp_doc, "\"%s\"", ds); Delete(ds); printSlot(f, tp_doc, "tp_doc"); Delete(tp_doc); } else { printSlot(f, quoted_tp_doc_str, "tp_doc"); } printSlot(f, getSlot(n, "feature:python:tp_traverse"), "tp_traverse", "traverseproc"); printSlot(f, getSlot(n, "feature:python:tp_clear"), "tp_clear", "inquiry"); printSlot(f, getSlot(n, "feature:python:tp_richcompare", richcompare_func), "tp_richcompare", "richcmpfunc"); printSlot(f, getSlot(n, "feature:python:tp_weaklistoffset"), "tp_weaklistoffset"); printSlot(f, getSlot(n, "feature:python:tp_iter"), "tp_iter", "getiterfunc"); printSlot(f, getSlot(n, "feature:python:tp_iternext"), "tp_iternext", "iternextfunc"); printSlot(f, getSlot(n, "feature:python:tp_methods", methods_name), "tp_methods"); printSlot(f, getSlot(n, "feature:python:tp_members"), "tp_members"); printSlot(f, getSlot(n, "feature:python:tp_getset", getset_name), "tp_getset"); printSlot(f, getSlot(n, "feature:python:tp_base"), "tp_base"); printSlot(f, getSlot(n, "feature:python:tp_dict"), "tp_dict"); printSlot(f, getSlot(n, "feature:python:tp_descr_get"), "tp_descr_get", "descrgetfunc"); printSlot(f, getSlot(n, "feature:python:tp_descr_set"), "tp_descr_set", "descrsetfunc"); printSlot(f, getSlot(n, "feature:python:tp_dictoffset", tp_dictoffset_default), "tp_dictoffset", "Py_ssize_t"); printSlot(f, getSlot(n, "feature:python:tp_init", tp_init), "tp_init", "initproc"); printSlot(f, getSlot(n, "feature:python:tp_alloc"), "tp_alloc", "allocfunc"); printSlot(f, getSlot(), "tp_new", "newfunc"); printSlot(f, getSlot(n, "feature:python:tp_free"), "tp_free", "freefunc"); printSlot(f, getSlot(n, "feature:python:tp_is_gc"), "tp_is_gc", "inquiry"); printSlot(f, getSlot(n, "feature:python:tp_bases"), "tp_bases", "PyObject *"); printSlot(f, getSlot(n, "feature:python:tp_mro"), "tp_mro", "PyObject *"); printSlot(f, getSlot(n, "feature:python:tp_cache"), "tp_cache", "PyObject *"); printSlot(f, getSlot(n, "feature:python:tp_subclasses"), "tp_subclasses", "PyObject *"); printSlot(f, getSlot(n, "feature:python:tp_weaklist"), "tp_weaklist", "PyObject *"); printSlot(f, getSlot(n, "feature:python:tp_del"), "tp_del", "destructor"); printSlot(f, getSlot(n, "feature:python:tp_version_tag"), "tp_version_tag", "int"); Printv(f, "#if PY_VERSION_HEX >= 0x03040000\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_finalize"), "tp_finalize", "destructor"); Printv(f, "#endif\n", NIL); Printv(f, "#if PY_VERSION_HEX >= 0x03080000\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_vectorcall"), "tp_vectorcall", "vectorcallfunc"); Printv(f, "#endif\n", NIL); Printv(f, "#if (PY_VERSION_HEX >= 0x03080000) && (PY_VERSION_HEX < 0x03090000)\n", NIL); printSlot(f, getSlot(), "tp_print"); Printv(f, "#endif\n", NIL); Printv(f, "#if PY_VERSION_HEX >= 0x030c0000\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_watched"), "tp_watched", "unsigned char"); Printv(f, "#endif\n", NIL); Printv(f, "#if PY_VERSION_HEX >= 0x030d00a4\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_versions_used"), "tp_versions_used", "uint16_t"); Printv(f, "#endif\n", NIL); Printv(f, "#ifdef COUNT_ALLOCS\n", NIL); printSlot(f, getSlot(n, "feature:python:tp_allocs"), "tp_allocs", "Py_ssize_t"); printSlot(f, getSlot(n, "feature:python:tp_frees"), "tp_frees", "Py_ssize_t"); printSlot(f, getSlot(n, "feature:python:tp_maxalloc"), "tp_maxalloc", "Py_ssize_t"); printSlot(f, getSlot(n, "feature:python:tp_prev"), "tp_prev"); printSlot(f, getSlot(n, "feature:python:tp_next"), "tp_next"); Printv(f, "#endif\n", NIL); Printf(f, " },\n"); // PyAsyncMethods as_async Printv(f, "#if PY_VERSION_HEX >= 0x03050000\n", NIL); Printf(f, " {\n"); printSlot(f, getSlot(n, "feature:python:am_await"), "am_await", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:am_aiter"), "am_aiter", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:am_anext"), "am_anext", "unaryfunc"); Printv(f, "# if PY_VERSION_HEX >= 0x030a0000\n", NIL); printSlot(f, getSlot(n, "feature:python:am_send"), "am_send", "sendfunc"); Printv(f, "# endif\n", NIL); Printf(f, " },\n"); Printv(f, "#endif\n", NIL); // PyNumberMethods as_number Printf(f, " {\n"); printSlot(f, getSlot(n, "feature:python:nb_add"), "nb_add", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_subtract"), "nb_subtract", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_multiply"), "nb_multiply", "binaryfunc"); Printv(f, "#if PY_VERSION_HEX < 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_divide"), "nb_divide", "binaryfunc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_remainder"), "nb_remainder", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_divmod"), "nb_divmod", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_power"), "nb_power", "ternaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_negative"), "nb_negative", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_positive"), "nb_positive", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_absolute"), "nb_absolute", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_nonzero"), "nb_nonzero", "inquiry"); printSlot(f, getSlot(n, "feature:python:nb_invert"), "nb_invert", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_lshift"), "nb_lshift", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_rshift"), "nb_rshift", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_and"), "nb_and", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_xor"), "nb_xor", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_or"), "nb_or", "binaryfunc"); Printv(f, "#if PY_VERSION_HEX < 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_coerce"), "nb_coerce", "coercion"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_int"), "nb_int", "unaryfunc"); Printv(f, "#if PY_VERSION_HEX >= 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_reserved"), "nb_reserved", "void *"); Printv(f, "#else\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_long"), "nb_long", "unaryfunc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_float"), "nb_float", "unaryfunc"); Printv(f, "#if PY_VERSION_HEX < 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_oct"), "nb_oct", "unaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_hex"), "nb_hex", "unaryfunc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_inplace_add"), "nb_inplace_add", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_subtract"), "nb_inplace_subtract", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_multiply"), "nb_inplace_multiply", "binaryfunc"); Printv(f, "#if PY_VERSION_HEX < 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_inplace_divide"), "nb_inplace_divide", "binaryfunc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_inplace_remainder"), "nb_inplace_remainder", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_power"), "nb_inplace_power", "ternaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_lshift"), "nb_inplace_lshift", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_rshift"), "nb_inplace_rshift", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_and"), "nb_inplace_and", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_xor"), "nb_inplace_xor", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_or"), "nb_inplace_or", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_floor_divide"), "nb_floor_divide", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_divide"), "nb_true_divide", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_floor_divide"), "nb_inplace_floor_divide", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_divide"), "nb_inplace_true_divide", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_index"), "nb_index", "unaryfunc"); Printv(f, "#if PY_VERSION_HEX >= 0x03050000\n", NIL); printSlot(f, getSlot(n, "feature:python:nb_matrix_multiply"), "nb_matrix_multiply", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:nb_inplace_matrix_multiply"), "nb_inplace_matrix_multiply", "binaryfunc"); Printv(f, "#endif\n", NIL); Printf(f, " },\n"); // PyMappingMethods as_mapping; Printf(f, " {\n"); printSlot(f, getSlot(n, "feature:python:mp_length"), "mp_length", "lenfunc"); printSlot(f, getSlot(n, "feature:python:mp_subscript"), "mp_subscript", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:mp_ass_subscript"), "mp_ass_subscript", "objobjargproc"); Printf(f, " },\n"); // PySequenceMethods as_sequence; Printf(f, " {\n"); printSlot(f, getSlot(n, "feature:python:sq_length"), "sq_length", "lenfunc"); printSlot(f, getSlot(n, "feature:python:sq_concat"), "sq_concat", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:sq_repeat"), "sq_repeat", "ssizeargfunc"); printSlot(f, getSlot(n, "feature:python:sq_item"), "sq_item", "ssizeargfunc"); Printv(f, "#if PY_VERSION_HEX >= 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:was_sq_slice"), "was_sq_slice", "void *"); Printv(f, "#else\n", NIL); printSlot(f, getSlot(n, "feature:python:sq_slice"), "sq_slice", "ssizessizeargfunc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:sq_ass_item"), "sq_ass_item", "ssizeobjargproc"); Printv(f, "#if PY_VERSION_HEX >= 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:was_sq_ass_slice"), "was_sq_ass_slice", "void *"); Printv(f, "#else\n", NIL); printSlot(f, getSlot(n, "feature:python:sq_ass_slice"), "sq_ass_slice", "ssizessizeobjargproc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:sq_contains"), "sq_contains", "objobjproc"); printSlot(f, getSlot(n, "feature:python:sq_inplace_concat"), "sq_inplace_concat", "binaryfunc"); printSlot(f, getSlot(n, "feature:python:sq_inplace_repeat"), "sq_inplace_repeat", "ssizeargfunc"); Printf(f, " },\n"); // PyBufferProcs as_buffer; Printf(f, " {\n"); Printv(f, "#if PY_VERSION_HEX < 0x03000000\n", NIL); printSlot(f, getSlot(n, "feature:python:bf_getreadbuffer"), "bf_getreadbuffer", "readbufferproc"); printSlot(f, getSlot(n, "feature:python:bf_getwritebuffer"), "bf_getwritebuffer", "writebufferproc"); printSlot(f, getSlot(n, "feature:python:bf_getsegcount"), "bf_getsegcount", "segcountproc"); printSlot(f, getSlot(n, "feature:python:bf_getcharbuffer"), "bf_getcharbuffer", "charbufferproc"); Printv(f, "#endif\n", NIL); printSlot(f, getSlot(n, "feature:python:bf_getbuffer"), "bf_getbuffer", "getbufferproc"); printSlot(f, getSlot(n, "feature:python:bf_releasebuffer"), "bf_releasebuffer", "releasebufferproc"); Printf(f, " },\n"); // PyObject *ht_name, *ht_slots, *ht_qualname; printSlot(f, getSlot(n, "feature:python:ht_name"), "ht_name", "PyObject *"); printSlot(f, getSlot(n, "feature:python:ht_slots"), "ht_slots", "PyObject *"); Printv(f, "#if PY_VERSION_HEX >= 0x03030000\n", NIL); printSlot(f, getSlot(n, "feature:python:ht_qualname"), "ht_qualname", "PyObject *"); // struct _dictkeysobject *ht_cached_keys; printSlot(f, getSlot(n, "feature:python:ht_cached_keys"), "ht_cached_keys"); Printv(f, "#endif\n", NIL); // PyObject *ht_module; Printv(f, "#if PY_VERSION_HEX >= 0x03090000\n", NIL); printSlot(f, getSlot(n, "feature:python:ht_module"), "ht_module", "PyObject *"); Printv(f, "#endif\n", NIL); // char *_ht_tpname; Printv(f, "#if PY_VERSION_HEX >= 0x030b0000\n", NIL); printSlot(f, getSlot(n, "feature:python:_ht_tpname"), "_ht_tpname", "char *"); // void *ht_token; Printv(f, "#if PY_VERSION_HEX >= 0x030e0000\n", NIL); printSlot(f, getSlot(n, "feature:python:ht_token"), "ht_token", "void *"); Printv(f, "#endif\n", NIL); // struct _specialization_cache _spec_cache; Printf(f, " {\n"); printSlot(f, getSlot(n, "feature:python:getitem"), "getitem", "PyObject *"); Printv(f, "#if PY_VERSION_HEX >= 0x030c0000\n", NIL); printSlot(f, getSlot(n, "feature:python:getitem_version"), "getitem_version", "uint32_t"); Printv(f, "#endif\n", NIL); Printv(f, "#if PY_VERSION_HEX >= 0x030d0000\n", NIL); printSlot(f, getSlot(n, "feature:python:init"), "init", "PyObject *"); Printv(f, "#endif\n", NIL); Printf(f, " }\n"); Printv(f, "#endif\n", NIL); Printf(f, "};\n\n"); Printf(f, "static PyTypeObject *%s_type_create(PyTypeObject *type, PyTypeObject **bases, PyObject *dict) {\n", templ); Printv(f, " PyObject *tuple_bases;\n", NIL); Printf(f, " PyTypeObject *pytype = (PyTypeObject *)&%s_type;\n", templ); Printf(f, " pytype->tp_dict = dict;\n"); Printv(f, " SwigPyBuiltin_SetMetaType(pytype, type);\n", NIL); Printf(f, " pytype->tp_new = %s;\n", getSlot(n, "feature:python:tp_new", tp_new)); Printv(f, " tuple_bases = SwigPyBuiltin_InitBases(bases);\n", NIL); Printv(f, " pytype->tp_base = bases[0];\n", NIL); Printv(f, " SWIG_Py_INCREF(pytype->tp_base);\n", NIL); Printv(f, " pytype->tp_bases = tuple_bases;\n", NIL); Printv(f, " if (PyType_Ready(pytype) < 0) {\n", NIL); Printv(f, " return NULL;\n", NIL); Printv(f, " }\n", NIL); Printf(f, " return pytype;\n"); Printf(f, "}\n\n"); Printv(f, "#else\n", NIL); Printf(f, "static PyTypeObject *%s_type_create(PyTypeObject *type, PyTypeObject **bases, PyObject *dict) {\n", templ); // All heap types slots Printf(f, " PyType_Slot slots[] = {\n"); // tp slots printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_init", tp_init), "tp_init", "initproc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_dealloc", tp_dealloc_bad), "tp_dealloc", "destructor"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_alloc"), "tp_alloc", "allocfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_new"), "tp_new", "newfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_free"), "tp_free", "freefunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_is_gc"), "tp_is_gc", "inquiry"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_finalize"), "tp_finalize", "destructor"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_vectorcall"), "tp_vectorcall", "vectorcallfunc"); if (have_docstring(n)) { String *ds = cdocstring(n, AUTODOC_CLASS); String *tp_doc = NewString(""); Printf(tp_doc, "\"%s\"", ds); Delete(ds); printHeapTypesSlot(f, tp_doc, "tp_doc"); Delete(tp_doc); } else { printHeapTypesSlot(f, quoted_tp_doc_str, "tp_doc"); } printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_repr"), "tp_repr", "reprfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_str"), "tp_str", "reprfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_traverse"), "tp_traverse", "traverseproc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_clear"), "tp_clear", "inquiry"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_richcompare", richcompare_func), "tp_richcompare", "richcmpfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_methods", methods_name), "tp_methods"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_members"), "tp_members"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_getset", getset_name), "tp_getset"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_hash", tp_hash), "tp_hash", "hashfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_call"), "tp_call", "ternaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_getattro"), "tp_getattro", "getattrofunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_setattro"), "tp_setattro", "setattrofunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_descr_get"), "tp_descr_get", "descrgetfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_descr_set"), "tp_descr_set", "descrsetfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_iter"), "tp_iter", "getiterfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:tp_iternext"), "tp_iternext", "iternextfunc"); // effectively deprecated slots, see https://docs.python.org/3/c-api/typeobj.html: // tp_getattr // tp_setattr // tp_weaklistoffset // tp_dictoffset // tp_del // async method slots printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:am_await"), "am_await", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:am_aiter"), "am_aiter", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:am_anext"), "am_anext", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:am_send"), "am_send", "sendfunc"); // number slots printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_add"), "nb_add", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_subtract"), "nb_subtract", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_multiply"), "nb_multiply", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_remainder"), "nb_remainder", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_divmod"), "nb_divmod", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_power"), "nb_power", "ternaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_negative"), "nb_negative", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_positive"), "nb_positive", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_absolute"), "nb_absolute", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_nonzero"), "nb_bool", "inquiry"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_invert"), "nb_invert", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_lshift"), "nb_lshift", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_rshift"), "nb_rshift", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_and"), "nb_and", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_xor"), "nb_xor", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_or"), "nb_or", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_int"), "nb_int", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_float"), "nb_float", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_add"), "nb_inplace_add", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_subtract"), "nb_inplace_subtract", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_multiply"), "nb_inplace_multiply", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_remainder"), "nb_inplace_remainder", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_power"), "nb_inplace_power", "ternaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_lshift"), "nb_inplace_lshift", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_rshift"), "nb_inplace_rshift", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_and"), "nb_inplace_and", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_xor"), "nb_inplace_xor", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_or"), "nb_inplace_or", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_floor_divide"), "nb_floor_divide", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_divide"), "nb_true_divide", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_floor_divide"), "nb_inplace_floor_divide", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_divide"), "nb_inplace_true_divide", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_index"), "nb_index", "unaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_matrix_multiply"), "nb_matrix_multiply", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:nb_inplace_matrix_multiply"), "nb_inplace_matrix_multiply", "binaryfunc"); // mapping slots printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:mp_length"), "mp_length", "lenfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:mp_subscript"), "mp_subscript", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:mp_ass_subscript"), "mp_ass_subscript", "objobjargproc"); // sequence slots printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_length"), "sq_length", "lenfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_concat"), "sq_concat", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_repeat"), "sq_repeat", "ssizeargfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_item"), "sq_item", "ssizeargfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_ass_item"), "sq_ass_item", "ssizeobjargproc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_contains"), "sq_contains", "objobjproc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_inplace_concat"), "sq_inplace_concat", "binaryfunc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:sq_inplace_repeat"), "sq_inplace_repeat", "ssizeargfunc"); // buffer slots String *bf_getbuffer = Getattr(n, "feature:python:bf_getbuffer"); String *bf_releasebuffer = Getattr(n, "feature:python:bf_releasebuffer"); if (bf_getbuffer || bf_releasebuffer) { Printv(f, "#if !defined(Py_LIMITED_API) && PY_VERSION_HEX >= 0x03090000 || Py_LIMITED_API+0 >= 0x030b0000\n", NIL); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:bf_getbuffer"), "bf_getbuffer", "getbufferproc"); printHeapTypesSlot(f, getHeapTypesSlot(n, "feature:python:bf_releasebuffer"), "bf_releasebuffer", "releasebufferproc"); Printv(f, "#endif\n", NIL); } Printf(f, " { 0, NULL }\n"); Printf(f, " };\n"); Printf(f, " PyType_Spec spec = {\n"); Printf(f, " %s,\n", quoted_symname); Printf(f, " sizeof(SwigPyObject),\n"); Printf(f, " 0,\n"); Printf(f, " %s,\n", getHeapTypesSlot(n, "feature:python:tp_flags", tp_flags_py3), "tp_flags"); Printf(f, " slots\n"); Printf(f, " };\n"); Printv(f, " PyObject *tuple_bases = SwigPyBuiltin_InitBases(bases);\n", NIL); Printf(f, " PyTypeObject *pytype = (PyTypeObject *)PyType_FromSpecWithBases(&spec, tuple_bases);\n"); if (bf_getbuffer || bf_releasebuffer) { Printv(f, "#if !defined(Py_LIMITED_API) && PY_VERSION_HEX < 0x03090000\n", NIL); Printf(f, " if (pytype) {\n"); if (bf_getbuffer) Printf(f, " pytype->tp_as_buffer->bf_getbuffer = %s;\n", getSlot(n, "feature:python:bf_getbuffer")); if (bf_releasebuffer) Printf(f, " pytype->tp_as_buffer->bf_releasebuffer = %s;\n", getSlot(n, "feature:python:bf_releasebuffer")); Printf(f, " }\n"); Printv(f, "#endif\n", NIL); } Printf(f, " if (pytype) {\n"); Printf(f, " if (PyDict_Merge(pytype->tp_dict, dict, 1) == 0) {\n"); Printv(f, " SwigPyBuiltin_SetMetaType(pytype, type);\n", NIL); Printf(f, " PyType_Modified(pytype);\n"); Printf(f, " } else {\n"); Printf(f, " pytype = 0;\n"); Printf(f, " }\n"); Printf(f, " }\n"); Printf(f, " Py_DECREF(dict);\n"); Printf(f, " return pytype;\n"); Printv(f, "}\n", NULL); Printv(f, "#endif\n", NIL); String *clientdata = NewString(""); Printf(clientdata, "&%s_clientdata", templ); SwigType_remember_mangleddata(pmname, clientdata); SwigType *smart = Getattr(n, "smart"); if (smart) { SwigType *psmart = Copy(smart); SwigType_add_pointer(psmart); String *smart_pmname = SwigType_manglestr(psmart); SwigType_remember_mangleddata(smart_pmname, clientdata); Delete(smart_pmname); Delete(psmart); } Printf(f, "SWIGINTERN SwigPyClientData %s_clientdata = {0, 0, 0, 0, 0, 0, 0};\n\n", templ); Printf(f_init, " builtin_pytype = %s_type_create(metatype, builtin_bases, d);\n", templ); Printf(f_init, " if(!builtin_pytype) {\n", templ); Printv(f_init, " return -1;\n", NIL); Printv(f_init, " }\n", NIL); Printf(f_init, " SwigPyBuiltin_%s_clientdata.pytype = builtin_pytype;\n", mname); if (GetFlag(n, "feature:implicitconv")) { Printf(f_init, " SwigPyBuiltin_%s_clientdata.klass = (PyObject *)builtin_pytype;\n", mname); } Printv(f_init, " SWIG_Py_INCREF((PyObject *)builtin_pytype);\n", NIL); Printf(f_init, " if (PyModule_AddObject(m, \"%s\", (PyObject *)builtin_pytype) != 0) {\n", symname); Printf(f_init, " SWIG_Py_DECREF((PyObject *)builtin_pytype);\n"); Printv(f_init, " return -1;\n", NIL); Printf(f_init, " }\n", symname); Printf(f_init, " SwigPyBuiltin_AddPublicSymbol(public_interface, \"%s\");\n", symname); Printv(f_init, " d = md;\n", NIL); Delete(clientdata); Delete(sname); Delete(rname); Delete(mname); Delete(pname); Delete(pmname); Delete(templ); Delete(tp_flags); Delete(tp_flags_py3); Delete(tp_as_buffer); Delete(tp_as_mapping); Delete(tp_as_sequence); Delete(tp_as_number); Delete(quoted_symname); Delete(quoted_tp_doc_str); Delete(tp_init); Delete(richcompare_func); Delete(getset_name); Delete(methods_name); } virtual int classHandler(Node *n) { File *f_shadow_file = f_shadow; Node *base_node = NULL; if (shadow) { /* Create new strings for building up a wrapper function */ have_constructor = 0; have_repr = 0; have_builtin_static_member_method_callback = false; class_name = Getattr(n, "sym:name"); real_classname = Getattr(n, "name"); if (!addSymbol(class_name, n)) return SWIG_ERROR; if (builtin) { List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist) > 0) { Iterator b = First(baselist); base_node = b.item; } } shadow_indent = (String *) tab4; /* Handle inheritance */ String *base_class = NewString(""); List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator b; b = First(baselist); while (b.item) { String *bname = Getattr(b.item, "python:proxy"); bool ignore = GetFlag(b.item, "feature:ignore") ? true : false; if (!bname || ignore) { if (!bname && !ignore) { Swig_warning(WARN_TYPE_UNDEFINED_CLASS, Getfile(n), Getline(n), "Base class '%s' ignored - unknown module name for base. Either import the appropriate module interface file or specify the name of the module in the %%import directive.\n", SwigType_namestr(Getattr(b.item, "name"))); } b = Next(b); continue; } Printv(base_class, bname, NIL); b = Next(b); if (b.item) { Printv(base_class, ", ", NIL); } } } if (builtin) { Hash *base_richcompare = NULL; Hash *richcompare = NULL; if (base_node) base_richcompare = Getattr(base_node, "python:richcompare"); if (base_richcompare) richcompare = Copy(base_richcompare); else richcompare = NewHash(); Setattr(n, "python:richcompare", richcompare); } /* dealing with abstract base class */ String *abcs = Getattr(n, "feature:python:abc"); if (abcs) { if (Len(base_class) > 0) Printv(base_class, ", ", NIL); Printv(base_class, abcs, NIL); } if (builtin) { if (have_docstring(n)) { String *ds = cdocstring(n, AUTODOC_CLASS); Setattr(n, "feature:python:tp_doc", ds); Delete(ds); } else { SwigType *name = Getattr(n, "name"); SwigType *sname = add_explicit_scope(name); String *rname = SwigType_namestr(sname); Setattr(n, "feature:python:tp_doc", rname); Delete(sname); Delete(rname); } } else { if (GetFlag(n, "feature:python:nondynamic")) Printv(f_shadow, "@_swig_add_metaclass(_SwigNonDynamicMeta)\n", NIL); Printv(f_shadow, "class ", class_name, NIL); if (Len(base_class)) { Printf(f_shadow, "(%s)", base_class); } else { if (GetFlag(n, "feature:exceptionclass")) { Printf(f_shadow, "(Exception)"); } else { Printf(f_shadow, "(object"); /* Replace @_swig_add_metaclass above with below when support for python 2.7 is dropped if (GetFlag(n, "feature:python:nondynamic")) { Printf(f_shadow, ", metaclass=_SwigNonDynamicMeta"); } */ Printf(f_shadow, ")"); } } Printf(f_shadow, ":\n"); // write docstrings if requested if (have_docstring(n)) { String *str = docstring(n, AUTODOC_CLASS, tab4); if (str && Len(str)) Printv(f_shadow, tab4, str, "\n\n", NIL); } Printv(f_shadow, tab4, "thisown = property(lambda x: x.this.own(), ", "lambda x, v: x.this.own(v), doc=\"The membership flag\")\n", NIL); /* Add static attribute */ if (GetFlag(n, "feature:python:nondynamic")) { Printv(f_shadow_file, tab4, "__setattr__ = _swig_setattr_nondynamic_instance_variable(object.__setattr__)\n", NIL); } } } /* Emit all of the members */ in_class = 1; if (builtin) builtin_pre_decl(n); /* Override the shadow file so we can capture its methods */ f_shadow = NewString(""); // Set up type check for director class constructor Clear(none_comparison); if (builtin && Swig_directorclass(n)) { String *p_real_classname = Copy(real_classname); SwigType_add_pointer(p_real_classname); String *mangle = SwigType_manglestr(p_real_classname); String *descriptor = NewStringf("SWIGTYPE%s", mangle); Printv(none_comparison, "Py_TYPE(self) != ((SwigPyClientData *)(", descriptor, ")->clientdata)->pytype", NIL); Delete(descriptor); Delete(mangle); Delete(p_real_classname); } else { Printv(none_comparison, "$arg != Py_None", NIL); } Language::classHandler(n); in_class = 0; /* Complete the class */ if (shadow) { /* Generate a class registration function */ // Replace storing a pointer to underlying class with a smart pointer (intended for use with non-intrusive smart pointers) SwigType *smart = Getattr(n, "smart"); SwigType *ct = Copy(smart ? smart : real_classname); SwigType_add_pointer(ct); SwigType *realct = Copy(real_classname); SwigType_add_pointer(realct); SwigType_remember(realct); if (builtin) { Printv(f_wrappers, builtin_closures_code, NIL); Delete(builtin_closures_code); builtin_closures_code = NewString(""); Clear(builtin_closures); } else { Printv(f_wrappers, "SWIGINTERN PyObject *", class_name, "_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", NIL); Printv(f_wrappers, " PyObject *obj = NULL;\n", NIL); Printv(f_wrappers, " if (!SWIG_Python_UnpackTuple(args, \"swigregister\", 1, 1, &obj)) return NULL;\n", NIL); Printv(f_wrappers, " SWIG_TypeNewClientData(SWIGTYPE", SwigType_manglestr(ct), ", SWIG_NewClientData(obj));\n", " return SWIG_Py_Void();\n", "}\n\n", NIL); String *cname = NewStringf("%s_swigregister", class_name); add_method(cname, cname, 0, 0, 1, 1, 1); Delete(cname); } Delete(ct); Delete(realct); if (!have_constructor) { if (!builtin) Printv(f_shadow_file, "\n", tab4, "def __init__(self, *args, **kwargs):\n", tab8, "raise AttributeError(\"", "No constructor defined", (Getattr(n, "abstracts") ? " - class is abstract" : ""), "\")\n", NIL); } else if (!builtin) { Printv(f_wrappers, "SWIGINTERN PyObject *", class_name, "_swiginit(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", NIL); Printv(f_wrappers, " return SWIG_Python_InitShadowInstance(args);\n", "}\n\n", NIL); String *cname = NewStringf("%s_swiginit", class_name); add_method(cname, cname, 0); Delete(cname); } if (!have_repr && !builtin) { /* Supply a repr method for this class */ String *rname = SwigType_namestr(real_classname); Printv(f_shadow_file, tab4, "__repr__ = _swig_repr\n", NIL); Delete(rname); } if (builtin) builtin_post_decl(f_builtins, n); if (builtin_tp_init) { Delete(builtin_tp_init); builtin_tp_init = 0; } if (!builtin) { /* Now emit methods */ Printv(f_shadow_file, f_shadow, NIL); Printf(f_shadow_file, "\n"); Printf(f_shadow_file, "# Register %s in %s:\n", class_name, module); Printf(f_shadow_file, "%s.%s_swigregister(%s)\n", module, class_name, class_name); } shadow_indent = 0; if (Len(f_shadow_stubs) > 0) Printf(f_shadow_file, "%s\n", f_shadow_stubs); Clear(f_shadow_stubs); } if (builtin) { Clear(class_members); Clear(builtin_getset); Clear(builtin_methods); } /* Restore shadow file back to original version */ Delete(f_shadow); f_shadow = f_shadow_file; return SWIG_OK; } /* ------------------------------------------------------------ * functionHandler() - Mainly overloaded for callback handling * ------------------------------------------------------------ */ virtual int functionHandler(Node *n) { String *pcb = GetFlagAttr(n, "feature:python:callback"); if (pcb) { if (Strcmp(pcb, "1") == 0) { SetFlagAttr(n, "feature:callback", "%s_cb_ptr"); } else { SetFlagAttr(n, "feature:callback", pcb); } autodoc_l dlevel = autodoc_level(Getattr(n, "feature:autodoc")); if (dlevel != NO_AUTODOC && dlevel > TYPES_AUTODOC) { Setattr(n, "feature:autodoc", "1"); } } return Language::functionHandler(n); } /* ------------------------------------------------------------ * memberfunctionHandler() * ------------------------------------------------------------ */ virtual int memberfunctionHandler(Node *n) { String *symname = Getattr(n, "sym:name"); int oldshadow; if (builtin) Swig_save("builtin_memberfunc", n, "python:argcount", NIL); /* Create the default member function */ oldshadow = shadow; /* Disable shadowing when wrapping member functions */ if (shadow) shadow = shadow | PYSHADOW_MEMBER; Language::memberfunctionHandler(n); shadow = oldshadow; if (builtin && in_class) { // Can't use checkAttribute(n, "access", "public") because // "access" attr isn't set on %extend methods if (!checkAttribute(n, "access", "private") && strncmp(Char(symname), "operator ", 9) && !Getattr(class_members, symname)) { String *fullname = Swig_name_member(NSPACE_TODO, class_name, symname); String *wname = Swig_name_wrapper(fullname); Setattr(class_members, symname, n); int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2; String *ds = have_docstring(n) ? cdocstring(n, AUTODOC_METHOD) : NewString(""); if (check_kwargs(n)) { // Cast via void(*)(void) to suppress GCC -Wcast-function-type // warning. Python should always call the function correctly, but // the Python C API requires us to store it in function pointer of a // different type. Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, METH_VARARGS|METH_KEYWORDS, \"%s\" },\n", symname, wname, ds); } else if (argcount == 0) { Printf(builtin_methods, " { \"%s\", %s, METH_NOARGS, \"%s\" },\n", symname, wname, ds); } else if (argcount == 1) { Printf(builtin_methods, " { \"%s\", %s, METH_O, \"%s\" },\n", symname, wname, ds); } else { Printf(builtin_methods, " { \"%s\", %s, METH_VARARGS, \"%s\" },\n", symname, wname, ds); } Delete(fullname); Delete(wname); Delete(ds); } } if (builtin) Swig_restore(n); if (!Getattr(n, "sym:nextSibling")) { if (shadow && !builtin) { int fproxy = fastproxy; String *fullname = Swig_name_member(NSPACE_TODO, class_name, symname); if (Strcmp(symname, "__repr__") == 0) { have_repr = 1; } if (Getattr(n, "feature:shadow")) { String *pycode = indent_pythoncode(Getattr(n, "feature:shadow"), tab4, Getfile(n), Getline(n), "%feature(\"shadow\")"); String *pyaction = NewStringf("%s.%s", module, fullname); Replaceall(pycode, "$action", pyaction); Delete(pyaction); Printv(f_shadow, pycode, "\n", NIL); Delete(pycode); fproxy = 0; } else { int allow_kwargs = (check_kwargs(n) && !Getattr(n, "sym:overloaded")) ? 1 : 0; String *parms = make_pyParmList(n, true, false, allow_kwargs); String *callParms = make_pyParmList(n, true, true, allow_kwargs); if (!have_addtofunc(n)) { if (!fastproxy || olddefs) { Printv(f_shadow, "\n", tab4, "def ", symname, "(", parms, ")", returnTypeAnnotation(n), ":\n", NIL); if (Node *node_with_doc = find_overload_with_docstring(n)) Printv(f_shadow, tab8, docstring(node_with_doc, AUTODOC_METHOD, tab8), "\n", NIL); Printv(f_shadow, tab8, "return ", funcCall(fullname, callParms), "\n", NIL); } } else { Printv(f_shadow, "\n", tab4, "def ", symname, "(", parms, ")", returnTypeAnnotation(n), ":\n", NIL); if (Node *node_with_doc = find_overload_with_docstring(n)) Printv(f_shadow, tab8, docstring(node_with_doc, AUTODOC_METHOD, tab8), "\n", NIL); if (have_pythonprepend(n)) { fproxy = 0; Printv(f_shadow, indent_pythoncode(pythonprepend(n), tab8, Getfile(n), Getline(n), "%pythonprepend or %feature(\"pythonprepend\")"), "\n", NIL); } if (have_pythonappend(n)) { fproxy = 0; Printv(f_shadow, tab8, "val = ", funcCall(fullname, callParms), "\n", NIL); Printv(f_shadow, indent_pythoncode(pythonappend(n), tab8, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n", NIL); Printv(f_shadow, tab8, "return val\n\n", NIL); } else { Printv(f_shadow, tab8, "return ", funcCall(fullname, callParms), "\n\n", NIL); } } } if (fproxy) { Printf(f_shadow, tab4); Printf(f_shadow, "%s = _swig_new_instance_method(%s.%s)\n", symname, module, Swig_name_member(NSPACE_TODO, class_name, symname)); } Delete(fullname); } } return SWIG_OK; } /* ------------------------------------------------------------ * staticmemberfunctionHandler() * ------------------------------------------------------------ */ virtual int staticmemberfunctionHandler(Node *n) { String *symname = Getattr(n, "sym:name"); if (builtin && in_class) { Swig_save("builtin_memberconstantHandler", n, "pybuiltin:symname", NIL); Setattr(n, "pybuiltin:symname", symname); } Language::staticmemberfunctionHandler(n); if (builtin && in_class) { Swig_restore(n); } int kw = (check_kwargs(n) && !Getattr(n, "sym:overloaded")) ? 1 : 0; if (builtin && in_class) { if ((GetFlagAttr(n, "feature:extend") || checkAttribute(n, "access", "public")) && !Getattr(class_members, symname)) { String *fullname = Swig_name_member(NSPACE_TODO, class_name, symname); String *wname = Swig_name_wrapper(fullname); Setattr(class_members, symname, n); int funpack = fastunpack && !Getattr(n, "sym:overloaded"); String *pyflags = NewString("METH_STATIC|"); int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2; if (funpack && argcount == 0) Append(pyflags, "METH_NOARGS"); else if (funpack && argcount == 1) Append(pyflags, "METH_O"); else Append(pyflags, kw ? "METH_VARARGS|METH_KEYWORDS" : "METH_VARARGS"); // Cast via void(*)(void) to suppress GCC -Wcast-function-type warning. // Python should always call the function correctly, but the Python C // API requires us to store it in function pointer of a different type. if (have_docstring(n)) { String *ds = cdocstring(n, AUTODOC_STATICFUNC); Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, %s, \"%s\" },\n", symname, wname, pyflags, ds); Delete(ds); } else if (Getattr(n, "feature:callback")) { String *ds = NewStringf("swig_ptr: %s", Getattr(n, "feature:callback:name")); Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, %s, \"%s\" },\n", symname, wname, pyflags, ds); Delete(ds); have_builtin_static_member_method_callback = true; } else { Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, %s, \"\" },\n", symname, wname, pyflags); } Delete(fullname); Delete(wname); Delete(pyflags); } return SWIG_OK; } if (Getattr(n, "sym:nextSibling")) { return SWIG_OK; } if (shadow) { String *staticfunc_name = NewString(fastproxy ? "_swig_new_static_method" : "staticmethod"); bool fast = (fastproxy && !have_addtofunc(n)) || Getattr(n, "feature:callback"); if (!fast || olddefs) { String *parms = make_pyParmList(n, false, false, kw); String *callParms = make_pyParmList(n, false, true, kw); Printv(f_shadow, "\n", tab4, "@staticmethod", NIL); Printv(f_shadow, "\n", tab4, "def ", symname, "(", parms, ")", returnTypeAnnotation(n), ":\n", NIL); if (Node *node_with_doc = find_overload_with_docstring(n)) Printv(f_shadow, tab8, docstring(node_with_doc, AUTODOC_STATICFUNC, tab8), "\n", NIL); if (have_pythonprepend(n)) Printv(f_shadow, indent_pythoncode(pythonprepend(n), tab8, Getfile(n), Getline(n), "%pythonprepend or %feature(\"pythonprepend\")"), "\n", NIL); if (have_pythonappend(n)) { Printv(f_shadow, tab8, "val = ", funcCall(Swig_name_member(NSPACE_TODO, class_name, symname), callParms), "\n", NIL); Printv(f_shadow, indent_pythoncode(pythonappend(n), tab8, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n", NIL); Printv(f_shadow, tab8, "return val\n", NIL); } else { Printv(f_shadow, tab8, "return ", funcCall(Swig_name_member(NSPACE_TODO, class_name, symname), callParms), "\n", NIL); } } // Below may result in a 2nd definition of the method when -olddefs is used. The Python interpreter will use the second definition as it overwrites the first. if (fast) { Printv(f_shadow, tab4, symname, " = ", staticfunc_name, "(", module, ".", Swig_name_member(NSPACE_TODO, class_name, symname), ")\n", NIL); } Delete(staticfunc_name); } return SWIG_OK; } /* ------------------------------------------------------------ * constructorDeclaration() * ------------------------------------------------------------ */ virtual int constructorHandler(Node *n) { String *symname = Getattr(n, "sym:name"); int oldshadow = shadow; int use_director = Swig_directorclass(n); /* * If we're wrapping the constructor of a C++ director class, prepend a new parameter * to receive the scripting language object (e.g. 'self') * */ Swig_save("python:constructorHandler", n, "parms", NIL); if (use_director) { Parm *parms = Getattr(n, "parms"); Parm *self; String *name = NewString("self"); String *type = NewString("PyObject"); SwigType_add_pointer(type); self = NewParm(type, name, n); Delete(type); Delete(name); Setattr(self, "lname", "O"); if (parms) set_nextSibling(self, parms); Setattr(n, "parms", self); Setattr(n, "wrap:self", "1"); Setattr(n, "hidden", "1"); Delete(self); } if (shadow) shadow = shadow | PYSHADOW_MEMBER; Language::constructorHandler(n); shadow = oldshadow; Delattr(n, "wrap:self"); Swig_restore(n); if (!Getattr(n, "sym:nextSibling")) { if (shadow) { int allow_kwargs = (check_kwargs(n) && (!Getattr(n, "sym:overloaded"))) ? 1 : 0; int handled_as_init = 0; if (!have_constructor) { String *nname = Getattr(n, "sym:name"); String *sname = Getattr(getCurrentClass(), "sym:name"); String *cname = Swig_name_construct(NSPACE_TODO, sname); handled_as_init = (Strcmp(nname, sname) == 0) || (Strcmp(nname, cname) == 0); Delete(cname); } String *subfunc = Swig_name_construct(NSPACE_TODO, symname); if (!have_constructor && handled_as_init) { if (!builtin) { if (Getattr(n, "feature:shadow")) { String *pycode = indent_pythoncode(Getattr(n, "feature:shadow"), tab4, Getfile(n), Getline(n), "%feature(\"shadow\")"); String *pyaction = NewStringf("%s.%s", module, subfunc); Replaceall(pycode, "$action", pyaction); Delete(pyaction); Printv(f_shadow, pycode, "\n", NIL); Delete(pycode); } else { String *pass_self = NewString(""); Node *parent = Swig_methodclass(n); String *classname = Swig_class_name(parent); String *rclassname = Swig_class_name(getCurrentClass()); assert(rclassname); (void)rclassname; String *parms = make_pyParmList(n, true, false, allow_kwargs); /* Pass 'self' only if using director */ String *callParms = make_pyParmList(n, false, true, allow_kwargs, true); if (use_director) { Insert(callParms, 0, "_self, "); Printv(pass_self, tab8, NIL); Printf(pass_self, "if self.__class__ == %s:\n", classname); //Printv(pass_self, tab8, tab4, "args = (None,) + args\n", tab8, "else:\n", tab8, tab4, "args = (self,) + args\n", NIL); Printv(pass_self, tab8, tab4, "_self = None\n", tab8, "else:\n", tab8, tab4, "_self = self\n", NIL); } Printv(f_shadow, "\n", tab4, "def __init__(", parms, ")", returnTypeAnnotation(n), ":\n", NIL); if (Node *node_with_doc = find_overload_with_docstring(n)) Printv(f_shadow, tab8, docstring(node_with_doc, AUTODOC_CTOR, tab8), "\n", NIL); if (have_pythonprepend(n)) Printv(f_shadow, indent_pythoncode(pythonprepend(n), tab8, Getfile(n), Getline(n), "%pythonprepend or %feature(\"pythonprepend\")"), "\n", NIL); Printv(f_shadow, pass_self, NIL); Printv(f_shadow, tab8, module, ".", class_name, "_swiginit(self, ", funcCall(subfunc, callParms), ")\n", NIL); if (have_pythonappend(n)) Printv(f_shadow, indent_pythoncode(pythonappend(n), tab8, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n\n", NIL); Delete(pass_self); } have_constructor = 1; } } else { /* Hmmm. We seem to be creating a different constructor. We're just going to create a function for it. */ if (!builtin) { if (Getattr(n, "feature:shadow")) { String *pycode = indent_pythoncode(Getattr(n, "feature:shadow"), "", Getfile(n), Getline(n), "%feature(\"shadow\")"); String *pyaction = NewStringf("%s.%s", module, subfunc); Replaceall(pycode, "$action", pyaction); Delete(pyaction); Printv(f_shadow_stubs, pycode, "\n", NIL); Delete(pycode); } else { String *parms = make_pyParmList(n, false, false, allow_kwargs); String *callParms = make_pyParmList(n, false, true, allow_kwargs); Printv(f_shadow_stubs, "\ndef ", symname, "(", parms, ")", returnTypeAnnotation(n), ":\n", NIL); if (Node *node_with_doc = find_overload_with_docstring(n)) Printv(f_shadow_stubs, tab4, docstring(node_with_doc, AUTODOC_CTOR, tab4), "\n", NIL); if (have_pythonprepend(n)) Printv(f_shadow_stubs, indent_pythoncode(pythonprepend(n), tab4, Getfile(n), Getline(n), "%pythonprepend or %feature(\"pythonprepend\")"), "\n", NIL); Printv(f_shadow_stubs, tab4, "val = ", funcCall(subfunc, callParms), "\n", NIL); if (have_pythonappend(n)) Printv(f_shadow_stubs, indent_pythoncode(pythonappend(n), tab4, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n", NIL); Printv(f_shadow_stubs, tab4, "return val\n", NIL); } } else { Printf(f_shadow_stubs, "%s = %s\n", symname, subfunc); } } Delete(subfunc); } } return SWIG_OK; } /* ------------------------------------------------------------ * destructorHandler() * ------------------------------------------------------------ */ virtual int destructorHandler(Node *n) { String *symname = Getattr(n, "sym:name"); int oldshadow = shadow; if (builtin && in_class) { Node *cls = Swig_methodclass(n); // Use the destructor for the tp_dealloc slot unless a user overrides it with another method if (!Getattr(cls, "feature:python:tp_dealloc")) { Setattr(n, "feature:python:slot", "tp_dealloc"); Setattr(n, "feature:python:slot:functype", "destructor"); } } if (shadow) shadow = shadow | PYSHADOW_MEMBER; //Setattr(n,"emit:dealloc","1"); Language::destructorHandler(n); shadow = oldshadow; if (shadow) { if (Getattr(n, "feature:shadow")) { String *pycode = indent_pythoncode(Getattr(n, "feature:shadow"), tab4, Getfile(n), Getline(n), "%feature(\"shadow\")"); String *pyaction = NewStringf("%s.%s", module, Swig_name_destroy(NSPACE_TODO, symname)); Replaceall(pycode, "$action", pyaction); Delete(pyaction); Printv(f_shadow, pycode, "\n", NIL); Delete(pycode); } else { Printv(f_shadow, tab4, "__swig_destroy__ = ", module, ".", Swig_name_destroy(NSPACE_TODO, symname), "\n", NIL); if (!have_pythonprepend(n) && !have_pythonappend(n)) { return SWIG_OK; } Printv(f_shadow, tab4, "def __del__(self):\n", NIL); if (have_docstring(n)) Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR, tab8), "\n", NIL); if (have_pythonprepend(n)) Printv(f_shadow, indent_pythoncode(pythonprepend(n), tab8, Getfile(n), Getline(n), "%pythonprepend or %feature(\"pythonprepend\")"), "\n", NIL); if (have_pythonappend(n)) Printv(f_shadow, indent_pythoncode(pythonappend(n), tab8, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n", NIL); Printv(f_shadow, tab8, "pass\n", NIL); Printv(f_shadow, "\n", NIL); } } return SWIG_OK; } /* ------------------------------------------------------------ * membervariableHandler() * ------------------------------------------------------------ */ virtual int membervariableHandler(Node *n) { String *symname = Getattr(n, "sym:name"); int oldshadow = shadow; if (shadow) shadow = shadow | PYSHADOW_MEMBER; Language::membervariableHandler(n); shadow = oldshadow; if (shadow && !builtin) { String *mname = Swig_name_member(NSPACE_TODO, class_name, symname); String *setname = Swig_name_set(NSPACE_TODO, mname); String *getname = Swig_name_get(NSPACE_TODO, mname); int assignable = !is_immutable(n); String *variable_annotation = variableAnnotation(n); Printv(f_shadow, tab4, symname, variable_annotation, " = property(", module, ".", getname, NIL); if (assignable) Printv(f_shadow, ", ", module, ".", setname, NIL); if (have_docstring(n)) { String *s = docstring(n, AUTODOC_VAR, tab4); if (Len(s)) Printv(f_shadow, ", doc=", s, NIL); } Printv(f_shadow, ")\n", NIL); Delete(variable_annotation); Delete(mname); Delete(setname); Delete(getname); } return SWIG_OK; } /* ------------------------------------------------------------ * staticmembervariableHandler() * ------------------------------------------------------------ */ virtual int staticmembervariableHandler(Node *n) { Swig_save("builtin_staticmembervariableHandler", n, "builtin_symname", NIL); Language::staticmembervariableHandler(n); Swig_restore(n); if (GetFlag(n, "wrappedasconstant")) return SWIG_OK; String *symname = Getattr(n, "sym:name"); if (shadow) { if (!builtin && GetFlag(n, "hasconsttype")) { String *mname = Swig_name_member(NSPACE_TODO, class_name, symname); Printf(f_shadow_stubs, "%s.%s = %s.%s.%s\n", class_name, symname, module, global_name, mname); Delete(mname); } else { String *mname = Swig_name_member(NSPACE_TODO, class_name, symname); String *getname = Swig_name_get(NSPACE_TODO, mname); String *wrapgetname = Swig_name_wrapper(getname); String *vargetname = NewStringf("Swig_var_%s", getname); String *setname = Swig_name_set(NSPACE_TODO, mname); String *wrapsetname = Swig_name_wrapper(setname); String *varsetname = NewStringf("Swig_var_%s", setname); Wrapper *f = NewWrapper(); Printv(f->def, "SWIGINTERN PyObject *", wrapgetname, "(PyObject *SWIGUNUSEDPARM(self), PyObject *SWIGUNUSEDPARM(args)) {", NIL); Printv(f->code, " return ", vargetname, "();\n", NIL); Append(f->code, "}\n"); add_method(getname, wrapgetname, 0); Wrapper_print(f, f_wrappers); DelWrapper(f); int assignable = !is_immutable(n); if (assignable) { int funpack = fastunpack; Wrapper *f = NewWrapper(); Printv(f->def, "SWIGINTERN PyObject *", wrapsetname, "(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {", NIL); Wrapper_add_local(f, "res", "int res"); if (!funpack) { Wrapper_add_local(f, "value", "PyObject *value"); Append(f->code, "if (!PyArg_ParseTuple(args, \"O:set\", &value)) return NULL;\n"); } Printf(f->code, "res = %s(%s);\n", varsetname, funpack ? "args" : "value"); Append(f->code, "return !res ? SWIG_Py_Void() : NULL;\n"); Append(f->code, "}\n"); Wrapper_print(f, f_wrappers); add_method(setname, wrapsetname, 0, 0, funpack, 1, 1); DelWrapper(f); } if (!builtin) { Printv(f_shadow, tab4, symname, " = property(", module, ".", getname, NIL); if (assignable) Printv(f_shadow, ", ", module, ".", setname, NIL); if (have_docstring(n)) { String *s = docstring(n, AUTODOC_VAR, tab4); if (Len(s)) Printv(f_shadow, ", doc=", s, NIL); } Printv(f_shadow, ")\n", NIL); } String *getter = Getattr(n, "pybuiltin:getter"); String *setter = Getattr(n, "pybuiltin:setter"); Hash *h = NULL; if (getter || setter) { h = Getattr(builtin_getset, symname); if (!h) { h = NewHash(); Setattr(h, "static", "1"); Setattr(builtin_getset, symname, h); } } if (getter) Setattr(h, "getter", getter); if (setter) Setattr(h, "setter", setter); if (h) Delete(h); Delete(mname); Delete(getname); Delete(wrapgetname); Delete(vargetname); Delete(setname); Delete(wrapsetname); Delete(varsetname); } } return SWIG_OK; } /* ------------------------------------------------------------ * memberconstantHandler() * ------------------------------------------------------------ */ virtual int memberconstantHandler(Node *n) { String *symname = Getattr(n, "sym:name"); if (builtin && in_class) { Swig_save("builtin_memberconstantHandler", n, "pybuiltin:symname", NIL); Setattr(n, "pybuiltin:symname", symname); } int oldshadow = shadow; if (shadow) shadow = shadow | PYSHADOW_MEMBER; Language::memberconstantHandler(n); shadow = oldshadow; if (builtin && in_class) { Swig_restore(n); } else if (shadow) { Printv(f_shadow, tab4, symname, " = ", module, ".", Swig_name_member(NSPACE_TODO, class_name, symname), "\n", NIL); if (have_docstring(n)) Printv(f_shadow, tab4, docstring(n, AUTODOC_CONST, tab4), "\n", NIL); } return SWIG_OK; } /* ------------------------------------------------------------ * insertDirective() * * Hook for %insert directive. We're going to look for special %shadow inserts * as a special case so we can do indenting correctly * ------------------------------------------------------------ */ virtual int insertDirective(Node *n) { String *code = Getattr(n, "code"); String *section = Getattr(n, "section"); if (!ImportMode && (Cmp(section, "python") == 0 || Cmp(section, "shadow") == 0)) { if (shadow) { String *pycode = indent_pythoncode(code, shadow_indent, Getfile(n), Getline(n), "%pythoncode or %insert(\"python\") block"); Printv(f_shadow, pycode, NIL); Delete(pycode); } } else if (!ImportMode && (Cmp(section, "pythonbegin") == 0)) { if (shadow) { String *pycode = indent_pythoncode(code, "", Getfile(n), Getline(n), "%pythonbegin or %insert(\"pythonbegin\") block"); Printv(f_shadow_begin, pycode, NIL); Delete(pycode); } } else { Language::insertDirective(n); } return SWIG_OK; } virtual String *runtimeCode() { String *s = NewString(""); String *shead = Swig_include_sys("pyhead.swg"); if (!shead) { Printf(stderr, "*** Unable to open 'pyhead.swg'\n"); } else { Append(s, shead); Delete(shead); } String *serrors = Swig_include_sys("pyerrors.swg"); if (!serrors) { Printf(stderr, "*** Unable to open 'pyerrors.swg'\n"); } else { Append(s, serrors); Delete(serrors); } String *sthread = Swig_include_sys("pythreads.swg"); if (!sthread) { Printf(stderr, "*** Unable to open 'pythreads.swg'\n"); } else { Append(s, sthread); Delete(sthread); } String *sapi = Swig_include_sys("pyapi.swg"); if (!sapi) { Printf(stderr, "*** Unable to open 'pyapi.swg'\n"); } else { Append(s, sapi); Delete(sapi); } String *srun = Swig_include_sys("pyrun.swg"); if (!srun) { Printf(stderr, "*** Unable to open 'pyrun.swg'\n"); } else { Append(s, srun); Delete(srun); } return s; } virtual String *defaultExternalRuntimeFilename() { return NewString("swigpyrun.h"); } /*---------------------------------------------------------------------- * kwargsSupport() *--------------------------------------------------------------------*/ bool kwargsSupport() const { return true; } }; /* --------------------------------------------------------------- * classDirectorMethod() * * Emit a virtual director method to pass a method call on to the * underlying Python object. * * ** Moved it here due to internal error on gcc-2.96 ** * --------------------------------------------------------------- */ int PYTHON::classDirectorMethods(Node *n) { director_method_index = 0; return Language::classDirectorMethods(n); } int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { int is_void = 0; int is_pointer = 0; String *decl = Getattr(n, "decl"); String *name = Getattr(n, "name"); String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *symname = Getattr(n, "sym:name"); String *declaration = NewString(""); ParmList *l = Getattr(n, "parms"); Wrapper *w = NewWrapper(); String *tm; String *wrap_args = NewString(""); SwigType *returntype = Getattr(n, "type"); String *value = Getattr(n, "value"); String *storage = Getattr(n, "storage"); bool pure_virtual = false; int status = SWIG_OK; int idx; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; if (builtin) { // Rename any wrapped parameters called 'self' as the generated code contains a variable with same name Parm *p; for (p = l; p; p = nextSibling(p)) { String *arg = Getattr(p, "name"); if (arg && Cmp(arg, "self") == 0) Delattr(p, "name"); } } if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { pure_virtual = true; } } /* determine if the method returns a pointer */ is_pointer = SwigType_ispointer_return(decl); is_void = (!Cmp(returntype, "void") && !is_pointer); /* virtual method definition */ String *target; String *pclassname = NewStringf("SwigDirector_%s", classname); String *qualified_name = NewStringf("%s::%s", pclassname, name); SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); /* header declaration */ target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Get any exception classes in the throws typemap if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = 0; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { Parm *p; int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } String *str = SwigType_str(Getattr(p, "type"), 0); Append(w->def, str); Append(declaration, str); Delete(str); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); /* declare method return value * if the return value is a reference or const reference, a specialized typemap must * handle it, including declaration of c_result ($result). */ if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (builtin) { Printv(w->code, "PyObject *self = NULL;\n", NIL); Printv(w->code, "(void)self;\n", NIL); } if (ignored_method) { if (!pure_virtual) { if (!is_void) Printf(w->code, "return "); String *super_call = Swig_method_call(super, l); Printf(w->code, "%s;\n", super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); } } else { /* attach typemaps to arguments (C/C++ -> Python) */ String *arglist = NewString(""); String *parse_args = NewString(""); Swig_director_parms_fixup(l); /* remove the wrapper 'w' since it was producing spurious temps */ Swig_typemap_attach_parms("in", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("directorargout", l, w); Parm *p; char source[256]; int outputs = 0; if (!is_void) outputs++; /* build argument list and type conversion string */ idx = 0; p = l; int use_parse = 0; while (p) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } if (Getattr(p, "tmap:directorargout") != 0) outputs++; String *pname = Getattr(p, "name"); String *ptype = Getattr(p, "type"); Putc(',', arglist); if ((tm = Getattr(p, "tmap:directorin")) != 0) { String *parse = Getattr(p, "tmap:directorin:parse"); if (!parse) { sprintf(source, "obj%d", idx++); String *input = NewString(source); Setattr(p, "emit:directorinput", input); Replaceall(tm, "$input", input); Delete(input); Replaceall(tm, "$owner", "0"); /* Wrapper_add_localv(w, source, "swig::SwigVar_PyObject", source, "= 0", NIL); */ Printv(wrap_args, "swig::SwigVar_PyObject ", source, ";\n", NIL); Printv(wrap_args, tm, "\n", NIL); Printv(arglist, "(PyObject *)", source, NIL); Putc('O', parse_args); } else { use_parse = 1; Append(parse_args, parse); Setattr(p, "emit:directorinput", pname); Replaceall(tm, "$input", pname); Replaceall(tm, "$owner", "0"); if (Len(tm) == 0) Append(tm, pname); Append(arglist, tm); } p = Getattr(p, "tmap:directorin:next"); continue; } else if (Cmp(ptype, "void")) { /* special handling for pointers to other C++ director classes. * ideally this would be left to a typemap, but there is currently no * way to selectively apply the dynamic_cast<> to classes that have * directors. in other words, the type "SwigDirector_$1_lname" only exists * for classes with directors. we avoid the problem here by checking * module.wrap::directormap, but it's not clear how to get a typemap to * do something similar. perhaps a new default typemap (in addition * to SWIGTYPE) called DIRECTORTYPE? */ if (SwigType_ispointer(ptype) || SwigType_isreference(ptype)) { Node *module = Getattr(parent, "module"); Node *target = Swig_directormap(module, ptype); sprintf(source, "obj%d", idx++); String *nonconst = 0; /* strip pointer/reference --- should move to Swig/stype.c */ String *nptype = NewString(Char(ptype) + 2); /* name as pointer */ String *ppname = Copy(pname); if (SwigType_isreference(ptype)) { Insert(ppname, 0, "&"); } /* if necessary, cast away const since Python doesn't support it! */ if (SwigType_isconst(nptype)) { nonconst = NewStringf("nc_tmp_%s", pname); String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(ptype, 0), ppname); Wrapper_add_localv(w, nonconst, SwigType_lstr(ptype, 0), nonconst, nonconst_i, NIL); Delete(nonconst_i); Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number, "Target language argument '%s' discards const in director method %s::%s.\n", SwigType_str(ptype, pname), SwigType_namestr(c_classname), SwigType_namestr(name)); } else { nonconst = Copy(ppname); } Delete(nptype); Delete(ppname); String *mangle = SwigType_manglestr(ptype); if (target) { String *director = NewStringf("director_%s", mangle); Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); Wrapper_add_localv(w, source, "swig::SwigVar_PyObject", source, "= 0", NIL); Printf(wrap_args, "%s = SWIG_DIRECTOR_CAST(%s);\n", director, nonconst); Printf(wrap_args, "if (!%s) {\n", director); Printf(wrap_args, "%s = SWIG_InternalNewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); Append(wrap_args, "} else {\n"); Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director); Printf(wrap_args, "SWIG_Py_INCREF((PyObject *)%s);\n", source); Append(wrap_args, "}\n"); Delete(director); Printv(arglist, source, NIL); } else { Wrapper_add_localv(w, source, "swig::SwigVar_PyObject", source, "= 0", NIL); Printf(wrap_args, "%s = SWIG_InternalNewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); //Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE_p_%s, 0);\n", // source, nonconst, base); Printv(arglist, source, NIL); } Putc('O', parse_args); Delete(mangle); Delete(nonconst); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_NOWRAP; break; } } p = nextSibling(p); } /* add the method name as a PyString */ String *pyname = Getattr(n, "sym:name"); int allow_thread = threads_enable(n); if (allow_thread) { thread_begin_block(n, w->code); Append(w->code, "{\n"); } /* wrap complex arguments to PyObjects */ Printv(w->code, wrap_args, NIL); /* pass the method call on to the Python object */ if (dirprot_mode() && !is_public(n)) { Printf(w->code, "swig_set_inner(\"%s\", true);\n", name); } Append(w->code, "if (!swig_get_self()) {\n"); Printf(w->code, " Swig::DirectorException::raise(\"'self' uninitialized, maybe you forgot to call %s.__init__.\");\n", classname); Append(w->code, "}\n"); Append(w->code, "#if defined(SWIG_PYTHON_DIRECTOR_VTABLE)\n"); Printf(w->code, "const size_t swig_method_index = %d;\n", director_method_index++); Printf(w->code, "const char *const swig_method_name = \"%s\";\n", pyname); Append(w->code, "PyObject *method = swig_get_method(swig_method_index, swig_method_name);\n"); if (Len(parse_args) > 0) { if (use_parse) { Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallFunction(method, (char *)\"(%s)\" %s);\n", Swig_cresult_name(), parse_args, arglist); } else { Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallFunctionObjArgs(method %s, NULL);\n", Swig_cresult_name(), arglist); } } else { Append(w->code, "swig::SwigVar_PyObject args = PyTuple_New(0);\n"); Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_Call(method, (PyObject *) args, NULL);\n", Swig_cresult_name()); } Append(w->code, "#else\n"); if (Len(parse_args) > 0) { if (use_parse) { Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallMethod(swig_get_self(), (char *)\"%s\", (char *)\"(%s)\" %s);\n", Swig_cresult_name(), pyname, parse_args, arglist); } else { Printf(w->code, "swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar(\"%s\");\n", pyname); Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallMethodObjArgs(swig_get_self(), (PyObject *) swig_method_name %s, NULL);\n", Swig_cresult_name(), arglist); } } else { Printf(w->code, "swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar(\"%s\");\n", pyname); Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallMethodObjArgs(swig_get_self(), (PyObject *) swig_method_name, NULL);\n", Swig_cresult_name()); } Append(w->code, "#endif\n"); if (dirprot_mode() && !is_public(n)) Printf(w->code, "swig_set_inner(\"%s\", false);\n", name); /* exception handling */ tm = Swig_typemap_lookup("director:except", n, Swig_cresult_name(), 0); if (!tm) { tm = Getattr(n, "feature:director:except"); if (tm) tm = Copy(tm); } Printf(w->code, "if (!%s) {\n", Swig_cresult_name()); Append(w->code, " PyObject *error = PyErr_Occurred();\n"); if ((tm) && Len(tm) && (Strcmp(tm, "1") != 0)) { Replaceall(tm, "$error", "error"); Printv(w->code, Str(tm), "\n", NIL); } else { Append(w->code, " if (error) {\n"); Printf(w->code, " Swig::DirectorMethodException::raise(\"Error detected when calling '%s.%s'\");\n", classname, pyname); Append(w->code, " }\n"); } Append(w->code, "}\n"); Delete(tm); /* * Python method may return a simple object, or a tuple. * for in/out arguments, we have to extract the appropriate PyObjects from the tuple, * then marshal everything back to C/C++ (return value and output arguments). * */ /* marshal return value and other outputs (if any) from PyObject to C/C++ type */ String *cleanup = NewString(""); String *outarg = NewString(""); if (outputs > 1) { Wrapper_add_local(w, "output", "PyObject *output"); Printf(w->code, "if (!PyTuple_Check(%s)) {\n", Swig_cresult_name()); Printf(w->code, " Swig::DirectorTypeMismatchException::raise(\"Python method %s.%sfailed to return a tuple.\");\n", classname, pyname); Append(w->code, "}\n"); } idx = 0; /* marshal return value */ if (!is_void) { tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w); if (tm != 0) { if (outputs > 1) { Printf(w->code, "output = PyTuple_GetItem(%s, %d);\n", Swig_cresult_name(), idx++); Replaceall(tm, "$input", "output"); } else { Replaceall(tm, "$input", Swig_cresult_name()); } char temp[24]; sprintf(temp, "%d", idx); Replaceall(tm, "$argnum", temp); /* TODO check this */ if (Getattr(n, "wrap:disown")) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } if (Getattr(n, "tmap:directorout:implicitconv")) { Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); } Replaceall(tm, "$result", "c_result"); Printv(w->code, tm, "\n", NIL); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_ERROR; } } /* marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { if (outputs > 1) { Printf(w->code, "output = PyTuple_GetItem(%s, %d);\n", Swig_cresult_name(), idx++); Replaceall(tm, "$result", "output"); } else { Replaceall(tm, "$result", Swig_cresult_name()); } Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } /* any existing helper functions to handle this? */ if (allow_thread) { Append(w->code, "}\n"); thread_end_block(n, w->code); } Delete(parse_args); Delete(arglist); Delete(cleanup); Delete(outarg); } if (!is_void) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { Printf(w->code, "return (%s) c_result;\n", rettype); } else { Printf(w->code, "return (%s) *c_result;\n", rettype); } Delete(rettype); } } Append(w->code, "}\n"); // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK) { Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } /* clean up */ Delete(wrap_args); Delete(pclassname); DelWrapper(w); return status; } /* ----------------------------------------------------------------------------- * swig_python() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_python() { return new PYTHON(); } extern "C" Language *swig_python(void) { return new_swig_python(); } swig-4.4.0/Source/Modules/perl5.cxx0000664000175000017500000024222315075443613017010 0ustar williamwilliam/* ---------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * perl5.cxx * * Perl5 language module for SWIG. * ------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include static const char *usage = "\ Perl 5 Options (available with -perl5)\n\ -compat - Compatibility mode\n\ -const - Wrap constants as constants and not variables (implies -proxy)\n\ -nopm - Do not generate the .pm file\n\ -noproxy - Don't create proxy classes\n\ -proxy - Create proxy classes (enabled by default)\n\ -static - Omit code related to dynamic loading\n\ \n"; static int compat = 0; static int no_pmfile = 0; static int export_all = 0; /* * pmfile * set by the -pm flag, overrides the name of the .pm file */ static String *pmfile = 0; /* * module * set by the %module directive, e.g. "Xerces". It will determine * the name of the .pm file, and the dynamic library, and the name * used by any module wanting to %import the module. */ static String *module = 0; /* * namespace_module * the fully namespace qualified name of the module. It will be used * to set the package namespace in the .pm file, as well as the name * of the initialization methods in the glue library. This will be * the same as module, above, unless the %module directive is given * the 'package' option, e.g. %module(package="Foo::Bar") "baz" */ static String *namespace_module = 0; /* * cmodule * the namespace of the internal glue code, set to the value of * module with a 'c' appended */ static String *cmodule = 0; /* * dest_package * an optional namespace to put all classes into. Specified by using * the %module(package="Foo::Bar") "baz" syntax */ static String *dest_package = 0; static String *command_tab = 0; static String *constant_tab = 0; static String *variable_tab = 0; static File *f_begin = 0; static File *f_runtime = 0; static File *f_runtime_h = 0; static File *f_header = 0; static File *f_wrappers = 0; static File *f_directors = 0; static File *f_directors_h = 0; static File *f_init = 0; static File *f_pm = 0; static String *pm; /* Package initialization code */ static String *magic; /* Magic variable wrappers */ static int staticoption = 0; // controlling verbose output static int verbose = 0; /* The following variables are used to manage Perl5 classes */ static int blessed = 1; /* Enable object oriented features */ static int do_constants = 0; /* Constant wrapping */ static List *classlist = 0; /* List of classes */ static int have_constructor = 0; static int have_destructor = 0; static int have_data_members = 0; static String *class_name = 0; /* Name of the class (what Perl thinks it is) */ static String *real_classname = 0; /* Real name of C/C++ class */ static String *fullclassname = 0; static String *pcode = 0; /* Perl code associated with each class */ /* static String *blessedmembers = 0; *//* Member data associated with each class */ static int member_func = 0; /* Set to 1 when wrapping a member function */ static String *func_stubs = 0; /* Function stubs */ static String *const_stubs = 0; /* Constant stubs */ static Node *const_stubs_enum_class = 0; /* Node for enum class if we're currently generating one */ static String *var_stubs = 0; /* Variable stubs */ static String *exported = 0; /* Exported symbols */ static String *pragma_include = 0; static String *additional_perl_code = 0; /* Additional Perl code from %perlcode %{ ... %} */ static Hash *operators = 0; static int have_operators = 0; class PERL5:public Language { public: PERL5() { Clear(argc_template_string); Printv(argc_template_string, "items", NIL); Clear(argv_template_string); Printv(argv_template_string, "ST(%d)", NIL); directorLanguage(); } /* Test to see if a type corresponds to something wrapped with a shadow class */ Node *is_shadow(SwigType *t) { Node *n; n = classLookup(t); /* Printf(stdout,"'%s' --> '%p'\n", t, n); */ if (n) { if (!Getattr(n, "perl5:proxy")) { setclassname(n); } return Getattr(n, "perl5:proxy"); } return 0; } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { int i = 1; SWIG_library_directory("perl5"); for (i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-package") == 0) { Printv(stderr, "*** -package is no longer supported\n*** use the directive '%module A::B::C' in your interface file instead\n*** see the Perl section in the manual for details.\n", NIL); Exit(EXIT_FAILURE); } else if (strcmp(argv[i], "-interface") == 0) { Printv(stderr, "*** -interface is no longer supported\n*** use the directive '%module A::B::C' in your interface file instead\n*** see the Perl section in the manual for details.\n", NIL); Exit(EXIT_FAILURE); } else if (strcmp(argv[i], "-exportall") == 0) { export_all = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-static") == 0) { staticoption = 1; Swig_mark_arg(i); } else if ((strcmp(argv[i], "-shadow") == 0) || ((strcmp(argv[i], "-proxy") == 0))) { blessed = 1; Swig_mark_arg(i); } else if ((strcmp(argv[i], "-noproxy") == 0)) { blessed = 0; Swig_mark_arg(i); } else if (strcmp(argv[i], "-const") == 0) { do_constants = 1; blessed = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-nopm") == 0) { no_pmfile = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-pm") == 0) { Swig_mark_arg(i); i++; pmfile = NewString(argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i],"-v") == 0) { Swig_mark_arg(i); verbose++; } else if (strcmp(argv[i], "-compat") == 0) { compat = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); } else if (strcmp(argv[i], "-cppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is now always on.\n", argv[i]); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocppcast") == 0) { Printf(stderr, "Deprecated command line option: %s. This option is no longer supported.\n", argv[i]); Swig_mark_arg(i); Exit(EXIT_FAILURE); } } } Preprocessor_define("SWIGPERL 1", 0); // SWIGPERL5 is deprecated, and no longer documented. Preprocessor_define("SWIGPERL5 1", 0); SWIG_config_file("perl5.swg"); allow_overloading(); } /* ------------------------------------------------------------ * top() * ------------------------------------------------------------ */ virtual int top(Node *n) { /* check if directors are enabled for this module. note: this * is a "master" switch, without which no director code will be * emitted. %feature("director") statements are also required * to enable directors for individual classes or methods. * * use %module(directors="1") modulename at the start of the * interface file to enable director generation. * * TODO: directors are disallowed in conjunction with many command * line options. Some of them are probably safe, but it will take * some effort to validate each one. */ { Node *mod = Getattr(n, "module"); if (mod) { Node *options = Getattr(mod, "options"); if (options) { int dirprot = 0; if (Getattr(options, "dirprot")) dirprot = 1; if (Getattr(options, "nodirprot")) dirprot = 0; if (Getattr(options, "directors")) { int allow = 1; if (export_all) { Printv(stderr, "*** directors are not supported with -exportall\n", NIL); allow = 0; } if (staticoption) { Printv(stderr, "*** directors are not supported with -static\n", NIL); allow = 0; } if (!blessed) { Printv(stderr, "*** directors are not supported with -noproxy\n", NIL); allow = 0; } if (no_pmfile) { Printv(stderr, "*** directors are not supported with -nopm\n", NIL); allow = 0; } if (compat) { Printv(stderr, "*** directors are not supported with -compat\n", NIL); allow = 0; } if (allow) { allow_directors(); if (dirprot) allow_dirprot(); } } } } } /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = Getattr(n, "outfile_h"); f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); f_directors_h = NewString(""); f_directors = NewString(""); if (Swig_directors_enabled()) { f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); classlist = NewList(); pm = NewString(""); func_stubs = NewString(""); var_stubs = NewString(""); const_stubs = NewString(""); exported = NewString(""); magic = NewString(""); pragma_include = NewString(""); additional_perl_code = NewString(""); command_tab = NewString("static swig_command_info swig_commands[] = {\n"); constant_tab = NewString("static swig_constant_info swig_constants[] = {\n"); variable_tab = NewString("static swig_variable_info swig_variables[] = {\n"); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "PERL"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); } Printf(f_runtime, "#define SWIG_CASTRANK_MODE\n"); Printf(f_runtime, "\n"); // Is the imported module in another package? (IOW, does it use the // %module(package="name") option and it's different than the package // of this module.) Node *mod = Getattr(n, "module"); Node *options = Getattr(mod, "options"); module = Copy(Getattr(n,"name")); String *underscore_module = Copy(module); Replaceall(underscore_module,":","_"); if (verbose > 0) { fprintf(stdout, "top: using namespace_module: %s\n", Char(namespace_module)); } if (Swig_directors_enabled()) { Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", underscore_module); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", underscore_module); if (dirprot_mode()) { Printf(f_directors_h, "#include \n"); Printf(f_directors_h, "#include \n\n"); } Printf(f_directors, "\n\n"); Printf(f_directors, "/* ---------------------------------------------------\n"); Printf(f_directors, " * C++ director class methods\n"); Printf(f_directors, " * --------------------------------------------------- */\n\n"); if (outfile_h) { String *filename = Swig_file_filename(outfile_h); Printf(magic, "#include \"%s\"\n\n", filename); Delete(filename); } } if (verbose > 0) { fprintf(stdout, "top: using module: %s\n", Char(module)); } dest_package = options ? Getattr(options, "package") : 0; if (dest_package) { namespace_module = Copy(dest_package); if (verbose > 0) { fprintf(stdout, "top: Found package: %s\n",Char(dest_package)); } } else { namespace_module = Copy(module); if (verbose > 0) { fprintf(stdout, "top: No package found\n"); } } /* If we're in blessed mode, change the package name to "packagec" */ if (blessed) { cmodule = NewStringf("%sc",namespace_module); } else { cmodule = NewString(namespace_module); } /* Create a .pm file * Need to strip off any prefixes that might be found in * the module name */ if (no_pmfile) { f_pm = NewString(0); } else { if (!pmfile) { char *m = Char(module) + Len(module); while (m != Char(module)) { if (*m == ':') { m++; break; } m--; } pmfile = NewStringf("%s.pm", m); } String *filen = NewStringf("%s%s", SWIG_output_directory(), pmfile); if ((f_pm = NewFile(filen, "w", SWIG_output_files())) == 0) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Delete(filen); filen = NULL; Swig_register_filebyname("pm", f_pm); Swig_register_filebyname("perl", f_pm); } { String *boot_name = NewStringf("boot_%s", underscore_module); Printf(f_header,"#define SWIG_init %s\n\n", boot_name); Printf(f_header,"#define SWIG_name \"%s::%s\"\n", cmodule, boot_name); Printf(f_header,"#define SWIG_prefix \"%s::\"\n", cmodule); Delete(boot_name); } Swig_banner_target_lang(f_pm, "#"); Printf(f_pm, "\n"); Printf(f_pm, "package %s;\n", module); /* * If the package option has been given we are placing our * symbols into some other packages namespace, so we do not * mess with @ISA or require for that package */ if (dest_package) { Printf(f_pm,"use base qw(DynaLoader);\n"); } else { Printf(f_pm,"use base qw(Exporter);\n"); if (!staticoption) { Printf(f_pm,"use base qw(DynaLoader);\n"); } } /* Start creating magic code */ Printv(magic, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n", "#define MAGIC_CLASS\n", "SWIGCLASS_STATIC int swig_magic_readonly(pTHX_ SV *SWIGUNUSEDPARM(sv), MAGIC *SWIGUNUSEDPARM(mg)) {\n", tab4, "MAGIC_PPERL\n", tab4, "croak(\"Value is read-only.\");\n", tab4, "return 0;\n", "}\n", NIL); Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); /* emit wrappers */ Language::top(n); if (Swig_directors_enabled()) { // Insert director runtime into the f_runtime file (make it occur before %header section) Swig_insert_file("director_common.swg", f_runtime); Swig_insert_file("director.swg", f_runtime); } String *base = NewString(""); /* Dump out variable wrappers */ Printv(magic, "\n#ifdef __cplusplus\n}\n#endif\n", NIL); Printf(f_header, "%s\n", magic); String *type_table = NewString(""); /* Patch the type table to reflect the names used by shadow classes */ if (blessed) { Iterator cls; for (cls = First(classlist); cls.item; cls = Next(cls)) { String *pname = Getattr(cls.item, "perl5:proxy"); if (pname) { SwigType *type = Getattr(cls.item, "classtypeobj"); if (!type) continue; /* If unnamed class, no type will be found */ type = Copy(type); SwigType_add_pointer(type); String *mangled = SwigType_manglestr(type); SwigType_remember_mangleddata(mangled, NewStringf("\"%s\"", pname)); Delete(type); Delete(mangled); } } } SwigType_emit_type_table(f_runtime, type_table); Printf(f_wrappers, "%s", type_table); Delete(type_table); Printf(constant_tab, "{0,0,0,0,0,0}\n};\n"); Printv(f_wrappers, constant_tab, NIL); Printf(f_wrappers, "#ifdef __cplusplus\n}\n#endif\n"); Printf(f_init, "\t ST(0) = &PL_sv_yes;\n"); Printf(f_init, "\t XSRETURN(1);\n"); Printf(f_init, "}\n"); /* Finish off tables */ Printf(variable_tab, "{0,0,0,0}\n};\n"); Printv(f_wrappers, variable_tab, NIL); Printf(command_tab, "{0,0}\n};\n"); Printv(f_wrappers, command_tab, NIL); Printf(f_pm, "package %s;\n", cmodule); if (!staticoption) { Printf(f_pm,"bootstrap %s;\n", module); } else { Printf(f_pm,"package %s;\n", cmodule); Printf(f_pm,"boot_%s();\n", underscore_module); } Printf(f_pm, "package %s;\n", module); /* * If the package option has been given we are placing our * symbols into some other packages namespace, so we do not * mess with @EXPORT */ if (!dest_package) { Printf(f_pm,"@EXPORT = qw(%s);\n", exported); } Printf(f_pm, "%s", pragma_include); if (blessed) { /* * These methods will be duplicated if package * has been specified, so we do not output them */ if (!dest_package) { Printv(base, "\n# ---------- BASE METHODS -------------\n\n", "package ", namespace_module, ";\n\n", NIL); /* Write out the TIE method */ Printv(base, "sub TIEHASH {\n", tab4, "my ($classname,$obj) = @_;\n", tab4, "return bless $obj, $classname;\n", "}\n\n", NIL); /* Output a CLEAR method. This is just a place-holder, but by providing it we * can make declarations such as * %$u = ( x => 2, y=>3, z =>4 ); * * Where x,y,z are the members of some C/C++ object. */ Printf(base, "sub CLEAR { }\n\n"); /* Output default firstkey/nextkey methods */ Printf(base, "sub FIRSTKEY { }\n\n"); Printf(base, "sub NEXTKEY { }\n\n"); /* Output a FETCH method. This is actually common to all classes */ Printv(base, "sub FETCH {\n", tab4, "my ($self,$field) = @_;\n", tab4, "my $member_func = \"swig_${field}_get\";\n", tab4, "$self->$member_func();\n", "}\n\n", NIL); /* Output a STORE method. This is also common to all classes (might move to base class) */ Printv(base, "sub STORE {\n", tab4, "my ($self,$field,$newval) = @_;\n", tab4, "my $member_func = \"swig_${field}_set\";\n", tab4, "$self->$member_func($newval);\n", "}\n\n", NIL); /* Output a 'this' method */ Printv(base, "sub this {\n", tab4, "my $ptr = shift;\n", tab4, "return tied(%$ptr);\n", "}\n\n", NIL); Printf(f_pm, "%s", base); } /* Emit function stubs for stand-alone functions */ Printf(f_pm, "\n# ------- FUNCTION WRAPPERS --------\n\n"); Printf(f_pm, "package %s;\n\n", namespace_module); Printf(f_pm, "%s", func_stubs); /* Emit package code for different classes */ Printf(f_pm, "%s", pm); if (Len(const_stubs) > 0) { /* Emit constant stubs */ Printf(f_pm, "\n# ------- CONSTANT STUBS -------\n\n"); Printf(f_pm, "%s", const_stubs); } /* Emit variable stubs */ Printf(f_pm, "\n# ------- VARIABLE STUBS --------\n\n"); Printf(f_pm, "package %s;\n\n", namespace_module); Printf(f_pm, "%s", var_stubs); } /* Add additional Perl code at the end */ Printf(f_pm, "%s", additional_perl_code); Printf(f_pm, "1;\n"); Delete(f_pm); Delete(base); Delete(dest_package); Delete(underscore_module); /* Close all of the files */ Dump(f_runtime, f_begin); Dump(f_header, f_begin); if (Swig_directors_enabled()) { Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); Dump(f_directors, f_begin); } Dump(f_wrappers, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_directors); Delete(f_directors_h); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } /* ------------------------------------------------------------ * importDirective(Node *n) * ------------------------------------------------------------ */ virtual int importDirective(Node *n) { if (blessed) { String *modname = Getattr(n, "module"); if (modname) { Printf(f_pm, "require %s;\n", modname); } } return Language::importDirective(n); } /* ------------------------------------------------------------ * functionWrapper() * ------------------------------------------------------------ */ virtual int functionWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *overname = 0; int director_method = 0; Parm *p; int i; Wrapper *f; char source[256], temp[256]; String *tm; String *cleanup, *outarg; int num_saved = 0; int num_arguments, num_required; int varargs = 0; if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } else { if (!addSymbol(iname, n)) return SWIG_ERROR; } f = NewWrapper(); cleanup = NewString(""); outarg = NewString(""); String *wname = Swig_name_wrapper(iname); if (overname) { Append(wname, overname); } Setattr(n, "wrap:name", wname); Printv(f->def, "XS(", wname, ") {\n", "{\n", /* scope to destroy C++ objects before croaking */ NIL); emit_parameter_variables(l, f); emit_attach_parmmaps(l, f); Setattr(n, "wrap:parms", l); num_arguments = emit_num_arguments(l); num_required = emit_num_required(l); varargs = emit_isvarargs(l); Wrapper_add_local(f, "argvi", "int argvi = 0"); /* Check the number of arguments */ if (!varargs) { Printf(f->code, " if ((items < %d) || (items > %d)) {\n", num_required, num_arguments); } else { Printf(f->code, " if (items < %d) {\n", num_required); } Printf(f->code, " SWIG_croak(\"Usage: %s\");\n", usage_func(Char(iname), returntype, l)); Printf(f->code, "}\n"); /* Write code to extract parameters. */ for (i = 0, p = l; i < num_arguments; i++) { /* Skip ignored arguments */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); /* Produce string representation of source and target arguments */ sprintf(source, "ST(%d)", i); if (i >= num_required) { Printf(f->code, " if (items > %d) {\n", i); } if ((tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); /* Save input location */ if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Printf(f->code, "%s\n", tm); p = Getattr(p, "tmap:in:next"); } else { Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); p = nextSibling(p); } if (i >= num_required) { Printf(f->code, " }\n"); } } if (varargs) { if (p && (tm = Getattr(p, "tmap:in"))) { sprintf(source, "ST(%d)", i); Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); Printf(f->code, "if (items >= %d) {\n", i); Printv(f->code, tm, "\n", NIL); Printf(f->code, "}\n"); } } /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (i = 0, p = l; p; i++) { if ((tm = Getattr(p, "tmap:freearg"))) { Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ num_saved = 0; for (i = 0, p = l; p; i++) { if ((tm = Getattr(p, "tmap:argout"))) { SwigType *t = Getattr(p, "type"); Replaceall(tm, "$result", "ST(argvi)"); if (is_shadow(t)) { Replaceall(tm, "$shadow", "SWIG_SHADOW"); } else { Replaceall(tm, "$shadow", "0"); } String *in = Getattr(p, "emit:input"); if (in) { sprintf(temp, "_saved[%d]", num_saved); Replaceall(tm, "$arg", temp); Replaceall(tm, "$input", temp); Printf(f->code, "_saved[%d] = %s;\n", num_saved, in); num_saved++; } Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } /* If there were any saved arguments, emit a local variable for them */ if (num_saved) { sprintf(temp, "_saved[%d]", num_saved); Wrapper_add_localv(f, "_saved", "SV *", temp, NIL); } director_method = is_member_director(n) && !is_smart_pointer() && 0 != Cmp(nodeType(n), "destructor"); if (director_method) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Append(f->code, "director = SWIG_DIRECTOR_CAST(arg1);\n"); if (dirprot_mode() && !is_public(n)) { Printf(f->code, "if (!director || !(director->swig_get_inner(\"%s\"))) {\n", name); Printf(f->code, "SWIG_exception_fail(SWIG_RuntimeError, \"accessing protected member %s\");\n", name); Append(f->code, "}\n"); } Wrapper_add_local(f, "upcall", "bool upcall = false"); Printf(f->code, "upcall = director && SvSTASH(SvRV(ST(0))) == gv_stashpv(director->swig_get_class(), 0);\n"); } /* Emit the function call */ if (director_method) { Append(f->code, "try {\n"); } /* Now write code to make the function call */ Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); if (director_method) { Append(actioncode, "} catch (Swig::DirectorException& swig_err) {\n"); Append(actioncode, " sv_setsv(ERRSV, swig_err.getNative());\n"); Append(actioncode, " SWIG_fail;\n"); Append(actioncode, "}\n"); } if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { SwigType *t = Getattr(n, "type"); Replaceall(tm, "$result", "ST(argvi)"); if (is_shadow(t)) { Replaceall(tm, "$shadow", "SWIG_SHADOW"); } else { Replaceall(tm, "$shadow", "0"); } if (GetFlag(n, "feature:new")) { Replaceall(tm, "$owner", "SWIG_OWNER"); } else { Replaceall(tm, "$owner", "0"); } Printf(f->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), name); } emit_return_variable(n, returntype, f); /* If there were any output args, take care of them. */ Printv(f->code, outarg, NIL); /* If there was any cleanup, do that. */ Printv(f->code, cleanup, NIL); if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } } if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); } if (director_method) { if ((tm = Swig_typemap_lookup("directorfree", n, Swig_cresult_name(), 0))) { Replaceall(tm, "$input", Swig_cresult_name()); Replaceall(tm, "$result", "ST(argvi)"); Printf(f->code, "%s\n", tm); Delete(tm); } } Printv(f->code, "XSRETURN(argvi);\n" "fail:;\n", cleanup, "}\n" "SWIG_croak_null();\n" "}\n", NIL); /* Add the dXSARGS last */ Wrapper_add_local(f, "dXSARGS", "dXSARGS"); /* Substitute the cleanup code */ Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(f->code, "$symname", iname); /* Dump the wrapper function */ Wrapper_print(f, f_wrappers); /* Now register the function */ if (!Getattr(n, "sym:overloaded")) { Printf(command_tab, "{\"%s::%s\", %s},\n", cmodule, iname, wname); } else if (!Getattr(n, "sym:nextSibling")) { /* Generate overloaded dispatch function */ int maxargs; bool check_emitted = false; String *dispatch = Swig_overload_dispatch_cast(n, "PUSHMARK(MARK); SWIG_CALLXS(%s); return;", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *df = NewWrapper(); String *dname = Swig_name_wrapper(iname); Printv(df->def, "XS(", dname, ") {\n", NIL); Wrapper_add_local(df, "dXSARGS", "dXSARGS"); Printv(df->code, dispatch, "\n", NIL); Printf(df->code, "croak(\"No matching function for overloaded '%s'\");\n", iname); Printf(df->code, "XSRETURN(0);\n"); Printv(df->code, "}\n", NIL); Wrapper_print(df, f_wrappers); Printf(command_tab, "{\"%s::%s\", %s},\n", cmodule, iname, dname); DelWrapper(df); Delete(dispatch); Delete(dname); } if (!Getattr(n, "sym:nextSibling")) { if (export_all) { Printf(exported, "%s ", iname); } /* -------------------------------------------------------------------- * Create a stub for this function, provided it's not a member function * -------------------------------------------------------------------- */ if ((blessed) && (!member_func)) { Printv(func_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL); } } Delete(cleanup); Delete(outarg); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------ * variableWrapper() * ------------------------------------------------------------ */ virtual int variableWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); Wrapper *getf, *setf; String *tm; String *getname = Swig_name_get(NSPACE_TODO, iname); String *setname = Swig_name_set(NSPACE_TODO, iname); String *get_name = Swig_name_wrapper(getname); String *set_name = Swig_name_wrapper(setname); if (!addSymbol(iname, n)) return SWIG_ERROR; getf = NewWrapper(); setf = NewWrapper(); /* Create a Perl function for setting the variable value */ int assignable = !is_immutable(n); if (assignable) { Setattr(n, "wrap:name", set_name); Printf(setf->def, "SWIGCLASS_STATIC int %s(pTHX_ SV* sv, MAGIC * SWIGUNUSEDPARM(mg)) {\n", set_name); Printv(setf->code, tab4, "MAGIC_PPERL\n", NIL); /* Check for a few typemaps */ tm = Swig_typemap_lookup("varin", n, name, 0); if (tm) { Replaceall(tm, "$input", "sv"); /* Printf(setf->code,"%s\n", tm); */ emit_action_code(n, setf->code, tm); } else { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0)); DelWrapper(setf); DelWrapper(getf); return SWIG_NOWRAP; } Printf(setf->code, "fail:\n"); Printf(setf->code, " return 1;\n}\n"); Replaceall(setf->code, "$symname", iname); Wrapper_print(setf, magic); } /* Now write a function to evaluate the variable */ Setattr(n, "wrap:name", get_name); int addfail = 0; Printf(getf->def, "SWIGCLASS_STATIC int %s(pTHX_ SV *sv, MAGIC *SWIGUNUSEDPARM(mg)) {\n", get_name); Printv(getf->code, tab4, "MAGIC_PPERL\n", NIL); if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { Replaceall(tm, "$result", "sv"); if (is_shadow(t)) { Replaceall(tm, "$shadow", "SWIG_SHADOW"); } else { Replaceall(tm, "$shadow", "0"); } /* Printf(getf->code,"%s\n", tm); */ addfail = emit_action_code(n, getf->code, tm); } else { Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); DelWrapper(setf); DelWrapper(getf); return SWIG_NOWRAP; } Printf(getf->code, " return 1;\n"); if (addfail) { Append(getf->code, "fail:\n"); Append(getf->code, " return 0;\n"); } Append(getf->code, "}\n"); Replaceall(getf->code, "$symname", iname); Wrapper_print(getf, magic); String *tt = Getattr(n, "tmap:varout:type"); if (tt) { tt = NewStringf("&%s", tt); } else { tt = NewString("0"); } /* Now add symbol to the PERL interpreter */ if (!assignable) { Printv(variable_tab, tab4, "{ \"", cmodule, "::", iname, "\", MAGIC_CLASS swig_magic_readonly, MAGIC_CLASS ", get_name, ",", tt, " },\n", NIL); } else { Printv(variable_tab, tab4, "{ \"", cmodule, "::", iname, "\", MAGIC_CLASS ", set_name, ", MAGIC_CLASS ", get_name, ",", tt, " },\n", NIL); } /* If we're blessed, try to figure out what to do with the variable 1. If it's a Perl object of some sort, create a tied-hash around it. 2. Otherwise, just hack Perl's symbol table */ if (blessed) { if (is_shadow(t)) { Printv(var_stubs, "\nmy %__", iname, "_hash;\n", "tie %__", iname, "_hash,\"", is_shadow(t), "\", $", cmodule, "::", iname, ";\n", "$", iname, "= \\%__", iname, "_hash;\n", "bless $", iname, ", ", is_shadow(t), ";\n", NIL); } else { Printv(var_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL); } } if (export_all) Printf(exported, "$%s ", iname); Delete(tt); DelWrapper(setf); DelWrapper(getf); Delete(getname); Delete(setname); Delete(set_name); Delete(get_name); return SWIG_OK; } /* ------------------------------------------------------------ * constantWrapper() * ------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { String *name = Getattr(n, "name"); String *iname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *tm; if (!addSymbol(iname, n)) return SWIG_ERROR; /* Special hook for member pointer */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(iname); Printf(f_wrappers, "static %s = %s;\n", SwigType_str(type, wname), value); value = Char(wname); } if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) { Replaceall(tm, "$value", value); if (is_shadow(type)) { Replaceall(tm, "$shadow", "SWIG_SHADOW"); } else { Replaceall(tm, "$shadow", "0"); } Printf(constant_tab, "%s,\n", tm); } else if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) { Replaceall(tm, "$value", value); if (is_shadow(type)) { Replaceall(tm, "$shadow", "SWIG_SHADOW"); } else { Replaceall(tm, "$shadow", "0"); } Printf(f_init, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); return SWIG_NOWRAP; } if (blessed) { if (is_shadow(type)) { Printv(var_stubs, "\nmy %__", iname, "_hash;\n", "tie %__", iname, "_hash,\"", is_shadow(type), "\", $", cmodule, "::", iname, ";\n", "$", iname, "= \\%__", iname, "_hash;\n", "bless $", iname, ", ", is_shadow(type), ";\n", NIL); } else if (do_constants) { char *dcolon = Strstr(name, "::"); if (!dcolon) { if (Len(const_stubs) == 0 || const_stubs_enum_class != NULL) { Printf(const_stubs, "package %s;\n", namespace_module); const_stubs_enum_class = NULL; } Printv(const_stubs, "sub ", iname, " () { $", cmodule, "::", iname, " }\n", NIL); } else { // C++11 strongly-typed enum. Node *parent = Getattr(n, "parentNode"); if (const_stubs_enum_class != parent) { Printf(const_stubs, "package %s::%s;\n", namespace_module, Getattr(parent, "sym:name")); const_stubs_enum_class = parent; } Printv(const_stubs, "sub ", Getattr(n, "enumvalueDeclaration:sym:name"), " () { $", cmodule, "::", iname, " }\n", NIL); } } else { Printv(var_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL); } } if (export_all) { if (do_constants && !is_shadow(type)) { Printf(exported, "%s ", name); } else { Printf(exported, "$%s ", iname); } } return SWIG_OK; } /* ------------------------------------------------------------ * usage_func() * ------------------------------------------------------------ */ char *usage_func(char *iname, SwigType *, ParmList *l) { static String *temp = 0; Parm *p; int i; if (!temp) temp = NewString(""); Clear(temp); Printf(temp, "%s(", iname); /* Now go through and print parameters */ p = l; i = 0; while (p != 0) { SwigType *pt = Getattr(p, "type"); String *pn = Getattr(p, "name"); if (!checkAttribute(p,"tmap:in:numinputs","0")) { /* If parameter has been named, use that. Otherwise, just print a type */ if (SwigType_type(pt) != T_VOID) { if (Len(pn) > 0) { Printf(temp, "%s", pn); } else { Printf(temp, "%s", SwigType_str(pt, 0)); } } i++; p = nextSibling(p); if (p) if (!checkAttribute(p,"tmap:in:numinputs","0")) Putc(',', temp); } else { p = nextSibling(p); if (p) if ((i > 0) && (!checkAttribute(p,"tmap:in:numinputs","0"))) Putc(',', temp); } } Printf(temp, ");"); return Char(temp); } /* ------------------------------------------------------------ * nativeWrapper() * ------------------------------------------------------------ */ virtual int nativeWrapper(Node *n) { String *name = Getattr(n, "sym:name"); String *funcname = Getattr(n, "wrap:name"); if (!addSymbol(funcname, n)) return SWIG_ERROR; Printf(command_tab, "{\"%s::%s\", %s},\n", cmodule, name, funcname); if (export_all) Printf(exported, "%s ", name); if (blessed) { Printv(func_stubs, "*", name, " = *", cmodule, "::", name, ";\n", NIL); } return SWIG_OK; } /* ---------------------------------------------------------------------------- * OBJECT-ORIENTED FEATURES * * These extensions provide a more object-oriented interface to C++ * classes and structures. The code here is based on extensions * provided by David Fletcher and Gary Holt. * * I have generalized these extensions to make them more general purpose * and to resolve object-ownership problems. * * The approach here is very similar to the Python module : * 1. All of the original methods are placed into a single * package like before except that a 'c' is appended to the * package name. * * 2. All methods and function calls are wrapped with a new * perl function. While possibly inefficient this allows * us to catch complex function arguments (which are hard to * track otherwise). * * 3. Classes are represented as tied-hashes in a manner similar * to Gary Holt's extension. This allows us to access * member data. * * 4. Stand-alone (global) C functions are modified to take * tied hashes as arguments for complex datatypes (if * appropriate). * * 5. Global variables involving a class/struct is encapsulated * in a tied hash. * * ------------------------------------------------------------------------- */ void setclassname(Node *n) { String *symname = Getattr(n, "sym:name"); String *fullname; String *actualpackage; Node *clsmodule = Getattr(n, "module"); if (!clsmodule) { /* imported module does not define a module name. Oh well */ return; } /* Do some work on the class name */ if (verbose > 0) { String *modulename = Getattr(clsmodule, "name"); fprintf(stdout, "setclassname: Found sym:name: %s\n", Char(symname)); fprintf(stdout, "setclassname: Found module: %s\n", Char(modulename)); fprintf(stdout, "setclassname: No package found\n"); } if (dest_package) { fullname = NewStringf("%s::%s", namespace_module, symname); } else { actualpackage = Getattr(clsmodule,"name"); if (verbose > 0) { fprintf(stdout, "setclassname: Found actualpackage: %s\n", Char(actualpackage)); } if ((!compat) && (!Strchr(symname,':'))) { fullname = NewStringf("%s::%s",actualpackage,symname); } else { fullname = NewString(symname); } } if (verbose > 0) { fprintf(stdout, "setclassname: setting proxy: %s\n", Char(fullname)); } Setattr(n, "perl5:proxy", fullname); } /* ------------------------------------------------------------ * classDeclaration() * ------------------------------------------------------------ */ virtual int classDeclaration(Node *n) { /* Do some work on the class name */ if (!Getattr(n, "feature:onlychildren")) { if (blessed) { setclassname(n); Append(classlist, n); } } return Language::classDeclaration(n); } /* ------------------------------------------------------------ * classHandler() * ------------------------------------------------------------ */ virtual int classHandler(Node *n) { if (blessed) { have_constructor = 0; have_operators = 0; have_destructor = 0; have_data_members = 0; operators = NewHash(); class_name = Getattr(n, "sym:name"); if (!addSymbol(class_name, n)) return SWIG_ERROR; /* Use the fully qualified name of the Perl class */ if (!compat) { fullclassname = NewStringf("%s::%s", namespace_module, class_name); } else { fullclassname = NewString(class_name); } real_classname = Getattr(n, "name"); pcode = NewString(""); // blessedmembers = NewString(""); } /* Emit all of the members */ Language::classHandler(n); /* Finish the rest of the class */ if (blessed) { /* Generate a client-data entry */ SwigType *ct = NewStringf("p.%s", real_classname); Printv(f_init, "SWIG_TypeClientData(SWIGTYPE", SwigType_manglestr(ct), ", (void*) \"", fullclassname, "\");\n", NIL); SwigType_remember(ct); Delete(ct); Printv(pm, "\n############# Class : ", fullclassname, " ##############\n", "\npackage ", fullclassname, ";\n", NIL); if (have_operators) { Printf(pm, "use overload\n"); Iterator ki; for (ki = First(operators); ki.key; ki = Next(ki)) { char *name = Char(ki.key); // fprintf(stderr,"found name: <%s>\n", name); if (strstr(name, "__eq__")) { Printv(pm, tab4, "\"==\" => sub { $_[0]->__eq__($_[1])},\n",NIL); } else if (strstr(name, "__ne__")) { Printv(pm, tab4, "\"!=\" => sub { $_[0]->__ne__($_[1])},\n",NIL); // there are no tests for this in operator_overload_runme.pl // it is likely to be broken // } else if (strstr(name, "__assign__")) { // Printv(pm, tab4, "\"=\" => sub { $_[0]->__assign__($_[1])},\n",NIL); } else if (strstr(name, "__str__")) { Printv(pm, tab4, "'\"\"' => sub { $_[0]->__str__()},\n",NIL); } else if (strstr(name, "__plusplus__")) { Printv(pm, tab4, "\"++\" => sub { $_[0]->__plusplus__()},\n",NIL); } else if (strstr(name, "__minmin__")) { Printv(pm, tab4, "\"--\" => sub { $_[0]->__minmin__()},\n",NIL); } else if (strstr(name, "__add__")) { Printv(pm, tab4, "\"+\" => sub { $_[0]->__add__($_[1])},\n",NIL); } else if (strstr(name, "__sub__")) { Printv(pm, tab4, "\"-\" => sub { if( not $_[2] ) { $_[0]->__sub__($_[1]) }\n",NIL); Printv(pm, tab8, "elsif( $_[0]->can('__rsub__') ) { $_[0]->__rsub__($_[1]) }\n",NIL); Printv(pm, tab8, "else { die(\"reverse subtraction not supported\") }\n",NIL); Printv(pm, tab8, "},\n",NIL); } else if (strstr(name, "__mul__")) { Printv(pm, tab4, "\"*\" => sub { $_[0]->__mul__($_[1])},\n",NIL); } else if (strstr(name, "__div__")) { Printv(pm, tab4, "\"/\" => sub { $_[0]->__div__($_[1])},\n",NIL); } else if (strstr(name, "__mod__")) { Printv(pm, tab4, "\"%\" => sub { $_[0]->__mod__($_[1])},\n",NIL); // there are no tests for this in operator_overload_runme.pl // it is likely to be broken // } else if (strstr(name, "__and__")) { // Printv(pm, tab4, "\"&\" => sub { $_[0]->__and__($_[1])},\n",NIL); // there are no tests for this in operator_overload_runme.pl // it is likely to be broken // } else if (strstr(name, "__or__")) { // Printv(pm, tab4, "\"|\" => sub { $_[0]->__or__($_[1])},\n",NIL); } else if (strstr(name, "__gt__")) { Printv(pm, tab4, "\">\" => sub { $_[0]->__gt__($_[1])},\n",NIL); } else if (strstr(name, "__ge__")) { Printv(pm, tab4, "\">=\" => sub { $_[0]->__ge__($_[1])},\n",NIL); } else if (strstr(name, "__not__")) { Printv(pm, tab4, "\"!\" => sub { $_[0]->__not__()},\n",NIL); } else if (strstr(name, "__lt__")) { Printv(pm, tab4, "\"<\" => sub { $_[0]->__lt__($_[1])},\n",NIL); } else if (strstr(name, "__le__")) { Printv(pm, tab4, "\"<=\" => sub { $_[0]->__le__($_[1])},\n",NIL); } else if (strstr(name, "__pluseq__")) { Printv(pm, tab4, "\"+=\" => sub { $_[0]->__pluseq__($_[1])},\n",NIL); } else if (strstr(name, "__mineq__")) { Printv(pm, tab4, "\"-=\" => sub { $_[0]->__mineq__($_[1])},\n",NIL); } else if (strstr(name, "__neg__")) { Printv(pm, tab4, "\"neg\" => sub { $_[0]->__neg__()},\n",NIL); } else { fprintf(stderr,"Unknown operator: %s\n", name); } } Printv(pm, tab4, "\"=\" => sub { my $class = ref($_[0]); $class->new($_[0]) },\n", NIL); Printv(pm, tab4, "\"fallback\" => 1;\n", NIL); } // make use strict happy Printv(pm, "use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);\n", NIL); /* If we are inheriting from a base class, set that up */ Printv(pm, "@ISA = qw(", NIL); /* Handle inheritance */ List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator b; b = First(baselist); while (b.item) { String *bname = Getattr(b.item, "perl5:proxy"); if (!bname) { b = Next(b); continue; } Printv(pm, " ", bname, NIL); b = Next(b); } } /* Module comes last */ if (!compat || Cmp(namespace_module, fullclassname)) { Printv(pm, " ", namespace_module, NIL); } Printf(pm, " );\n"); /* Dump out a hash table containing the pointers that we own */ Printf(pm, "%%OWNER = ();\n"); if (have_data_members || have_destructor) Printf(pm, "%%ITERATORS = ();\n"); /* Dump out the package methods */ Printv(pm, pcode, NIL); Delete(pcode); /* Output methods for managing ownership */ String *director_disown; if (Getattr(n, "perl5:directordisown")) { director_disown = NewStringf("%s%s($self);\n", tab4, Getattr(n, "perl5:directordisown")); } else { director_disown = NewString(""); } Printv(pm, "sub DISOWN {\n", tab4, "my $self = shift;\n", director_disown, tab4, "my $ptr = tied(%$self);\n", tab4, "delete $OWNER{$ptr};\n", "}\n\n", "sub ACQUIRE {\n", tab4, "my $self = shift;\n", tab4, "my $ptr = tied(%$self);\n", tab4, "$OWNER{$ptr} = 1;\n", "}\n\n", NIL); Delete(director_disown); /* Only output the following methods if a class has member data */ Delete(operators); operators = 0; if (Swig_directorclass(n)) { /* director classes need a way to recover subclass instance attributes */ Node *get_attr = NewHash(); String *mrename; String *symname = Getattr(n, "sym:name"); mrename = Swig_name_disown(NSPACE_TODO, symname); Replaceall(mrename, "disown", "swig_get_attr"); String *type = NewString(getClassType()); String *name = NewString("self"); SwigType_add_pointer(type); Parm *p = NewParm(type, name, n); Delete(name); Delete(type); type = NewString("SV"); SwigType_add_pointer(type); String *action = NewString(""); Printv(action, "{\n", " Swig::Director *director = SWIG_DIRECTOR_CAST(arg1);\n", " result = sv_newmortal();\n" " if (director) sv_setsv(result, director->swig_get_self());\n", "}\n", NIL); Setfile(get_attr, Getfile(n)); Setline(get_attr, Getline(n)); Setattr(get_attr, "wrap:action", action); Setattr(get_attr, "name", mrename); Setattr(get_attr, "sym:name", mrename); Setattr(get_attr, "type", type); Setattr(get_attr, "parms", p); Delete(action); Delete(type); Delete(p); member_func = 1; functionWrapper(get_attr); member_func = 0; Delete(get_attr); Printv(pm, "sub FETCH {\n", tab4, "my ($self,$field) = @_;\n", tab4, "my $member_func = \"swig_${field}_get\";\n", tab4, "if (not $self->can($member_func)) {\n", tab8, "my $h = ", cmodule, "::", mrename, "($self);\n", tab8, "return $h->{$field} if $h;\n", tab4, "}\n", tab4, "return $self->$member_func;\n", "}\n", "\n", "sub STORE {\n", tab4, "my ($self,$field,$newval) = @_;\n", tab4, "my $member_func = \"swig_${field}_set\";\n", tab4, "if (not $self->can($member_func)) {\n", tab8, "my $h = ", cmodule, "::", mrename, "($self);\n", tab8, "return $h->{$field} = $newval if $h;\n", tab4, "}\n", tab4, "return $self->$member_func($newval);\n", "}\n", NIL); Delete(mrename); } } return SWIG_OK; } /* ------------------------------------------------------------ * memberfunctionHandler() * ------------------------------------------------------------ */ virtual int memberfunctionHandler(Node *n) { String *symname = Getattr(n, "sym:name"); member_func = 1; Language::memberfunctionHandler(n); member_func = 0; if ((blessed) && (!Getattr(n, "sym:nextSibling"))) { if (Strstr(symname, "__eq__")) { SetInt(operators, "__eq__", 1); have_operators = 1; } else if (Strstr(symname, "__ne__")) { SetInt(operators, "__ne__", 1); have_operators = 1; } else if (Strstr(symname, "__assign__")) { SetInt(operators, "__assign__", 1); have_operators = 1; } else if (Strstr(symname, "__str__")) { SetInt(operators, "__str__", 1); have_operators = 1; } else if (Strstr(symname, "__add__")) { SetInt(operators, "__add__", 1); have_operators = 1; } else if (Strstr(symname, "__sub__")) { SetInt(operators, "__sub__", 1); have_operators = 1; } else if (Strstr(symname, "__mul__")) { SetInt(operators, "__mul__", 1); have_operators = 1; } else if (Strstr(symname, "__div__")) { SetInt(operators, "__div__", 1); have_operators = 1; } else if (Strstr(symname, "__mod__")) { SetInt(operators, "__mod__", 1); have_operators = 1; } else if (Strstr(symname, "__and__")) { SetInt(operators, "__and__", 1); have_operators = 1; } else if (Strstr(symname, "__or__")) { SetInt(operators, "__or__", 1); have_operators = 1; } else if (Strstr(symname, "__not__")) { SetInt(operators, "__not__", 1); have_operators = 1; } else if (Strstr(symname, "__gt__")) { SetInt(operators, "__gt__", 1); have_operators = 1; } else if (Strstr(symname, "__ge__")) { SetInt(operators, "__ge__", 1); have_operators = 1; } else if (Strstr(symname, "__lt__")) { SetInt(operators, "__lt__", 1); have_operators = 1; } else if (Strstr(symname, "__le__")) { SetInt(operators, "__le__", 1); have_operators = 1; } else if (Strstr(symname, "__neg__")) { SetInt(operators, "__neg__", 1); have_operators = 1; } else if (Strstr(symname, "__plusplus__")) { SetInt(operators, "__plusplus__", 1); have_operators = 1; } else if (Strstr(symname, "__minmin__")) { SetInt(operators, "__minmin__", 1); have_operators = 1; } else if (Strstr(symname, "__mineq__")) { SetInt(operators, "__mineq__", 1); have_operators = 1; } else if (Strstr(symname, "__pluseq__")) { SetInt(operators, "__pluseq__", 1); have_operators = 1; } if (Getattr(n, "feature:shadow")) { String *plcode = perlcode(Getattr(n, "feature:shadow"), 0); String *plaction = NewStringf("%s::%s", cmodule, Swig_name_member(NSPACE_TODO, class_name, symname)); Replaceall(plcode, "$action", plaction); Delete(plaction); Printv(pcode, plcode, NIL); } else { Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL); } } return SWIG_OK; } /* ------------------------------------------------------------ * membervariableHandler() * * Adds an instance member. * ----------------------------------------------------------------------------- */ virtual int membervariableHandler(Node *n) { String *symname = Getattr(n, "sym:name"); /* SwigType *t = Getattr(n,"type"); */ /* Emit a pair of get/set functions for the variable */ member_func = 1; Language::membervariableHandler(n); member_func = 0; if (blessed) { Printv(pcode, "*swig_", symname, "_get = *", cmodule, "::", Swig_name_get(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)), ";\n", NIL); Printv(pcode, "*swig_", symname, "_set = *", cmodule, "::", Swig_name_set(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)), ";\n", NIL); /* Now we need to generate a little Perl code for this */ /* if (is_shadow(t)) { *//* This is a Perl object that we have already seen. Add an entry to the members list *//* Printv(blessedmembers, tab4, symname, " => '", is_shadow(t), "',\n", NIL); } */ } have_data_members++; return SWIG_OK; } /* ------------------------------------------------------------ * constructorDeclaration() * * Emits a blessed constructor for our class. In addition to our construct * we manage a Perl hash table containing all of the pointers created by * the constructor. This prevents us from accidentally trying to free * something that wasn't necessarily allocated by malloc or new * ------------------------------------------------------------ */ virtual int constructorHandler(Node *n) { String *symname = Getattr(n, "sym:name"); member_func = 1; Swig_save("perl5:constructorHandler", n, "parms", NIL); if (Swig_directorclass(n)) { Parm *parms = Getattr(n, "parms"); Parm *self; String *name = NewString("self"); String *type = NewString("SV"); SwigType_add_pointer(type); self = NewParm(type, name, n); Delete(type); Delete(name); Setattr(self, "lname", "O"); if (parms) set_nextSibling(self, parms); Setattr(n, "parms", self); Setattr(n, "wrap:self", "1"); Setattr(n, "hidden", "1"); Delete(self); } String *saved_nc = none_comparison; none_comparison = NewStringf("strcmp(SvPV_nolen(ST(0)), \"%s::%s\") != 0", module, class_name); String *saved_director_prot_ctor_code = director_prot_ctor_code; director_prot_ctor_code = NewStringf("if ($comparison) { /* subclassed */\n" " $director_new\n" "} else {\n" "SWIG_exception_fail(SWIG_RuntimeError, \"accessing abstract class or protected constructor\");\n" "}\n"); Language::constructorHandler(n); Delete(none_comparison); none_comparison = saved_nc; Delete(director_prot_ctor_code); director_prot_ctor_code = saved_director_prot_ctor_code; Swig_restore(n); if ((blessed) && (!Getattr(n, "sym:nextSibling"))) { if (Getattr(n, "feature:shadow")) { String *plcode = perlcode(Getattr(n, "feature:shadow"), 0); String *plaction = NewStringf("%s::%s", module, Swig_name_member(NSPACE_TODO, class_name, symname)); Replaceall(plcode, "$action", plaction); Delete(plaction); Printv(pcode, plcode, NIL); } else { if ((Cmp(symname, class_name) == 0)) { /* Emit a blessed constructor */ Printf(pcode, "sub new {\n"); } else { /* Constructor doesn't match classname so we'll just use the normal name */ Printv(pcode, "sub ", Swig_name_construct(NSPACE_TODO, symname), " {\n", NIL); } const char *pkg = getCurrentClass() && Swig_directorclass(getCurrentClass())? "$_[0]" : "shift"; Printv(pcode, tab4, "my $pkg = ", pkg, ";\n", tab4, "my $self = ", cmodule, "::", Swig_name_construct(NSPACE_TODO, symname), "(@_);\n", tab4, "bless $self, $pkg if defined($self);\n", "}\n\n", NIL); have_constructor = 1; } } member_func = 0; return SWIG_OK; } /* ------------------------------------------------------------ * destructorHandler() * ------------------------------------------------------------ */ virtual int destructorHandler(Node *n) { String *symname = Getattr(n, "sym:name"); member_func = 1; Language::destructorHandler(n); if (blessed) { if (Getattr(n, "feature:shadow")) { String *plcode = perlcode(Getattr(n, "feature:shadow"), 0); String *plaction = NewStringf("%s::%s", module, Swig_name_member(NSPACE_TODO, class_name, symname)); Replaceall(plcode, "$action", plaction); Delete(plaction); Printv(pcode, plcode, NIL); } else { Printv(pcode, "sub DESTROY {\n", tab4, "return unless $_[0]->isa('HASH');\n", tab4, "my $self = tied(%{$_[0]});\n", tab4, "return unless defined $self;\n", tab4, "delete $ITERATORS{$self};\n", tab4, "if (exists $OWNER{$self}) {\n", tab8, cmodule, "::", Swig_name_destroy(NSPACE_TODO, symname), "($self);\n", tab8, "delete $OWNER{$self};\n", tab4, "}\n}\n\n", NIL); have_destructor = 1; } } member_func = 0; return SWIG_OK; } /* ------------------------------------------------------------ * staticmemberfunctionHandler() * ------------------------------------------------------------ */ virtual int staticmemberfunctionHandler(Node *n) { member_func = 1; Language::staticmemberfunctionHandler(n); member_func = 0; if ((blessed) && (!Getattr(n, "sym:nextSibling"))) { String *symname = Getattr(n, "sym:name"); Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL); } return SWIG_OK; } /* ------------------------------------------------------------ * staticmembervariableHandler() * ------------------------------------------------------------ */ virtual int staticmembervariableHandler(Node *n) { Language::staticmembervariableHandler(n); if (blessed) { String *symname = Getattr(n, "sym:name"); Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL); } return SWIG_OK; } /* ------------------------------------------------------------ * memberconstantHandler() * ------------------------------------------------------------ */ virtual int memberconstantHandler(Node *n) { String *symname = Getattr(n, "sym:name"); int oldblessed = blessed; /* Create a normal constant */ blessed = 0; Language::memberconstantHandler(n); blessed = oldblessed; if (blessed) { Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL); } return SWIG_OK; } /* ------------------------------------------------------------ * pragma() * * Pragma directive. * * %pragma(perl5) code="String" # Includes a string in the .pm file * %pragma(perl5) include="file.pl" # Includes a file in the .pm file * ------------------------------------------------------------ */ virtual int pragmaDirective(Node *n) { String *lang; String *code; String *value; if (!ImportMode) { lang = Getattr(n, "lang"); code = Getattr(n, "name"); value = Getattr(n, "value"); if (Strcmp(lang, "perl5") == 0) { if (Strcmp(code, "code") == 0) { /* Dump the value string into the .pm file */ if (value) { Printf(pragma_include, "%s\n", value); } } else if (Strcmp(code, "include") == 0) { /* Include a file into the .pm file */ if (value) { FILE *f = Swig_include_open(value); if (!f) { Swig_error(input_file, line_number, "Unable to locate file %s\n", value); } else { char buffer[4096]; while (fgets(buffer, 4095, f)) { Printf(pragma_include, "%s", buffer); } fclose(f); } } } else { Swig_error(input_file, line_number, "Unrecognized pragma.\n"); } } } return Language::pragmaDirective(n); } /* ------------------------------------------------------------ * perlcode() - Output perlcode code into the shadow file * ------------------------------------------------------------ */ String *perlcode(String *code, const String *indent) { String *out = NewString(""); String *temp; char *t; if (!indent) indent = ""; temp = NewString(code); t = Char(temp); if (*t == '{') { Delitem(temp, 0); Delitem(temp, DOH_END); } /* Split the input text into lines */ List *clist = SplitLines(temp); Delete(temp); int initial = 0; String *s = 0; Iterator si; /* Get the initial indentation */ for (si = First(clist); si.item; si = Next(si)) { s = si.item; if (Len(s)) { char *c = Char(s); while (*c) { if (!isspace(*c)) break; initial++; c++; } if (*c && !isspace(*c)) break; else { initial = 0; } } } while (si.item) { s = si.item; if (Len(s) > initial) { char *c = Char(s); c += initial; Printv(out, indent, c, "\n", NIL); } else { Printv(out, "\n", NIL); } si = Next(si); } Delete(clist); return out; } /* ------------------------------------------------------------ * insertDirective() * * Hook for %insert directive. * ------------------------------------------------------------ */ virtual int insertDirective(Node *n) { String *code = Getattr(n, "code"); String *section = Getattr(n, "section"); if ((!ImportMode) && (Cmp(section, "perl") == 0)) { Printv(additional_perl_code, code, NIL); } else { Language::insertDirective(n); } return SWIG_OK; } String *runtimeCode() { String *s = NewString(""); String *shead = Swig_include_sys("perlhead.swg"); if (!shead) { Printf(stderr, "*** Unable to open 'perlhead.swg'\n"); } else { Append(s, shead); Delete(shead); } String *serrors = Swig_include_sys("perlerrors.swg"); if (!serrors) { Printf(stderr, "*** Unable to open 'perlerrors.swg'\n"); } else { Append(s, serrors); Delete(serrors); } String *srun = Swig_include_sys("perlrun.swg"); if (!srun) { Printf(stderr, "*** Unable to open 'perlrun.swg'\n"); } else { Append(s, srun); Delete(srun); } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigperlrun.h"); } virtual int classDirectorInit(Node *n) { String *declaration = Swig_director_declaration(n); Printf(f_directors_h, "\n"); Printf(f_directors_h, "%s\n", declaration); Printf(f_directors_h, "public:\n"); Delete(declaration); return Language::classDirectorInit(n); } virtual int classDirectorEnd(Node *n) { if (dirprot_mode()) { /* This implementation uses a std::map. It should be possible to rewrite it using a more elegant way, like copying the Java approach for the 'override' array. But for now, this seems to be the least intrusive way. */ Printf(f_directors_h, "\n"); Printf(f_directors_h, "/* Internal director utilities */\n"); Printf(f_directors_h, "public:\n"); Printf(f_directors_h, " bool swig_get_inner(const char *swig_protected_method_name) const {\n"); Printf(f_directors_h, " std::map::const_iterator iv = swig_inner.find(swig_protected_method_name);\n"); Printf(f_directors_h, " return (iv != swig_inner.end() ? iv->second : false);\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, " void swig_set_inner(const char *swig_protected_method_name, bool swig_val) const {\n"); Printf(f_directors_h, " swig_inner[swig_protected_method_name] = swig_val;\n"); Printf(f_directors_h, " }\n"); Printf(f_directors_h, "private:\n"); Printf(f_directors_h, " mutable std::map swig_inner;\n"); } Printf(f_directors_h, "};\n"); return Language::classDirectorEnd(n); } virtual int classDirectorConstructor(Node *n) { Node *parent = Getattr(n, "parentNode"); String *sub = NewString(""); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *classname = NewString(""); Printf(classname, "SwigDirector_%s", supername); /* insert self parameter */ Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("SV"); SwigType_add_pointer(type); p = NewParm(type, NewString("self"), n); set_nextSibling(p, parms); parms = p; if (!Getattr(n, "defaultargs")) { /* constructor */ { Wrapper *w = NewWrapper(); String *call; String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, classname, parms, 0); call = Swig_csuperclass_call(0, basetype, superparms); Printf(w->def, "%s::%s: %s, Swig::Director(self) { \n", classname, target, call); Printf(w->def, " SWIG_DIRECTOR_RGTR((%s *)this, this); \n", basetype); Append(w->def, "}\n"); Delete(target); Wrapper_print(w, f_directors); Delete(call); DelWrapper(w); } /* constructor header */ { String *target = Swig_method_decl(0, decl, classname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } Delete(sub); Delete(classname); Delete(supername); Delete(parms); return Language::classDirectorConstructor(n); } virtual int classDirectorMethod(Node *n, Node *parent, String *super) { int is_void = 0; int is_pointer = 0; String *decl = Getattr(n, "decl"); String *name = Getattr(n, "name"); String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *symname = Getattr(n, "sym:name"); String *declaration = NewString(""); ParmList *l = Getattr(n, "parms"); Wrapper *w = NewWrapper(); String *tm; String *wrap_args = NewString(""); SwigType *returntype = Getattr(n, "type"); String *value = Getattr(n, "value"); String *storage = Getattr(n, "storage"); bool pure_virtual = false; int status = SWIG_OK; int idx; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { pure_virtual = true; } } /* determine if the method returns a pointer */ is_pointer = SwigType_ispointer_return(decl); is_void = (!Cmp(returntype, "void") && !is_pointer); /* virtual method definition */ String *target; String *pclassname = NewStringf("SwigDirector_%s", classname); String *qualified_name = NewStringf("%s::%s", pclassname, name); SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); /* header declaration */ target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Get any exception classes in the throws typemap if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = 0; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { Parm *p; int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } String *str = SwigType_str(Getattr(p, "type"), 0); Append(w->def, str); Append(declaration, str); Delete(str); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); /* declare method return value * if the return value is a reference or const reference, a specialized typemap must * handle it, including declaration of c_result ($result). */ if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (!is_void && !ignored_method) { String *pres = NewStringf("SV *%s", Swig_cresult_name()); Wrapper_add_local(w, Swig_cresult_name(), pres); Delete(pres); } if (ignored_method) { if (!pure_virtual) { if (!is_void) Printf(w->code, "return "); String *super_call = Swig_method_call(super, l); Printf(w->code, "%s;\n", super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); } } else { /* attach typemaps to arguments (C/C++ -> Perl) */ String *parse_args = NewString(""); String *pstack = NewString(""); Swig_director_parms_fixup(l); /* remove the wrapper 'w' since it was producing spurious temps */ Swig_typemap_attach_parms("in", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("directorargout", l, w); Wrapper_add_local(w, "SP", "dSP"); { String *ptype = Copy(getClassType()); SwigType_add_pointer(ptype); String *mangle = SwigType_manglestr(ptype); Wrapper_add_local(w, "swigself", "SV *swigself"); Printf(w->code, "swigself = SWIG_NewPointerObj(SWIG_as_voidptr(this), SWIGTYPE%s, SWIG_SHADOW);\n", mangle); Printf(w->code, "sv_bless(swigself, gv_stashpv(swig_get_class(), 0));\n"); Delete(mangle); Delete(ptype); Append(pstack, "XPUSHs(swigself);\n"); } Parm *p; char source[256]; int outputs = 0; if (!is_void) outputs++; /* build argument list and type conversion string */ idx = 0; p = l; while (p) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } if (Getattr(p, "tmap:directorargout") != 0) outputs++; String *pname = Getattr(p, "name"); String *ptype = Getattr(p, "type"); if ((tm = Getattr(p, "tmap:directorin")) != 0) { sprintf(source, "obj%d", idx++); String *input = NewString(source); Setattr(p, "emit:directorinput", input); Replaceall(tm, "$input", input); Delete(input); Replaceall(tm, "$owner", "0"); Replaceall(tm, "$shadow", "0"); /* Wrapper_add_localv(w, source, "SV *", source, "= 0", NIL); */ Printv(wrap_args, "SV *", source, ";\n", NIL); Printv(wrap_args, tm, "\n", NIL); Putc('O', parse_args); Printv(pstack, "XPUSHs(", source, ");\n", NIL); p = Getattr(p, "tmap:directorin:next"); continue; } else if (Cmp(ptype, "void")) { /* special handling for pointers to other C++ director classes. * ideally this would be left to a typemap, but there is currently no * way to selectively apply the dynamic_cast<> to classes that have * directors. in other words, the type "SwigDirector_$1_lname" only exists * for classes with directors. we avoid the problem here by checking * module.wrap::directormap, but it's not clear how to get a typemap to * do something similar. perhaps a new default typemap (in addition * to SWIGTYPE) called DIRECTORTYPE? */ if (SwigType_ispointer(ptype) || SwigType_isreference(ptype)) { Node *module = Getattr(parent, "module"); Node *target = Swig_directormap(module, ptype); sprintf(source, "obj%d", idx++); String *nonconst = 0; /* strip pointer/reference --- should move to Swig/stype.c */ String *nptype = NewString(Char(ptype) + 2); /* name as pointer */ String *ppname = Copy(pname); if (SwigType_isreference(ptype)) { Insert(ppname, 0, "&"); } /* if necessary, cast away const since Perl doesn't support it! */ if (SwigType_isconst(nptype)) { nonconst = NewStringf("nc_tmp_%s", pname); String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(ptype, 0), ppname); Wrapper_add_localv(w, nonconst, SwigType_lstr(ptype, 0), nonconst, nonconst_i, NIL); Delete(nonconst_i); Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number, "Target language argument '%s' discards const in director method %s::%s.\n", SwigType_str(ptype, pname), SwigType_namestr(c_classname), SwigType_namestr(name)); } else { nonconst = Copy(ppname); } Delete(nptype); Delete(ppname); String *mangle = SwigType_manglestr(ptype); if (target) { String *director = NewStringf("director_%s", mangle); Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); Wrapper_add_localv(w, source, "SV *", source, "= 0", NIL); Printf(wrap_args, "%s = SWIG_DIRECTOR_CAST(%s);\n", director, nonconst); Printf(wrap_args, "if (!%s) {\n", director); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); Append(wrap_args, "} else {\n"); Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director); Printf(wrap_args, "SvREFCNT_inc((SV *)%s);\n", source); Append(wrap_args, "}\n"); Delete(director); } else { Wrapper_add_localv(w, source, "SV *", source, "= 0", NIL); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); Printf(pstack, "XPUSHs(sv_2mortal(%s));\n", source); } Putc('O', parse_args); Delete(mangle); Delete(nonconst); } else { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_NOWRAP; break; } } p = nextSibling(p); } /* add the method name as a PyString */ String *pyname = Getattr(n, "sym:name"); /* wrap complex arguments to PyObjects */ Printv(w->code, wrap_args, NIL); /* pass the method call on to the Python object */ if (dirprot_mode() && !is_public(n)) { Printf(w->code, "swig_set_inner(\"%s\", true);\n", name); } Append(w->code, "ENTER;\n"); Append(w->code, "SAVETMPS;\n"); Append(w->code, "PUSHMARK(SP);\n"); Append(w->code, pstack); Delete(pstack); Append(w->code, "PUTBACK;\n"); Printf(w->code, "call_method(\"%s\", G_EVAL | G_SCALAR);\n", pyname); if (dirprot_mode() && !is_public(n)) Printf(w->code, "swig_set_inner(\"%s\", false);\n", name); /* exception handling */ tm = Swig_typemap_lookup("director:except", n, Swig_cresult_name(), 0); if (!tm) { tm = Getattr(n, "feature:director:except"); if (tm) tm = Copy(tm); } Append(w->code, "if (SvTRUE(ERRSV)) {\n"); Append(w->code, " PUTBACK;\n FREETMPS;\n LEAVE;\n"); if ((tm) && Len(tm) && (Strcmp(tm, "1") != 0)) { Replaceall(tm, "$error", "ERRSV"); Printv(w->code, Str(tm), "\n", NIL); } else { Printf(w->code, " Swig::DirectorMethodException::raise(ERRSV);\n"); } Append(w->code, "}\n"); Delete(tm); /* * Python method may return a simple object, or a tuple. * for in/out arguments, we have to extract the appropriate PyObjects from the tuple, * then marshal everything back to C/C++ (return value and output arguments). * */ /* marshal return value and other outputs (if any) from PyObject to C/C++ type */ String *cleanup = NewString(""); String *outarg = NewString(""); if (outputs > 1) { Wrapper_add_local(w, "output", "SV *output"); Printf(w->code, "if (count != %d) {\n", outputs); Printf(w->code, " Swig::DirectorTypeMismatchException::raise(\"Perl method %s.%sfailed to return a list.\");\n", classname, pyname); Append(w->code, "}\n"); } idx = 0; /* marshal return value */ if (!is_void) { Append(w->code, "SPAGAIN;\n"); Printf(w->code, "%s = POPs;\n", Swig_cresult_name()); tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w); if (tm != 0) { if (outputs > 1) { Printf(w->code, "output = POPs;\n"); Replaceall(tm, "$input", "output"); } else { Replaceall(tm, "$input", Swig_cresult_name()); } char temp[24]; sprintf(temp, "%d", idx); Replaceall(tm, "$argnum", temp); /* TODO check this */ if (Getattr(n, "wrap:disown")) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Replaceall(tm, "$result", "c_result"); Printv(w->code, tm, "\n", NIL); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_ERROR; } } /* marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { if (outputs > 1) { Printf(w->code, "output = POPs;\n"); Replaceall(tm, "$result", "output"); } else { Replaceall(tm, "$result", Swig_cresult_name()); } Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } Delete(parse_args); Delete(cleanup); Delete(outarg); } if (!ignored_method) { Append(w->code, "PUTBACK;\n"); Append(w->code, "FREETMPS;\n"); Append(w->code, "LEAVE;\n"); } if (!is_void) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { Printf(w->code, "return (%s) c_result;\n", rettype); } else { Printf(w->code, "return (%s) *c_result;\n", rettype); } Delete(rettype); } } Append(w->code, "}\n"); // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewString(""); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK) { Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } /* clean up */ Delete(wrap_args); Delete(pclassname); DelWrapper(w); return status; } int classDirectorDisown(Node *n) { int rv; member_func = 1; rv = Language::classDirectorDisown(n); member_func = 0; if (rv == SWIG_OK && Swig_directorclass(n)) { String *symname = Getattr(n, "sym:name"); String *disown = Swig_name_disown(NSPACE_TODO, symname); Setattr(n, "perl5:directordisown", NewStringf("%s::%s", cmodule, disown)); } return rv; } int classDirectorDestructor(Node *n) { /* TODO: it would be nice if this didn't have to copy the body of Language::classDirectorDestructor() */ String *DirectorClassName = directorClassName(getCurrentClass()); String *body = NewString("\n"); String *ptype = Copy(getClassType()); SwigType_add_pointer(ptype); String *mangle = SwigType_manglestr(ptype); Printv(body, tab4, "dSP;\n", tab4, "SV *self = SWIG_NewPointerObj(SWIG_as_voidptr(this), SWIGTYPE", mangle, ", SWIG_SHADOW);\n", tab4, "\n", tab4, "sv_bless(self, gv_stashpv(swig_get_class(), 0));\n", tab4, "ENTER;\n", tab4, "SAVETMPS;\n", tab4, "PUSHMARK(SP);\n", tab4, "XPUSHs(self);\n", tab4, "XPUSHs(&PL_sv_yes);\n", tab4, "PUTBACK;\n", tab4, "call_method(\"DESTROY\", G_EVAL | G_VOID);\n", tab4, "FREETMPS;\n", tab4, "LEAVE;\n", NIL); Delete(mangle); Delete(ptype); if (Getattr(n, "noexcept")) { Printf(f_directors_h, " virtual ~%s() noexcept;\n", DirectorClassName); Printf(f_directors, "%s::~%s() noexcept {%s}\n\n", DirectorClassName, DirectorClassName, body); } else if (Getattr(n, "throw")) { Printf(f_directors_h, " virtual ~%s() throw();\n", DirectorClassName); Printf(f_directors, "%s::~%s() throw() {%s}\n\n", DirectorClassName, DirectorClassName, body); } else { Printf(f_directors_h, " virtual ~%s();\n", DirectorClassName); Printf(f_directors, "%s::~%s() {%s}\n\n", DirectorClassName, DirectorClassName, body); } return SWIG_OK; } }; /* ----------------------------------------------------------------------------- * swig_perl5() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_perl5() { return new PERL5(); } extern "C" Language *swig_perl5(void) { return new_swig_perl5(); } swig-4.4.0/Source/Modules/emit.cxx0000664000175000017500000003702515075443613016721 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * emit.cxx * * Useful functions for emitting various pieces of code. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" /* ----------------------------------------------------------------------------- * emit_return_variable() * * Emits a variable declaration for a function return value. * The variable name is always called result. * n => Node of the method being wrapped * rt => the return type * f => the wrapper to generate code into * ----------------------------------------------------------------------------- */ void emit_return_variable(Node *n, SwigType *rt, Wrapper *f) { if (!GetFlag(n, "tmap:out:optimal")) { if (rt && (SwigType_type(rt) != T_VOID)) { SwigType *vt = cplus_value_type(rt); SwigType *tt = vt ? vt : rt; SwigType *lt = SwigType_ltype(tt); String *lstr = SwigType_str(lt, Swig_cresult_name()); if (SwigType_ispointer(lt)) { Wrapper_add_localv(f, Swig_cresult_name(), lstr, "= 0", NULL); } else { Wrapper_add_local(f, Swig_cresult_name(), lstr); } if (vt) { Delete(vt); } Delete(lt); Delete(lstr); } } } /* ----------------------------------------------------------------------------- * emit_parameter_variables() * * Emits a list of variable declarations for function parameters. * The variable names are always called arg1, arg2, etc... * l => the parameter list * f => the wrapper to generate code into * ----------------------------------------------------------------------------- */ void emit_parameter_variables(ParmList *l, Wrapper *f) { Parm *p; String *tm; /* Emit function arguments */ Swig_cargs(f, l); /* Attach typemaps to parameters */ Swig_typemap_attach_parms("default", l, f); Swig_typemap_attach_parms("arginit", l, f); /* Apply the arginit and default */ p = l; while (p) { tm = Getattr(p, "tmap:arginit"); if (tm) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:arginit:next"); } else { p = nextSibling(p); } } /* Apply the default typemap */ p = l; while (p) { tm = Getattr(p, "tmap:default"); if (tm) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:default:next"); } else { p = nextSibling(p); } } } /* ----------------------------------------------------------------------------- * emit_attach_parmmaps() * * Attach the standard parameter related typemaps. * ----------------------------------------------------------------------------- */ void emit_attach_parmmaps(ParmList *l, Wrapper *f) { Swig_typemap_attach_parms("in", l, f); Swig_typemap_attach_parms("typecheck", l, 0); Swig_typemap_attach_parms("argout", l, f); Swig_typemap_attach_parms("check", l, f); Swig_typemap_attach_parms("freearg", l, f); { /* Handle in typemaps with numinputs=0. */ Parm *p = l; while (p) { String *tm = Getattr(p, "tmap:in"); if (tm) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { Printv(f->code, tm, "\n", NIL); } p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } } /* Perform a sanity check on "in" and "freearg" typemaps. These must exactly match to avoid chaos. If a mismatch occurs, we nuke the freearg typemap */ { Parm *p = l; Parm *npin, *npfreearg; while (p) { npin = Getattr(p, "tmap:in:next"); if (Getattr(p, "tmap:freearg")) { npfreearg = Getattr(p, "tmap:freearg:next"); if (npin != npfreearg) { while (p != npin) { Delattr(p, "tmap:freearg"); Delattr(p, "tmap:freearg:next"); p = nextSibling(p); } } } p = npin; } } /* Check for variable length arguments with no input typemap. If no input is defined, we set this to ignore and print a message. */ { Parm *p = l; Parm *lp = 0; while (p) { if (!checkAttribute(p, "tmap:in:numinputs", "0")) { lp = p; p = Getattr(p, "tmap:in:next"); continue; } if (SwigType_isvarargs(Getattr(p, "type"))) { Swig_warning(WARN_LANG_VARARGS, input_file, line_number, "Variable length arguments discarded.\n"); Setattr(p, "tmap:in", ""); } lp = 0; p = nextSibling(p); } /* Check if last input argument is variable length argument */ if (lp) { p = lp; while (p) { if (SwigType_isvarargs(Getattr(p, "type"))) { // Mark the head of the ParmList that it has varargs Setattr(l, "emit:varargs", lp); //Printf(stdout, "setting emit:varargs %s ... %s +++ %s\n", Getattr(l, "emit:varargs"), Getattr(l, "type"), Getattr(p, "type")); break; } p = nextSibling(p); } } } /* * An equivalent type can be used in the typecheck typemap for SWIG to detect the overloading of equivalent * target language types. This is primarily for the smartptr feature, where a pointer and a smart pointer * are seen as equivalent types in the target language. */ { Parm *p = l; while (p) { String *tm = Getattr(p, "tmap:typecheck"); if (tm) { String *equivalent = Getattr(p, "tmap:typecheck:equivalent"); if (equivalent) { String *precedence = Getattr(p, "tmap:typecheck:precedence"); if (precedence && Strcmp(precedence, "0") != 0) Swig_error(Getfile(tm), Getline(tm), "The 'typecheck' typemap for %s contains an 'equivalent' attribute for a 'precedence' that is not set to SWIG_TYPECHECK_POINTER or 0.\n", SwigType_str(Getattr(p, "type"), 0)); SwigType *cpt = Swig_cparse_type(equivalent); if (cpt) { Setattr(p, "equivtype", cpt); Delete(cpt); } else { Swig_error(Getfile(tm), Getline(tm), "Invalid type (%s) in 'equivalent' attribute in 'typecheck' typemap for type %s.\n", equivalent, SwigType_str(Getattr(p, "type"), 0)); } } p = Getattr(p, "tmap:typecheck:next"); } else { p = nextSibling(p); } } } } /* ----------------------------------------------------------------------------- * emit_num_arguments() * * Calculate the total number of arguments. This function is safe for use * with multi-argument typemaps which may change the number of arguments in * strange ways. * ----------------------------------------------------------------------------- */ int emit_num_arguments(ParmList *parms) { Parm *p = parms; int nargs = 0; while (p) { if (Getattr(p, "tmap:in")) { nargs += GetInt(p, "tmap:in:numinputs"); p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } /* DB 04/02/2003: Not sure this is necessary with tmap:in:numinputs */ /* if (parms && (p = Getattr(parms,"emit:varargs"))) { if (!nextSibling(p)) { nargs--; } } */ return nargs; } /* ----------------------------------------------------------------------------- * emit_num_required() * * Computes the number of required arguments. This function is safe for * use with multi-argument typemaps and knows how to skip over everything * properly. Note that parameters with default values are counted unless * the compact default args option is on. * ----------------------------------------------------------------------------- */ int emit_num_required(ParmList *parms) { Parm *p = parms; int nargs = 0; Parm *first_default_arg = 0; int compactdefargs = ParmList_is_compactdefargs(p); while (p) { if (Getattr(p, "tmap:in") && checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } else { if (Getattr(p, "tmap:default")) break; if (Getattr(p, "value")) { if (!first_default_arg) first_default_arg = p; if (compactdefargs) break; } nargs += GetInt(p, "tmap:in:numinputs"); if (Getattr(p, "tmap:in")) { p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } } /* Print error message for non-default arguments following default arguments */ /* The error message is printed more than once with most language modules, this ought to be fixed */ if (first_default_arg) { p = first_default_arg; while (p) { if (Getattr(p, "tmap:in") && checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } else { if (!Getattr(p, "value") && (!Getattr(p, "tmap:default"))) { Swig_error(Getfile(p), Getline(p), "Non-optional argument '%s' follows an optional argument.\n", Getattr(p, "name")); } if (Getattr(p, "tmap:in")) { p = Getattr(p, "tmap:in:next"); } else { p = nextSibling(p); } } } } /* DB 04/02/2003: Not sure this is necessary with tmap:in:numinputs */ /* if (parms && (p = Getattr(parms,"emit:varargs"))) { if (!nextSibling(p)) { nargs--; } } */ return nargs; } /* ----------------------------------------------------------------------------- * emit_isvarargs() * * Checks if a ParmList is a parameter list containing varargs. * This function requires emit_attach_parmmaps to have been called beforehand. * ----------------------------------------------------------------------------- */ int emit_isvarargs(ParmList *p) { if (!p) return 0; if (Getattr(p, "emit:varargs")) return 1; return 0; } /* ----------------------------------------------------------------------------- * emit_isvarargs_function() * * Checks for varargs in a function/constructor (can be overloaded) * ----------------------------------------------------------------------------- */ bool emit_isvarargs_function(Node *n) { bool has_varargs = false; Node *over = Getattr(n, "sym:overloaded"); if (over) { for (Node *sibling = over; sibling; sibling = Getattr(sibling, "sym:nextSibling")) { if (ParmList_has_varargs(Getattr(sibling, "parms"))) { has_varargs = true; break; } } } else { has_varargs = ParmList_has_varargs(Getattr(n, "parms")) ? true : false; } return has_varargs; } /* ----------------------------------------------------------------------------- * void emit_mark_vararg_parms() * * Marks the vararg parameters which are to be ignored. * Vararg parameters are marked as ignored if there is no 'in' varargs (...) * typemap. * ----------------------------------------------------------------------------- */ void emit_mark_varargs(ParmList *l) { Parm *p = l; while (p) { if (SwigType_isvarargs(Getattr(p, "type"))) if (!Getattr(p, "tmap:in")) Setattr(p, "varargs:ignore", "1"); p = nextSibling(p); } } #if 0 /* replace_contract_args. This function replaces argument names in contract specifications. Used in conjunction with the %contract directive. */ static void replace_contract_args(Parm *cp, Parm *rp, String *s) { while (cp && rp) { String *n = Getattr(cp, "name"); if (n) { Replace(s, n, Getattr(rp, "lname"), DOH_REPLACE_ID); } cp = nextSibling(cp); rp = nextSibling(rp); } } #endif /* ----------------------------------------------------------------------------- * int emit_action_code() * * Emits action code for a wrapper. Adds in exception handling code (%exception). * eaction -> the action code to emit * wrappercode -> the emitted code (output) * ----------------------------------------------------------------------------- */ int emit_action_code(Node *n, String *wrappercode, String *eaction) { assert(Getattr(n, "wrap:name")); /* Look for except feature (%exception) */ String *tm = GetFlagAttr(n, "feature:except"); if (tm) tm = Copy(tm); if ((tm) && Len(tm) && (Strcmp(tm, "1") != 0)) { if (Strchr(tm, '$')) { Swig_replace_special_variables(n, parentNode(n), tm); Replaceall(tm, "$action", eaction); } Printv(wrappercode, tm, "\n", NIL); Delete(tm); return 1; } else { Printv(wrappercode, eaction, "\n", NIL); return 0; } } /* ----------------------------------------------------------------------------- * int emit_action() * * Emits the call to the wrapped function. * Adds in exception specification exception handling and %exception code. * ----------------------------------------------------------------------------- */ String *emit_action(Node *n) { String *actioncode = NewStringEmpty(); String *tm; String *action; String *wrap; ParmList *catchlist = Getattr(n, "catchlist"); /* Look for fragments */ { String *fragment = Getattr(n, "feature:fragment"); if (fragment) { char *c, *tok; String *t = Copy(fragment); c = Char(t); tok = strtok(c, ","); while (tok) { String *fname = NewString(tok); Setfile(fname, Getfile(n)); Setline(fname, Getline(n)); Swig_fragment_emit(fname); Delete(fname); tok = strtok(NULL, ","); } Delete(t); } } /* Emit wrapper code (if any) */ wrap = Getattr(n, "wrap:code"); if (wrap && Swig_filebyname("header") != Getattr(n, "wrap:code:done")) { File *f_code = Swig_filebyname("header"); if (f_code) { Printv(f_code, wrap, NIL); } Setattr(n, "wrap:code:done", f_code); } action = Getattr(n, "feature:action"); if (!action) action = Getattr(n, "wrap:action"); assert(action != 0); /* Emit contract code (if any) */ if (Swig_contract_mode_get()) { /* Preassertion */ tm = Getattr(n, "contract:preassert"); if (Len(tm)) { Printv(actioncode, tm, "\n", NIL); } } /* Exception handling code */ /* saves action -> eaction for postcatching exception */ String *eaction = NewString(""); /* If we are in C++ mode and there is an exception specification. We're going to enclose the block in a try block */ if (catchlist) { Printf(eaction, "try {\n"); } String *preaction = Getattr(n, "wrap:preaction"); if (preaction) Printv(eaction, preaction, NIL); Printv(eaction, action, NIL); String *postaction = Getattr(n, "wrap:postaction"); if (postaction) Printv(eaction, postaction, NIL); if (catchlist) { int unknown_catch = 0; int has_varargs = 0; Printf(eaction, "}"); for (Parm *ep = catchlist; ep; ep = nextSibling(ep)) { String *em = Swig_typemap_lookup("throws", ep, "_e", 0); if (em) { SwigType *et = Getattr(ep, "type"); SwigType *etr = SwigType_typedef_resolve_all(et); if (SwigType_isreference(etr) || SwigType_ispointer(etr) || SwigType_isarray(etr)) { Printf(eaction, " catch(%s) {", SwigType_str(et, "_e")); } else if (SwigType_isvarargs(etr)) { Printf(eaction, " catch(...) {"); has_varargs = 1; } else { Printf(eaction, " catch(%s) {", SwigType_str(et, "&_e")); } Printv(eaction, em, "\n", NIL); Printf(eaction, "}"); } else { Swig_warning(WARN_TYPEMAP_THROW, Getfile(n), Getline(n), "No 'throws' typemap defined for exception type '%s'\n", SwigType_str(Getattr(ep, "type"), 0)); unknown_catch = 1; } } if (unknown_catch && !has_varargs) { Printf(eaction, " catch(...) {\nthrow;\n}"); } } /* emit the except feature code */ emit_action_code(n, actioncode, eaction); Delete(eaction); /* Emit contract code (if any) */ if (Swig_contract_mode_get()) { /* Postassertion */ tm = Getattr(n, "contract:postassert"); if (Len(tm)) { Printv(actioncode, tm, "\n", NIL); } } return actioncode; } swig-4.4.0/Source/Modules/nested.cxx0000664000175000017500000002525215075443613017244 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * nested.cxx * * Nested structs support * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" // Nested classes processing section static Hash *classhash = 0; static String *make_name(Node *n, String *name, SwigType *decl) { int destructor = name && (*(Char(name)) == '~'); if (String *yyrename = Getattr(n, "class_rename")) { String *s = NewString(yyrename); Delattr(n, "class_rename"); if (destructor && (*(Char(s)) != '~')) { Insert(s, 0, "~"); } return s; } if (!name) return 0; return Swig_name_make(n, 0, name, decl, 0); } // C version of add_symbols() static void add_symbols_c(Node *n) { String *decl; String *wrn = 0; String *symname = 0; int iscdecl = Cmp(nodeType(n), "cdecl") == 0; Setattr(n, "ismember", "1"); Setattr(n, "access", "public"); if (Getattr(n, "sym:name")) return; decl = Getattr(n, "decl"); if (!SwigType_isfunction(decl)) { String *name = Getattr(n, "name"); String *makename = Getattr(n, "parser:makename"); if (iscdecl) { String *storage = Getattr(n, "storage"); if (Cmp(storage, "typedef") == 0) { Setattr(n, "kind", "typedef"); } else { SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); Setattr(n, "kind", "variable"); if (value && Len(value)) { Setattr(n, "hasvalue", "1"); } if (!type) { Printf(stderr, "notype name %s\n", name); } } } Swig_features_get(Swig_cparse_features(), 0, name, 0, n); if (makename) { symname = make_name(n, makename, 0); Delattr(n, "parser:makename"); /* temporary information, don't leave it hanging around */ } else { makename = name; symname = make_name(n, makename, 0); } if (!symname) { symname = Copy(Getattr(n, "unnamed")); } if (symname) { wrn = Swig_name_warning(n, 0, symname, 0); } } else { String *name = Getattr(n, "name"); SwigType *fdecl = Copy(decl); SwigType *fun = SwigType_pop_function(fdecl); if (iscdecl) { Setattr(n, "kind", "function"); } Swig_features_get(Swig_cparse_features(), 0, name, fun, n); symname = make_name(n, name, fun); wrn = Swig_name_warning(n, 0, symname, fun); Delete(fdecl); Delete(fun); } if (!symname) return; if (GetFlag(n, "feature:ignore")) { /* Only add to C symbol table and continue */ Swig_symbol_add(0, n); } else if (strncmp(Char(symname), "$ignore", 7) == 0) { char *c = Char(symname) + 7; SetFlag(n, "feature:ignore"); if (strlen(c)) { SWIG_WARN_NODE_BEGIN(n); Swig_warning(0, Getfile(n), Getline(n), "%s\n", c + 1); SWIG_WARN_NODE_END(n); } Swig_symbol_add(0, n); } else { Node *c; if ((wrn) && (Len(wrn))) { String *metaname = symname; if (!Getmeta(metaname, "already_warned")) { SWIG_WARN_NODE_BEGIN(n); Swig_warning(0, Getfile(n), Getline(n), "%s\n", wrn); SWIG_WARN_NODE_END(n); Setmeta(metaname, "already_warned", "1"); } } c = Swig_symbol_add(symname, n); if (c != n) { /* symbol conflict attempting to add in the new symbol */ if (Getattr(n, "sym:weak")) { Setattr(n, "sym:name", symname); } else { int inclass = 1; Swig_symbol_conflict_warn(n, c, symname, inclass); } } } Delete(symname); } /* Strips C-style and C++-style comments from string in-place. */ static void strip_comments(char *string) { int state = 0; /* * 0 - not in comment * 1 - in c-style comment * 2 - in c++-style comment * 3 - in string * 4 - after reading / not in comments * 5 - after reading * in c-style comments * 6 - after reading \ in strings */ char *c = string; while (*c) { switch (state) { case 0: if (*c == '\"') state = 3; else if (*c == '/') state = 4; break; case 1: if (*c == '*') state = 5; *c = ' '; break; case 2: if (*c == '\n') state = 0; else *c = ' '; break; case 3: if (*c == '\"') state = 0; else if (*c == '\\') state = 6; break; case 4: if (*c == '/') { *(c - 1) = ' '; *c = ' '; state = 2; } else if (*c == '*') { *(c - 1) = ' '; *c = ' '; state = 1; } else state = 0; break; case 5: if (*c == '/') state = 0; else state = 1; *c = ' '; break; case 6: state = 3; break; } ++c; } } // Create a %insert with a typedef to make a new name visible to C static Node *create_insert(Node *n, bool noTypedef = false) { // format a typedef String *ccode = Getattr(n, "code"); Push(ccode, " "); if (noTypedef) { Push(ccode, Getattr(n, "name")); Push(ccode, " "); Push(ccode, Getattr(n, "kind")); } else { Push(ccode, Getattr(n, "kind")); Push(ccode, "typedef "); Append(ccode, " "); Append(ccode, Getattr(n, "tdname")); } Append(ccode, ";"); /* Strip comments - further code may break in presence of comments. */ strip_comments(Char(ccode)); /* Make all SWIG created typedef structs/unions/classes unnamed else redefinition errors occur - nasty hack alert. */ if (!noTypedef) { const char *types_array[3] = { "struct", "union", "class" }; for (int i = 0; i < 3; i++) { char *code_ptr = Char(ccode); while (code_ptr) { /* Replace struct name (as in 'struct name {...}' ) with whitespace name will be between struct and opening brace */ code_ptr = strstr(code_ptr, types_array[i]); if (code_ptr) { char *open_bracket_pos; code_ptr += strlen(types_array[i]); open_bracket_pos = strchr(code_ptr, '{'); if (open_bracket_pos) { /* Make sure we don't have something like struct A a; */ char *semi_colon_pos = strchr(code_ptr, ';'); if (!(semi_colon_pos && (semi_colon_pos < open_bracket_pos))) while (code_ptr < open_bracket_pos) *code_ptr++ = ' '; } } } } } { /* Remove SWIG directive %constant which may be left in the SWIG created typedefs */ char *code_ptr = Char(ccode); while (code_ptr) { code_ptr = strstr(code_ptr, "%constant"); if (code_ptr) { char *directive_end_pos = strchr(code_ptr, ';'); if (directive_end_pos) { while (code_ptr <= directive_end_pos) *code_ptr++ = ' '; } } } } Node *newnode = NewHash(); set_nodeType(newnode, "insert"); Setfile(newnode, Getfile(n)); Setline(newnode, Getline(n)); String *code = NewStringEmpty(); Wrapper_pretty_print(ccode, code); Setattr(newnode, "code", code); Delete(code); Delattr(n, "code"); return newnode; } static void insertNodeAfter(Node *n, Node *c) { Node *g = parentNode(n); set_parentNode(c, g); Node *ns = nextSibling(n); if (Node *outer = Getattr(c, "nested:outer")) { while (ns && outer == Getattr(ns, "nested:outer")) { n = ns; ns = nextSibling(n); } } if (!ns) { set_lastChild(g, c); } else { set_nextSibling(c, ns); set_previousSibling(ns, c); } set_nextSibling(n, c); set_previousSibling(c, n); } void Swig_nested_name_unnamed_c_structs(Node *n) { if (!n) return; if (!classhash) classhash = Getattr(n, "classes"); Node *c = firstChild(n); while (c) { Node *next = nextSibling(c); if (String *declName = Getattr(c, "nested:unnamed")) { if (Node *outer = Getattr(c, "nested:outer")) { // generate a name String *name = NewStringf("%s_%s", Getattr(outer, "name"), declName); Delattr(c, "nested:unnamed"); // set the name to the class and symbol table Setattr(c, "tdname", name); Setattr(c, "name", name); Swig_symbol_setscope(Getattr(c, "symtab")); Swig_symbol_setscopename(name); // now that we have a name - gather base symbols if (List *publicBases = Getattr(c, "baselist")) { List *bases = Swig_make_inherit_list(name, publicBases, 0); Swig_inherit_base_symbols(bases); Delete(bases); } Setattr(classhash, name, c); // Merge the extension into the symbol table if (Node *am = Getattr(Swig_extend_hash(), name)) { Swig_extend_merge(c, am); Swig_extend_append_previous(c, am); Delattr(Swig_extend_hash(), name); } Swig_symbol_popscope(); // process declarations following this type (assign correct new type) SwigType *ty = Copy(name); Node *decl = nextSibling(c); List *declList = NewList(); while (decl && Getattr(decl, "nested:unnamedtype") == c) { Setattr(decl, "type", ty); Append(declList, decl); Delattr(decl, "nested:unnamedtype"); SetFlag(decl, "feature:immutable"); add_symbols_c(decl); decl = nextSibling(decl); } Delete(ty); Swig_symbol_setscope(Swig_symbol_global_scope()); add_symbols_c(c); Node *ins = create_insert(c); insertNodeAfter(c, ins); removeNode(c); insertNodeAfter(n, c); Delete(ins); Delattr(c, "nested:outer"); } else { // global unnamed struct - ignore it and its instances SetFlag(c, "feature:ignore"); while (next && Getattr(next, "nested:unnamedtype") == c) { SetFlag(next, "feature:ignore"); next = nextSibling(next); } c = next; continue; } } else if (cparse_cplusplusout) { if (Getattr(c, "nested:outer")) { Node *ins = create_insert(c, true); insertNodeAfter(c, ins); Delete(ins); Delattr(c, "nested:outer"); } } // process children Swig_nested_name_unnamed_c_structs(c); c = next; } } static void remove_outer_class_reference(Node *n) { for (Node *c = firstChild(n); c; c = nextSibling(c)) { if (GetFlag(c, "feature:flatnested") || Language::instance()->nestedClassesSupport() == Language::NCS_None) { Delattr(c, "nested:outer"); remove_outer_class_reference(c); } } } void Swig_nested_process_classes(Node *n) { if (!n) return; Node *c = firstChild(n); while (c) { Node *next = nextSibling(c); if (!Getattr(c, "templatetype")) { if (GetFlag(c, "nested") && (GetFlag(c, "feature:flatnested") || Language::instance()->nestedClassesSupport() == Language::NCS_None)) { removeNode(c); if (!checkAttribute(c, "access", "public")) SetFlag(c, "feature:ignore"); else if (Strcmp(nodeType(n),"extend") == 0 && Strcmp(nodeType(parentNode(n)),"class") == 0) insertNodeAfter(parentNode(n), c); else insertNodeAfter(n, c); } Swig_nested_process_classes(c); } c = next; } remove_outer_class_reference(n); } swig-4.4.0/Source/Modules/allocate.cxx0000664000175000017500000015156015075443613017550 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * allocate.cxx * * This module also has two main purposes modifying the parse tree. * * First, it is responsible for adding in using declarations from base class * members into the parse tree. * * Second, after each class declaration, it analyses if the class/struct supports * default constructors and destructors in C++. There are several rules that * define this behavior including pure abstract methods, private sections, * and non-default constructors in base classes. See the ARM or * Doc/Manual/SWIGPlus.html for details. * * Once the analysis is complete, the non-explicit/implied default constructors * and destructors are added to the parse tree. Implied copy constructors are * added too if requested via the copyctor feature. Detection of implied * assignment operators is also handled as assigment is required in the generated * code for variable setters. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" static int virtual_elimination_mode = 0; /* set to 0 on default */ /* Set virtual_elimination_mode */ void Wrapper_virtual_elimination_mode_set(int flag) { virtual_elimination_mode = flag; } /* Helper function to assist with abstract class checking. This is a major hack. Sorry. */ extern "C" { static String *search_decl = 0; /* Declarator being searched */ static Node *check_implemented(Node *n) { String *decl; if (!n) return 0; while (n) { if (Strcmp(nodeType(n), "cdecl") == 0) { decl = Getattr(n, "decl"); if (SwigType_isfunction(decl)) { SwigType *decl1 = SwigType_typedef_resolve_all(decl); SwigType *decl2 = SwigType_pop_function(decl1); if (Strcmp(decl2, search_decl) == 0) { if (!GetFlag(n, "abstract")) { Delete(decl1); Delete(decl2); return n; } } Delete(decl1); Delete(decl2); } } n = Getattr(n, "csym:nextSibling"); } return 0; } } class Allocate:public Dispatcher { Node *inclass; int extendmode; /* Checks if a function, n, is the same as any in the base class, ie if the method is polymorphic. * Also checks for methods which will be hidden (ie a base has an identical non-virtual method). * Both methods must have public access for a match to occur. */ int function_is_defined_in_bases(Node *n, Node *bases) { if (!bases) return 0; String *this_decl = Getattr(n, "decl"); if (!this_decl) return 0; String *name = Getattr(n, "name"); String *this_type = Getattr(n, "type"); String *resolved_decl = SwigType_typedef_resolve_all(this_decl); // Search all base classes for methods with same signature for (int i = 0; i < Len(bases); i++) { Node *b = Getitem(bases, i); Node *base = firstChild(b); while (base) { if (Strcmp(nodeType(base), "extend") == 0) { // Loop through all the %extend methods Node *extend = firstChild(base); while (extend) { if (function_is_defined_in_bases_seek(n, b, extend, this_decl, name, this_type, resolved_decl)) { Delete(resolved_decl); return 1; } extend = nextSibling(extend); } } else if (Strcmp(nodeType(base), "using") == 0) { // Loop through all the using declaration methods Node *usingdecl = firstChild(base); while (usingdecl) { if (function_is_defined_in_bases_seek(n, b, usingdecl, this_decl, name, this_type, resolved_decl)) { Delete(resolved_decl); return 1; } usingdecl = nextSibling(usingdecl); } } else { // normal methods if (function_is_defined_in_bases_seek(n, b, base, this_decl, name, this_type, resolved_decl)) { Delete(resolved_decl); return 1; } } base = nextSibling(base); } } Delete(resolved_decl); resolved_decl = 0; for (int j = 0; j < Len(bases); j++) { Node *b = Getitem(bases, j); if (function_is_defined_in_bases(n, Getattr(b, "allbases"))) return 1; } return 0; } /* Helper function for function_is_defined_in_bases */ int function_is_defined_in_bases_seek(Node *n, Node *b, Node *base, String *this_decl, String *name, String *this_type, String *resolved_decl) { String *base_decl = Getattr(base, "decl"); SwigType *base_type = Getattr(base, "type"); if (base_decl && base_type) { if (checkAttribute(base, "name", name) && !GetFlag(b, "feature:ignore") /* whole class is ignored */ ) { if (SwigType_isfunction(resolved_decl) && SwigType_isfunction(base_decl)) { // We have found a method that has the same name as one in a base class bool covariant_returntype = false; bool returntype_match = Strcmp(base_type, this_type) == 0 ? true : false; bool decl_match = Strcmp(base_decl, this_decl) == 0 ? true : false; if (returntype_match && decl_match) { // Exact match - we have found a method with identical signature // No typedef resolution was done, but skipping it speeds things up slightly } else { // Either we have: // 1) matching methods but are one of them uses a different typedef (return type or parameter) to the one in base class' method // 2) matching polymorphic methods with covariant return type // 3) a non-matching method (ie an overloaded method of some sort) // 4) a matching method which is not polymorphic, ie it hides the base class' method // Check if fully resolved return types match (including // covariant return types) if (!returntype_match) { String *this_returntype = function_return_type(n); String *base_returntype = function_return_type(base); returntype_match = Strcmp(this_returntype, base_returntype) == 0 ? true : false; if (!returntype_match) { covariant_returntype = SwigType_issubtype(this_returntype, base_returntype) ? true : false; returntype_match = covariant_returntype; } Delete(this_returntype); Delete(base_returntype); } // The return types must match at this point, for the whole method to match if (returntype_match && !decl_match) { // Now need to check the parameter list // First do an inexpensive parameter count ParmList *this_parms = Getattr(n, "parms"); ParmList *base_parms = Getattr(base, "parms"); if (ParmList_len(this_parms) == ParmList_len(base_parms)) { // Number of parameters are the same, now check that all the parameters match SwigType *base_fn = NewString(""); SwigType *this_fn = NewString(""); SwigType_add_function(base_fn, base_parms); SwigType_add_function(this_fn, this_parms); base_fn = SwigType_typedef_resolve_all(base_fn); this_fn = SwigType_typedef_resolve_all(this_fn); if (Strcmp(base_fn, this_fn) == 0) { // Finally check that the qualifiers match int base_qualifier = SwigType_isqualifier(resolved_decl); int this_qualifier = SwigType_isqualifier(base_decl); if (base_qualifier == this_qualifier) { decl_match = true; } } Delete(base_fn); Delete(this_fn); } } } //Printf(stderr,"look %s %s %d %d\n",base_decl, this_decl, returntype_match, decl_match); if (decl_match && returntype_match) { // Found an identical method in the base class bool this_wrapping_protected_members = is_member_director(n) ? true : false; // This should really check for dirprot rather than just being a director method bool base_wrapping_protected_members = is_member_director(base) ? true : false; // This should really check for dirprot rather than just being a director method bool both_have_public_access = is_public(n) && is_public(base); bool both_have_protected_access = (is_protected(n) && this_wrapping_protected_members) && (is_protected(base) && base_wrapping_protected_members); bool both_have_private_access = is_private(n) && is_private(base); if (checkAttribute(base, "storage", "virtual")) { // Found a polymorphic method. // Mark the polymorphic method, in case the virtual keyword was not used. Setattr(n, "storage", "virtual"); if (!GetFlag(b, "feature:interface")) { // interface implementation neither hides nor overrides if (both_have_public_access || both_have_protected_access) { if (!is_non_public_base(inclass, b)) Setattr(n, "override", base); // Note C# definition of override, ie access must be the same } else if (!both_have_private_access) { // Different access if (this_wrapping_protected_members || base_wrapping_protected_members) if (!is_non_public_base(inclass, b)) Setattr(n, "hides", base); // Note C# definition of hiding, ie hidden if access is different } } // Try and find the most base's covariant return type SwigType *most_base_covariant_type = Getattr(base, "covariant"); if (!most_base_covariant_type && covariant_returntype) most_base_covariant_type = function_return_type(base, false); if (!most_base_covariant_type) { // Eliminate the derived virtual method. if (virtual_elimination_mode && !is_member_director(n)) if (both_have_public_access) if (!is_non_public_base(inclass, b)) if (!Swig_symbol_isoverloaded(n)) { // Don't eliminate if an overloaded method as this hides the method // in the scripting languages: the dispatch function will hide the base method if ignored. SetFlag(n, "feature:ignore"); SetFlag(n, "fvirtual:ignore"); } } else { // Some languages need to know about covariant return types Setattr(n, "covariant", most_base_covariant_type); } } else { // Found an identical method in the base class, but it is not polymorphic. if (both_have_public_access || both_have_protected_access) if (!is_non_public_base(inclass, b)) Setattr(n, "hides", base); } if (both_have_public_access || both_have_protected_access) return 1; } } } } return 0; } /* Determines whether the base class, b, is in the list of private * or protected base classes for class n. */ bool is_non_public_base(Node *n, Node *b) { bool non_public_base = false; Node *bases = Getattr(n, "privatebases"); if (bases) { for (int i = 0; i < Len(bases); i++) { Node *base = Getitem(bases, i); if (base == b) non_public_base = true; } } bases = Getattr(n, "protectedbases"); if (bases) { for (int i = 0; i < Len(bases); i++) { Node *base = Getitem(bases, i); if (base == b) non_public_base = true; } } return non_public_base; } /* Returns the return type for a function. The node n should be a function. If resolve is true the fully returned type is fully resolved. Caller is responsible for deleting returned string. */ String *function_return_type(Node *n, bool resolve = true) { String *decl = Getattr(n, "decl"); SwigType *type = Getattr(n, "type"); String *ty = NewString(type); SwigType_push(ty, decl); if (SwigType_isqualifier(ty)) Delete(SwigType_pop(ty)); Delete(SwigType_pop_function(ty)); if (resolve) { String *unresolved = ty; ty = SwigType_typedef_resolve_all(unresolved); Delete(unresolved); } return ty; } /* Checks if a class member is the same as inherited from the class bases */ int class_member_is_defined_in_bases(Node *member, Node *classnode) { Node *bases; /* bases is the closest ancestors of classnode */ int defined = 0; bases = Getattr(classnode, "allbases"); if (!bases) return 0; { int old_mode = virtual_elimination_mode; if (is_member_director(classnode, member)) virtual_elimination_mode = 0; if (function_is_defined_in_bases(member, bases)) { defined = 1; } virtual_elimination_mode = old_mode; } if (defined) return 1; else return 0; } /* Checks to see if a class is abstract through inheritance, and saves the first node that seems to be abstract. */ int is_abstract_inherit(Node *n, Node *base = 0, int first = 0) { if (!first && (base == n)) return 0; if (!base) { /* Root node */ Symtab *stab = Getattr(n, "symtab"); /* Get symbol table for node */ Symtab *oldtab = Swig_symbol_setscope(stab); int ret = is_abstract_inherit(n, n, 1); Swig_symbol_setscope(oldtab); return ret; } List *abstracts = Getattr(base, "abstracts"); if (abstracts) { int dabstract = 0; int len = Len(abstracts); for (int i = 0; i < len; i++) { Node *nn = Getitem(abstracts, i); String *name = Getattr(nn, "name"); if (!name) continue; if (Strchr(name, '~')) continue; /* Don't care about destructors */ String *base_decl = Getattr(nn, "decl"); if (base_decl) base_decl = SwigType_typedef_resolve_all(base_decl); if (SwigType_isfunction(base_decl)) search_decl = SwigType_pop_function(base_decl); Node *dn = Swig_symbol_clookup_local_check(name, 0, check_implemented); Delete(search_decl); Delete(base_decl); if (!dn) { List *nabstracts = Getattr(n, "abstracts"); if (!nabstracts) { nabstracts = NewList(); Setattr(n, "abstracts", nabstracts); Delete(nabstracts); } Append(nabstracts, nn); if (!Getattr(n, "abstracts:firstnode")) { Setattr(n, "abstracts:firstnode", nn); } dabstract = base != n; } } if (dabstract) return 1; } List *bases = Getattr(base, "allbases"); if (!bases) return 0; for (int i = 0; i < Len(bases); i++) { if (is_abstract_inherit(n, Getitem(bases, i))) { return 1; } } return 0; } /* Grab methods used by smart pointers */ List *smart_pointer_methods(Node *cls, List *methods, int isconst, String *classname = 0) { if (!methods) { methods = NewList(); } Node *c = firstChild(cls); while (c) { if (Getattr(c, "error") || GetFlag(c, "feature:ignore")) { c = nextSibling(c); continue; } if (!isconst && (Strcmp(nodeType(c), "extend") == 0)) { methods = smart_pointer_methods(c, methods, isconst, Getattr(cls, "name")); } else if (Strcmp(nodeType(c), "cdecl") == 0) { if (!GetFlag(c, "feature:ignore")) { String *storage = Getattr(c, "storage"); if (!((Cmp(storage, "typedef") == 0)) && !Strstr(storage, "friend")) { String *name = Getattr(c, "name"); String *symname = Getattr(c, "sym:name"); Node *e = Swig_symbol_clookup_local(name, 0); if (e && is_public(e) && !GetFlag(e, "feature:ignore") && (Cmp(symname, Getattr(e, "sym:name")) == 0)) { Swig_warning(WARN_LANG_DEREF_SHADOW, Getfile(e), Getline(e), "Declaration of '%s' shadows declaration accessible via operator->(),\n", name); Swig_warning(WARN_LANG_DEREF_SHADOW, Getfile(c), Getline(c), "previous declaration of '%s'.\n", name); } else { /* Make sure node with same name doesn't already exist */ int k; int match = 0; for (k = 0; k < Len(methods); k++) { e = Getitem(methods, k); if (Cmp(symname, Getattr(e, "sym:name")) == 0) { match = 1; break; } if (!Getattr(e, "sym:name") && (Cmp(name, Getattr(e, "name")) == 0)) { match = 1; break; } } if (!match) { Node *cc = c; while (cc) { Node *cp = cc; if (classname) { Setattr(cp, "extendsmartclassname", classname); } Setattr(cp, "allocate:smartpointeraccess", "1"); /* If constant, we have to be careful */ if (isconst) { SwigType *decl = Getattr(cp, "decl"); if (decl) { if (SwigType_isfunction(decl)) { /* If method, we only add if it's a const method */ if (SwigType_isconst(decl)) { Append(methods, cp); } } else { Append(methods, cp); } } else { Append(methods, cp); } } else { Append(methods, cp); } cc = Getattr(cc, "sym:nextSibling"); } } } } } } c = nextSibling(c); } /* Look for methods in base classes */ { Node *bases = Getattr(cls, "bases"); int k; for (k = 0; k < Len(bases); k++) { smart_pointer_methods(Getitem(bases, k), methods, isconst); } } /* Remove protected/private members */ { for (int i = 0; i < Len(methods);) { Node *n = Getitem(methods, i); if (!is_public(n)) { Delitem(methods, i); continue; } i++; } } return methods; } void mark_exception_classes(ParmList *p) { while (p) { SwigType *ty = Getattr(p, "type"); SwigType *t = SwigType_typedef_resolve_all(ty); if (SwigType_isreference(t) || SwigType_ispointer(t) || SwigType_isarray(t)) { Delete(SwigType_pop(t)); } Node *c = Swig_symbol_clookup(t, 0); if (c) { if (!GetFlag(c, "feature:exceptionclass")) { SetFlag(c, "feature:exceptionclass"); } } p = nextSibling(p); Delete(t); } } void process_exceptions(Node *n) { ParmList *catchlist = 0; /* the "catchlist" attribute is used to emit the block try {$action;} catch ; in emit.cxx and is either constructed from the "feature:catches" feature or copied from the node "throws" list. */ String *scatchlist = Getattr(n, "feature:catches"); if (scatchlist) { catchlist = Swig_cparse_parms(scatchlist, n); if (catchlist) { Setattr(n, "catchlist", catchlist); mark_exception_classes(catchlist); Delete(catchlist); } } ParmList *throws = Getattr(n, "throws"); if (throws) { /* if there is no explicit catchlist, we catch everything in the throws list */ if (!catchlist) { Setattr(n, "catchlist", throws); } mark_exception_classes(throws); } } /* ----------------------------------------------------------------------------- * clone_member_for_using_declaration() * * Create a new member (constructor or method) by copying it from member c, ready * for adding as a child to the using declaration node n. * ----------------------------------------------------------------------------- */ Node *clone_member_for_using_declaration(Node *c, Node *n) { Node *parent = parentNode(n); String *decl = Getattr(c, "decl"); String *symname = Getattr(n, "sym:name"); int match = 0; Node *over = Getattr(n, "sym:overloaded"); while (over) { String *odecl = Getattr(over, "decl"); if (Cmp(decl, odecl) == 0) { match = 1; break; } over = Getattr(over, "sym:nextSibling"); } if (match) { /* Don't generate a method if the method is overridden in this class, * for example don't generate another m(bool) should there be a Base::m(bool) : * struct Derived : Base { * void m(bool); * using Base::m; * }; */ return 0; } Node *nn = copyNode(c); Setfile(nn, Getfile(n)); Setline(nn, Getline(n)); if (!Getattr(nn, "sym:name")) Setattr(nn, "sym:name", symname); Symtab *st = Getattr(n, "sym:symtab"); assert(st); Setattr(nn, "sym:symtab", st); // The real parent is the "using" declaration node, but subsequent code generally handles // and expects a class member to point to the parent class node Setattr(nn, "parentNode", parent); if (Equal(nodeType(c), "constructor")) { Setattr(nn, "name", Getattr(n, "name")); Setattr(nn, "sym:name", Getattr(n, "sym:name")); // Note that the added constructor's access is the same as that of // the base class' constructor not of the using declaration. // It has already been set correctly and should not be changed. } else { // Access might be different from the method in the base class Delattr(nn, "access"); Setattr(nn, "access", Getattr(n, "access")); } if (!GetFlag(nn, "feature:ignore")) { ParmList *parms = CopyParmList(Getattr(c, "parms")); int is_pointer = SwigType_ispointer_return(Getattr(nn, "decl")); int is_void = checkAttribute(nn, "type", "void") && !is_pointer; Setattr(nn, "parms", parms); Delete(parms); if (Getattr(n, "feature:extend")) { String *ucode = is_void ? NewStringf("{ self->%s(", Getattr(n, "uname")) : NewStringf("{ return self->%s(", Getattr(n, "uname")); for (ParmList *p = parms; p;) { Append(ucode, Getattr(p, "name")); p = nextSibling(p); if (p) Append(ucode, ","); } Append(ucode, "); }"); Setattr(nn, "code", ucode); Delete(ucode); } ParmList *throw_parm_list = Getattr(c, "throws"); if (throw_parm_list) Setattr(nn, "throws", CopyParmList(throw_parm_list)); } else { Delete(nn); nn = 0; } return nn; } /* ----------------------------------------------------------------------------- * add_member_for_using_declaration() * * Add a new member (constructor or method) by copying it from member c. * Add it into the linked list of members under the using declaration n (ccount, * unodes and last_unodes are used for this). * ----------------------------------------------------------------------------- */ void add_member_for_using_declaration(Node *c, Node *n, int &ccount, Node *&unodes, Node *&last_unodes) { if (GetFlag(c, "fvirtual:ignore")) { // This node was ignored by fvirtual. Thus, it has feature:ignore set. // However, we may have new sibling overrides that will make us want to keep it. // Hence, temporarily unset the feature:ignore flag. UnsetFlag(c, "feature:ignore"); } if (!(Swig_storage_isstatic(c) || checkAttribute(c, "storage", "typedef") || Strstr(Getattr(c, "storage"), "friend") || (Getattr(c, "feature:extend") && !Getattr(c, "code")) || GetFlag(c, "feature:ignore"))) { String *symname = Getattr(n, "sym:name"); String *csymname = Getattr(c, "sym:name"); Node *parent = parentNode(n); bool using_inherited_constructor_symname_okay = Equal(nodeType(c), "constructor") && Equal(symname, Getattr(parent, "sym:name")); if (!csymname || Equal(csymname, symname) || using_inherited_constructor_symname_okay) { Node *nn = clone_member_for_using_declaration(c, n); if (nn) { ccount++; if (!last_unodes) { last_unodes = nn; unodes = nn; } else { Setattr(nn, "previousSibling", last_unodes); Setattr(last_unodes, "nextSibling", nn); Setattr(nn, "sym:previousSibling", last_unodes); Setattr(last_unodes, "sym:nextSibling", nn); Setattr(nn, "sym:overloaded", unodes); Setattr(unodes, "sym:overloaded", unodes); last_unodes = nn; } } } else { Swig_warning(WARN_LANG_USING_NAME_DIFFERENT, Getfile(n), Getline(n), "Using declaration %s, with name '%s', is not actually using\n", SwigType_namestr(Getattr(n, "uname")), symname); Swig_warning(WARN_LANG_USING_NAME_DIFFERENT, Getfile(c), Getline(c), "the method from %s, with name '%s', as the names are different.\n", Swig_name_decl(c), csymname); } } if (GetFlag(c, "fvirtual:ignore")) { SetFlag(c, "feature:ignore"); } } bool is_assignable_type(const SwigType *type) { bool assignable = true; if (SwigType_type(type) == T_USER) { Node *cn = Swig_symbol_clookup(type, 0); if (cn) { if ((Strcmp(nodeType(cn), "class") == 0)) { if (Getattr(cn, "allocate:noassign")) { assignable = false; } } } } else if (SwigType_isarray(type)) { SwigType *array_type = SwigType_array_type(type); assignable = is_assignable_type(array_type); } else if (SwigType_isreference(type) || SwigType_isrvalue_reference(type)) { SwigType *base_type = Copy(type); SwigType_del_element(base_type); assignable = is_assignable_type(base_type); Delete(base_type); } return assignable; } bool is_assignable(Node *n, bool &is_reference, bool &is_const) { SwigType *ty = Copy(Getattr(n, "type")); SwigType_push(ty, Getattr(n, "decl")); SwigType *ftd = SwigType_typedef_resolve_all(ty); SwigType *td = SwigType_strip_qualifiers(ftd); bool assignable = is_assignable_type(td); is_reference = SwigType_isreference(td) || SwigType_isrvalue_reference(td); is_const = !SwigType_ismutable(ftd); if (GetFlag(n, "hasconsttype")) is_const = true; Delete(ty); Delete(ftd); Delete(td); return assignable; } public: Allocate(): inclass(NULL), extendmode(0) { } virtual int top(Node *n) { cplus_mode = PUBLIC; inclass = 0; extendmode = 0; emit_children(n); return SWIG_OK; } virtual int importDirective(Node *n) { return emit_children(n); } virtual int includeDirective(Node *n) { return emit_children(n); } virtual int externDeclaration(Node *n) { return emit_children(n); } virtual int namespaceDeclaration(Node *n) { return emit_children(n); } virtual int extendDirective(Node *n) { extendmode = 1; emit_children(n); extendmode = 0; return SWIG_OK; } virtual int classDeclaration(Node *n) { Symtab *symtab = Swig_symbol_current(); Swig_symbol_setscope(Getattr(n, "symtab")); save_value oldInclass(inclass); save_value oldAcessMode(cplus_mode); save_value oldExtendMode(extendmode); if (Getattr(n, "template")) extendmode = 0; if (!CPlusPlus) { /* Always have default constructors/destructors in C */ Setattr(n, "allocate:default_constructor", "1"); Setattr(n, "allocate:default_destructor", "1"); } if (Getattr(n, "allocate:visit")) { Swig_symbol_setscope(symtab); return SWIG_OK; } Setattr(n, "allocate:visit", "1"); /* Always visit base classes first */ { List *bases = Getattr(n, "bases"); if (bases) { for (int i = 0; i < Len(bases); i++) { Node *b = Getitem(bases, i); classDeclaration(b); } } } inclass = n; String *kind = Getattr(n, "kind"); if (Strcmp(kind, "class") == 0) { cplus_mode = PRIVATE; } else { cplus_mode = PUBLIC; } emit_children(n); /* Check if the class is abstract via inheritance. This might occur if a class didn't have any pure virtual methods of its own, but it didn't implement all of the pure methods in a base class */ if (!Getattr(n, "abstracts") && is_abstract_inherit(n)) { if (((Getattr(n, "allocate:public_constructor") || (!GetFlag(n, "feature:nodefault") && !Getattr(n, "allocate:has_constructor"))))) { if (!GetFlag(n, "feature:notabstract")) { Node *na = Getattr(n, "abstracts:firstnode"); if (na) { Swig_warning(WARN_TYPE_ABSTRACT, Getfile(n), Getline(n), "Class '%s' might be abstract, " "no constructors generated,\n", SwigType_namestr(Getattr(n, "name"))); Swig_warning(WARN_TYPE_ABSTRACT, Getfile(na), Getline(na), "Method %s might not be implemented.\n", Swig_name_decl(na)); if (!Getattr(n, "abstracts")) { List *abstracts = NewList(); Append(abstracts, na); Setattr(n, "abstracts", abstracts); Delete(abstracts); } } } } } if (!Getattr(n, "allocate:has_constructor")) { /* No constructor is defined. We need to check a few things */ /* If class is abstract. No default constructor. Sorry */ if (Getattr(n, "abstracts")) { Delattr(n, "allocate:default_constructor"); } if (!Getattr(n, "allocate:default_constructor")) { // No default constructor if either the default constructor or copy constructor is declared as deleted if (!GetFlag(n, "allocate:deleted_default_constructor") && !GetFlag(n, "allocate:deleted_copy_constructor")) { /* Check base classes */ List *bases = Getattr(n, "allbases"); int allows_default = 1; for (int i = 0; i < Len(bases); i++) { Node *n = Getitem(bases, i); /* If base class does not allow default constructor, we don't allow it either */ if (!Getattr(n, "allocate:default_constructor") && (!Getattr(n, "allocate:default_base_constructor"))) { allows_default = 0; } /* not constructible if base destructor is deleted */ if (Getattr(n, "allocate:deleted_default_destructor")) { allows_default = 0; } } if (allows_default) { Setattr(n, "allocate:default_constructor", "1"); } } } } if (!Getattr(n, "allocate:has_copy_constructor")) { if (Getattr(n, "abstracts")) { Delattr(n, "allocate:copy_constructor"); Delattr(n, "allocate:copy_constructor_non_const"); } if (!Getattr(n, "allocate:copy_constructor")) { // No copy constructor if the copy constructor is declared as deleted if (!GetFlag(n, "allocate:deleted_copy_constructor")) { /* Check base classes */ List *bases = Getattr(n, "allbases"); int allows_copy = 1; int must_be_copy_non_const = 0; for (int i = 0; i < Len(bases); i++) { Node *n = Getitem(bases, i); /* If base class does not allow copy constructor, we don't allow it either */ if (!Getattr(n, "allocate:copy_constructor") && (!Getattr(n, "allocate:copy_base_constructor"))) { allows_copy = 0; } /* not constructible if base destructor is deleted */ if (Getattr(n, "allocate:deleted_default_destructor")) { allows_copy = 0; } if (Getattr(n, "allocate:copy_constructor_non_const") || (Getattr(n, "allocate:copy_base_constructor_non_const"))) { must_be_copy_non_const = 1; } } if (allows_copy) { Setattr(n, "allocate:copy_constructor", "1"); } if (must_be_copy_non_const) { Setattr(n, "allocate:copy_constructor_non_const", "1"); } } } } if (!Getattr(n, "allocate:has_destructor")) { /* No destructor was defined */ /* No destructor if the destructor is declared as deleted */ if (!GetFlag(n, "allocate:deleted_default_destructor")) { /* Check base classes */ List *bases = Getattr(n, "allbases"); int allows_destruct = 1; for (int i = 0; i < Len(bases); i++) { Node *n = Getitem(bases, i); /* If base class does not allow default destructor, we don't allow it either */ if (!Getattr(n, "allocate:default_destructor") && (!Getattr(n, "allocate:default_base_destructor"))) { allows_destruct = 0; } } if (allows_destruct) { Setattr(n, "allocate:default_destructor", "1"); } } } if (!Getattr(n, "allocate:has_assign")) { /* No assignment operator was defined */ List *bases = Getattr(n, "allbases"); int allows_assign = 1; for (int i = 0; i < Len(bases); i++) { Node *n = Getitem(bases, i); /* If base class does not allow assignment, we don't allow it either */ if (Getattr(n, "allocate:noassign")) { allows_assign = 0; } } /* If any member variables are non assignable, this class is also non assignable by default */ if (GetFlag(n, "allocate:has_nonassignable")) { allows_assign = 0; } if (!allows_assign) { Setattr(n, "allocate:noassign", "1"); } } if (!Getattr(n, "allocate:has_new")) { /* No new operator was defined */ List *bases = Getattr(n, "allbases"); int allows_new = 1; for (int i = 0; i < Len(bases); i++) { Node *n = Getitem(bases, i); /* If base class does not allow new operator, we don't allow it either */ if (Getattr(n, "allocate:has_new")) { allows_new = !Getattr(n, "allocate:nonew"); } } if (!allows_new) { Setattr(n, "allocate:nonew", "1"); } } /* Check if base classes allow smart pointers, but might be hidden */ if (!Getattr(n, "allocate:smartpointer")) { Node *sp = Swig_symbol_clookup("operator ->", 0); if (sp) { /* Look for parent */ Node *p = parentNode(sp); if (Strcmp(nodeType(p), "extend") == 0) { p = parentNode(p); } if (Strcmp(nodeType(p), "class") == 0) { if (GetFlag(p, "feature:ignore")) { Setattr(n, "allocate:smartpointer", Getattr(p, "allocate:smartpointer")); } } } } Swig_interface_propagate_methods(n); /* Only care about default behavior. Remove temporary values */ Setattr(n, "allocate:visit", "1"); Swig_symbol_setscope(symtab); /* Now we can add the additional implied constructors and destructors to the parse tree */ if (!ImportMode && !GetFlag(n, "feature:ignore")) { int dir = 0; if (Swig_directors_enabled()) { int ndir = GetFlag(n, "feature:director"); int nndir = GetFlag(n, "feature:nodirector"); /* 'nodirector' has precedence over 'director' */ dir = (ndir || nndir) ? (ndir && !nndir) : 0; } int abstract = !dir && abstractClassTest(n); int odefault = !GetFlag(n, "feature:nodefault"); /* default constructor */ if (!abstract && !GetFlag(n, "feature:nodefaultctor") && odefault) { if (!Getattr(n, "allocate:has_constructor") && Getattr(n, "allocate:default_constructor")) { addDefaultConstructor(n); } } /* copy constructor */ if (CPlusPlus && !abstract && GetFlag(n, "feature:copyctor")) { if (!Getattr(n, "allocate:has_copy_constructor") && Getattr(n, "allocate:copy_constructor")) { addCopyConstructor(n); } } /* default destructor */ if (!GetFlag(n, "feature:nodefaultdtor") && odefault) { if (!Getattr(n, "allocate:has_destructor") && Getattr(n, "allocate:default_destructor")) { addDestructor(n); } } } return SWIG_OK; } virtual int accessDeclaration(Node *n) { cplus_mode = accessModeFromString(Getattr(n, "kind")); return SWIG_OK; } virtual int usingDeclaration(Node *n) { if (GetFlag(n, "feature:ignore")) return SWIG_OK; if (!Getattr(n, "namespace")) { Node *ns; /* using id */ Symtab *stab = Getattr(n, "sym:symtab"); if (stab) { String *uname = Getattr(n, "uname"); ns = Swig_symbol_clookup(uname, stab); if (!ns && SwigType_istemplate(uname)) { String *tmp = Swig_symbol_template_deftype(uname, 0); if (!Equal(tmp, uname)) { ns = Swig_symbol_clookup(tmp, stab); } Delete(tmp); } } else { ns = 0; } if (!ns) { if (is_public(n)) { Swig_warning(WARN_PARSE_USING_UNDEF, Getfile(n), Getline(n), "Nothing known about '%s'.\n", SwigType_namestr(Getattr(n, "uname"))); } } else if (Equal(nodeType(ns), "constructor") && !GetFlag(n, "usingctor")) { Swig_warning(WARN_PARSE_USING_CONSTRUCTOR, Getfile(n), Getline(n), "Using declaration '%s' for inheriting constructors uses base '%s' which is not an immediate base of '%s'.\n", SwigType_namestr(Getattr(n, "uname")), SwigType_namestr(Getattr(ns, "name")), SwigType_namestr(Getattr(parentNode(n), "name"))); } else { if (inclass && Getattr(n, "sym:name")) { { String *ntype = nodeType(ns); if (Equal(ntype, "cdecl") || Equal(ntype, "constructor") || Equal(ntype, "template") || Equal(ntype, "using")) { /* Add a new class member to the parse tree (copy it from the base class member pointed to by the using declaration in node n) */ Node *c = ns; Node *unodes = 0, *last_unodes = 0; int ccount = 0; while (c) { String *cnodetype = nodeType(c); if (Equal(cnodetype, "cdecl")) { add_member_for_using_declaration(c, n, ccount, unodes, last_unodes); } else if (Equal(cnodetype, "constructor")) { add_member_for_using_declaration(c, n, ccount, unodes, last_unodes); } else if (Equal(cnodetype, "template")) { // A templated member (in a non-template class or in a template class that where the member has a separate template declaration) // Find the template instantiations in the using declaration (base class) for (Node *member = ns; member; member = nextSibling(member)) { /* Constructors have already been handled, only add member functions * This adds an implicit template instantiation and is a bit unusual as SWIG requires explicit %template for other template instantiations. * However, of note, is that there is no valid C++ syntax for a template instantiation to introduce a name via a using declaration... * * struct Base { template void template_method(T, T) {} }; * struct Derived : Base { using Base::template_method; }; * %template() Base::template_method; // SWIG template instantiation * template void Base::template_method(int, int); // C++ template instantiation * template void Derived::template_method(int, int); // Not valid C++ */ if (Getattr(member, "template") == ns && checkAttribute(ns, "templatetype", "cdecl")) { if (!GetFlag(member, "feature:ignore") && !Getattr(member, "error")) { add_member_for_using_declaration(member, n, ccount, unodes, last_unodes); } } } } else if (Equal(cnodetype, "using")) { for (Node *member = firstChild(c); member; member = nextSibling(member)) { add_member_for_using_declaration(member, n, ccount, unodes, last_unodes); } } c = Getattr(c, "csym:nextSibling"); } if (unodes) { set_firstChild(n, unodes); if (ccount > 1) { if (!Getattr(n, "sym:overloaded")) { Setattr(n, "sym:overloaded", n); Setattr(n, "sym:overname", "_SWIG_0"); } } } /* Hack the parse tree symbol table for overloaded methods. Replace the "using" node with the * list of overloaded methods we have just added in as child nodes to the "using" node. * The node will still exist, it is just the symbol table linked list of overloaded methods * which is hacked. */ if (Getattr(n, "sym:overloaded")) { int cnt = 0; Node *ps = Getattr(n, "sym:previousSibling"); Node *ns = Getattr(n, "sym:nextSibling"); Node *fc = firstChild(n); Node *firstoverloaded = Getattr(n, "sym:overloaded"); #ifdef DEBUG_OVERLOADED show_overloaded(firstoverloaded); #endif if (firstoverloaded == n) { // This 'using' node we are cutting out was the first node in the overloaded list. // Change the first node in the list Delattr(firstoverloaded, "sym:overloaded"); firstoverloaded = fc ? fc : ns; // Correct all the sibling overloaded methods (before adding in new methods) Node *nnn = ns; while (nnn) { Setattr(nnn, "sym:overloaded", firstoverloaded); nnn = Getattr(nnn, "sym:nextSibling"); } } if (!fc) { // Remove from overloaded list ('using' node does not actually end up adding in any methods) if (ps) { Setattr(ps, "sym:nextSibling", ns); } if (ns) { Setattr(ns, "sym:previousSibling", ps); } } else { // The 'using' node results in methods being added in - slot in these methods here Node *pp = fc; while (pp) { Node *ppn = Getattr(pp, "sym:nextSibling"); Setattr(pp, "sym:overloaded", firstoverloaded); Setattr(pp, "sym:overname", NewStringf("%s_%d", Getattr(n, "sym:overname"), cnt++)); if (ppn) pp = ppn; else break; } if (ps) { Setattr(ps, "sym:nextSibling", fc); Setattr(fc, "sym:previousSibling", ps); } if (ns) { Setattr(ns, "sym:previousSibling", pp); Setattr(pp, "sym:nextSibling", ns); } } Delattr(n, "sym:previousSibling"); Delattr(n, "sym:nextSibling"); Delattr(n, "sym:overloaded"); Delattr(n, "sym:overname"); clean_overloaded(firstoverloaded); #ifdef DEBUG_OVERLOADED show_overloaded(firstoverloaded); #endif } } } } } Node *c = 0; for (c = firstChild(n); c; c = nextSibling(c)) { if (Equal(nodeType(c), "cdecl")) { process_exceptions(c); if (inclass) class_member_is_defined_in_bases(c, inclass); } else if (Equal(nodeType(c), "constructor")) { constructorDeclaration(c); } } } return SWIG_OK; } virtual int cDeclaration(Node *n) { process_exceptions(n); if (inclass) { /* check whether the member node n is defined in class node in class's bases */ class_member_is_defined_in_bases(n, inclass); /* Check to see if this is a static member or not. If so, we add an attribute cplus:staticbase that saves the current class */ int is_static = Swig_storage_isstatic(n); if (is_static) { Setattr(n, "cplus:staticbase", inclass); } if (Cmp(Getattr(n, "kind"), "variable") == 0) { /* Check member variable to determine whether assignment is valid */ bool is_reference; bool is_const; bool assignable = is_assignable(n, is_reference, is_const); if (!assignable || is_const) { SetFlag(n, "feature:immutable"); } if (!is_static) { if (!assignable || is_reference || is_const) SetFlag(inclass, "allocate:has_nonassignable"); // The class has a variable that cannot be assigned to } } String *name = Getattr(n, "name"); if (cplus_mode != PUBLIC) { if (Strcmp(name, "operator =") == 0) { /* Look for a private assignment operator */ if (!GetFlag(n, "deleted")) Setattr(inclass, "allocate:has_assign", "1"); Setattr(inclass, "allocate:noassign", "1"); } else if (Strcmp(name, "operator new") == 0) { /* Look for a private new operator */ if (!GetFlag(n, "deleted")) Setattr(inclass, "allocate:has_new", "1"); Setattr(inclass, "allocate:nonew", "1"); } } else { if (Strcmp(name, "operator =") == 0) { if (!GetFlag(n, "deleted")) Setattr(inclass, "allocate:has_assign", "1"); else Setattr(inclass, "allocate:noassign", "1"); } else if (Strcmp(name, "operator new") == 0) { if (!GetFlag(n, "deleted")) Setattr(inclass, "allocate:has_new", "1"); else Setattr(inclass, "allocate:nonew", "1"); } /* Look for smart pointer operator */ if ((Strcmp(name, "operator ->") == 0) && (!GetFlag(n, "feature:ignore"))) { /* Look for version with no parameters */ Node *sn = n; while (sn) { if (!Getattr(sn, "parms")) { SwigType *type = SwigType_typedef_resolve_all(Getattr(sn, "type")); SwigType_push(type, Getattr(sn, "decl")); Delete(SwigType_pop_function(type)); SwigType *base = SwigType_base(type); Node *sc = Swig_symbol_clookup(base, 0); if ((sc) && (Strcmp(nodeType(sc), "class") == 0)) { if (SwigType_check_decl(type, "p.")) { /* Need to check if type is a const pointer */ int isconst = 0; Delete(SwigType_pop(type)); if (SwigType_isconst(type)) { isconst = !Getattr(inclass, "allocate:smartpointermutable"); Setattr(inclass, "allocate:smartpointerconst", "1"); } else { Setattr(inclass, "allocate:smartpointermutable", "1"); } List *methods = smart_pointer_methods(sc, 0, isconst); Setattr(inclass, "allocate:smartpointer", methods); Setattr(inclass, "allocate:smartpointerpointeeclassname", Getattr(sc, "name")); } else { /* Hmmm. The return value is not a pointer. If the type is a value or reference. We're going to chase it to see if another operator->() can be found */ if ((SwigType_check_decl(type, "")) || (SwigType_check_decl(type, "r."))) { Node *nn = Swig_symbol_clookup("operator ->", Getattr(sc, "symtab")); if (nn) { Delete(base); Delete(type); sn = nn; continue; } } } } Delete(base); Delete(type); break; } } } } } else { if (Cmp(Getattr(n, "kind"), "variable") == 0) { bool is_reference; bool is_const; bool assignable = is_assignable(n, is_reference, is_const); if (!assignable || is_const) { SetFlag(n, "feature:immutable"); } } } return SWIG_OK; } virtual int templateDeclaration(Node *n) { String *ttype = Getattr(n, "templatetype"); if (Equal(ttype, "constructor")) { // Templated constructors need to be taken account of even if not instantiated with %template constructorDeclaration(n); } return SWIG_OK; } virtual int constructorDeclaration(Node *n) { if (!inclass) return SWIG_OK; Parm *parms = Getattr(n, "parms"); bool deleted_constructor = (GetFlag(n, "deleted")); bool default_constructor = !ParmList_numrequired(parms); AccessMode access_mode = accessModeFromString(Getattr(n, "access")); process_exceptions(n); if (!deleted_constructor) { if (!extendmode) { if (default_constructor) { /* Class does define a default constructor */ /* However, we had better see where it is defined */ if (access_mode == PUBLIC) { Setattr(inclass, "allocate:default_constructor", "1"); } else if (access_mode == PROTECTED) { Setattr(inclass, "allocate:default_base_constructor", "1"); } } /* Class defines some kind of constructor. May or may not be public */ Setattr(inclass, "allocate:has_constructor", "1"); if (access_mode == PUBLIC) { Setattr(inclass, "allocate:public_constructor", "1"); } } else { Setattr(inclass, "allocate:has_constructor", "1"); Setattr(inclass, "allocate:public_constructor", "1"); } } else { if (default_constructor && !extendmode) SetFlag(inclass, "allocate:deleted_default_constructor"); } /* See if this is a copy constructor */ if (parms && (ParmList_numrequired(parms) == 1)) { /* Look for a few cases. X(const X &), X(X &), X(X *) */ int copy_constructor = 0; int copy_constructor_non_const = 0; SwigType *type = Getattr(inclass, "name"); String *tn = NewStringf("r.q(const).%s", type); String *cc = SwigType_typedef_resolve_all(tn); SwigType *rt = SwigType_typedef_resolve_all(Getattr(parms, "type")); if (SwigType_istemplate(type)) { String *tmp = Swig_symbol_template_deftype(cc, 0); Delete(cc); cc = tmp; tmp = Swig_symbol_template_deftype(rt, 0); Delete(rt); rt = tmp; } if (Strcmp(cc, rt) == 0) { copy_constructor = 1; } else { Delete(cc); cc = NewStringf("r.%s", Getattr(inclass, "name")); if (Strcmp(cc, Getattr(parms, "type")) == 0) { copy_constructor = 1; copy_constructor_non_const = 1; } else { Delete(cc); cc = NewStringf("p.%s", Getattr(inclass, "name")); String *ty = SwigType_strip_qualifiers(Getattr(parms, "type")); if (Strcmp(cc, ty) == 0) { copy_constructor = 1; } Delete(ty); } } Delete(cc); Delete(rt); Delete(tn); if (copy_constructor) { if (!deleted_constructor) { Setattr(n, "copy_constructor", "1"); Setattr(inclass, "allocate:has_copy_constructor", "1"); if (access_mode == PUBLIC) { Setattr(inclass, "allocate:copy_constructor", "1"); } else if (access_mode == PROTECTED) { Setattr(inclass, "allocate:copy_base_constructor", "1"); } if (copy_constructor_non_const) { Setattr(n, "copy_constructor_non_const", "1"); Setattr(inclass, "allocate:has_copy_constructor_non_const", "1"); if (access_mode == PUBLIC) { Setattr(inclass, "allocate:copy_constructor_non_const", "1"); } else if (access_mode == PROTECTED) { Setattr(inclass, "allocate:copy_base_constructor_non_const", "1"); } } } else { if (!extendmode) SetFlag(inclass, "allocate:deleted_copy_constructor"); } } } return SWIG_OK; } virtual int destructorDeclaration(Node *n) { (void) n; if (!inclass) return SWIG_OK; if (!GetFlag(n, "deleted")) { if (!extendmode) { Setattr(inclass, "allocate:has_destructor", "1"); if (cplus_mode == PUBLIC) { Setattr(inclass, "allocate:default_destructor", "1"); } else if (cplus_mode == PROTECTED) { Setattr(inclass, "allocate:default_base_destructor", "1"); } else if (cplus_mode == PRIVATE) { Setattr(inclass, "allocate:private_destructor", "1"); } } else { Setattr(inclass, "allocate:has_destructor", "1"); Setattr(inclass, "allocate:default_destructor", "1"); } } else { if (!extendmode) SetFlag(inclass, "allocate:deleted_default_destructor"); } return SWIG_OK; } static void addCopyConstructor(Node *n) { Node *cn = NewHash(); set_nodeType(cn, "constructor"); Setattr(cn, "access", "public"); Setfile(cn, Getfile(n)); Setline(cn, Getline(n)); int copy_constructor_non_const = GetFlag(n, "allocate:copy_constructor_non_const"); String *cname = Getattr(n, "name"); SwigType *type = Copy(cname); String *lastname = Swig_scopename_last(cname); String *name = SwigType_templateprefix(lastname); String *cc = NewStringf(copy_constructor_non_const ? "r.%s" : "r.q(const).%s", type); String *decl = NewStringf("f(%s).", cc); String *oldname = Getattr(n, "sym:name"); if (Getattr(n, "allocate:has_constructor")) { // to work properly with '%rename Class', we must look // for any other constructor in the class, which has not been // renamed, and use its name as oldname. Node *c; for (c = firstChild(n); c; c = nextSibling(c)) { if (Equal(nodeType(c), "constructor")) { String *csname = Getattr(c, "sym:name"); String *clast = Swig_scopename_last(Getattr(c, "name")); if (Equal(csname, clast)) { oldname = csname; Delete(clast); break; } Delete(clast); } } } String *symname = Swig_name_make(cn, cname, name, decl, oldname); if (Strcmp(symname, "$ignore") != 0) { Parm *p = NewParm(cc, "other", n); Setattr(cn, "name", name); Setattr(cn, "sym:name", symname); SetFlag(cn, "feature:new"); Setattr(cn, "decl", decl); Setattr(cn, "ismember", "1"); Setattr(cn, "parentNode", n); Setattr(cn, "parms", p); Setattr(cn, "copy_constructor", "1"); Symtab *oldscope = Swig_symbol_setscope(Getattr(n, "symtab")); Node *on = Swig_symbol_add(symname, cn); Swig_features_get(Swig_cparse_features(), Swig_symbol_qualifiedscopename(0), name, decl, cn); Swig_symbol_setscope(oldscope); if (on == cn) { Node *access = NewHash(); set_nodeType(access, "access"); Setattr(access, "kind", "public"); appendChild(n, access); appendChild(n, cn); Setattr(n, "has_copy_constructor", "1"); Setattr(n, "copy_constructor_decl", decl); Setattr(n, "allocate:copy_constructor", "1"); Delete(access); } } Delete(cn); Delete(lastname); Delete(name); Delete(decl); Delete(symname); } static void addDefaultConstructor(Node *n) { Node *cn = NewHash(); set_nodeType(cn, "constructor"); Setattr(cn, "access", "public"); Setfile(cn, Getfile(n)); Setline(cn, Getline(n)); String *cname = Getattr(n, "name"); String *lastname = Swig_scopename_last(cname); String *name = SwigType_templateprefix(lastname); String *decl = NewString("f()."); String *oldname = Getattr(n, "sym:name"); String *symname = Swig_name_make(cn, cname, name, decl, oldname); if (Strcmp(symname, "$ignore") != 0) { Setattr(cn, "name", name); Setattr(cn, "sym:name", symname); SetFlag(cn, "feature:new"); Setattr(cn, "decl", decl); Setattr(cn, "ismember", "1"); Setattr(cn, "parentNode", n); Setattr(cn, "default_constructor", "1"); Symtab *oldscope = Swig_symbol_setscope(Getattr(n, "symtab")); Node *on = Swig_symbol_add(symname, cn); Swig_features_get(Swig_cparse_features(), Swig_symbol_qualifiedscopename(0), name, decl, cn); Swig_symbol_setscope(oldscope); if (on == cn) { Node *access = NewHash(); set_nodeType(access, "access"); Setattr(access, "kind", "public"); appendChild(n, access); appendChild(n, cn); Setattr(n, "has_default_constructor", "1"); Setattr(n, "allocate:default_constructor", "1"); Delete(access); } } Delete(cn); Delete(lastname); Delete(name); Delete(decl); Delete(symname); } static void addDestructor(Node *n) { Node *cn = NewHash(); set_nodeType(cn, "destructor"); Setattr(cn, "access", "public"); Setfile(cn, Getfile(n)); Setline(cn, Getline(n)); String *cname = Getattr(n, "name"); String *lastname = Swig_scopename_last(cname); String *name = SwigType_templateprefix(lastname); Insert(name, 0, "~"); String *decl = NewString("f()."); String *symname = Swig_name_make(cn, cname, name, decl, 0); if (Strcmp(symname, "$ignore") != 0) { String *possible_nonstandard_symname = NewStringf("~%s", Getattr(n, "sym:name")); Setattr(cn, "name", name); Setattr(cn, "sym:name", symname); Setattr(cn, "decl", "f()."); Setattr(cn, "ismember", "1"); Setattr(cn, "parentNode", n); Symtab *oldscope = Swig_symbol_setscope(Getattr(n, "symtab")); Node *nonstandard_destructor = Equal(possible_nonstandard_symname, symname) ? 0 : Swig_symbol_clookup(possible_nonstandard_symname, 0); Node *on = Swig_symbol_add(symname, cn); Swig_features_get(Swig_cparse_features(), Swig_symbol_qualifiedscopename(0), name, decl, cn); Swig_symbol_setscope(oldscope); if (on == cn) { // SWIG accepts a non-standard named destructor in %extend that uses a typedef for the destructor name // For example: typedef struct X {} XX; %extend X { ~XX() {...} } // Don't add another destructor if a nonstandard one has been declared if (!nonstandard_destructor) { Node *access = NewHash(); set_nodeType(access, "access"); Setattr(access, "kind", "public"); appendChild(n, access); appendChild(n, cn); Setattr(n, "has_destructor", "1"); Setattr(n, "allocate:has_destructor", "1"); Delete(access); } } Delete(possible_nonstandard_symname); } Delete(cn); Delete(lastname); Delete(name); Delete(decl); Delete(symname); } }; void Swig_default_allocators(Node *n) { if (!n) return; Allocate *a = new Allocate; a->top(n); delete a; } swig-4.4.0/Source/Modules/guile.cxx0000664000175000017500000015034315075443613017067 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * guile.cxx * * Guile language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include #include // Note string broken in half for compilers that can't handle long strings static const char *usage = "\ Guile Options (available with -guile)\n\ -emitsetters - Emit procedures-with-setters for variables\n\ and structure slots.\n\ -emitslotaccessors - Emit accessor methods for all GOOPS slots\n" "\ -exportprimitive - Add the (export ...) code from scmstub into the\n\ GOOPS file.\n\ -goopsprefix - Prepend to all goops identifiers\n\ -Linkage - Use linkage protocol (default `simple')\n\ Use `module' for native Guile module linking\n\ (requires Guile >= 1.5.0). Use `passive' for\n\ passive linking (no C-level module-handling code),\n\ or `hobbit' for hobbit modules.\n\ -onlysetters - Don't emit traditional getter and setter\n\ procedures for structure slots,\n\ only emit procedures-with-setters.\n\ -package - Set the path of the module to \n\ (default NULL)\n\ -prefix - Use as prefix [default \"gswig_\"]\n\ -procdoc - Output procedure documentation to \n\ -procdocformat - Output procedure documentation in ;\n\ one of `guile-1.4', `plain', `texinfo'\n\ -proxy - Export GOOPS class definitions\n\ -primsuffix - Name appended to primitive module when exporting\n\ GOOPS classes. (default = \"primitive\")\n\ -scmstub - Output Scheme file with module declaration and\n\ exports; only with `passive' and `simple' linkage\n\ -useclassprefix - Prepend the class name to all goops identifiers\n\ \n"; static File *f_begin = 0; static File *f_runtime = 0; static File *f_header = 0; static File *f_wrappers = 0; static File *f_init = 0; static String *prefix = NewString("gswig_"); static char *module = 0; static String *package = 0; static enum { GUILE_LSTYLE_SIMPLE, // call `SWIG_init()' GUILE_LSTYLE_PASSIVE, // passive linking (no module code) GUILE_LSTYLE_MODULE, // native guile module linking (Guile >= 1.4.1) GUILE_LSTYLE_HOBBIT // use (hobbit4d link) } linkage = GUILE_LSTYLE_SIMPLE; static File *procdoc = 0; static bool scmstub = false; static String *scmtext; static bool goops = false; static String *goopstext; static String *goopscode; static String *goopsexport; static enum { GUILE_1_4, PLAIN, TEXINFO } docformat = GUILE_1_4; static int emit_setters = 0; static int only_setters = 0; static int emit_slot_accessors = 0; static int struct_member = 0; static String *beforereturn = 0; static String *return_nothing_doc = 0; static String *return_one_doc = 0; static String *return_multi_doc = 0; static String *exported_symbols = 0; static int exporting_destructor = 0; static String *swigtype_ptr = 0; /* GOOPS stuff */ static String *primsuffix = 0; static String *class_name = 0; static String *short_class_name = 0; static String *goops_class_methods; static int in_class = 0; static int have_constructor = 0; static int useclassprefix = 0; // -useclassprefix argument static String *goopsprefix = 0; // -goopsprefix argument static int primRenamer = 0; // if (use-modules ((...) :renamer ...) is exported to GOOPS file static int exportprimitive = 0; // -exportprimitive argument static String *memberfunction_name = 0; extern "C" { static Node *has_classname(Node *class_node) { return Getattr(class_node, "guile:goopsclassname") ? class_node : 0; } } class GUILE:public Language { public: /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { int i; SWIG_library_directory("guile"); // Look for certain command line options for (i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); Exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else 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], "-Linkage") == 0 || strcmp(argv[i], "-linkage") == 0) { if (argv[i + 1]) { if (0 == strcmp(argv[i + 1], "hobbit")) linkage = GUILE_LSTYLE_HOBBIT; else if (0 == strcmp(argv[i + 1], "simple")) linkage = GUILE_LSTYLE_SIMPLE; else if (0 == strcmp(argv[i + 1], "passive")) linkage = GUILE_LSTYLE_PASSIVE; else if (0 == strcmp(argv[i + 1], "module")) linkage = GUILE_LSTYLE_MODULE; else Swig_arg_error(); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-procdoc") == 0) { if (argv[i + 1]) { procdoc = NewFile(argv[i + 1], "w", SWIG_output_files()); if (!procdoc) { FileErrorDisplay(argv[i + 1]); Exit(EXIT_FAILURE); } Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-procdocformat") == 0) { if (strcmp(argv[i + 1], "guile-1.4") == 0) docformat = GUILE_1_4; else if (strcmp(argv[i + 1], "plain") == 0) docformat = PLAIN; else if (strcmp(argv[i + 1], "texinfo") == 0) docformat = TEXINFO; else Swig_arg_error(); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else if (strcmp(argv[i], "-emit-setters") == 0 || strcmp(argv[i], "-emitsetters") == 0) { emit_setters = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-only-setters") == 0 || strcmp(argv[i], "-onlysetters") == 0) { emit_setters = 1; only_setters = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-emit-slot-accessors") == 0 || strcmp(argv[i], "-emitslotaccessors") == 0) { emit_slot_accessors = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-scmstub") == 0) { scmstub = true; Swig_mark_arg(i); } else if ((strcmp(argv[i], "-shadow") == 0) || ((strcmp(argv[i], "-proxy") == 0))) { goops = true; Swig_mark_arg(i); } else if (strcmp(argv[i], "-primsuffix") == 0) { if (argv[i + 1]) { primsuffix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-goopsprefix") == 0) { if (argv[i + 1]) { goopsprefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-useclassprefix") == 0) { useclassprefix = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-exportprimitive") == 0) { exportprimitive = 1; // should use Swig_warning() here? Swig_mark_arg(i); } } } // set default value for primsuffix if (!primsuffix) primsuffix = NewString("primitive"); //goops support can only be enabled if passive or module linkage is used if (goops) { if (linkage != GUILE_LSTYLE_PASSIVE && linkage != GUILE_LSTYLE_MODULE) { Printf(stderr, "guile: GOOPS support requires passive or module linkage\n"); Exit(EXIT_FAILURE); } } if (goops) { // -proxy implies -emit-setters emit_setters = 1; } if ((linkage == GUILE_LSTYLE_PASSIVE && scmstub) || linkage == GUILE_LSTYLE_MODULE) primRenamer = 1; if (exportprimitive && primRenamer) { // should use Swig_warning() ? Printf(stderr, "guile: Warning: -exportprimitive only makes sense with passive linkage without a scmstub.\n"); } // Make sure `prefix' ends in an underscore if (prefix) { const char *px = Char(prefix); if (px[Len(prefix) - 1] != '_') Printf(prefix, "_"); } /* Add a symbol for this module */ Preprocessor_define("SWIGGUILE 1", 0); /* Read in default typemaps */ SWIG_config_file("guile_scm.swg"); allow_overloading(); } /* ------------------------------------------------------------ * top() * ------------------------------------------------------------ */ virtual int top(Node *n) { /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewString(""); f_init = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); /* Register file targets with the SWIG file handler */ Swig_register_filebyname("header", f_header); Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); scmtext = NewString(""); Swig_register_filebyname("scheme", scmtext); exported_symbols = NewString(""); goopstext = NewString(""); Swig_register_filebyname("goops", goopstext); goopscode = NewString(""); goopsexport = NewString(""); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "GUILE"); /* Write out directives and declarations */ module = Swig_copy_string(Char(Getattr(n, "name"))); switch (linkage) { case GUILE_LSTYLE_SIMPLE: /* Simple linkage; we have to export the SWIG_init function. The user can rename the function by a #define. */ Printf(f_runtime, "#define SWIG_GUILE_INIT_STATIC extern\n"); break; default: /* Other linkage; we make the SWIG_init function static */ Printf(f_runtime, "#define SWIG_GUILE_INIT_STATIC static\n"); break; } if (CPlusPlus) { Printf(f_runtime, "extern \"C\" {\n\n"); } Printf(f_runtime, "SWIG_GUILE_INIT_STATIC void\nSWIG_init (void);\n"); if (CPlusPlus) { Printf(f_runtime, "\n}\n"); } Printf(f_runtime, "\n"); Language::top(n); /* Close module */ Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); SwigType_emit_type_table(f_runtime, f_wrappers); Printf(f_init, "}\n\n"); Printf(f_init, "#ifdef __cplusplus\n}\n#endif\n"); String *module_name = NewString(""); if (!module) Printv(module_name, "swig", NIL); else { if (package) Printf(module_name, "%s/%s", package, module); else Printv(module_name, module, NIL); } emit_linkage(module_name); Delete(module_name); if (procdoc) { Delete(procdoc); procdoc = NULL; } Delete(goopscode); Delete(goopsexport); Delete(goopstext); /* Close all of the files */ Dump(f_runtime, f_begin); Dump(f_header, f_begin); Dump(f_wrappers, f_begin); Wrapper_pretty_print(f_init, f_begin); Delete(f_header); Delete(f_wrappers); Delete(f_init); Delete(f_runtime); Delete(f_begin); return SWIG_OK; } void emit_linkage(String *module_name) { String *module_func = NewString(""); if (CPlusPlus) { Printf(f_init, "extern \"C\" {\n\n"); } Printv(module_func, module_name, NIL); Replaceall(module_func, "-", "_"); switch (linkage) { case GUILE_LSTYLE_SIMPLE: Printf(f_init, "\n/* Linkage: simple */\n"); break; case GUILE_LSTYLE_PASSIVE: Printf(f_init, "\n/* Linkage: passive */\n"); Replaceall(module_func, "/", "_"); Insert(module_func, 0, "scm_init_"); Append(module_func, "_module"); Printf(f_init, "SCM\n%s (void)\n{\n", module_func); Printf(f_init, " SWIG_init();\n"); Printf(f_init, " return SCM_UNSPECIFIED;\n"); Printf(f_init, "}\n"); break; case GUILE_LSTYLE_MODULE: Printf(f_init, "\n/* Linkage: module */\n"); Replaceall(module_func, "/", "_"); Insert(module_func, 0, "scm_init_"); Append(module_func, "_module"); Printf(f_init, "static void SWIG_init_helper(void *data)\n"); Printf(f_init, "{\n SWIG_init();\n"); if (Len(exported_symbols) > 0) Printf(f_init, " scm_c_export(%sNULL);", exported_symbols); Printf(f_init, "\n}\n\n"); Printf(f_init, "SCM\n%s (void)\n{\n", module_func); { String *mod = NewString(module_name); if (goops) Printv(mod, "-", primsuffix, NIL); Replaceall(mod, "/", " "); Printf(f_init, " scm_c_define_module(\"%s\",\n", mod); Printf(f_init, " SWIG_init_helper, NULL);\n"); Printf(f_init, " return SCM_UNSPECIFIED;\n"); Delete(mod); } Printf(f_init, "}\n"); break; case GUILE_LSTYLE_HOBBIT: Printf(f_init, "\n/* Linkage: hobbit */\n"); Replaceall(module_func, "/", "_slash_"); Insert(module_func, 0, "scm_init_"); Printf(f_init, "SCM\n%s (void)\n{\n", module_func); { String *mod = NewString(module_name); Replaceall(mod, "/", " "); Printf(f_init, " scm_register_module_xxx (\"%s\", (void *) SWIG_init);\n", mod); Printf(f_init, " return SCM_UNSPECIFIED;\n"); Delete(mod); } Printf(f_init, "}\n"); break; default: Printf(stderr, "Internal error: Invalid Guile linkage setting.\n"); Exit(EXIT_FAILURE); } if (scmstub) { /* Emit Scheme stub if requested */ String *primitive_name = NewString(module_name); if (goops) Printv(primitive_name, "-", primsuffix, NIL); String *mod = NewString(primitive_name); Replaceall(mod, "/", " "); String *fname = NewStringf("%s%s.scm", SWIG_output_directory(), primitive_name); Delete(primitive_name); File *scmstubfile = NewFile(fname, "w", SWIG_output_files()); if (!scmstubfile) { FileErrorDisplay(fname); Exit(EXIT_FAILURE); } Delete(fname); Swig_banner_target_lang(scmstubfile, ";;;"); Printf(scmstubfile, "\n"); if (linkage == GUILE_LSTYLE_SIMPLE || linkage == GUILE_LSTYLE_PASSIVE) Printf(scmstubfile, "(define-module (%s))\n\n", mod); Delete(mod); Printf(scmstubfile, "%s", scmtext); if ((linkage == GUILE_LSTYLE_SIMPLE || linkage == GUILE_LSTYLE_PASSIVE) && Len(exported_symbols) > 0) { String *ex = NewString(exported_symbols); Replaceall(ex, ", ", "\n "); Replaceall(ex, "\"", ""); Chop(ex); Printf(scmstubfile, "\n(export %s)\n", ex); Delete(ex); } Delete(scmstubfile); } if (goops) { String *mod = NewString(module_name); Replaceall(mod, "/", " "); String *fname = NewStringf("%s%s.scm", SWIG_output_directory(), module_name); File *goopsfile = NewFile(fname, "w", SWIG_output_files()); if (!goopsfile) { FileErrorDisplay(fname); Exit(EXIT_FAILURE); } Delete(fname); Swig_banner_target_lang(goopsfile, ";;;"); Printf(goopsfile, "\n"); Printf(goopsfile, "(define-module (%s))\n", mod); Printf(goopsfile, "%s\n", goopstext); Printf(goopsfile, "(use-modules (oop goops) (Swig common))\n"); if (primRenamer) { Printf(goopsfile, "(use-modules ((%s-%s) :renamer (symbol-prefix-proc 'primitive:)))\n", mod, primsuffix); } Printf(goopsfile, "%s\n(export %s)", goopscode, goopsexport); if (exportprimitive) { String *ex = NewString(exported_symbols); Replaceall(ex, ", ", "\n "); Replaceall(ex, "\"", ""); Chop(ex); Printf(goopsfile, "\n(export %s)", ex); Delete(ex); } Delete(mod); Delete(goopsfile); } Delete(module_func); if (CPlusPlus) { Printf(f_init, "\n}\n"); } } /* Return true iff T is a pointer type */ int is_a_pointer(SwigType *t) { return SwigType_ispointer(SwigType_typedef_resolve_all(t)); } /* Report an error handling the given type. */ void throw_unhandled_guile_type_error(SwigType *d) { Swig_warning(WARN_TYPEMAP_UNDEF, input_file, line_number, "Unable to handle type %s.\n", SwigType_str(d, 0)); } /* Write out procedure documentation */ void write_doc(const String *proc_name, const String *signature, const String *doc, const String *signature2 = NULL) { switch (docformat) { case GUILE_1_4: Printv(procdoc, "\f\n", NIL); Printv(procdoc, "(", signature, ")\n", NIL); if (signature2) Printv(procdoc, "(", signature2, ")\n", NIL); Printv(procdoc, doc, "\n", NIL); break; case PLAIN: Printv(procdoc, "\f", proc_name, "\n\n", NIL); Printv(procdoc, "(", signature, ")\n", NIL); if (signature2) Printv(procdoc, "(", signature2, ")\n", NIL); Printv(procdoc, doc, "\n\n", NIL); break; case TEXINFO: Printv(procdoc, "\f", proc_name, "\n", NIL); Printv(procdoc, "@deffn primitive ", signature, "\n", NIL); if (signature2) Printv(procdoc, "@deffnx primitive ", signature2, "\n", NIL); Printv(procdoc, doc, "\n", NIL); Printv(procdoc, "@end deffn\n\n", NIL); break; } } /* returns false if the typemap is an empty string */ bool handle_documentation_typemap(String *output, const String *maybe_delimiter, Parm *p, const String *typemap, const String *default_doc, const String *name = NULL) { String *tmp = NewString(""); String *tm; if (!(tm = Getattr(p, typemap))) { Printf(tmp, "%s", default_doc); tm = tmp; } bool result = (Len(tm) > 0); if (maybe_delimiter && Len(output) > 0 && Len(tm) > 0) { Printv(output, maybe_delimiter, NIL); } const String *pn = !name ? (const String *) Getattr(p, "name") : name; String *pt = Getattr(p, "type"); Replaceall(tm, "$name", pn); // legacy for $parmname Replaceall(tm, "$type", SwigType_str(pt, 0)); /* $NAME is like $name, but marked-up as a variable. */ String *ARGNAME = NewString(""); if (docformat == TEXINFO) Printf(ARGNAME, "@var{%s}", pn); else Printf(ARGNAME, "%(upper)s", pn); Replaceall(tm, "$NAME", ARGNAME); Replaceall(tm, "$PARMNAME", ARGNAME); Printv(output, tm, NIL); Delete(tmp); return result; } /* ------------------------------------------------------------ * functionWrapper() * Create a function declaration and register it with the interpreter. * ------------------------------------------------------------ */ virtual int functionWrapper(Node *n) { String *iname = Getattr(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); Parm *p; String *proc_name = 0; char source[256]; Wrapper *f = NewWrapper(); String *cleanup = NewString(""); String *outarg = NewString(""); String *signature = NewString(""); String *doc_body = NewString(""); String *returns = NewString(""); String *method_signature = NewString(""); String *primitive_args = NewString(""); Hash *scheme_arg_names = NewHash(); int num_results = 1; String *tmp = NewString(""); String *tm; int i; int numargs = 0; int numreq = 0; String *overname = 0; int args_passed_as_array = 0; int scheme_argnum = 0; bool any_specialized_arg = false; // Make a wrapper name for this String *wname = Swig_name_wrapper(iname); if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); args_passed_as_array = 1; } else { if (!addSymbol(iname, n)) { DelWrapper(f); return SWIG_ERROR; } } if (overname) { Append(wname, overname); } Setattr(n, "wrap:name", wname); // Build the name for scheme. proc_name = NewString(iname); Replaceall(proc_name, "_", "-"); /* Emit locals etc. into f->code; figure out which args to ignore */ emit_parameter_variables(l, f); /* Attach the standard typemaps */ emit_attach_parmmaps(l, f); Setattr(n, "wrap:parms", l); /* Get number of required and total arguments */ numargs = emit_num_arguments(l); numreq = emit_num_required(l); /* Declare return variable */ Wrapper_add_local(f, "gswig_result", "SCM gswig_result"); Wrapper_add_local(f, "gswig_list_p", "SWIGUNUSED int gswig_list_p = 0"); /* Open prototype and signature */ Printv(f->def, "static SCM\n", wname, " (", NIL); if (args_passed_as_array) { Printv(f->def, "int argc, SCM *argv", NIL); } Printv(signature, proc_name, NIL); /* Now write code to extract the parameters */ for (i = 0, p = l; i < numargs; i++) { while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } SwigType *pt = Getattr(p, "type"); int opt_p = (i >= numreq); // Produce names of source and target if (args_passed_as_array) sprintf(source, "argv[%d]", i); else sprintf(source, "s_%d", i); if (!args_passed_as_array) { if (i != 0) Printf(f->def, ", "); Printf(f->def, "SCM s_%d", i); } if (opt_p) { Printf(f->code, " if (%s != SCM_UNDEFINED) {\n", source); } if ((tm = Getattr(p, "tmap:in"))) { Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); Printv(f->code, tm, "\n", NIL); SwigType *pb = SwigType_typedef_resolve_all(SwigType_base(pt)); SwigType *pn = Getattr(p, "name"); String *argname; scheme_argnum++; if (pn && !Getattr(scheme_arg_names, pn)) argname = pn; else { /* Anonymous arg or re-used argument name -- choose a name that cannot clash */ argname = NewStringf("%%arg%d", scheme_argnum); } if (procdoc) { if (i == numreq) { /* First optional argument */ Printf(signature, " #:optional"); } /* Add to signature (arglist) */ handle_documentation_typemap(signature, " ", p, "tmap:in:arglist", "$name", argname); /* Document the type of the arg in the documentation body */ handle_documentation_typemap(doc_body, ", ", p, "tmap:in:doc", "$NAME is of type <$type>", argname); } if (goops) { if (i < numreq) { if (strcmp("void", Char(pt)) != 0) { Node *class_node = Swig_symbol_clookup_check(pb, Getattr(n, "sym:symtab"), has_classname); String *goopsclassname = !class_node ? NULL : Getattr(class_node, "guile:goopsclassname"); /* do input conversion */ if (goopsclassname) { Printv(method_signature, " (", argname, " ", goopsclassname, ")", NIL); any_specialized_arg = true; } else { Printv(method_signature, " ", argname, NIL); } Printv(primitive_args, " ", argname, NIL); Setattr(scheme_arg_names, argname, p); } } } if (!pn) { Delete(argname); } p = Getattr(p, "tmap:in:next"); } else { throw_unhandled_guile_type_error(pt); p = nextSibling(p); } if (opt_p) Printf(f->code, " }\n"); } if (Len(doc_body) > 0) Printf(doc_body, ".\n"); /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Pass output arguments back to the caller. */ /* Insert argument output code */ String *returns_argout = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); if (procdoc) { if (handle_documentation_typemap(returns_argout, ", ", p, "tmap:argout:doc", "$NAME (of type $type)")) { /* A documentation typemap that is not the empty string indicates that a value is returned to Scheme. */ num_results++; } } p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:freearg"))) { Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } if (exporting_destructor) { /* Mark the destructor's argument as destroyed. */ String *tm = NewString("SWIG_Guile_MarkPointerDestroyed($input);"); Replaceall(tm, "$input", Getattr(l, "emit:input")); Printv(cleanup, tm, "\n", NIL); Delete(tm); } /* Close prototype */ Printf(f->def, ")\n{\n"); /* Define the scheme name in C. This define is used by several Guile macros. */ Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL); // Now write code to make the function call String *actioncode = emit_action(n); // Now have return value, figure out what to do with it. if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { Replaceall(tm, "$result", "gswig_result"); if (GetFlag(n, "feature:new")) Replaceall(tm, "$owner", "1"); else Replaceall(tm, "$owner", "0"); Printv(f->code, tm, "\n", NIL); } else { throw_unhandled_guile_type_error(returntype); } emit_return_variable(n, returntype, f); // Documentation if ((tm = Getattr(n, "tmap:out:doc"))) { Printv(returns, tm, NIL); if (Len(tm) > 0) num_results = 1; else num_results = 0; } else { String *s = SwigType_str(returntype, 0); Chop(s); Printf(returns, "<%s>", s); Delete(s); num_results = 1; } Append(returns, returns_argout); // Dump the argument output code Printv(f->code, outarg, NIL); // Dump the argument cleanup code Printv(f->code, cleanup, NIL); // Look for any remaining cleanup if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printv(f->code, tm, "\n", NIL); } } // Free any memory allocated by the function being wrapped.. if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printv(f->code, tm, "\n", NIL); } // Wrap things up (in a manner of speaking) if (beforereturn) Printv(f->code, beforereturn, "\n", NIL); Printv(f->code, "return gswig_result;\n", NIL); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); /* Substitute the function name */ Replaceall(f->code, "$symname", iname); // Undefine the scheme name Printf(f->code, "#undef FUNC_NAME\n"); Printf(f->code, "}\n"); Wrapper_print(f, f_wrappers); if (!Getattr(n, "sym:overloaded")) { if (numargs > 10) { /* "The total number of these arguments should match the actual number * of arguments to fcn, but may not exceed 10" says: * https://www.gnu.org/software/guile/manual/html_node/Primitive-Procedures.html * We handle this case by passing all the arguments as "rest". */ int i; /* Build a wrapper wrapper */ Printv(f_wrappers, "static SCM\n", wname, "_rest (SCM rest)\n", NIL); Printv(f_wrappers, "{\n", NIL); Printf(f_wrappers, "SCM arg[%d];\n", numargs); Printf(f_wrappers, "SWIG_Guile_GetArgs (arg, rest, %d, %d, \"%s\");\n", numreq, numargs - numreq, proc_name); Printv(f_wrappers, "return ", wname, "(", NIL); Printv(f_wrappers, "arg[0]", NIL); for (i = 1; i < numargs; i++) Printf(f_wrappers, ", arg[%d]", i); Printv(f_wrappers, ");\n", NIL); Printv(f_wrappers, "}\n", NIL); /* Register it */ Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, 0, 1, (swig_guile_proc) %s_rest);\n", proc_name, wname); } else if (emit_setters && struct_member && strlen(Char(proc_name)) > 3) { int len = Len(proc_name); const char *pc = Char(proc_name); /* MEMBER-set and MEMBER-get functions. */ int is_setter = (pc[len - 3] == 's'); if (is_setter) { Printf(f_init, "SCM setter = "); struct_member = 2; /* have a setter */ } else Printf(f_init, "SCM getter = "); /* GOOPS support uses the MEMBER-set and MEMBER-get functions, so ignore only_setters in this case. */ if (only_setters && !goops) Printf(f_init, "scm_c_make_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n", proc_name, numreq, numargs - numreq, wname); else Printf(f_init, "scm_c_define_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n", proc_name, numreq, numargs - numreq, wname); if (!is_setter) { /* Strip off "-get" */ if (struct_member == 2) { /* There was a setter, so create a procedure with setter */ Printf(f_init, "scm_c_define"); Printf(f_init, "(\"%.*s\", " "scm_make_procedure_with_setter(getter, setter));\n", pc, len - 4); } else { /* There was no setter, so make an alias to the getter */ Printf(f_init, "scm_c_define"); Printf(f_init, "(\"%.*s\", getter);\n", pc, len - 4); } Printf(exported_symbols, "\"%.*s\", ", pc, len - 4); } } else { /* Register the function */ if (exporting_destructor) { Printf(f_init, "((swig_guile_clientdata *)(SWIGTYPE%s->clientdata))->destroy = (guile_destructor) %s;\n", swigtype_ptr, wname); //Printf(f_init, "SWIG_TypeClientData(SWIGTYPE%s, (void *) %s);\n", swigtype_ptr, wname); } Printf(f_init, "scm_c_define_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n", proc_name, numreq, numargs - numreq, wname); } } else { /* overloaded function; don't export the single methods */ if (!Getattr(n, "sym:nextSibling")) { /* Emit overloading dispatch function */ int maxargs; bool check_emitted = false; String *dispatch = Swig_overload_dispatch(n, "return %s(argc,argv);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *df = NewWrapper(); String *dname = Swig_name_wrapper(iname); Printv(df->def, "static SCM\n", dname, "(SCM rest)\n{\n", NIL); Printf(df->code, "#define FUNC_NAME \"%s\"\n", proc_name); Printf(df->code, "SCM argv[%d];\n", maxargs); Printf(df->code, "int argc = SWIG_Guile_GetArgs (argv, rest, %d, %d, \"%s\");\n", 0, maxargs, proc_name); Printv(df->code, dispatch, "\n", NIL); Printf(df->code, "scm_misc_error(\"%s\", \"No matching method for generic function `%s'\", SCM_EOL);\n", proc_name, iname); Printf(df->code, "#undef FUNC_NAME\n"); Printv(df->code, "}\n", NIL); Wrapper_print(df, f_wrappers); Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, 0, 1, (swig_guile_proc) %s);\n", proc_name, dname); DelWrapper(df); Delete(dispatch); Delete(dname); } } Printf(exported_symbols, "\"%s\", ", proc_name); if (!in_class || memberfunction_name) { // export wrapper into goops file String *method_def = NewString(""); String *goops_name; if (in_class) goops_name = NewString(memberfunction_name); else goops_name = goopsNameMapping(proc_name, ""); String *primitive_name = NewString(""); if (primRenamer) Printv(primitive_name, "primitive:", proc_name, NIL); else Printv(primitive_name, proc_name, NIL); Replaceall(method_signature, "_", "-"); Replaceall(primitive_args, "_", "-"); if (!any_specialized_arg) { /* If there would not be any specialized argument in the method declaration, we simply re-export the function. This is a performance optimization. */ Printv(method_def, "(define ", goops_name, " ", primitive_name, ")\n", NIL); } else if (numreq == numargs) { Printv(method_def, "(define-method (", goops_name, method_signature, ")\n", NIL); Printv(method_def, " (", primitive_name, primitive_args, "))\n", NIL); } else { /* Handle optional args. For the rest argument, use a name that cannot clash. */ Printv(method_def, "(define-method (", goops_name, method_signature, " . %args)\n", NIL); Printv(method_def, " (apply ", primitive_name, primitive_args, " %args))\n", NIL); } if (in_class) { /* Defer method definition till end of class definition. */ Printv(goops_class_methods, method_def, NIL); } else { Printv(goopscode, method_def, NIL); } Printf(goopsexport, "%s ", goops_name); Delete(primitive_name); Delete(goops_name); Delete(method_def); } if (procdoc) { String *returns_text = NewString(""); if (num_results == 0) Printv(returns_text, return_nothing_doc, NIL); else if (num_results == 1) Printv(returns_text, return_one_doc, NIL); else Printv(returns_text, return_multi_doc, NIL); /* Substitute documentation variables */ static const char *numbers[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve" }; if (num_results <= 12) Replaceall(returns_text, "$num_values", numbers[num_results]); else { String *num_results_str = NewStringf("%d", num_results); Replaceall(returns_text, "$num_values", num_results_str); Delete(num_results_str); } Replaceall(returns_text, "$values", returns); Printf(doc_body, "\n%s", returns_text); write_doc(proc_name, signature, doc_body); Delete(returns_text); } Delete(proc_name); Delete(outarg); Delete(cleanup); Delete(signature); Delete(method_signature); Delete(primitive_args); Delete(doc_body); Delete(returns_argout); Delete(returns); Delete(tmp); Delete(scheme_arg_names); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------ * variableWrapper() * * Create a link to a C variable. * This creates a single function PREFIX_var_VARNAME(). * This function takes a single optional argument. If supplied, it means * we are setting this variable to some value. If omitted, it means we are * simply evaluating this variable. Either way, we return the variables * value. * ------------------------------------------------------------ */ virtual int variableWrapper(Node *n) { char *name = GetChar(n, "name"); char *iname = GetChar(n, "sym:name"); SwigType *t = Getattr(n, "type"); String *proc_name; Wrapper *f; String *tm; if (!addSymbol(iname, n)) return SWIG_ERROR; f = NewWrapper(); // evaluation function names String *var_name = Swig_name_wrapper(iname); // Build the name for scheme. proc_name = NewString(iname); Replaceall(proc_name, "_", "-"); Setattr(n, "wrap:name", proc_name); int assignable = !is_immutable(n); { Printf(f->def, "static SCM\n%s(SCM s_0)\n{\n", var_name); /* Define the scheme name in C. This define is used by several Guile macros. */ Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL); Wrapper_add_local(f, "gswig_result", "SCM gswig_result"); if (assignable) { /* Check for a setting of the variable value */ Printf(f->code, "if (s_0 != SCM_UNDEFINED) {\n"); if ((tm = Swig_typemap_lookup("varin", n, name, 0))) { Replaceall(tm, "$input", "s_0"); /* Printv(f->code,tm,"\n",NIL); */ emit_action_code(n, f->code, tm); } else { // The fake variable constantWrapper() creates is immutable. assert(!GetFlag(n, "guile:reallywrappingaconstant")); throw_unhandled_guile_type_error(t); } Printf(f->code, "}\n"); } // Now return the value of the variable (regardless // of evaluating or setting) if ((tm = Swig_typemap_lookup("varout", n, name, 0))) { Replaceall(tm, "$result", "gswig_result"); /* Printv(f->code,tm,"\n",NIL); */ emit_action_code(n, f->code, tm); } else { if (GetFlag(n, "guile:reallywrappingaconstant")) { Delete(var_name); Delete(proc_name); DelWrapper(f); return SWIG_ERROR; } throw_unhandled_guile_type_error(t); } Printf(f->code, "\nreturn gswig_result;\n"); Printf(f->code, "#undef FUNC_NAME\n"); Printf(f->code, "}\n"); Wrapper_print(f, f_wrappers); // Now add symbol to the Guile interpreter if (!emit_setters || !assignable) { /* Read-only variables become a simple procedure returning the value; read-write variables become a simple procedure with an optional argument. */ if (!goops && GetFlag(n, "feature:constasvar")) { /* need to export this function as a variable instead of a procedure */ if (scmstub) { /* export the function in the wrapper, and (set!) it in scmstub */ Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, %d, 0, (swig_guile_proc) %s);\n", proc_name, assignable, var_name); Printf(scmtext, "(set! %s (%s))\n", proc_name, proc_name); } else { /* export the variable directly */ Printf(f_init, "scm_c_define(\"%s\", %s(SCM_UNDEFINED));\n", proc_name, var_name); } } else { /* Export the function as normal */ Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, %d, 0, (swig_guile_proc) %s);\n", proc_name, assignable, var_name); } } else { /* Read/write variables become a procedure with setter. */ Printf(f_init, "{ SCM p = scm_c_define_gsubr(\"%s\", 0, 1, 0, (swig_guile_proc) %s);\n", proc_name, var_name); Printf(f_init, "scm_c_define"); Printf(f_init, "(\"%s\", " "scm_make_procedure_with_setter(p, p)); }\n", proc_name); } Printf(exported_symbols, "\"%s\", ", proc_name); // export wrapper into goops file if (!in_class) { // only if the variable is not part of a class String *class_name = SwigType_typedef_resolve_all(SwigType_base(t)); String *goops_name = goopsNameMapping(proc_name, ""); String *primitive_name = NewString(""); if (primRenamer) Printv(primitive_name, "primitive:", NIL); Printv(primitive_name, proc_name, NIL); /* Simply re-export the procedure */ if ((!emit_setters || !assignable) && GetFlag(n, "feature:constasvar")) { Printv(goopscode, "(define ", goops_name, " (", primitive_name, "))\n", NIL); } else { Printv(goopscode, "(define ", goops_name, " ", primitive_name, ")\n", NIL); } Printf(goopsexport, "%s ", goops_name); Delete(primitive_name); Delete(class_name); Delete(goops_name); } if (procdoc) { /* Compute documentation */ String *signature = NewString(""); String *signature2 = NULL; String *doc = NewString(""); if (!assignable) { Printv(signature, proc_name, NIL); if (GetFlag(n, "feature:constasvar")) { Printv(doc, "Is constant ", NIL); } else { Printv(doc, "Returns constant ", NIL); } if ((tm = Getattr(n, "tmap:varout:doc"))) { Printv(doc, tm, NIL); } else { String *s = SwigType_str(t, 0); Chop(s); Printf(doc, "<%s>", s); Delete(s); } } else if (emit_setters) { Printv(signature, proc_name, NIL); signature2 = NewString(""); Printv(signature2, "set! (", proc_name, ") ", NIL); handle_documentation_typemap(signature2, NIL, n, "tmap:varin:arglist", "new-value"); Printv(doc, "Get or set the value of the C variable, \n", NIL); Printv(doc, "which is of type ", NIL); handle_documentation_typemap(doc, NIL, n, "tmap:varout:doc", "$1_type"); Printv(doc, "."); } else { Printv(signature, proc_name, " #:optional ", NIL); if ((tm = Getattr(n, "tmap:varin:doc"))) { Printv(signature, tm, NIL); } else { String *s = SwigType_str(t, 0); Chop(s); Printf(signature, "new-value <%s>", s); Delete(s); } Printv(doc, "If NEW-VALUE is provided, " "set C variable to this value.\n", NIL); Printv(doc, "Returns variable value ", NIL); if ((tm = Getattr(n, "tmap:varout:doc"))) { Printv(doc, tm, NIL); } else { String *s = SwigType_str(t, 0); Chop(s); Printf(doc, "<%s>", s); Delete(s); } } write_doc(proc_name, signature, doc, signature2); Delete(signature); if (signature2) Delete(signature2); Delete(doc); } } Delete(var_name); Delete(proc_name); DelWrapper(f); return SWIG_OK; } /* ------------------------------------------------------------ * constantWrapper() * * We create a read-only variable. * ------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { char *name = GetChar(n, "name"); char *iname = GetChar(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); int constasvar = GetFlag(n, "feature:constasvar"); String *proc_name; String *var_name; Wrapper *f; SwigType *nctype; String *tm; f = NewWrapper(); // Make a static variable; var_name = NewStringf("%sconst_%s", prefix, iname); // Strip const qualifier from type if present nctype = NewString(type); if (SwigType_isconst(nctype)) { Delete(SwigType_pop(nctype)); } // Build the name for scheme. proc_name = NewString(iname); Replaceall(proc_name, "_", "-"); // See if there's a typemap if ((tm = Swig_typemap_lookup("constant", n, name, 0))) { Replaceall(tm, "$value", value); Printv(f_header, tm, "\n", NIL); } else { // Create variable and assign it a value Printf(f_header, "static %s = (%s)(%s);\n", SwigType_str(type, var_name), SwigType_str(type, 0), value); } int result = SWIG_OK; if (Len(nctype) > 0) { /* Hack alert: will cleanup later -- Dave */ Node *nn = NewHash(); Setfile(nn, Getfile(n)); Setline(nn, Getline(n)); Setattr(nn, "name", var_name); Setattr(nn, "sym:name", iname); Setattr(nn, "type", nctype); SetFlag(nn, "feature:immutable"); if (constasvar) { SetFlag(nn, "feature:constasvar"); } SetFlag(nn, "guile:reallywrappingaconstant"); if (variableWrapper(nn) == SWIG_ERROR) { Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); result = SWIG_NOWRAP; } Delete(nn); } else { Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n"); result = SWIG_NOWRAP; } Delete(var_name); Delete(nctype); Delete(proc_name); DelWrapper(f); return result; } /* ------------------------------------------------------------ * classDeclaration() * ------------------------------------------------------------ */ virtual int classDeclaration(Node *n) { String *class_name = NewStringf("<%s>", Getattr(n, "sym:name")); Setattr(n, "guile:goopsclassname", class_name); return Language::classDeclaration(n); } /* ------------------------------------------------------------ * classHandler() * ------------------------------------------------------------ */ virtual int classHandler(Node *n) { /* Create new strings for building up a wrapper function */ have_constructor = 0; class_name = NewString(""); short_class_name = NewString(""); Printv(class_name, "<", Getattr(n, "sym:name"), ">", NIL); Printv(short_class_name, Getattr(n, "sym:name"), NIL); Replaceall(class_name, "_", "-"); Replaceall(short_class_name, "_", "-"); if (!addSymbol(class_name, n)) return SWIG_ERROR; /* Handle inheritance */ String *base_class = NewString("<"); List *baselist = Getattr(n, "bases"); if (baselist && Len(baselist)) { Iterator i = First(baselist); while (i.item) { Printv(base_class, Getattr(i.item, "sym:name"), NIL); i = Next(i); if (i.item) { Printf(base_class, "> <"); } } } Printf(base_class, ">"); Replaceall(base_class, "_", "-"); Printv(goopscode, "(define-class ", class_name, " ", NIL); Printf(goopsexport, "%s ", class_name); if (Len(base_class) > 2) { Printv(goopscode, "(", base_class, ")\n", NIL); } else { Printv(goopscode, "()\n", NIL); } SwigType *ct = NewStringf("p.%s", Getattr(n, "name")); swigtype_ptr = SwigType_manglestr(ct); String *mangled_classname = Swig_name_mangle_string(Getattr(n, "sym:name")); /* Export clientdata structure */ Printf(f_runtime, "static swig_guile_clientdata _swig_guile_clientdata%s = { NULL, SCM_EOL };\n", mangled_classname); Printv(f_init, "SWIG_TypeClientData(SWIGTYPE", swigtype_ptr, ", (void *) &_swig_guile_clientdata", mangled_classname, ");\n", NIL); SwigType_remember(ct); Delete(ct); /* Emit all of the members */ goops_class_methods = NewString(""); in_class = 1; Language::classHandler(n); in_class = 0; Printv(goopscode, " #:metaclass \n", NIL); if (have_constructor) Printv(goopscode, " #:new-function ", primRenamer ? "primitive:" : "", "new-", short_class_name, "\n", NIL); Printf(goopscode, ")\n%s\n", goops_class_methods); Delete(goops_class_methods); goops_class_methods = 0; /* export class initialization function */ if (goops) { /* export the wrapper function */ String *funcName = NewString(mangled_classname); Printf(funcName, "_swig_guile_setgoopsclass"); String *guileFuncName = NewString(funcName); Replaceall(guileFuncName, "_", "-"); Printv(f_wrappers, "static SCM ", funcName, "(SCM cl) \n", NIL); Printf(f_wrappers, "#define FUNC_NAME %s\n{\n", guileFuncName); Printv(f_wrappers, " ((swig_guile_clientdata *)(SWIGTYPE", swigtype_ptr, "->clientdata))->goops_class = cl;\n", NIL); Printf(f_wrappers, " return SCM_UNSPECIFIED;\n"); Printf(f_wrappers, "}\n#undef FUNC_NAME\n\n"); Printf(f_init, "scm_c_define_gsubr(\"%s\", 1, 0, 0, (swig_guile_proc) %s);\n", guileFuncName, funcName); Printf(exported_symbols, "\"%s\", ", guileFuncName); /* export the call to the wrapper function */ Printf(goopscode, "(%s%s %s)\n\n", primRenamer ? "primitive:" : "", guileFuncName, class_name); Delete(guileFuncName); Delete(funcName); } Delete(mangled_classname); Delete(swigtype_ptr); swigtype_ptr = 0; Delete(class_name); Delete(short_class_name); class_name = 0; short_class_name = 0; return SWIG_OK; } /* ------------------------------------------------------------ * memberfunctionHandler() * ------------------------------------------------------------ */ int memberfunctionHandler(Node *n) { String *iname = Getattr(n, "sym:name"); String *proc = NewString(iname); Replaceall(proc, "_", "-"); memberfunction_name = goopsNameMapping(proc, short_class_name); Language::memberfunctionHandler(n); Delete(memberfunction_name); memberfunction_name = NULL; Delete(proc); return SWIG_OK; } /* ------------------------------------------------------------ * membervariableHandler() * ------------------------------------------------------------ */ int membervariableHandler(Node *n) { String *iname = Getattr(n, "sym:name"); if (emit_setters) { struct_member = 1; Printf(f_init, "{\n"); } Language::membervariableHandler(n); if (emit_setters) { Printf(f_init, "}\n"); struct_member = 0; } String *proc = NewString(iname); Replaceall(proc, "_", "-"); String *goops_name = goopsNameMapping(proc, short_class_name); /* The slot name is never qualified with the class, even if useclassprefix is true. */ Printv(goopscode, " (", proc, " #:allocation #:virtual", NIL); /* GOOPS (at least in Guile 1.6.3) only accepts closures, not primitive procedures for slot-ref and slot-set. */ Printv(goopscode, "\n #:slot-ref (lambda (obj) (", primRenamer ? "primitive:" : "", short_class_name, "-", proc, "-get", " obj))", NIL); if (!GetFlag(n, "feature:immutable")) { Printv(goopscode, "\n #:slot-set! (lambda (obj value) (", primRenamer ? "primitive:" : "", short_class_name, "-", proc, "-set", " obj value))", NIL); } else { Printf(goopscode, "\n #:slot-set! (lambda (obj value) (error \"Immutable slot\"))"); } if (emit_slot_accessors) { if (GetFlag(n, "feature:immutable")) { Printv(goopscode, "\n #:getter ", goops_name, NIL); } else { Printv(goopscode, "\n #:accessor ", goops_name, NIL); } Printf(goopsexport, "%s ", goops_name); } Printv(goopscode, ")\n", NIL); Delete(proc); Delete(goops_name); return SWIG_OK; } /* ------------------------------------------------------------ * constructorHandler() * ------------------------------------------------------------ */ int constructorHandler(Node *n) { Language::constructorHandler(n); have_constructor = 1; return SWIG_OK; } /* ------------------------------------------------------------ * destructorHandler() * ------------------------------------------------------------ */ virtual int destructorHandler(Node *n) { exporting_destructor = true; Language::destructorHandler(n); exporting_destructor = false; return SWIG_OK; } /* ------------------------------------------------------------ * pragmaDirective() * ------------------------------------------------------------ */ virtual int pragmaDirective(Node *n) { if (!ImportMode) { String *lang = Getattr(n, "lang"); String *cmd = Getattr(n, "name"); String *value = Getattr(n, "value"); # define store_pragma(PRAGMANAME) \ if (Strcmp(cmd, #PRAGMANAME) == 0) { \ if (PRAGMANAME) Delete(PRAGMANAME); \ PRAGMANAME = value ? NewString(value) : NULL; \ } if (Strcmp(lang, "guile") == 0) { store_pragma(beforereturn) store_pragma(return_nothing_doc) store_pragma(return_one_doc) store_pragma(return_multi_doc); # undef store_pragma } } return Language::pragmaDirective(n); } /* ------------------------------------------------------------ * goopsNameMapping() * Maps the identifier from C++ to the GOOPS based * on command * line parameters and such. * If class_name = "" that means the mapping is for a function or * variable not attached to any class. * ------------------------------------------------------------ */ String *goopsNameMapping(String *name, const_String_or_char_ptr class_name) { String *n = NewString(""); if (Strcmp(class_name, "") == 0) { // not part of a class, so no class name to prefix if (goopsprefix) { Printf(n, "%s%s", goopsprefix, name); } else { Printf(n, "%s", name); } } else { if (useclassprefix) { Printf(n, "%s-%s", class_name, name); } else { if (goopsprefix) { Printf(n, "%s%s", goopsprefix, name); } else { Printf(n, "%s", name); } } } return n; } /* ------------------------------------------------------------ * validIdentifier() * ------------------------------------------------------------ */ virtual int validIdentifier(String *s) { char *c = Char(s); /* Check whether we have an R5RS identifier. Guile supports a superset of R5RS identifiers, but it's probably a bad idea to use those. */ /* --> * | */ /* --> | */ if (!(isalpha(*c) || (*c == '!') || (*c == '$') || (*c == '%') || (*c == '&') || (*c == '*') || (*c == '/') || (*c == ':') || (*c == '<') || (*c == '=') || (*c == '>') || (*c == '?') || (*c == '^') || (*c == '_') || (*c == '~'))) { /* --> + | - | ... */ if ((strcmp(c, "+") == 0) || strcmp(c, "-") == 0 || strcmp(c, "...") == 0) return 1; else return 0; } /* --> | | */ while (*c) { if (!(isalnum(*c) || (*c == '!') || (*c == '$') || (*c == '%') || (*c == '&') || (*c == '*') || (*c == '/') || (*c == ':') || (*c == '<') || (*c == '=') || (*c == '>') || (*c == '?') || (*c == '^') || (*c == '_') || (*c == '~') || (*c == '+') || (*c == '-') || (*c == '.') || (*c == '@'))) return 0; c++; } return 1; } String *runtimeCode() { String *s; s = Swig_include_sys("guile_scm_run.swg"); if (!s) { Printf(stderr, "*** Unable to open 'guile_scm_run.swg"); s = NewString(""); } return s; } String *defaultExternalRuntimeFilename() { return NewString("swigguilerun.h"); } }; /* ----------------------------------------------------------------------------- * swig_guile() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_guile() { return new GUILE(); } extern "C" Language *swig_guile(void) { return new_swig_guile(); } swig-4.4.0/Source/Modules/go.cxx0000664000175000017500000050555415075443613016377 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * go.cxx * * Go language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include /* ---------------------------------------------------------------------- * 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; ilo = 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 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; /* 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_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), 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_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_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; directorLanguage(); 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 saw_nocgo_flag = 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); } else if (strcmp(argv[i], "-no-cgo") == 0) { Swig_mark_arg(i); saw_nocgo_flag = true; } 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) { Printf(stdout, "%s\n", usage); } } } if (saw_nocgo_flag) { Printf(stderr, "SWIG -go: -no-cgo option is no longer supported\n"); Exit(EXIT_FAILURE); } if (gccgo_flag && !pkgpath_option && !prefix_option) { prefix_option = NewString("go"); } // Add preprocessor symbol to parser. Preprocessor_define("SWIGGO 1", 0); if (gccgo_flag) { Preprocessor_define("SWIGGO_GCCGO 1", 0); } 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); } SWIG_config_file("go.swg"); allow_overloading(); } /* --------------------------------------------------------------------- * top() * * For gc, 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); // 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); 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); Exit(EXIT_FAILURE); } if (Swig_directors_enabled()) { if (!c_filename_h) { Printf(stderr, "Unable to determine outfile_h\n"); Exit(EXIT_FAILURE); } f_c_directors_h = NewFile(c_filename_h, "w", SWIG_output_files()); if (!f_c_directors_h) { FileErrorDisplay(c_filename_h); Exit(EXIT_FAILURE); } } f_go_begin = NewFile(go_filename, "w", SWIG_output_files()); if (!f_go_begin) { FileErrorDisplay(go_filename); 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(""); 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); Swig_register_filebyname("cgo_comment", f_cgo_comment); Swig_register_filebyname("cgo_comment_typedefs", f_cgo_comment_typedefs); Swig_banner(f_c_begin); Swig_obligatory_macros(f_c_runtime, "GO"); 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 (Swig_directors_enabled()) { 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); 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)); // 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 (Swig_directors_enabled()) { // 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 (Swig_directors_enabled()) { 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); // 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); bool need_panic = false; if (Strstr(f_c_runtime, "SWIG_contract_assert(") != 0 || Strstr(f_c_wrappers, "SWIG_contract_assert(") != 0) { Printv(f_c_begin, "\n#define SWIG_contract_assert(expr, msg) if (!(expr)) { _swig_gopanic(msg); } else\n\n", NULL); need_panic = true; } if (!gccgo_flag && (need_panic || Strstr(f_c_runtime, "_swig_gopanic") != 0 || Strstr(f_c_wrappers, "_swig_gopanic") != 0)) { Printv(f_go_header, "//export cgo_panic_", unique_id, "\n", NULL); Printv(f_go_header, "func cgo_panic_", unique_id, "(p *byte) {\n", NULL); Printv(f_go_header, "\ts := (*[1024]byte)(unsafe.Pointer(p))[:]\n", NULL); Printv(f_go_header, "\tfor i, b := range s {\n", NULL); Printv(f_go_header, "\t\tif b == 0 {\n", NULL); Printv(f_go_header, "\t\t\tpanic(string(s[:i]))\n", NULL); Printv(f_go_header, "\t\t}\n", NULL); Printv(f_go_header, "\t}\n", NULL); Printv(f_go_header, "\tpanic(string(s))\n", NULL); Printv(f_go_header, "}\n\n", NULL); Printv(f_c_begin, "\nextern\n", NULL); Printv(f_c_begin, "#ifdef __cplusplus\n", NULL); Printv(f_c_begin, " \"C\"\n", NULL); Printv(f_c_begin, "#endif\n", NULL); Printv(f_c_begin, " void cgo_panic_", unique_id, "(const char*);\n", NULL); Printv(f_c_begin, "static void _swig_gopanic(const char *p) {\n", NULL); Printv(f_c_begin, " cgo_panic_", unique_id, "(p);\n", NULL); Printv(f_c_begin, "}\n\n", NULL); } Dump(f_c_runtime, f_c_begin); Dump(f_c_wrappers, f_c_begin); Dump(f_c_init, f_c_begin); 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 (Swig_directors_enabled()) { Dump(f_go_directors, f_go_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); Delete(f_cgo_comment); Delete(f_cgo_comment_typedefs); Delete(f_c_begin); Delete(f_go_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; } 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); if (!Equal(Getattr(n, "wrap:action"), director_prot_ctor_code)) { 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, 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. * 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 *go_name, String *overname, String *wname, List *base, ParmList *parms, SwigType *result, bool is_static) { assert(result); int ret = SWIG_OK; int r = makeCgoWrappers(n, go_name, overname, wname, base, parms, result, is_static); 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, " := ", NULL); bool need_close = false; if ((i == 0 && info->is_destructor) || ((i > 0 || !info->receiver || info->base || info->is_constructor) && goTypeIsInterface(p, pt))) { Printv(f_go_wrappers, "getSwigcptr(", NULL); need_close = true; } Printv(f_go_wrappers, ln, NULL); if (need_close) { Printv(f_go_wrappers, ")", 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) && Getattr(p, "tmap:gotype") == NULL) { 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; } /* ---------------------------------------------------------------------- * 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; } /* ----------------------------------------------------------------------- * 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) { 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(""); 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); } Swig_restore(n); } /* ----------------------------------------------------------------------- * argout() * * Handle argument output code 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 argout(ParmList *parms, Wrapper *f) { Parm *p = parms; while (p) { String *tm = Getattr(p, "tmap:argout"); if (!tm) { p = nextSibling(p); } else { tm = Copy(tm); Replaceall(tm, "$result", Swig_cresult_name()); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(f->code, tm, "\n", NULL); Delete(tm); p = Getattr(p, "tmap:argout:next"); } } } /* ----------------------------------------------------------------------- * goargout() * * Handle Go argument output code if any. This is used for the Go * function. This assumes that each parameter has an "emit:goinput" * property with the name to use to refer to that parameter. * ----------------------------------------------------------------------- */ void goargout(ParmList *parms) { Parm *p = parms; while (p) { String *tm = Getattr(p, "tmap:goargout"); if (!tm) { p = nextSibling(p); } else { tm = Copy(tm); Replaceall(tm, "$result", "swig_r"); Replaceall(tm, "$input", Getattr(p, "emit:goinput")); Printv(f_go_wrappers, tm, "\n", NULL); Delete(tm); p = Getattr(p, "tmap:goargout:next"); } } // If we need to memcpy a parameter to pass it to the C code, the // compiler may think that the parameter is not live during the // function call. If the garbage collector runs while the C/C++ // function is running, the parameter may be freed. Force the // compiler to see the parameter as live across the C/C++ function. int parm_count = emit_num_arguments(parms); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); bool c_struct_type; Delete(cgoTypeForGoValue(p, Getattr(p, "type"), &c_struct_type)); if (c_struct_type) { 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); } } /* ----------------------------------------------------------------------- * freearg() * * Handle argument cleanup code 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. * ----------------------------------------------------------------------- */ String *freearg(ParmList *parms) { String *ret = NewString(""); Parm *p = parms; while (p) { String *tm = Getattr(p, "tmap:freearg"); if (!tm) { p = nextSibling(p); } else { tm = Copy(tm); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(ret, tm, "\n", NULL); Delete(tm); p = Getattr(p, "tmap:freearg:next"); } } return ret; } /* ----------------------------------------------------------------------- * cleanupFunction() * * Final function cleanup code. * ----------------------------------------------------------------------- */ void cleanupFunction(Node *n, Wrapper *f, ParmList *parms) { SwigType *returntype = Getattr(n, "type"); String *cleanup = freearg(parms); Printv(f->code, cleanup, NULL); if (GetFlag(n, "feature:new")) { String *tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0); if (tm) { Printv(f->code, tm, "\n", NULL); Delete(tm); } } Replaceall(f->code, "$cleanup", cleanup); Delete(cleanup); /* See if there is any return cleanup code */ String *tm; if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(f->code, "$symname", Getattr(n, "sym:name")); } /* ----------------------------------------------------------------------- * variableHandler() * * This exists just to set the making_variable_wrappers flag. * ----------------------------------------------------------------------- */ virtual int variableHandler(Node *n) { assert(!making_variable_wrappers); making_variable_wrappers = true; int r = Language::variableHandler(n); making_variable_wrappers = false; return r; } /* ----------------------------------------------------------------------- * constantWrapper() * * Product a const declaration. * ------------------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { SwigType *type = Getattr(n, "type"); if (Swig_storage_isstatic(n)) { return goComplexConstant(n, type); } String *value = Getattr(n, "value"); String *copy = NULL; int typecode = SwigType_type(type); if (typecode == T_STRING) { String *stringval = Getattr(n, "stringval"); if (!stringval) { return goComplexConstant(n, type); } // Backslash sequences are somewhat different in Go and C/C++. copy = NewStringf("\"%(goescape)s\"", stringval); value = copy; } else if (typecode == T_CHAR) { String *stringval = Getattr(n, "stringval"); if (!stringval || Len(stringval) != 1) { return goComplexConstant(n, type); } // Backslash sequences are somewhat different in Go and C/C++. copy = NewStringf("'%(goescape)s'", stringval); value = copy; } else if (!SwigType_issimple(type)) { return goComplexConstant(n, type); } else if (Swig_storage_isstatic(n)) { return goComplexConstant(n, type); } else if (Getattr(n, "numval")) { value = Getattr(n, "numval"); if (typecode == T_BOOL) { copy = NewString(*Char(value) == '0' ? "false" : "true"); value = copy; } } else { // Currently numval only gets set for integer and boolean literals, so // check for a floating point literal we can just use in Go here. // // Accept digits, decimal point, and exponentiation. Treat anything else // as too complicated to handle as a Go constant. char *p = Char(value); for (int i = 0; p[i]; ++i) { switch (p[i]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': case 'e': case 'E': case '+': case '-': break; default: return goComplexConstant(n, type); } } } String *go_name = buildGoName(Getattr(n, "sym:name"), false, false); if (!checkNameConflict(go_name, n, NULL)) { Delete(go_name); Delete(copy); return SWIG_NOWRAP; } String *tm = goType(n, type); Printv(f_go_wrappers, "const ", go_name, " ", tm, " = ", value, "\n", NIL); Delete(tm); Delete(go_name); Delete(copy); return SWIG_OK; } /* ---------------------------------------------------------------------- * enumDeclaration() * * A C++ enum type turns into a Named go int type. * ---------------------------------------------------------------------- */ virtual int enumDeclaration(Node *n) { if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; String *name = goEnumName(n); if (Strcmp(name, "int") != 0) { if (!ImportMode || !imported_package) { if (!checkNameConflict(name, n, NULL)) { Delete(name); return SWIG_NOWRAP; } Printv(f_go_wrappers, "type ", name, " int\n", NULL); } else { String *nw = NewString(""); Printv(nw, getModuleName(imported_package), ".", name, NULL); Setattr(n, "go:enumname", nw); } } Delete(name); return Language::enumDeclaration(n); } /* ----------------------------------------------------------------------- * enumvalueDeclaration() * * Declare a single value of an enum type. We fetch the value by * calling a C/C++ function. * ------------------------------------------------------------------------ */ virtual int enumvalueDeclaration(Node *n) { if (!is_public(n)) { return SWIG_OK; } Swig_require("enumvalueDeclaration", n, "*sym:name", NIL); Node *parent = parentNode(n); if (Getattr(parent, "unnamed")) { Setattr(n, "type", NewString("int")); } else { Setattr(n, "type", Getattr(parent, "enumtype")); } if (GetFlag(parent, "scopedenum")) { String *symname = Getattr(n, "sym:name"); symname = Swig_name_member(0, Getattr(parent, "sym:name"), symname); Setattr(n, "sym:name", symname); Delete(symname); } int ret = goComplexConstant(n, Getattr(n, "type")); Swig_restore(n); return ret; } /* ----------------------------------------------------------------------- * goComplexConstant() * * Handle a const declaration for something which is not a Go constant. * ------------------------------------------------------------------------ */ int goComplexConstant(Node *n, SwigType *type) { String *symname = Getattr(n, "sym:name"); if (!symname) { symname = Getattr(n, "name"); } String *varname = buildGoName(symname, true, false); if (!checkNameConflict(varname, n, NULL)) { Delete(varname); return SWIG_NOWRAP; } if (!Getattr(n, "stringval") && !Getattr(n, "enumvalueDeclaration:sym:name")) { // Based on Swig_VargetToFunction String *nname = NewStringf("(%s)", Getattr(n, "value")); String *call; if (SwigType_isclass(type)) { call = NewStringf("%s", nname); } else { call = SwigType_lcaststr(type, nname); } String *cres = Swig_cresult(type, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres); Delete(nname); Delete(call); Delete(cres); } else { String *get = NewString(""); Printv(get, Swig_cresult_name(), " = ", NULL); if (SwigType_type(type) == T_STRING) { Printv(get, "(char *)", NULL); } Printv(get, Getattr(n, "value"), NULL); Printv(get, ";\n", NULL); Setattr(n, "wrap:action", get); Delete(get); } String *sname = Copy(symname); if (class_name) { Append(sname, "_"); Append(sname, class_name); } String *go_name = NewString("_swig_get"); if (class_name) { Append(go_name, class_name); Append(go_name, "_"); } Append(go_name, sname); String *wname = Swig_name_wrapper(sname); Append(wname, unique_id); Setattr(n, "wrap:name", wname); int r = makeWrappers(n, go_name, NULL, wname, NULL, NULL, type, true); if (r != SWIG_OK) { return r; } String *t = goType(n, type); Printv(f_go_wrappers, "var ", varname, " ", t, " = ", go_name, "()\n", NULL); Delete(varname); Delete(t); Delete(go_name); Delete(sname); return SWIG_OK; } /* ------------------------------------------------------------ * classHandler() * * For a C++ class, in Go we generate both a struct and an * interface. The interface will declare all the class public * methods. We will define all the methods on the struct, so that * the struct meets the interface. We then expect users of the * class to use the interface. * ------------------------------------------------------------ */ virtual int classHandler(Node *n) { class_node = n; List *baselist = Getattr(n, "bases"); bool has_base_classes = baselist && Len(baselist) > 0; String *name = Getattr(n, "sym:name"); String *go_name = exportedName(name); if (!checkNameConflict(go_name, n, NULL)) { Delete(go_name); SetFlag(n, "go:conflict"); return SWIG_NOWRAP; } String *go_type_name = goCPointerType(Getattr(n, "classtypeobj"), true); class_name = name; class_receiver = go_type_name; class_methods = NewHash(); int isdir = GetFlag(n, "feature:director"); int isnodir = GetFlag(n, "feature:nodirector"); bool is_director = isdir && !isnodir; Printv(f_go_wrappers, "type ", go_type_name, " uintptr\n\n", NULL); // A method to return the pointer to the C++ class. This is used // by generated code to convert between the interface and the C++ // value. Printv(f_go_wrappers, "func (p ", go_type_name, ") Swigcptr() uintptr {\n", NULL); Printv(f_go_wrappers, "\treturn (uintptr)(p)\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); // A method used as a marker for the class, to avoid invalid // interface conversions when using multiple inheritance. Printv(f_go_wrappers, "func (p ", go_type_name, ") SwigIs", go_name, "() {\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); if (is_director) { // Return the interface passed to the NewDirector function. Printv(f_go_wrappers, "func (p ", go_type_name, ") DirectorInterface() interface{} {\n", NULL); Printv(f_go_wrappers, "\treturn nil\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); } // We have seen a definition for this type. Setattr(defined_types, go_name, go_name); Setattr(defined_types, go_type_name, go_type_name); interfaces = NewString(""); int r = Language::classHandler(n); if (r != SWIG_OK) { return r; } if (has_base_classes) { // For each method defined in a base class but not defined in // this class, we need to define the method in this class. We // can't use anonymous field inheritance because it works // differently in Go and in C++. Hash *local = NewHash(); for (Node *ni = Getattr(n, "firstChild"); ni; ni = nextSibling(ni)) { if (!is_public(ni)) { continue; } String *type = Getattr(ni, "nodeType"); if (Cmp(type, "constructor") == 0 || Cmp(type, "destructor") == 0) { continue; } String *cname = Getattr(ni, "sym:name"); if (!cname) { cname = Getattr(ni, "name"); } if (cname) { Setattr(local, cname, NewString("")); } } for (Iterator b = First(baselist); b.item; b = Next(b)) { List *bases = NewList(); Append(bases, Getattr(b.item, "classtype")); int r = addBase(n, b.item, bases, local); if (r != SWIG_OK) { return r; } Delete(bases); } Delete(local); Hash *parents = NewHash(); addFirstBaseInterface(n, parents, baselist); int r = addExtraBaseInterfaces(n, parents, baselist); Delete(parents); if (r != SWIG_OK) { return r; } } Printv(f_go_wrappers, "type ", go_name, " interface {\n", NULL); Printv(f_go_wrappers, "\tSwigcptr() uintptr\n", NULL); Printv(f_go_wrappers, "\tSwigIs", go_name, "()\n", NULL); if (is_director) { Printv(f_go_wrappers, "\tDirectorInterface() interface{}\n", NULL); } Append(f_go_wrappers, interfaces); Printv(f_go_wrappers, "}\n\n", NULL); Delete(interfaces); interfaces = NULL; class_name = NULL; class_receiver = NULL; class_node = NULL; Delete(class_methods); class_methods = NULL; Delete(go_type_name); return SWIG_OK; } /* ------------------------------------------------------------ * addBase() * * Implement methods and members defined in a parent class for a * child class. * ------------------------------------------------------------ */ int addBase(Node *n, Node *base, List *bases, Hash *local) { if (GetFlag(base, "feature:ignore")) { return SWIG_OK; } for (Node *ni = Getattr(base, "firstChild"); ni; ni = nextSibling(ni)) { int r = goBaseEntry(n, bases, local, ni); if (r != SWIG_OK) { return r; } } List *baselist = Getattr(base, "bases"); if (baselist && Len(baselist) > 0) { for (Iterator b = First(baselist); b.item; b = Next(b)) { List *nb = Copy(bases); Append(nb, Getattr(b.item, "classtype")); int r = addBase(n, b.item, nb, local); Delete(nb); if (r != SWIG_OK) { return r; } } } return SWIG_OK; } /* ------------------------------------------------------------ * goBaseEntry() * * Implement one entry defined in a parent class for a child class. * n is the child class. * ------------------------------------------------------------ */ int goBaseEntry(Node* n, List* bases, Hash *local, Node* entry) { if (GetFlag(entry, "feature:ignore")) { return SWIG_OK; } if (!is_public(entry)) { return SWIG_OK; } String *type = Getattr(entry, "nodeType"); if (Strcmp(type, "constructor") == 0 || Strcmp(type, "destructor") == 0 || Strcmp(type, "enum") == 0 || Strcmp(type, "using") == 0 || Strcmp(type, "classforward") == 0 || Strcmp(type, "template") == 0) { return SWIG_OK; } if (Strcmp(type, "extend") == 0) { for (Node* extend = firstChild(entry); extend; extend = nextSibling(extend)) { if (isStatic(extend)) { // If we don't do this, the extend_default test case fails. continue; } int r = goBaseEntry(n, bases, local, extend); if (r != SWIG_OK) { return r; } } return SWIG_OK; } String *storage = Getattr(entry, "storage"); if (storage && (Strcmp(storage, "typedef") == 0 || Strstr(storage, "friend"))) { return SWIG_OK; } String *mname = Getattr(entry, "sym:name"); if (!mname) { return SWIG_OK; } String *lname = Getattr(entry, "name"); if (Getattr(class_methods, lname)) { return SWIG_OK; } if (Getattr(local, lname)) { return SWIG_OK; } Setattr(local, lname, NewString("")); String *ty = NewString(Getattr(entry, "type")); SwigType_push(ty, Getattr(entry, "decl")); String *fullty = SwigType_typedef_resolve_all(ty); bool is_function = SwigType_isfunction(fullty) ? true : false; Delete(ty); Delete(fullty); if (is_function) { int r = goBaseMethod(n, bases, entry); if (r != SWIG_OK) { return r; } if (Getattr(entry, "sym:overloaded")) { for (Node *on = Getattr(entry, "sym:nextSibling"); on; on = Getattr(on, "sym:nextSibling")) { r = goBaseMethod(n, bases, on); if (r != SWIG_OK) { return r; } } String *receiver = class_receiver; bool is_static = isStatic(entry); if (is_static) { receiver = NULL; } String *go_name = buildGoName(Getattr(entry, "sym:name"), is_static, false); r = makeDispatchFunction(entry, go_name, receiver, is_static, NULL, false); Delete(go_name); if (r != SWIG_OK) { return r; } } } else { int r = goBaseVariable(n, bases, entry); if (r != SWIG_OK) { return r; } } return SWIG_OK; } /* ------------------------------------------------------------ * goBaseMethod() * * Implement a method defined in a parent class for a child class. * ------------------------------------------------------------ */ int goBaseMethod(Node *method_class, List *bases, Node *method) { String *symname = Getattr(method, "sym:name"); if (!validIdentifier(symname)) { return SWIG_OK; } String *name = NewString(""); Printv(name, Getattr(method_class, "sym:name"), "_", symname, NULL); bool is_static = isStatic(method); String *go_name = buildGoName(name, is_static, false); String *overname = NULL; if (Getattr(method, "sym:overloaded")) { overname = Getattr(method, "sym:overname"); } String *wname = Swig_name_wrapper(name); if (overname) { Append(wname, overname); } Append(wname, unique_id); String *result = NewString(Getattr(method, "type")); SwigType_push(result, Getattr(method, "decl")); if (SwigType_isqualifier(result)) { Delete(SwigType_pop(result)); } Delete(SwigType_pop_function(result)); // If the base method is imported, wrap:action may not be set. Swig_save("goBaseMethod", method, "wrap:name", "wrap:action", "parms", NULL); Setattr(method, "wrap:name", wname); if (!Getattr(method, "wrap:action")) { if (!is_static) { Swig_MethodToFunction(method, getNSpace(), getClassType(), (Getattr(method, "template") ? SmartPointer : Extend | SmartPointer), NULL, false); // Remove any self parameter that was just added. ParmList *parms = Getattr(method, "parms"); if (parms && Getattr(parms, "self")) { parms = CopyParmList(nextSibling(parms)); Setattr(method, "parms", parms); } } else { String *call = Swig_cfunction_call(Getattr(method, "name"), Getattr(method, "parms")); Setattr(method, "wrap:action", Swig_cresult(Getattr(method, "type"), Swig_cresult_name(), call)); } } // A method added by %extend in a base class may have void parms. ParmList* parms = Getattr(method, "parms"); if (parms != NULL && SwigType_type(Getattr(parms, "type")) == T_VOID) { parms = NULL; } int r = makeWrappers(method, go_name, overname, wname, bases, parms, result, is_static); Swig_restore(method); Delete(result); Delete(go_name); Delete(name); return r; } /* ------------------------------------------------------------ * goBaseVariable() * * Add accessors for a member variable defined in a parent class for * a child class. * ------------------------------------------------------------ */ int goBaseVariable(Node *var_class, List *bases, Node *var) { if (isStatic(var)) { return SWIG_OK; } String *var_name = buildGoName(Getattr(var, "sym:name"), false, false); Swig_save("goBaseVariable", var, "type", "wrap:action", NULL); // For a pointer type we apparently have to wrap in the decl. SwigType *var_type = NewString(Getattr(var, "type")); SwigType_push(var_type, Getattr(var, "decl")); Setattr(var, "type", var_type); SwigType *vt = Copy(var_type); int flags = Extend | SmartPointer | use_naturalvar_mode(var); if (isNonVirtualProtectedAccess(var)) { flags |= CWRAP_ALL_PROTECTED_ACCESS; } // Copied from Swig_wrapped_member_var_type. if (SwigType_isclass(vt)) { if (flags & CWRAP_NATURAL_VAR) { if (CPlusPlus) { if (!SwigType_isconst(vt)) { SwigType_add_qualifier(vt, "const"); } SwigType_add_reference(vt); } } else { SwigType_add_pointer(vt); } } String *mname = Swig_name_member(getNSpace(), Getattr(var_class, "sym:name"), var_name); if (!is_immutable(var)) { for (Iterator ki = First(var); ki.key; ki = Next(ki)) { if (Strncmp(ki.key, "tmap:", 5) == 0) { Delattr(var, ki.key); } } Swig_save("goBaseVariableSet", var, "name", "sym:name", "type", NULL); String *mname_set = NewString("Set"); Append(mname_set, mname); String *go_name = NewString("Set"); Append(go_name, var_name); Swig_MembersetToFunction(var, class_name, flags); String *wname = Swig_name_wrapper(mname_set); Append(wname, unique_id); ParmList *parms = NewParm(vt, var_name, var); String *result = NewString("void"); int r = makeWrappers(var, go_name, NULL, wname, bases, parms, result, false); if (r != SWIG_OK) { return r; } Delete(wname); Delete(parms); Delete(result); Delete(go_name); Delete(mname_set); Swig_restore(var); for (Iterator ki = First(var); ki.key; ki = Next(ki)) { if (Strncmp(ki.key, "tmap:", 5) == 0) { Delattr(var, ki.key); } } } Swig_MembergetToFunction(var, class_name, flags); String *mname_get = NewString("Get"); Append(mname_get, mname); String *go_name = NewString("Get"); Append(go_name, var_name); String *wname = Swig_name_wrapper(mname_get); Append(wname, unique_id); int r = makeWrappers(var, go_name, NULL, wname, bases, NULL, vt, false); if (r != SWIG_OK) { return r; } Delete(wname); Delete(mname_get); Delete(go_name); Delete(mname); Delete(var_name); Delete(var_type); Delete(vt); Swig_restore(var); return SWIG_OK; } /* ------------------------------------------------------------ * addFirstBaseInterface() * * When a C++ class uses multiple inheritance, we can use the C++ * pointer for the first base class but not for any subsequent base * classes. However, the Go interface will match the interface for * all the base classes. To avoid accidentally treating a class as * a pointer to a base class other than the first one, we use an * isClassname method. This function adds those methods as * required. * * For convenience when using multiple inheritance, we also add * functions to retrieve the base class pointers. * ------------------------------------------------------------ */ void addFirstBaseInterface(Node *n, Hash *parents, List *bases) { if (!bases || Len(bases) == 0) { return; } Iterator b = First(bases); if (!GetFlag(b.item, "feature:ignore")) { String *go_name = buildGoName(Getattr(n, "sym:name"), false, false); String *go_type_name = goCPointerType(Getattr(n, "classtypeobj"), true); String *go_base_name = exportedName(Getattr(b.item, "sym:name")); String *go_base_type = goType(n, Getattr(b.item, "classtypeobj")); String *go_base_type_name = goCPointerType(Getattr(b.item, "classtypeobj"), true); Printv(f_go_wrappers, "func (p ", go_type_name, ") SwigIs", go_base_name, "() {\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(interfaces, "\tSwigIs", go_base_name, "()\n", NULL); Printv(f_go_wrappers, "func (p ", go_type_name, ") SwigGet", go_base_name, "() ", go_base_type, " {\n", NULL); Printv(f_go_wrappers, "\treturn ", go_base_type_name, "(getSwigcptr(p))\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(interfaces, "\tSwigGet", go_base_name, "() ", go_base_type, "\n", NULL); Setattr(parents, go_base_name, NewString("")); Delete(go_name); Delete(go_type_name); Delete(go_base_type); Delete(go_base_type_name); } addFirstBaseInterface(n, parents, Getattr(b.item, "bases")); } /* ------------------------------------------------------------ * addExtraBaseInterfaces() * * Add functions to retrieve the base class pointers for all base * classes other than the first. * ------------------------------------------------------------ */ int addExtraBaseInterfaces(Node *n, Hash *parents, List *bases) { Iterator b = First(bases); Node *fb = b.item; for (b = Next(b); b.item; b = Next(b)) { if (GetFlag(b.item, "feature:ignore")) { continue; } String *go_base_name = exportedName(Getattr(b.item, "sym:name")); Swig_save("addExtraBaseInterface", n, "wrap:action", "wrap:name", "wrap:parms", NULL); SwigType *type = Copy(Getattr(n, "classtypeobj")); SwigType_add_pointer(type); Parm *parm = NewParm(type, "self", n); Setattr(n, "wrap:parms", parm); String *pn = Swig_cparm_name(parm, 0); String *action = NewString(""); Printv(action, Swig_cresult_name(), " = (", Getattr(b.item, "classtype"), "*)", pn, ";", NULL); Delete(pn); Setattr(n, "wrap:action", action); String *name = Copy(class_name); Append(name, "_SwigGet"); Append(name, go_base_name); String *go_name = NewString("SwigGet"); String *c1 = exportedName(go_base_name); Append(go_name, c1); Delete(c1); String *wname = Swig_name_wrapper(name); Append(wname, unique_id); Setattr(n, "wrap:name", wname); SwigType *result = Copy(Getattr(b.item, "classtypeobj")); SwigType_add_pointer(result); int r = makeWrappers(n, go_name, NULL, wname, NULL, parm, result, false); if (r != SWIG_OK) { return r; } Swig_restore(n); Setattr(parents, go_base_name, NewString("")); Delete(go_name); Delete(type); Delete(parm); Delete(action); Delete(result); String *ns = NewString(""); addParentExtraBaseInterfaces(n, parents, b.item, false, ns); Delete(ns); } if (!GetFlag(fb, "feature:ignore")) { String *ns = NewString(""); addParentExtraBaseInterfaces(n, parents, fb, true, ns); Delete(ns); } return SWIG_OK; } /* ------------------------------------------------------------ * addParentExtraBaseInterfaces() * * Add functions to retrieve the base class pointers for all base * classes of parents other than the first base class at each level. * ------------------------------------------------------------ */ void addParentExtraBaseInterfaces(Node *n, Hash *parents, Node *base, bool is_base_first, String *sofar) { List *baselist = Getattr(base, "bases"); if (!baselist || Len(baselist) == 0) { return; } String *go_this_base_name = exportedName(Getattr(base, "sym:name")); String *sf = NewString(""); Printv(sf, sofar, ".SwigGet", go_this_base_name, "()", NULL); Iterator b = First(baselist); if (is_base_first) { if (!b.item) { return; } if (!GetFlag(b.item, "feature:ignore")) { addParentExtraBaseInterfaces(n, parents, b.item, true, sf); } b = Next(b); } String *go_name = buildGoName(Getattr(n, "sym:name"), false, false); String *go_type_name = goCPointerType(Getattr(n, "classtypeobj"), true); for (; b.item; b = Next(b)) { if (GetFlag(b.item, "feature:ignore")) { continue; } String *go_base_name = exportedName(Getattr(b.item, "sym:name")); if (!Getattr(parents, go_base_name)) { Printv(f_go_wrappers, "func (p ", go_type_name, ") SwigGet", go_base_name, "() ", go_base_name, " {\n", NULL); Printv(f_go_wrappers, "\treturn p", sf, ".SwigGet", go_base_name, "()\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(interfaces, "\tSwigGet", go_base_name, "() ", go_base_name, "\n", NULL); addParentExtraBaseInterfaces(n, parents, b.item, false, sf); Setattr(parents, go_base_name, NewString("")); } } Delete(go_name); Delete(go_type_name); Delete(go_this_base_name); Delete(sf); } /* ------------------------------------------------------------ * classDirectorInit * * Add support for a director class. * * Virtual inheritance is different in Go and C++. We implement * director classes by defining a new function in Go, * NewDirectorClassname, which takes a empty interface value and * creates an instance of a new child class. The new child class * refers all methods back to Go. The Go code checks whether the * value passed to NewDirectorClassname implements that method; if * it does, it calls it, otherwise it calls back into C++. * ------------------------------------------------------------ */ int classDirectorInit(Node *n) { // Because we use a different function to handle inheritance in // Go, ordinary creations of the object should not create a // director object. Delete(director_ctor_code); director_ctor_code = NewString("$nondirector_new"); class_node = n; String *name = Getattr(n, "sym:name"); assert(!class_name); class_name = name; String *go_name = exportedName(name); String *go_type_name = goCPointerType(Getattr(n, "classtypeobj"), true); assert(!class_receiver); class_receiver = go_type_name; String *director_struct_name = NewString("_swig_Director"); Append(director_struct_name, go_name); String *cxx_director_name = NewString("SwigDirector_"); Append(cxx_director_name, name); // The Go type of the director class. Printv(f_go_wrappers, "type ", director_struct_name, " struct {\n", NULL); Printv(f_go_wrappers, "\t", go_type_name, "\n", NULL); Printv(f_go_wrappers, "\tv interface{}\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(f_go_wrappers, "func (p *", director_struct_name, ") Swigcptr() uintptr {\n", NULL); Printv(f_go_wrappers, "\treturn getSwigcptr(p.", go_type_name, ")\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(f_go_wrappers, "func (p *", director_struct_name, ") SwigIs", go_name, "() {\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(f_go_wrappers, "func (p *", director_struct_name, ") DirectorInterface() interface{} {\n", NULL); Printv(f_go_wrappers, "\treturn p.v\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); // Start defining the director class. Printv(f_c_directors_h, "class ", cxx_director_name, " : public ", Getattr(n, "classtype"), "\n", NULL); Printv(f_c_directors_h, "{\n", NULL); Printv(f_c_directors_h, " public:\n", NULL); Delete(director_struct_name); Delete(cxx_director_name); class_methods = NewHash(); return SWIG_OK; } /* ------------------------------------------------------------ * classDirectorConstructor * * Emit a constructor for a director class. * ------------------------------------------------------------ */ int classDirectorConstructor(Node *n) { bool is_ignored = GetFlag(n, "feature:ignore") ? true : false; String *name = Getattr(n, "sym:name"); if (!name) { assert(is_ignored); name = Getattr(n, "name"); } String *overname = NULL; if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } String *go_name = exportedName(name); ParmList *parms = Getattr(n, "parms"); Setattr(n, "wrap:parms", parms); String *cn = exportedName(Getattr(parentNode(n), "sym:name")); String *go_type_name = goCPointerType(Getattr(parentNode(n), "classtypeobj"), true); String *director_struct_name = NewString("_swig_Director"); Append(director_struct_name, cn); String *fn_name = NewString("_swig_NewDirector"); Append(fn_name, cn); Append(fn_name, go_name); if (!overname && !is_ignored) { if (!checkNameConflict(fn_name, n, NULL)) { return SWIG_NOWRAP; } } String *fn_with_over_name = Copy(fn_name); if (overname) { Append(fn_with_over_name, overname); } String *wname = Swig_name_wrapper(fn_name); if (overname) { Append(wname, overname); } Append(wname, unique_id); Setattr(n, "wrap:name", wname); bool is_static = isStatic(n); Wrapper *dummy = NewWrapper(); emit_attach_parmmaps(parms, dummy); DelWrapper(dummy); Swig_typemap_attach_parms("gotype", parms, NULL); Swig_typemap_attach_parms("goin", parms, NULL); Swig_typemap_attach_parms("goargout", parms, NULL); Swig_typemap_attach_parms("imtype", parms, NULL); int parm_count = emit_num_arguments(parms); String *func_name = NewString("NewDirector"); Append(func_name, go_name); String *func_with_over_name = Copy(func_name); if (overname) { Append(func_with_over_name, overname); } SwigType *first_type = NewString("int"); Parm *first_parm = NewParm(first_type, "swig_p", n); set_nextSibling(first_parm, parms); Setattr(first_parm, "lname", "p"); Parm *p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); Swig_cparm_name(p, i); p = nextParm(p); } if (!is_ignored) { Printv(f_cgo_comment, "extern uintptr_t ", wname, "(int", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); bool c_struct_type; String *ct = cgoTypeForGoValue(p, Getattr(p, "type"), &c_struct_type); Printv(f_cgo_comment, ", ", ct, " ", Getattr(p, "lname"), NULL); p = nextParm(p); } Printv(f_cgo_comment, ");\n", NULL); // Write out the Go function that calls the wrapper. Printv(f_go_wrappers, "func ", func_with_over_name, "(v interface{}", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); Printv(f_go_wrappers, ", ", Getattr(p, "lname"), " ", NULL); String *tm = goType(p, Getattr(p, "type")); Printv(f_go_wrappers, tm, NULL); Delete(tm); p = nextParm(p); } Printv(f_go_wrappers, ") ", cn, " {\n", NULL); Printv(f_go_wrappers, "\tp := &", director_struct_name, "{0, v}\n", NULL); String *call = NewString(""); Printv(call, "\tp.", class_receiver, " = ", NULL); Printv(call, go_type_name, "(C.", wname, "(C.int(swigDirectorAdd(p))", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { Printv(call, ", ", NULL); p = getParm(p); String *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, " := ", NULL); bool need_close = false; if (goTypeIsInterface(p, pt)) { Printv(f_go_wrappers, "getSwigcptr(", NULL); need_close = true; } Printv(f_go_wrappers, ln, NULL); if (need_close) { Printv(f_go_wrappers, ")", NULL); } Printv(f_go_wrappers, "\n", NULL); } 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(call, "))", NULL); Printv(f_go_wrappers, call, "\n", NULL); goargout(parms); Printv(f_go_wrappers, "\treturn p\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); SwigType *result = Copy(Getattr(parentNode(n), "classtypeobj")); SwigType_add_pointer(result); Swig_save("classDirectorConstructor", n, "wrap:name", "wrap:action", NULL); String *dwname = Swig_name_wrapper(name); Append(dwname, unique_id); Setattr(n, "wrap:name", dwname); String *action = NewString(""); Printv(action, Swig_cresult_name(), " = new SwigDirector_", class_name, "(", NULL); String *pname = Swig_cparm_name(NULL, 0); Printv(action, pname, NULL); Delete(pname); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); String *pname = Swig_cparm_name(NULL, i + 1); Printv(action, ", ", NULL); if (SwigType_isreference(Getattr(p, "type"))) { Printv(action, "*", NULL); } Printv(action, pname, NULL); Delete(pname); p = nextParm(p); } Printv(action, ");", NULL); Setattr(n, "wrap:action", action); cgoWrapperInfo info; info.n = n; info.go_name = func_name; info.overname = overname; info.wname = wname; info.base = NULL; info.parms = first_parm; info.result = result; info.is_static = false; info.receiver = NULL; info.is_constructor = true; info.is_destructor = false; int r = cgoGccWrapper(&info); if (r != SWIG_OK) { return r; } Swig_restore(n); Delete(result); } String *cxx_director_name = NewString("SwigDirector_"); Append(cxx_director_name, class_name); String *decl = Swig_method_decl(NULL, Getattr(n, "decl"), cxx_director_name, first_parm, 0); Printv(f_c_directors_h, " ", decl, ";\n", NULL); Delete(decl); decl = Swig_method_decl(NULL, Getattr(n, "decl"), cxx_director_name, first_parm, 0); Printv(f_c_directors, cxx_director_name, "::", decl, "\n", NULL); Delete(decl); Printv(f_c_directors, " : ", Getattr(parentNode(n), "classtype"), "(", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); if (i > 0) { Printv(f_c_directors, ", ", NULL); } String *pn = Getattr(p, "name"); assert(pn); Printv(f_c_directors, pn, NULL); p = nextParm(p); } Printv(f_c_directors, "),\n", NULL); Printv(f_c_directors, " go_val(swig_p), swig_mem(0)\n", NULL); Printv(f_c_directors, "{ }\n\n", NULL); if (Getattr(n, "sym:overloaded") && !Getattr(n, "sym:nextSibling")) { int r = makeDispatchFunction(n, func_name, cn, is_static, Getattr(parentNode(n), "classtypeobj"), false); if (r != SWIG_OK) { return r; } } Delete(cxx_director_name); Delete(go_name); Delete(cn); Delete(go_type_name); Delete(director_struct_name); Delete(fn_name); Delete(fn_with_over_name); Delete(func_name); Delete(func_with_over_name); Delete(wname); Delete(first_type); Delete(first_parm); return SWIG_OK; } /* ------------------------------------------------------------ * classDirectorDestructor * * Emit a destructor for a director class. * ------------------------------------------------------------ */ int classDirectorDestructor(Node *n) { if (!is_public(n)) { return SWIG_OK; } bool is_ignored = GetFlag(n, "feature:ignore") ? true : false; if (!is_ignored) { String *fnname = NewString("DeleteDirector"); String *c1 = exportedName(class_name); Append(fnname, c1); Delete(c1); String *wname = Swig_name_wrapper(fnname); Append(wname, unique_id); Setattr(n, "wrap:name", fnname); Swig_DestructorToFunction(n, getNSpace(), getClassType(), CPlusPlus, Extend); ParmList *parms = Getattr(n, "parms"); Setattr(n, "wrap:parms", parms); String *result = NewString("void"); int r = makeWrappers(n, fnname, NULL, wname, NULL, parms, result, isStatic(n)); if (r != SWIG_OK) { return r; } Delete(result); Delete(fnname); Delete(wname); } // Generate the destructor for the C++ director class. Since the // Go code is keeping a pointer to the C++ object, we need to call // back to the Go code to let it know that the C++ object is gone. String *go_name = NewString("Swiggo_DeleteDirector_"); Append(go_name, class_name); String *cn = exportedName(class_name); String *director_struct_name = NewString("_swig_Director"); Append(director_struct_name, cn); Printv(f_c_directors_h, " virtual ~SwigDirector_", class_name, "()", NULL); String *throws = buildThrow(n); if (throws) { Printv(f_c_directors_h, " ", throws, NULL); } Printv(f_c_directors_h, ";\n", NULL); String *director_sig = NewString(""); Printv(director_sig, "SwigDirector_", class_name, "::~SwigDirector_", class_name, "()", NULL); if (throws) { Printv(director_sig, " ", throws, NULL); Delete(throws); } Printv(director_sig, "\n", NULL); Printv(director_sig, "{\n", NULL); if (is_ignored) { Printv(f_c_directors, director_sig, NULL); } else { makeDirectorDestructorWrapper(go_name, director_struct_name, director_sig); } Printv(f_c_directors, " delete swig_mem;\n", NULL); Printv(f_c_directors, "}\n\n", NULL); Delete(director_sig); Delete(go_name); Delete(cn); Delete(director_struct_name); return SWIG_OK; } /* ------------------------------------------------------------ * makeDirectorDestructorWrapper * * Emit the function wrapper for the destructor of a director class. * ------------------------------------------------------------ */ void makeDirectorDestructorWrapper(String *go_name, String *director_struct_name, String *director_sig) { String *wname = Copy(go_name); Append(wname, unique_id); Printv(f_go_wrappers, "//export ", wname, "\n", NULL); Printv(f_go_wrappers, "func ", wname, "(c int) {\n", NULL); Printv(f_go_wrappers, "\tswigDirectorLookup(c).(*", director_struct_name, ").", class_receiver, " = 0\n", NULL); Printv(f_go_wrappers, "\tswigDirectorDelete(c)\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Printv(f_c_directors, "extern \"C\" void ", wname, "(intgo);\n", NULL); Printv(f_c_directors, director_sig, NULL); Printv(f_c_directors, " ", wname, "(go_val);\n", NULL); } /* ------------------------------------------------------------ * classDirectorMethod * * Emit a method for a director class, plus its overloads. * ------------------------------------------------------------ */ int classDirectorMethod(Node *n, Node *parent, String *super) { bool is_ignored = GetFlag(n, "feature:ignore") ? true : false; // We don't need explicit calls. if (GetFlag(n, "explicitcall")) { return SWIG_OK; } String *name = Getattr(n, "sym:name"); if (!name) { assert(is_ignored); (void)is_ignored; name = Getattr(n, "name"); } bool overloaded = Getattr(n, "sym:overloaded") && !Getattr(n, "explicitcallnode"); if (!overloaded) { int r = oneClassDirectorMethod(n, parent, super); if (r != SWIG_OK) { return r; } } else { // Handle overloaded methods here, because otherwise we will // reject them in the class_methods hash table. We need to use // class_methods so that we correctly handle cases where a // function in one class hides a function of the same name in a // parent class. if (!Getattr(class_methods, name)) { for (Node *on = Getattr(n, "sym:overloaded"); on; on = Getattr(on, "sym:nextSibling")) { // Swig_overload_rank expects wrap:name and wrap:parms to be // set. String *wn = Swig_name_wrapper(Getattr(on, "sym:name")); Append(wn, Getattr(on, "sym:overname")); Append(wn, unique_id); Setattr(on, "wrap:name", wn); Delete(wn); Setattr(on, "wrap:parms", Getattr(on, "parms")); } } int r = oneClassDirectorMethod(n, parent, super); if (r != SWIG_OK) { return r; } if (!Getattr(n, "sym:nextSibling")) { // Last overloaded function Node *on = Getattr(n, "sym:overloaded"); bool is_static = isStatic(on); String *cn = exportedName(Getattr(parent, "sym:name")); String *go_name = buildGoName(name, is_static, false); String *director_struct_name = NewString("_swig_Director"); Append(director_struct_name, cn); int r = makeDispatchFunction(on, go_name, director_struct_name, is_static, director_struct_name, false); if (r != SWIG_OK) { return r; } if (!GetFlag(n, "abstract")) { String *go_upcall = NewString("Director"); Append(go_upcall, cn); Append(go_upcall, go_name); r = makeDispatchFunction(on, go_upcall, director_struct_name, is_static, director_struct_name, true); if (r != SWIG_OK) { return r; } Delete(go_upcall); } Delete(director_struct_name); Delete(go_name); Delete(cn); } } Setattr(class_methods, name, NewString("")); return SWIG_OK; } /* ------------------------------------------------------------ * oneClassDirectorMethod * * Emit a method for a director class. * ------------------------------------------------------------ */ int oneClassDirectorMethod(Node *n, Node *parent, String *super) { String *symname = Getattr(n, "sym:name"); if (!checkFunctionVisibility(n, parent)) { return SWIG_OK; } bool is_ignored = GetFlag(n, "feature:ignore") ? true : false; bool is_pure_virtual = (Cmp(Getattr(n, "storage"), "virtual") == 0 && Cmp(Getattr(n, "value"), "0") == 0); String *name = Getattr(n, "sym:name"); if (!name) { assert(is_ignored); name = Getattr(n, "name"); } String *overname = NULL; if (Getattr(n, "sym:overloaded")) { overname = Getattr(n, "sym:overname"); } String *cn = exportedName(Getattr(parent, "sym:name")); String *go_type_name = goCPointerType(Getattr(parent, "classtypeobj"), true); String *director_struct_name = NewString("_swig_Director"); Append(director_struct_name, cn); bool is_static = isStatic(n); String *go_name = buildGoName(name, is_static, false); ParmList *parms = Getattr(n, "parms"); Setattr(n, "wrap:parms", parms); Wrapper *dummy = NewWrapper(); emit_attach_parmmaps(parms, dummy); Swig_typemap_attach_parms("gotype", parms, NULL); Swig_typemap_attach_parms("imtype", parms, NULL); int parm_count = emit_num_arguments(parms); SwigType *returntype = Getattr(n, "type"); bool is_void = !(Cmp(returntype, "void")); // Save the type for overload processing. Setattr(n, "go:type", returntype); String *interface_name = NewString("_swig_DirectorInterface"); Append(interface_name, cn); Append(interface_name, go_name); if (overname) { Append(interface_name, overname); } String *callback_name = Copy(director_struct_name); Append(callback_name, "_callback_"); Append(callback_name, name); Replace(callback_name, "_swig", "Swig", DOH_REPLACE_FIRST); if (overname) { Append(callback_name, overname); } Append(callback_name, unique_id); String *upcall_name = Copy(director_struct_name); Append(upcall_name, "_upcall_"); Append(upcall_name, go_name); String *upcall_wname = Swig_name_wrapper(upcall_name); if (overname) { Append(upcall_wname, overname); } Append(upcall_wname, unique_id); String *upcall_gc_name = buildGoWrapperName(upcall_name, overname); String *go_with_over_name = Copy(go_name); if (overname) { Append(go_with_over_name, overname); } Parm *p = 0; Wrapper *w = NewWrapper(); Swig_director_parms_fixup(parms); Swig_typemap_attach_parms("directorin", parms, w); Swig_typemap_attach_parms("directorargout", parms, w); Swig_typemap_attach_parms("godirectorin", parms, w); Swig_typemap_attach_parms("goin", parms, dummy); Swig_typemap_attach_parms("goargout", parms, dummy); DelWrapper(dummy); if (!is_ignored) { // We use an interface to see if this method is defined in Go. Printv(f_go_wrappers, "type ", interface_name, " interface {\n", NULL); Printv(f_go_wrappers, "\t", go_with_over_name, "(", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); if (i > 0) { Printv(f_go_wrappers, ", ", NULL); } String *tm = goType(p, Getattr(p, "type")); Printv(f_go_wrappers, tm, NULL); Delete(tm); p = nextParm(p); } Printv(f_go_wrappers, ")", NULL); if (!is_void) { String *tm = goType(n, returntype); Printv(f_go_wrappers, " ", tm, NULL); Delete(tm); } Printv(f_go_wrappers, "\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); if (!GetFlag(n, "abstract")) { Printv(f_cgo_comment, "extern ", NULL); if (is_void) { Printv(f_cgo_comment, "void", NULL); } else { bool c_struct_type; String *ret_type = cgoTypeForGoValue(n, returntype, &c_struct_type); Printv(f_cgo_comment, ret_type, NULL); Delete(ret_type); } Printv(f_cgo_comment, " ", upcall_wname, "(uintptr_t", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); bool c_struct_type; String *ct = cgoTypeForGoValue(p, Getattr(p, "type"), &c_struct_type); Printv(f_cgo_comment, ", ", ct, " ", Getattr(p, "lname"), NULL); p = nextParm(p); } Printv(f_cgo_comment, ");\n", NULL); } // Define the method on the director class in Go. Printv(f_go_wrappers, "func (swig_p *", director_struct_name, ") ", go_with_over_name, "(", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); if (i > 0) { Printv(f_go_wrappers, ", ", NULL); } Printv(f_go_wrappers, Getattr(p, "lname"), " ", NULL); String *tm = goType(p, Getattr(p, "type")); Printv(f_go_wrappers, tm, NULL); Delete(tm); p = nextParm(p); } Printv(f_go_wrappers, ")", NULL); if (!is_void) { String *tm = goType(n, returntype); Printv(f_go_wrappers, " ", tm, NULL); Delete(tm); } Printv(f_go_wrappers, " {\n", NULL); Printv(f_go_wrappers, "\tif swig_g, swig_ok := swig_p.v.(", interface_name, "); swig_ok {\n", NULL); Printv(f_go_wrappers, "\t\t", NULL); if (!is_void) { Printv(f_go_wrappers, "return ", NULL); } Printv(f_go_wrappers, "swig_g.", go_with_over_name, "(", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); if (i > 0) { Printv(f_go_wrappers, ", ", NULL); } Printv(f_go_wrappers, Getattr(p, "lname"), NULL); p = nextParm(p); } Printv(f_go_wrappers, ")\n", NULL); if (is_void) { Printv(f_go_wrappers, "\t\treturn\n", NULL); } Printv(f_go_wrappers, "\t}\n", NULL); if (GetFlag(n, "abstract")) { Printv(f_go_wrappers, "\tpanic(\"call to pure virtual method\")\n", NULL); } else { String *ret_type = NULL; bool memcpy_ret = false; String *wt = NULL; String *goout = NULL; if (!is_void) { ret_type = goImType(n, returntype); Printv(f_go_wrappers, "\tvar swig_r ", ret_type, "\n", NULL); goout = goTypemapLookup("goout", n, "swig_r"); bool c_struct_type; Delete(cgoTypeForGoValue(n, returntype, &c_struct_type)); if (c_struct_type) { memcpy_ret = true; } } String *call = NewString(""); Printv(call, "\t", NULL); if (!is_void) { if (memcpy_ret) { Printv(call, "swig_r_p := ", NULL); } else { Printv(call, "swig_r = (", ret_type, ")(", NULL); } if (goTypeIsInterface(n, returntype)) { wt = goWrapperType(n, returntype, true); Printv(call, "(", wt, ")(", NULL); } } Printv(call, "C.", upcall_wname, "(C.uintptr_t(swig_p.", go_type_name, ")", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { Printv(call, ", ", NULL); p = getParm(p); SwigType *pt = Getattr(p, "type"); String *ln = Getattr(p, "lname"); String *ivar = NewStringf("_swig_i_%d", i); // This is an ordinary call from Go to C++, so adjust using // the goin typemap. String *goin = goGetattr(p, "tmap:goin"); if (goin == NULL) { Printv(f_go_wrappers, "\t", ivar, " := ", NULL); bool need_close = false; if (goTypeIsInterface(p, pt)) { Printv(f_go_wrappers, "getSwigcptr(", NULL); need_close = true; } Printv(f_go_wrappers, ln, NULL); if (need_close) { Printv(f_go_wrappers, ")", NULL); } Printv(f_go_wrappers, "\n", NULL); } 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); } p = nextParm(p); } Printv(call, ")", NULL); if (wt) { // Close the type conversion to the wrapper type. Printv(call, ")", NULL); } if (!is_void && !memcpy_ret) { // Close the type conversion of the return value. Printv(call, ")", NULL); } Printv(call, "\n", NULL); Printv(f_go_wrappers, call, NULL); Delete(call); if (memcpy_ret) { Printv(f_go_wrappers, "\tswig_r = *(*", ret_type, ")(unsafe.Pointer(&swig_r_p))\n", NULL); } goargout(parms); if (!is_void) { if (goout == NULL) { Printv(f_go_wrappers, "\treturn swig_r\n", NULL); } else { String *tm = goType(n, returntype); 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); } } if (ret_type) { Delete(ret_type); } if (wt) { Delete(wt); } } Printv(f_go_wrappers, "}\n\n", NULL); if (!GetFlag(n, "abstract")) { // Define a function that uses the Go director type that other // methods in the Go type can call to get parent methods. Printv(f_go_wrappers, "func Director", cn, go_with_over_name, "(swig_p ", cn, NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); Printv(f_go_wrappers, ", ", Getattr(p, "lname"), " ", NULL); String *tm = goType(p, Getattr(p, "type")); Printv(f_go_wrappers, tm, NULL); Delete(tm); p = nextParm(p); } Printv(f_go_wrappers, ")", NULL); if (!is_void) { String *tm = goType(n, returntype); Printv(f_go_wrappers, " ", tm, NULL); Delete(tm); } Printv(f_go_wrappers, " {\n", NULL); String *ret_type = NULL; bool memcpy_ret = false; String *wt = NULL; String *goout = NULL; if (!is_void) { ret_type = goImType(n, returntype); Printv(f_go_wrappers, "\tvar swig_r ", ret_type, "\n", NULL); goout = goTypemapLookup("goout", n, "swig_r"); bool c_struct_type; Delete(cgoTypeForGoValue(n, returntype, &c_struct_type)); if (c_struct_type) { memcpy_ret = true; } } String *call = NewString(""); Printv(call, "\t", NULL); if (!is_void) { if (memcpy_ret) { Printv(call, "swig_r_p := ", NULL); } else { Printv(call, "swig_r = (", ret_type, ")(", NULL); } if (goTypeIsInterface(n, returntype)) { wt = goWrapperType(n, returntype, true); Printv(call, "(", wt, ")(", NULL); } } Printv(call, "C.", upcall_wname, "(C.uintptr_t(swig_p.(*", director_struct_name, ").", go_type_name, ")", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { Printv(call, ", ", NULL); p = getParm(p); SwigType *pt = Getattr(p, "type"); String *ivar = NewStringf("_swig_i_%d", i); String *ln = Copy(Getattr(p, "lname")); String *goin = goGetattr(p, "tmap:goin"); if (goin == NULL) { Printv(f_go_wrappers, "\t", ivar, " := ", NULL); bool need_close = false; if (goTypeIsInterface(p, pt)) { Printv(f_go_wrappers, "getSwigcptr(", NULL); need_close = true; } Printv(f_go_wrappers, ln, NULL); if (need_close) { Printv(f_go_wrappers, ")", NULL); } Printv(f_go_wrappers, "\n", NULL); } 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(ln); p = nextParm(p); } Printv(call, ")", NULL); if (wt) { // Close the type conversion to the wrapper type. Printv(call, ")", NULL); } if (!is_void && !memcpy_ret) { // Close the type conversion of the return value. Printv(call, ")", NULL); } Printv(call, "\n", NULL); Printv(f_go_wrappers, call, NULL); Delete(call); if (memcpy_ret) { Printv(f_go_wrappers, "\tswig_r = *(*", ret_type, ")(unsafe.Pointer(&swig_r_p))\n", NULL); } goargout(parms); if (!is_void) { if (goout == NULL) { Printv(f_go_wrappers, "\treturn swig_r\n", NULL); } else { String *tm = goType(n, returntype); 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\n", NULL); if (ret_type) { Delete(ret_type); } if (wt) { Delete(wt); } // Define a method in the C++ director class that the C++ // upcall function can call. This permits an upcall to a // protected method. String *upcall_method_name = NewString("_swig_upcall_"); Append(upcall_method_name, name); if (overname) { Append(upcall_method_name, overname); } SwigType *rtype = Getattr(n, "classDirectorMethods:type"); String *upcall_decl = Swig_method_decl(rtype, Getattr(n, "decl"), upcall_method_name, parms, 0); Printv(f_c_directors_h, " ", upcall_decl, " {\n", NULL); Delete(upcall_decl); Printv(f_c_directors_h, " ", NULL); if (!is_void) { Printv(f_c_directors_h, "return ", NULL); } String *super_call = Swig_method_call(super, parms); Printv(f_c_directors_h, super_call, ";\n", NULL); Delete(super_call); Printv(f_c_directors_h, " }\n", NULL); // Define the C++ function that the Go function calls. SwigType *first_type = NULL; Parm *first_parm = parms; if (!is_static) { first_type = NewString("SwigDirector_"); Append(first_type, class_name); SwigType_add_pointer(first_type); first_parm = NewParm(first_type, "p", n); set_nextSibling(first_parm, parms); } Swig_save("classDirectorMethod", n, "wrap:name", "wrap:action", NULL); Setattr(n, "wrap:name", upcall_wname); String *action = NewString(""); if (!is_void) { Printv(action, Swig_cresult_name(), " = (", SwigType_lstr(returntype, 0), ")", NULL); if (SwigType_isreference(returntype)) { Printv(action, "&", NULL); } } Printv(action, Swig_cparm_name(NULL, 0), "->", upcall_method_name, "(", NULL); p = parms; int i = 0; while (p != NULL) { if (SwigType_type(Getattr(p, "type")) != T_VOID) { String *pname = Swig_cparm_name(NULL, i + 1); if (i > 0) { Printv(action, ", ", NULL); } // A parameter whose type is a reference is converted into a // pointer type by gcCTypeForGoValue. We are calling a // function which expects a reference so we need to convert // back. if (SwigType_isreference(Getattr(p, "type"))) { Printv(action, "*", NULL); } Printv(action, pname, NULL); Delete(pname); i++; } p = nextSibling(p); } Printv(action, ");", NULL); Setattr(n, "wrap:action", action); cgoWrapperInfo info; info.n = n; info.go_name = go_name; info.overname = overname; info.wname = upcall_wname; info.base = NULL; info.parms = first_parm; info.result = returntype; info.is_static = is_static; info.receiver = NULL; info.is_constructor = false; info.is_destructor = false; int r = cgoGccWrapper(&info); if (r != SWIG_OK) { return r; } Delete(first_type); if (first_parm != parms) { Delete(first_parm); } Swig_restore(n); Delete(upcall_method_name); } // The Go function which invokes the method. This is called by // the C++ method on the director class. Printv(f_go_wrappers, "//export ", callback_name, "\n", "func ", callback_name, "(swig_c int", NULL); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); String *tm = goWrapperType(p, Getattr(p, "type"), false); Printv(f_go_wrappers, ", ", Getattr(p, "lname"), " ", tm, NULL); Delete(tm); p = nextParm(p); } Printv(f_go_wrappers, ") ", NULL); String *result_wrapper = NULL; if (!is_void) { result_wrapper = goWrapperType(n, returntype, true); Printv(f_go_wrappers, "(swig_result ", result_wrapper, ") ", NULL); } Printv(f_go_wrappers, "{\n", NULL); if (is_ignored) { Printv(f_go_wrappers, "\treturn\n", NULL); } else { bool result_is_interface = false; String *goout = NULL; if (!is_void) { result_is_interface = goTypeIsInterface(NULL, returntype); Printv(f_go_wrappers, "\tvar swig_r ", NULL); if (!result_is_interface) { Printv(f_go_wrappers, goType(n, returntype), NULL); } else { Printv(f_go_wrappers, result_wrapper, NULL); } Printv(f_go_wrappers, "\n", NULL); goout = goTypemapLookup("godirectorout", n, "swig_r"); } String *call = NewString(""); Printv(call, "\t", NULL); if (!is_void) { Printv(call, "swig_r = ", NULL); if (result_is_interface) { Printv(call, result_wrapper, "(getSwigcptr(", NULL); } } Printv(call, "swig_p.", go_with_over_name, "(", NULL); String *goincode = NewString(""); p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); if (i > 0) { Printv(call, ", ", NULL); } SwigType *pt = Getattr(p, "type"); String *ln = NewString(""); // If the Go representation is an interface type class, then // we are receiving a uintptr, and must convert to the // interface. bool is_interface = goTypeIsInterface(p, pt); if (is_interface) { // Passing is_result as true to goWrapperType gives us the // name of the Go type we need to convert to an interface. String *wt = goWrapperType(p, pt, true); Printv(ln, wt, "(", NULL); Delete(wt); } Printv(ln, Getattr(p, "lname"), NULL); if (is_interface) { Printv(ln, ")", NULL); } 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, "\n", NULL); goin = Copy(goin); Replaceall(goin, "$input", ln); Replaceall(goin, "$result", ivar); Printv(goincode, goin, "\n", NULL); Delete(goin); Printv(call, ivar, NULL); Delete(ivar); } Delete(ln); p = nextParm(p); } Printv(call, ")", NULL); if (result_is_interface) { Printv(call, "))", NULL); } Printv(call, "\n", NULL); Printv(f_go_wrappers, "\tswig_p := swigDirectorLookup(swig_c).(*", director_struct_name, ")\n", NULL); Printv(f_go_wrappers, goincode, NULL); Printv(f_go_wrappers, call, NULL); Delete(call); if (!is_void) { if (goout == NULL) { Printv(f_go_wrappers, "\treturn swig_r\n", NULL); } else { String *tm = goImType(n, returntype); 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\n", NULL); Delete(result_wrapper); Delete(upcall_wname); Delete(upcall_gc_name); Delete(go_with_over_name); } if (!is_ignored || is_pure_virtual) { // Declare the method for the director class. SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); String *decl = Swig_method_decl(rtype, Getattr(n, "decl"), Getattr(n, "name"), parms, 0); Printv(f_c_directors_h, " virtual ", decl, NULL); Delete(decl); String *qname = NewString(""); Printv(qname, "SwigDirector_", class_name, "::", Getattr(n, "name"), NULL); decl = Swig_method_decl(rtype, Getattr(n, "decl"), qname, parms, 0); Printv(w->def, decl, NULL); Delete(decl); Delete(qname); String *throws = buildThrow(n); if (throws) { Printv(f_c_directors_h, " ", throws, NULL); Printv(w->def, " ", throws, NULL); Delete(throws); } Printv(f_c_directors_h, ";\n", NULL); Printv(w->def, " {\n", NULL); if (!is_void) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (!is_ignored) { makeDirectorMethodWrapper(n, w, callback_name); } else { assert(is_pure_virtual); Printv(w->code, " _swig_gopanic(\"call to pure virtual function ", Getattr(parent, "sym:name"), name, "\");\n", NULL); if (!is_void) { String *retstr = SwigType_rcaststr(returntype, "c_result"); Printv(w->code, " return ", retstr, ";\n", NULL); Delete(retstr); } } Printv(w->code, "}", NULL); Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_c_directors); } Delete(cn); Delete(go_type_name); Delete(director_struct_name); Delete(interface_name); Delete(callback_name); Delete(upcall_name); Delete(go_name); DelWrapper(w); return SWIG_OK; } /* ------------------------------------------------------------ * makeDirectorMethodWrapper * * Emit the function wrapper for a director method. * ------------------------------------------------------------ */ void makeDirectorMethodWrapper(Node *n, Wrapper *w, String *callback_name) { ParmList *parms = Getattr(n, "wrap:parms"); SwigType *returntype = Getattr(n, "type"); bool is_void = !(Cmp(returntype, "void")); Printv(f_c_directors, "extern \"C\" ", NULL); String *fnname = Copy(callback_name); Append(fnname, "(int"); Parm *p = parms; while (p) { while (checkAttribute(p, "tmap:directorin:numinputs", "0")) { p = Getattr(p, "tmap:directorin:next"); } String *cg = gcCTypeForGoValue(p, Getattr(p, "type"), Getattr(p, "lname")); Printv(fnname, ", ", cg, NULL); Delete(cg); p = Getattr(p, "tmap:directorin:next"); } Printv(fnname, ")", NULL); if (is_void) { Printv(f_c_directors, "void ", fnname, NULL); } else { String *tm = gcCTypeForGoValue(n, returntype, fnname); Printv(f_c_directors, tm, NULL); Delete(tm); } Delete(fnname); Printv(f_c_directors, ";\n", NULL); if (!is_void) { String *r = NewString(Swig_cresult_name()); String *tm = gcCTypeForGoValue(n, returntype, r); Wrapper_add_local(w, r, tm); Delete(tm); Delete(r); } String *args = NewString(""); p = parms; while (p) { while (checkAttribute(p, "tmap:directorin:numinputs", "0")) { p = Getattr(p, "tmap:directorin:next"); } String *pn = NewString("swig_"); Append(pn, Getattr(p, "lname")); Setattr(p, "emit:directorinput", pn); String *tm = gcCTypeForGoValue(p, Getattr(p, "type"), pn); Wrapper_add_local(w, pn, tm); Delete(tm); tm = Getattr(p, "tmap:directorin"); if (!tm) { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as director method argument\n", SwigType_str(Getattr(p, "type"), 0)); } else { tm = Copy(tm); Replaceall(tm, "$input", pn); Replaceall(tm, "$owner", 0); Printv(w->code, " ", tm, "\n", NULL); Delete(tm); Printv(args, ", ", pn, NULL); } p = Getattr(p, "tmap:directorin:next"); } Printv(w->code, " ", NULL); if (!is_void) { Printv(w->code, Swig_cresult_name(), " = ", NULL); } Printv(w->code, callback_name, "(go_val", args, ");\n", NULL); /* Marshal outputs */ for (p = parms; p; ) { String *tm; if ((tm = Getattr(p, "tmap:directorargout"))) { tm = Copy(tm); Replaceall(tm, "$result", "jresult"); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NULL); Delete(tm); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } if (!is_void) { String *result_str = NewString("c_result"); String *tm = Swig_typemap_lookup("directorout", n, result_str, NULL); if (!tm) { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use type %s as director method returntype\n", SwigType_str(returntype, 0)); } else { tm = Copy(tm); Replaceall(tm, "$input", Swig_cresult_name()); Replaceall(tm, "$result", "c_result"); Printv(w->code, " ", tm, "\n", NULL); String *retstr = SwigType_rcaststr(returntype, "c_result"); Printv(w->code, " return ", retstr, ";\n", NULL); Delete(retstr); Delete(tm); } Delete(result_str); } } /* ------------------------------------------------------------ * classDirectorEnd * * Complete support for a director class. * ------------------------------------------------------------ */ int classDirectorEnd(Node *n) { (void) n; Printv(f_c_directors_h, " private:\n", NULL); Printv(f_c_directors_h, " intgo go_val;\n", NULL); Printv(f_c_directors_h, " Swig_memory *swig_mem;\n", NULL); Printv(f_c_directors_h, "};\n\n", NULL); class_name = NULL; class_node = NULL; Delete(class_receiver); class_receiver = NULL; Delete(class_methods); class_methods = NULL; return SWIG_OK; } /* ------------------------------------------------------------ * classDirectorDisown * * I think Go does not require a disown method. * ------------------------------------------------------------ */ int classDirectorDisown(Node *n) { (void) n; return SWIG_OK; } /*---------------------------------------------------------------------- * buildThrow() * * Build and return a throw clause if needed. *--------------------------------------------------------------------*/ String *buildThrow(Node *n) { if (Getattr(n, "noexcept")) return NewString("noexcept"); ParmList *throw_parm_list = Getattr(n, "throws"); if (!throw_parm_list && !Getattr(n, "throw")) return NULL; String *ret = NewString("throw("); if (throw_parm_list) { Swig_typemap_attach_parms("throws", throw_parm_list, NULL); } bool first = true; for (Parm *p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (first) { first = false; } else { Printv(ret, ", ", NULL); } String *s = SwigType_str(Getattr(p, "type"), 0); Printv(ret, s, NULL); Delete(s); } } Printv(ret, ")", NULL); return ret; } /*---------------------------------------------------------------------- * extraDirectorProtectedCPPMethodsRequired() * * We don't need to check upcall when calling methods. *--------------------------------------------------------------------*/ bool extraDirectorProtectedCPPMethodsRequired() const { return false; } /*---------------------------------------------------------------------- * makeDispatchFunction * * Make a dispatch function for an overloaded C++ function. The * receiver parameter is the receiver for a method, unless is_upcall * is true. If is_upcall is true, then the receiver parameter is * the type of the first argument to the function. *--------------------------------------------------------------------*/ int makeDispatchFunction(Node *n, String *go_name, String *receiver, bool is_static, SwigType *director_struct, bool is_upcall) { bool is_director = director_struct ? true : false; String *nodetype = Getattr(n, "nodeType"); bool is_constructor = Cmp(nodetype, "constructor") == 0; bool is_destructor = Cmp(nodetype, "destructor") == 0; bool can_use_receiver = (!is_constructor && !is_destructor && !is_upcall); bool use_receiver = (!is_static && can_use_receiver); bool add_to_interface = (interfaces && !is_constructor && !is_destructor && !is_static && !is_upcall); List *dispatch = Swig_overload_rank(n, false); int nfunc = Len(dispatch); SwigType *all_result; bool mismatch; if (is_constructor) { assert(!is_upcall); if (!is_director) { all_result = Copy(Getattr(class_node, "classtypeobj")); } else { all_result = Copy(director_struct); } mismatch = false; } else { all_result = NULL; mismatch = false; bool any_void = false; for (int i = 0; i < nfunc; ++i) { Node *nn = Getitem(dispatch, i); Node *ni = Getattr(nn, "directorNode") ? Getattr(nn, "directorNode") : nn; SwigType *result = Getattr(ni, "go:type"); assert(result); if (SwigType_type(result) == T_VOID) { if (all_result) { mismatch = true; } any_void = true; } else { if (any_void) { mismatch = true; } else if (!all_result) { all_result = Copy(result); } else if (Cmp(result, all_result) != 0) { mismatch = true; } } } if (mismatch) { Delete(all_result); all_result = NULL; } else if (all_result) { ; } else { all_result = NewString("void"); } } Printv(f_go_wrappers, "func ", NULL); if (receiver && use_receiver) { Printv(f_go_wrappers, "(p ", receiver, ") ", NULL); } Printv(f_go_wrappers, go_name, "(", NULL); if (is_director && is_constructor) { Printv(f_go_wrappers, "abi interface{}, ", NULL); assert(!add_to_interface); } if (is_upcall) { Printv(f_go_wrappers, "p *", receiver, ", ", NULL); assert(!add_to_interface); } Printv(f_go_wrappers, "a ...interface{})", NULL); if (add_to_interface) { Printv(interfaces, "\t", go_name, "(a ...interface{})", NULL); } if (mismatch) { Printv(f_go_wrappers, " interface{}", NULL); if (add_to_interface) { Printv(interfaces, " interface{}", NULL); } } else if (all_result && SwigType_type(all_result) != T_VOID) { if (is_director && is_constructor) { Printv(f_go_wrappers, " ", receiver, NULL); if (add_to_interface) { Printv(interfaces, " ", receiver, NULL); } } else { String *tm = goType(n, all_result); Printv(f_go_wrappers, " ", tm, NULL); if (add_to_interface) { Printv(interfaces, " ", tm, NULL); } Delete(tm); } } Printv(f_go_wrappers, " {\n", NULL); if (add_to_interface) { Printv(interfaces, "\n", NULL); } Printv(f_go_wrappers, "\targc := len(a)\n", NULL); for (int i = 0; i < nfunc; ++i) { int fn = 0; Node *nn = Getitem(dispatch, i); Node *ni = Getattr(nn, "directorNode") ? Getattr(nn, "directorNode") : nn; Parm *pi = Getattr(ni, "wrap:parms"); // If we are using a receiver, we want to ignore a leading self // parameter. Because of the way this is called, there may or // may not be a self parameter at this point. if (use_receiver && pi && Getattr(pi, "self")) { pi = getParm(pi); if (pi) { pi = nextParm(pi); } } int num_required = emit_num_required(pi); int num_arguments = emit_num_arguments(pi); bool varargs = emit_isvarargs(pi) ? true : false; if (varargs) { Printf(f_go_wrappers, "\tif argc >= %d {\n", num_required); } else { if (num_required == num_arguments) { Printf(f_go_wrappers, "\tif argc == %d {\n", num_required); } else { Printf(f_go_wrappers, "\tif argc >= %d && argc <= %d {\n", num_required, num_arguments); } } // Build list of collisions with the same number of arguments. List *coll = NewList(); for (int k = i + 1; k < nfunc; ++k) { Node *nnk = Getitem(dispatch, k); Node *nk = Getattr(nnk, "directorNode") ? Getattr(nnk, "directorNode") : nnk; Parm *pk = Getattr(nk, "wrap:parms"); if (use_receiver && pk && Getattr(pk, "self")) { pk = getParm(pk); if (pk) { pk = nextParm(pk); } } int nrk = emit_num_required(pk); int nak = emit_num_arguments(pk); if ((nrk >= num_required && nrk <= num_arguments) || (nak >= num_required && nak <= num_arguments) || (nrk <= num_required && nak >= num_arguments) || (varargs && nrk >= num_required)) { Append(coll, nk); } } int num_braces = 0; if (Len(coll) > 0 && num_arguments > 0) { int j = 0; Parm *pj = pi; while (pj) { pj = getParm(pj); if (!pj) { break; } // If all the overloads have the same type in this position, // we can omit the check. SwigType *tm = goOverloadType(pj, Getattr(pj, "type")); bool emitcheck = false; for (int k = 0; k < Len(coll) && !emitcheck; ++k) { Node *nk = Getitem(coll, k); Parm *pk = Getattr(nk, "wrap:parms"); if (use_receiver && pk && Getattr(pk, "self")) { pk = getParm(pk); if (pk) { pk = nextParm(pk); } } int nak = emit_num_arguments(pk); if (nak <= j) continue; int l = 0; Parm *pl = pk; while (pl && l <= j) { pl = getParm(pl); if (!pl) { break; } if (l == j) { SwigType *tml = goOverloadType(pl, Getattr(pl, "type")); if (Cmp(tm, tml) != 0) { emitcheck = true; } Delete(tml); } pl = nextParm(pl); ++l; } } if (emitcheck) { if (j >= num_required) { Printf(f_go_wrappers, "\t\tif argc > %d {\n", j); ++num_braces; } fn = i + 1; Printf(f_go_wrappers, "\t\tif _, ok := a[%d].(%s); !ok {\n", j, tm); Printf(f_go_wrappers, "\t\t\tgoto check_%d\n", fn); Printv(f_go_wrappers, "\t\t}\n", NULL); } Delete(tm); pj = nextParm(pj); ++j; } } for (; num_braces > 0; --num_braces) { Printv(f_go_wrappers, "\t\t}\n", NULL); } // We may need to generate multiple calls if there are variable // argument lists involved. Build the start of the call. String *start = NewString(""); SwigType *result = Getattr(ni, "go:type"); if (is_constructor) { result = all_result; } else if (is_destructor) { result = NULL; } if (result && SwigType_type(result) != T_VOID && (!all_result || SwigType_type(all_result) != T_VOID)) { Printv(start, "return ", NULL); } bool advance_parm = false; if (receiver && use_receiver) { Printv(start, "p.", go_name, NULL); } else if (can_use_receiver && !isStatic(ni) && pi && Getattr(pi, "self")) { // This is an overload of a static function and a non-static // function. assert(num_required > 0); SwigType *tm = goWrapperType(pi, Getattr(pi, "type"), true); String *nm = buildGoName(Getattr(ni, "sym:name"), false, isFriend(ni)); Printv(start, "a[0].(", tm, ").", nm, NULL); Delete(nm); Delete(tm); advance_parm = true; } else { Printv(start, go_name, NULL); } Printv(start, Getattr(ni, "sym:overname"), "(", NULL); bool need_comma = false; if (is_director && is_constructor) { Printv(start, "abi", NULL); need_comma = true; } if (is_upcall) { Printv(start, "p", NULL); need_comma = true; } Parm *p = pi; int pn = 0; if (advance_parm) { p = getParm(p); if (p) { p = nextParm(p); } ++pn; } while (pn < num_required) { p = getParm(p); if (need_comma) { Printv(start, ", ", NULL); } SwigType *tm = goType(p, Getattr(p, "type")); Printf(start, "a[%d].(%s)", pn, tm); Delete(tm); need_comma = true; ++pn; p = nextParm(p); } String *end = NULL; if (!result || SwigType_type(result) == T_VOID || (all_result && SwigType_type(all_result) == T_VOID)) { end = NewString(""); Printv(end, "return", NULL); if (!all_result || SwigType_type(all_result) != T_VOID) { Printv(end, " 0", NULL); } } if (num_required == num_arguments) { Printv(f_go_wrappers, "\t\t", start, ")\n", NULL); if (end) { Printv(f_go_wrappers, "\t\t", end, "\n", NULL); } } else { Printv(f_go_wrappers, "\t\tswitch argc {\n", NULL); for (int j = num_required; j <= num_arguments; ++j) { Printf(f_go_wrappers, "\t\tcase %d:\n", j); Printv(f_go_wrappers, "\t\t\t", start, NULL); bool nc = need_comma; for (int k = num_required; k < j; ++k) { if (nc) { Printv(f_go_wrappers, ", ", NULL); } Printf(f_go_wrappers, "a[%d]", k); nc = true; } Printv(f_go_wrappers, ")\n", NULL); if (end) { Printv(f_go_wrappers, "\t\t\t", end, "\n", NULL); } } Printv(f_go_wrappers, "\t\t}\n", NULL); } Printv(f_go_wrappers, "\t}\n", NULL); if (fn != 0) { Printf(f_go_wrappers, "check_%d:\n", fn); } Delete(coll); } Printv(f_go_wrappers, "\tpanic(\"No match for overloaded function call\")\n", NULL); Printv(f_go_wrappers, "}\n\n", NULL); Delete(all_result); Delete(dispatch); return SWIG_OK; } /* ---------------------------------------------------------------------- * checkFunctionVisibility() * * Return true if we should write out a function based on its * visibility, false otherwise. * ---------------------------------------------------------------------- */ bool checkFunctionVisibility(Node *n, Node *parent) { // Write out a public function. if (is_public(n)) return true; // Don't write out a private function. if (is_private(n)) return false; // Write a protected function for a director class in // dirprot_mode. if (parent == NULL) { return false; } if (dirprot_mode() && Swig_directorclass(parent)) return true; // Otherwise don't write out a protected function. return false; } /* ---------------------------------------------------------------------- * exportedName() * * Given a C/C++ name, return a name in Go which will be exported. * If the first character is an upper case letter, this returns a * copy of its argument. If the first character is a lower case * letter, this forces it to upper case. Otherwise, this prepends * 'X'. * ---------------------------------------------------------------------- */ String *exportedName(SwigType *name) { SwigType *copy = Copy(name); char c = *Char(copy); if (islower(c)) { char l[2]; char u[2]; l[0] = c; l[1] = '\0'; u[0] = toupper(c); u[1] = '\0'; Replace(copy, l, u, DOH_REPLACE_FIRST); } else if (!isalpha(c)) { char l[2]; char u[3]; l[0] = c; l[1] = '\0'; u[0] = 'X'; u[1] = c; u[2] = '\0'; Replace(copy, l, u, DOH_REPLACE_FIRST); } String *ret = Swig_name_mangle_type(copy); Delete(copy); return ret; } /* ---------------------------------------------------------------------- * removeClassname() * * If the name starts with the current class name, followed by an * underscore, remove it. If there is no current class name, this * simply returns a copy of the name. This undoes Swig's way of * recording the class name in a member name. * ---------------------------------------------------------------------- */ String *removeClassname(String *name) { String *copy = Copy(name); if (class_name) { char *p = Char(name); if (Strncmp(name, class_name, Len(class_name)) == 0 && p[Len(class_name)] == '_') { Replace(copy, class_name, "", DOH_REPLACE_FIRST); Replace(copy, "_", "", DOH_REPLACE_FIRST); } } return copy; } /* ---------------------------------------------------------------------- * buildGoName() * * Build the name to use for an ordinary function, variable, or * whatever in Go. The name argument is something like the sym:name * attribute of the node. If is_static is false, this could be a * method, and the returned name will be the name of the * method--i.e., it will not include the class name. * ---------------------------------------------------------------------- */ String *buildGoName(String *name, bool is_static, bool is_friend) { String *nw = NewString(""); if (is_static && !is_friend && class_name) { String *c1 = exportedName(class_name); Append(nw, c1); Delete(c1); } String *c2 = removeClassname(name); String *c3 = exportedName(c2); Append(nw, c3); Delete(c2); Delete(c3); String *ret = Swig_name_mangle_string(nw); Delete(nw); return ret; } /* ---------------------------------------------------------------------- * buildGoWrapperName() * * Build the name to use for a Go wrapper function. This is a * function called by the real Go function in order to convert C++ * classes from interfaces to pointers, and other such conversions * between the Go type and the C++ type. * ---------------------------------------------------------------------- */ String *buildGoWrapperName(String *name, String *overname) { String *s1 = NewString("_swig_wrap_"); Append(s1, name); String *s2 = Swig_name_mangle_string(s1); Delete(s1); if (overname) { Append(s2, overname); } return s2; } /* ---------------------------------------------------------------------- * checkNameConflict() * * Check for a name conflict on the name we are going to use in Go. * These conflicts are likely because of the enforced * capitalization. When we find one, issue a warning and return * false. If the name is OK, return true. * ---------------------------------------------------------------------- */ bool checkNameConflict(String* name, Node* n, const_String_or_char_ptr scope) { Node *lk = symbolLookup(name, scope); if (lk) { String *n1 = Getattr(n, "sym:name"); if (!n1) { n1 = Getattr(n, "name"); } String *n2 = Getattr(lk, "sym:name"); if (!n2) { n2 = Getattr(lk, "name"); } Swig_warning(WARN_GO_NAME_CONFLICT, input_file, line_number, "Ignoring '%s' due to Go name ('%s') conflict with '%s'\n", n1, name, n2); return false; } bool r = addSymbol(name, n, scope) ? true : false; assert(r); (void)r; return true; } /* ---------------------------------------------------------------------- * checkIgnoredParameters() * * If any of the parameters of this function, or the return type, * are ignored due to a name conflict, give a warning and return * false. * ---------------------------------------------------------------------- */ bool checkIgnoredParameters(Node *n, String *go_name) { ParmList *parms = Getattr(n, "parms"); if (parms) { Wrapper *dummy = NewWrapper(); emit_attach_parmmaps(parms, dummy); int parm_count = emit_num_arguments(parms); Parm *p = parms; for (int i = 0; i < parm_count; ++i) { p = getParm(p); if (!checkIgnoredType(n, go_name, Getattr(p, "type"))) { DelWrapper(dummy); return false; } p = nextParm(p); } DelWrapper(dummy); } if (!checkIgnoredType(n, go_name, Getattr(n, "type"))) { return false; } return true; } /* ---------------------------------------------------------------------- * checkIgnoredType() * * If this type is being ignored due to a name conflict, give a * warning and return false. * ---------------------------------------------------------------------- */ bool checkIgnoredType(Node *n, String *go_name, SwigType *type) { if (hasGoTypemap(n, type)) { return true; } SwigType *t = SwigType_typedef_resolve_all(type); bool ret = true; bool is_conflict = false; Node *e = Language::enumLookup(t); if (e) { if (GetFlag(e, "go:conflict")) { is_conflict = true; } } else if (SwigType_issimple(t)) { Node *cn = classLookup(t); if (cn) { if (GetFlag(cn, "go:conflict")) { is_conflict = true; } } } else if (SwigType_ispointer(t) || SwigType_isarray(t) || SwigType_isqualifier(t) || SwigType_isreference(t)) { SwigType *r = Copy(t); if (SwigType_ispointer(r)) { SwigType_del_pointer(r); } else if (SwigType_isarray(r)) { SwigType_del_array(r); } else if (SwigType_isqualifier(r)) { SwigType_del_qualifier(r); } else { SwigType_del_reference(r); } if (!checkIgnoredType(n, go_name, r)) { ret = false; } Delete(r); } if (is_conflict) { String *s = SwigType_str(t, NULL); Swig_warning(WARN_GO_NAME_CONFLICT, input_file, line_number, "Ignoring '%s' (Go name '%s') due to Go name conflict for parameter or result type '%s'\n", Getattr(n, "name"), go_name, s); Delete(s); ret = false; } Delete(t); return ret; } /* ---------------------------------------------------------------------- * goType() * * Given a SWIG type, return a string for the type in Go. * ---------------------------------------------------------------------- */ String *goType(Node *n, SwigType *type) { return goTypeWithInfo(n, type, false, NULL); } /* ---------------------------------------------------------------------- * goImType() * * Given a SWIG type, return a string for the intermediate Go type * to pass to C/C++. This is like goType except that it looks for * an imtype typemap entry first. * ---------------------------------------------------------------------- */ String *goImType(Node *n, SwigType *type) { return goTypeWithInfo(n, type, true, NULL); } /* ---------------------------------------------------------------------- * goTypeWithInfo() * * Like goType, but return some more information. * * If use_imtype is true, this look for a imtype typemap entry. * * If the p_is_interface parameter is not NULL, this sets * *p_is_interface to indicate whether this type is going to be * represented by a Go interface type. These are cases where the Go * code needs to make some adjustments when passing values back and * forth with C/C++. * ---------------------------------------------------------------------- */ String *goTypeWithInfo(Node *n, SwigType *type, bool use_imtype, bool *p_is_interface) { if (p_is_interface) { *p_is_interface = false; } String *ret = NULL; if (use_imtype) { if (n && Cmp(type, Getattr(n, "type")) == 0) { if (Strcmp(Getattr(n, "nodeType"), "parm") == 0) { ret = Getattr(n, "tmap:imtype"); } if (!ret) { ret = Swig_typemap_lookup("imtype", n, "", NULL); } } else { Parm *p = NewParm(type, "goImType", n); ret = Swig_typemap_lookup("imtype", p, "", NULL); Delete(p); } } if (!ret) { if (n && Cmp(type, Getattr(n, "type")) == 0) { if (Strcmp(Getattr(n, "nodeType"), "parm") == 0) { ret = Getattr(n, "tmap:gotype"); } if (!ret) { ret = Swig_typemap_lookup("gotype", n, "", NULL); } } else { Parm *p = NewParm(type, "goType", n); ret = Swig_typemap_lookup("gotype", p, "", NULL); Delete(p); } } if (ret && Strstr(ret, "$gotypename") != 0) { ret = NULL; } if (ret) { return Copy(ret); } SwigType *t = SwigType_typedef_resolve_all(type); if (SwigType_isenum(t)) { Node *e = Language::enumLookup(t); if (e) { ret = goEnumName(e); } else if (Strcmp(t, "enum ") == 0) { ret = NewString("int"); } else { // An unknown enum - one that has not been parsed (neither a C enum forward reference nor a definition) or an ignored enum String *tt = Copy(t); Replace(tt, "enum ", "", DOH_REPLACE_ANY); ret = exportedName(tt); Setattr(undefined_enum_types, t, ret); Delete(tt); } } else if (SwigType_isfunctionpointer(t) || SwigType_isfunction(t)) { ret = NewString("_swig_fnptr"); } else if (SwigType_ismemberpointer(t)) { ret = NewString("_swig_memberptr"); } else if (SwigType_issimple(t)) { Node *cn = classLookup(t); if (cn) { ret = Getattr(cn, "sym:name"); if (!ret) { ret = Getattr(cn, "name"); } ret = exportedName(ret); Node *cnmod = Getattr(cn, "module"); if (!cnmod || Strcmp(Getattr(cnmod, "name"), module) == 0) { Setattr(undefined_types, t, t); } else { String *nw = NewString(""); Printv(nw, getModuleName(Getattr(cnmod, "name")), ".", ret, NULL); Delete(ret); ret = nw; } } else { // SWIG does not know about this type. ret = exportedName(t); Setattr(undefined_types, t, t); } if (p_is_interface) { *p_is_interface = true; } } else if (SwigType_ispointer(t) || SwigType_isarray(t)) { SwigType *r = Copy(t); if (SwigType_ispointer(r)) { SwigType_del_pointer(r); } else { SwigType_del_array(r); } if (SwigType_type(r) == T_VOID) { ret = NewString("uintptr"); } else { bool is_interface; String *base = goTypeWithInfo(n, r, false, &is_interface); // At the Go level, an unknown or class type is handled as an // interface wrapping a pointer. This means that if a // function returns the C type X, we will be wrapping the C // type X*. In Go we will call that type X. That means that // if a C function expects X*, we can pass the Go type X. And // that means that when we see the C type X*, we should use // the Go type X. // The is_interface variable tells us this. However, it will // be true both for the case of X and for the case of X*. If // r is a pointer here, then we are looking at X**. There is // really no good way for us to handle that. bool is_pointer_to_pointer = false; if (is_interface) { SwigType *c = Copy(r); if (SwigType_isqualifier(c)) { SwigType_del_qualifier(c); if (SwigType_ispointer(c) || SwigType_isarray(c)) { is_pointer_to_pointer = true; } } Delete(c); } if (is_interface) { if (!is_pointer_to_pointer) { ret = base; if (p_is_interface) { *p_is_interface = true; } } else { ret = NewString("uintptr"); } } else { ret = NewString("*"); Append(ret, base); Delete(base); } } Delete(r); } else if (SwigType_isreference(t)) { SwigType *r = Copy(t); SwigType_del_reference(r); // If this is a const reference, and we are looking at a pointer // to it, then we just use the pointer we already have. bool add_pointer = true; if (SwigType_isqualifier(r)) { String *q = SwigType_parm(r); if (Strcmp(q, "const") == 0) { SwigType *c = Copy(r); SwigType_del_qualifier(c); if (SwigType_ispointer(c)) { add_pointer = false; } Delete(c); } } if (add_pointer) { SwigType_add_pointer(r); } ret = goTypeWithInfo(n, r, false, p_is_interface); Delete(r); } else if (SwigType_isqualifier(t)) { SwigType *r = Copy(t); SwigType_del_qualifier(r); ret = goTypeWithInfo(n, r, false, p_is_interface); Delete(r); } else if (SwigType_isvarargs(t)) { ret = NewString("[]interface{}"); } Delete(t); if (!ret) { Swig_warning(WARN_LANG_NATIVE_UNIMPL, input_file, line_number, "No Go typemap defined for %s\n", SwigType_str(type, 0)); ret = NewString("uintptr"); } return ret; } /* ---------------------------------------------------------------------- * cgoTypeForGoValue() * * Given a SWIG type, return a string for the C type to use for the * cgo wrapper code. This always returns a simple identifier, since * it is used in Go code as C.name. * * This sets *c_struct_type if the C type uses a struct where the Go * type uses a simple type. This is true for strings and slices. * When this is true the Go code has to jump through unsafe hoops to * pass the type checker. * ---------------------------------------------------------------------- */ String *cgoTypeForGoValue(Node *n, SwigType *type, bool *c_struct_type) { *c_struct_type = false; bool is_interface; String *go_type = goTypeWithInfo(n, type, true, &is_interface); if (is_interface) { Delete(go_type); return NewString("uintptr_t"); } if (Strcmp(go_type, "uintptr") == 0) { Delete(go_type); return NewString("uintptr_t"); } if (((char*)Char(go_type))[0] == '*') { // Treat all pointers as void*. There is no meaningful type // checking going on here anyhow, and that lets us avoid // worrying about defining the base type of the pointer. Delete(go_type); return NewString("swig_voidp"); } // Check for some Go types that are really pointers under the covers. bool is_hidden_pointer = Strncmp(go_type, "func(", 5) == 0 || Strncmp(go_type, "map[", 4) == 0 || Strncmp(go_type, "chan ", 5) == 0; Delete(go_type); String *ct = Getattr(n, "emit:cgotype"); if (ct) { *c_struct_type = Getattr(n, "emit:cgotypestruct") ? true : false; return Copy(ct); } String *t = Copy(type); if (SwigType_isarray(t) && Getattr(n, "tmap:gotype") == NULL) { SwigType_del_array(t); SwigType_add_pointer(t); } bool add_typedef = true; static int count; ++count; ct = NewStringf("swig_type_%d", count); String *gct = gcCTypeForGoValue(n, t, ct); Delete(t); if (Strncmp(gct, "_gostring_", 10) == 0 || Strncmp(gct, "_goslice_", 9) == 0) { *c_struct_type = true; Setattr(n, "emit:cgotypestruct", type); } else { char *p = Strstr(gct, ct); if (p != NULL && p > (char*)Char(gct) && p[-1] == '*' && p[Len(ct)] == '\0') { // Treat all pointers as void*. See above. Delete(ct); --count; ct = NewString("swig_voidp"); add_typedef = false; if (is_hidden_pointer) { // A Go type that is really a pointer, like func, map, chan, // is being represented in C by a pointer. This is fine, // but we have to memcpy the type rather than simply // converting it. *c_struct_type = true; Setattr(n, "emit:cgotypestruct", type); } } if (Strncmp(gct, "bool ", 5) == 0) { // Change the C++ type bool to the C type _Bool. Replace(gct, "bool", "_Bool", DOH_REPLACE_FIRST); } if (Strncmp(gct, "intgo ", 6) == 0) { // We #define intgo to swig_intgo for the cgo comment. Replace(gct, "intgo", "swig_intgo", DOH_REPLACE_FIRST); } p = Strstr(gct, ct); if (p != NULL && p > (char*)Char(gct) && p[-1] == ' ' && p[Len(ct)] == '\0') { String *q = NewStringWithSize(gct, Len(gct) - Len(ct) - 1); if (validIdentifier(q)) { // This is a simple type name, and we can use it directly. Delete(ct); --count; ct = q; add_typedef = false; } } } if (add_typedef) { Printv(f_cgo_comment_typedefs, "typedef ", gct, ";\n", NULL); } Setattr(n, "emit:cgotype", ct); Delete(gct); return Copy(ct); } /* ---------------------------------------------------------------------- * goWrapperType() * * Given a type, return a string for the type to use for the wrapped * Go function. This function exists because for a C++ class we * need to convert interface and reference types. * ---------------------------------------------------------------------- */ String *goWrapperType(Node *n, SwigType *type, bool is_result) { bool is_interface; String *ret = goTypeWithInfo(n, type, true, &is_interface); // If this is an interface, we want to pass the real type. if (is_interface) { Delete(ret); if (!is_result) { ret = NewString("uintptr"); } else { SwigType *ty = SwigType_typedef_resolve_all(type); while (true) { if (SwigType_ispointer(ty)) { SwigType_del_pointer(ty); } else if (SwigType_isarray(ty)) { SwigType_del_array(ty); } else if (SwigType_isreference(ty)) { SwigType_del_reference(ty); } else if (SwigType_isqualifier(ty)) { SwigType_del_qualifier(ty); } else { break; } } assert(SwigType_issimple(ty)); String *p = goCPointerType(ty, true); Delete(ty); ret = p; } } return ret; } /* ---------------------------------------------------------------------- * goOverloadType() * * Given a type, return the Go type to use when dispatching of * overloaded functions. This is normally just the usual Go type. * However, for a C++ class, the usual Go type is an interface type. * And if that interface type represents a C++ type that SWIG does * not know about, then the interface type generated for any C++ * class will match that interface. So for that case, we match on * the underlying integer type. * * It has to work this way so that we can handle a derived type of a * %ignore'd type. It's unlikely that anybody will have a value of * an undefined type, but we support it because it worked in the * past. * ---------------------------------------------------------------------- */ String *goOverloadType(Node *n, SwigType *type) { SwigType *ty = SwigType_typedef_resolve_all(type); while (true) { if (SwigType_ispointer(ty)) { SwigType_del_pointer(ty); } else if (SwigType_isarray(ty)) { SwigType_del_array(ty); } else if (SwigType_isreference(ty)) { SwigType_del_reference(ty); } else if (SwigType_isqualifier(ty)) { SwigType_del_qualifier(ty); } else { break; } } String* go_type = goType(n, ty); if (Getattr(undefined_types, ty) && !Getattr(defined_types, go_type)) { Delete(go_type); return goWrapperType(n, type, true); } Delete(go_type); return goType(n, type); } /* ---------------------------------------------------------------------- * goCPointerType() * * Return the name of the Go type to use for the C pointer value. * The regular C type is the name of an interface type which wraps a * pointer whose name is returned by this function. * ---------------------------------------------------------------------- */ String *goCPointerType(SwigType *type, bool add_to_hash) { SwigType *ty = SwigType_typedef_resolve_all(type); Node *cn = classLookup(ty); String *ex; String *ret; if (!cn) { if (add_to_hash) { Setattr(undefined_types, ty, ty); } ret = NewString("Swigcptr"); ex = exportedName(ty); Append(ret, ex); } else { String *cname = Getattr(cn, "sym:name"); if (!cname) { cname = Getattr(cn, "name"); } ex = exportedName(cname); Node *cnmod = Getattr(cn, "module"); if (!cnmod || Strcmp(Getattr(cnmod, "name"), module) == 0) { if (add_to_hash) { Setattr(undefined_types, ty, ty); } ret = NewString("Swigcptr"); Append(ret, ex); } else { ret = NewString(""); Printv(ret, getModuleName(Getattr(cnmod, "name")), ".Swigcptr", ex, NULL); } } Delete(ty); Delete(ex); return ret; } /* ---------------------------------------------------------------------- * gcCTypeForGoValue() * * Given a type, return the C/C++ type which will be used to catch * the value in Go. This is the gc version. * ---------------------------------------------------------------------- */ String *gcCTypeForGoValue(Node *n, SwigType *type, String *name) { bool is_interface; String *gt = goTypeWithInfo(n, type, true, &is_interface); String *tail = NewString(""); SwigType *t = SwigType_typedef_resolve_all(type); bool is_const_ref = false; if (SwigType_isreference(t)) { SwigType* tt = Copy(t); SwigType_del_reference(tt); if (SwigType_isqualifier(tt)) { String* q = SwigType_parm(tt); if (Strcmp(q, "const") == 0) { is_const_ref = true; } } Delete(tt); } if (!is_const_ref) { while (Strncmp(gt, "*", 1) == 0) { Replace(gt, "*", "", DOH_REPLACE_FIRST); Printv(tail, "*", NULL); } } Delete(t); bool is_string = Strcmp(gt, "string") == 0; bool is_slice = Strncmp(gt, "[]", 2) == 0; bool is_function = Strcmp(gt, "_swig_fnptr") == 0; bool is_member = Strcmp(gt, "_swig_memberptr") == 0; bool is_complex64 = Strcmp(gt, "complex64") == 0; bool is_complex128 = Strcmp(gt, "complex128") == 0; bool is_bool = false; bool is_int8 = false; bool is_int16 = false; bool is_int = Strcmp(gt, "int") == 0 || Strcmp(gt, "uint") == 0; bool is_int32 = false; bool is_int64 = false; bool is_float32 = false; bool is_float64 = false; bool has_typemap = (n != NULL && Getattr(n, "tmap:gotype") != NULL) || hasGoTypemap(n, type); if (has_typemap) { is_bool = Strcmp(gt, "bool") == 0; is_int8 = Strcmp(gt, "int8") == 0 || Strcmp(gt, "uint8") == 0 || Strcmp(gt, "byte") == 0; is_int16 = Strcmp(gt, "int16") == 0 || Strcmp(gt, "uint16") == 0; is_int32 = Strcmp(gt, "int32") == 0 || Strcmp(gt, "uint32") == 0; is_int64 = Strcmp(gt, "int64") == 0 || Strcmp(gt, "uint64") == 0; is_float32 = Strcmp(gt, "float32") == 0; is_float64 = Strcmp(gt, "float64") == 0; } Delete(gt); String *ret; if (is_string) { // Note that we don't turn a reference to a string into a // pointer to a string. Strings are immutable anyhow. ret = NewString(""); Printv(ret, "_gostring_", tail, " ", name, NULL); Delete(tail); return ret; } else if (is_slice) { // Slices are always passed as a _goslice_, whether or not references // are involved. ret = NewString(""); Printv(ret, "_goslice_", tail, " ", name, NULL); Delete(tail); return ret; } else if (is_function || is_member) { ret = NewString(""); Printv(ret, "void*", tail, " ", name, NULL); Delete(tail); return ret; } else if (is_complex64) { ret = NewString("_Complex float "); } else if (is_complex128) { ret = NewString("_Complex double "); } else if (is_interface) { SwigType *t = SwigType_typedef_resolve_all(type); if (SwigType_ispointer(t)) { SwigType_del_pointer(t); } if (SwigType_isreference(t)) { SwigType_del_reference(t); } SwigType_add_pointer(t); ret = SwigType_lstr(t, name); Delete(t); Delete(tail); return ret; } else { SwigType *t = SwigType_typedef_resolve_all(type); if (!has_typemap && SwigType_isreference(t)) { // A const reference to a known type, or to a pointer, is not // mapped to a pointer. SwigType_del_reference(t); if (SwigType_isqualifier(t)) { String *q = SwigType_parm(t); if (Strcmp(q, "const") == 0) { SwigType_del_qualifier(t); if (hasGoTypemap(n, t) || SwigType_ispointer(t)) { if (is_int) { ret = NewString("intgo "); Append(ret, name); } else if (is_int64) { ret = NewString("long long "); Append(ret, name); } else { ret = SwigType_lstr(t, name); } Delete(q); Delete(t); Delete(tail); return ret; } } Delete(q); } } if (Language::enumLookup(t) != NULL) { is_int = true; } else { SwigType *tstripped = SwigType_strip_qualifiers(t); if (SwigType_isenum(tstripped)) is_int = true; Delete(tstripped); } Delete(t); if (is_bool) { ret = NewString("bool "); } else if (is_int8) { ret = NewString("char "); } else if (is_int16) { ret = NewString("short "); } else if (is_int) { ret = NewString("intgo "); } else if (is_int32) { ret = NewString("int "); } else if (is_int64) { ret = NewString("long long "); } else if (is_float32) { ret = NewString("float "); } else if (is_float64) { ret = NewString("double "); } else { Delete(tail); return SwigType_lstr(type, name); } } Append(ret, tail); if (!has_typemap && SwigType_isreference(type)) { Append(ret, "* "); } Append(ret, name); Delete(tail); return ret; } /* ---------------------------------------------------------------------- * goTypeIsInterface * * Return whether this C++ type is represented as an interface type * in Go. These types require adjustments in the Go code when * passing them back and forth between Go and C++. * ---------------------------------------------------------------------- */ bool goTypeIsInterface(Node *n, SwigType *type) { bool is_interface; Delete(goTypeWithInfo(n, type, false, &is_interface)); return is_interface; } /* ---------------------------------------------------------------------- * hasGoTypemap * * Return whether a type has a "gotype" typemap entry. * ---------------------------------------------------------------------- */ bool hasGoTypemap(Node *n, SwigType *type) { Parm *p = NewParm(type, "test", n); SwigType *tm = Swig_typemap_lookup("gotype", p, "", NULL); Delete(p); if (tm && Strstr(tm, "$gotypename") == 0) { Delete(tm); return true; } Delete(tm); return false; } /* ---------------------------------------------------------------------- * goEnumName() * * Given an enum node, return a string to use for the enum type in Go. * ---------------------------------------------------------------------- */ String *goEnumName(Node *n) { String *ret = Getattr(n, "go:enumname"); if (ret) { return Copy(ret); } if (Equal(Getattr(n, "type"), "enum ")) { return NewString("int"); } SwigType *type = Getattr(n, "enumtype"); assert(type); char *p = Char(type); int len = Len(type); SwigType *s = NewString(""); bool capitalize = true; for (int i = 0; i < len; ++i, ++p) { if (*p == ':') { ++i; ++p; assert(*p == ':'); capitalize = true; } else if (capitalize) { Putc(toupper(*p), s); capitalize = false; } else { Putc(*p, s); } } ret = Swig_name_mangle_type(s); Delete(s); return ret; } /* ---------------------------------------------------------------------- * getParm() * * Get the real parameter to use. * ---------------------------------------------------------------------- */ Parm *getParm(Parm *p) { while (p && checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } return p; } /* ---------------------------------------------------------------------- * nextParm() * * Return the next parameter. * ---------------------------------------------------------------------- */ Parm *nextParm(Parm *p) { if (!p) { return NULL; } else if (Getattr(p, "tmap:in")) { return Getattr(p, "tmap:in:next"); } else { return nextSibling(p); } } /* ---------------------------------------------------------------------- * isStatic * * Return whether a node should be considered as static rather than * as a member. * ---------------------------------------------------------------------- */ bool isStatic(Node *n) { String *storage = Getattr(n, "storage"); return (storage && (Swig_storage_isstatic(n) || Strstr(storage, "friend")) && (!SmartPointer || !Getattr(n, "allocate:smartpointeraccess"))); } /* ---------------------------------------------------------------------- * isFriend * * Return whether a node is a friend. * ---------------------------------------------------------------------- */ bool isFriend(Node *n) { String *storage = Getattr(n, "storage"); return storage && Strstr(storage, "friend"); } /* ---------------------------------------------------------------------- * 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; } /* ---------------------------------------------------------------------- * getModuleName * * Return the name of a module. This is different from module path: * "some/path/to/module" -> "module". * ---------------------------------------------------------------------- */ String *getModuleName(String *module_path) { char *suffix = strrchr(Char(module_path), '/'); if (suffix == NULL) { return module_path; } return Str(suffix + 1); } }; /* class GO */ /* ----------------------------------------------------------------------------- * swig_go() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_go() { return new GO(); } extern "C" Language *swig_go(void) { return new_swig_go(); } /* ----------------------------------------------------------------------------- * Static member variables * ----------------------------------------------------------------------------- */ // Usage message. const char * const GO::usage = "\ Go Options (available with -go)\n\ -cgo - Generate cgo input files\n\ -no-cgo - Do not generate cgo input files\n\ -gccgo - Generate code for gccgo rather than gc\n\ -go-pkgpath

- Like gccgo -fgo-pkgpath option\n\ -go-prefix

- Like gccgo -fgo-prefix option\n\ -import-prefix

- Prefix to add to %import directives\n\ -intgosize - Set size of Go int type--32 or 64 bits\n\ -package - Set name of the Go package to \n\ -use-shlib - Force use of a shared library\n\ -soname - Set shared library holding C/C++ code to \n\ \n"; swig-4.4.0/Source/Modules/README0000664000175000017500000000047215075443613016113 0ustar williamwilliam06/25/2002 This directory contains all of the SWIG language modules. Many of these modules contain code that dates back to SWIG1.0. The module API has changed a lot in the development releases so this is fairly messy. We're working on cleaning it up, but you'll have to bear with us until it's done. -- Dave swig-4.4.0/Source/Modules/lang.cxx0000664000175000017500000035666315075443613016720 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * lang.cxx * * Language base class functions. Default C++ handling is also implemented here. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include "cparse.h" #include /* default mode settings */ static int directors_allowed = 0; static int director_language = 0; static int director_mode = 0; static int director_protected_mode = 1; static int all_protected_mode = 0; static int naturalvar_mode = 0; Language *Language::this_ = 0; int Swig_directors_enabled() { return director_language && CPlusPlus && (directors_allowed || director_mode); } void Wrapper_director_mode_set(int flag) { director_mode = flag; } void Wrapper_director_protected_mode_set(int flag) { director_protected_mode = flag; } void Wrapper_all_protected_mode_set(int flag) { all_protected_mode = flag; } void Wrapper_naturalvar_mode_set(int flag) { naturalvar_mode = flag; } extern "C" { int Swig_director_mode() { return director_mode; } int Swig_director_protected_mode() { return director_protected_mode; } int Swig_all_protected_mode() { return all_protected_mode; } void Language_replace_special_variables(String *method, String *tm, Parm *parm) { Language::instance()->replaceSpecialVariables(method, tm, parm); } } /* Some status variables used during parsing */ static int InClass = 0; /* Parsing C++ or not */ static String *ClassName = 0; /* This is the real name of the current class */ static String *EnumClassName = 0; /* Enum class name */ static String *ClassPrefix = 0; /* Class prefix */ static String *EnumClassPrefix = 0; /* Prefix for strongly typed enums (including ClassPrefix) */ static String *NSpace = 0; /* Namespace for the nspace feature */ static String *ClassType = 0; /* Fully qualified type name to use */ static String *DirectorClassName = 0; /* Director name of the current class */ int Abstract = 0; int ImportMode = 0; int IsVirtual = 0; static String *AttributeFunctionGet = 0; static String *AttributeFunctionSet = 0; static Node *CurrentClass = 0; int line_number = 0; String *input_file = 0; int SmartPointer = 0; static Hash *classhash; extern int ForceExtern; extern int AddExtern; extern "C" { extern int UseWrapperSuffix; } /* import modes */ #define IMPORT_MODE 1 /* ---------------------------------------------------------------------- * Dispatcher::emit_one() * * Dispatch a single node * ---------------------------------------------------------------------- */ int Dispatcher::emit_one(Node *n) { int ret = SWIG_OK; char *tag = Char(nodeType(n)); if (!tag) { /* Printf(stderr,"SWIG: Fatal internal error. Malformed parse tree node!\n"); */ return SWIG_OK; } /* Do not proceed if marked with an error */ if (Getattr(n, "error")) return SWIG_OK; /* Look for warnings */ String *wrn = Getattr(n, "feature:warnfilter"); if (wrn) Swig_warnfilter(wrn, 1); /* ============================================================ * C/C++ parsing * ============================================================ */ if (strcmp(tag, "extern") == 0) { ret = externDeclaration(n); } else if (strcmp(tag, "cdecl") == 0) { ret = cDeclaration(n); } else if (strcmp(tag, "enum") == 0) { ret = enumDeclaration(n); } else if (strcmp(tag, "enumitem") == 0) { ret = enumvalueDeclaration(n); } else if (strcmp(tag, "enumforward") == 0) { ret = enumforwardDeclaration(n); } else if (strcmp(tag, "class") == 0) { ret = classDeclaration(n); } else if (strcmp(tag, "classforward") == 0) { ret = classforwardDeclaration(n); } else if (strcmp(tag, "constructor") == 0) { ret = constructorDeclaration(n); } else if (strcmp(tag, "destructor") == 0) { ret = destructorDeclaration(n); } else if (strcmp(tag, "access") == 0) { ret = accessDeclaration(n); } else if (strcmp(tag, "using") == 0) { ret = usingDeclaration(n); } else if (strcmp(tag, "namespace") == 0) { ret = namespaceDeclaration(n); } else if (strcmp(tag, "template") == 0) { ret = templateDeclaration(n); } else if (strcmp(tag, "lambda") == 0) { ret = lambdaDeclaration(n); } /* =============================================================== * SWIG directives * =============================================================== */ else if (strcmp(tag, "top") == 0) { ret = top(n); } else if (strcmp(tag, "extend") == 0) { ret = extendDirective(n); } else if (strcmp(tag, "apply") == 0) { ret = applyDirective(n); } else if (strcmp(tag, "clear") == 0) { ret = clearDirective(n); } else if (strcmp(tag, "constant") == 0) { ret = constantDirective(n); } else if (strcmp(tag, "fragment") == 0) { ret = fragmentDirective(n); } else if (strcmp(tag, "import") == 0) { ret = importDirective(n); } else if (strcmp(tag, "include") == 0) { ret = includeDirective(n); } else if (strcmp(tag, "insert") == 0) { ret = insertDirective(n); } else if (strcmp(tag, "module") == 0) { ret = moduleDirective(n); } else if (strcmp(tag, "native") == 0) { ret = nativeDirective(n); } else if (strcmp(tag, "pragma") == 0) { ret = pragmaDirective(n); } else if (strcmp(tag, "typemap") == 0) { ret = typemapDirective(n); } else if (strcmp(tag, "typemapcopy") == 0) { ret = typemapcopyDirective(n); } else if (strcmp(tag, "typemapitem") == 0) { ret = typemapitemDirective(n); } else if (strcmp(tag, "types") == 0) { ret = typesDirective(n); } else { Swig_error(input_file, line_number, "Unrecognized parse tree node type '%s'\n", tag); ret = SWIG_ERROR; } if (wrn) Swig_warnfilter(wrn, 0); return ret; } /* ---------------------------------------------------------------------- * Dispatcher::emit_children() * * Emit all children that match the given type. type = 0 means all types. * ---------------------------------------------------------------------- */ int Dispatcher::emit_children(Node *n) { Node *c; char *eo = Char(Getattr(n, "feature:emitonlychildren")); for (c = firstChild(n); c; c = nextSibling(c)) { if (eo) { const char *tag = Char(nodeType(c)); if (strcmp(tag, "cdecl") == 0) { if (checkAttribute(c, "storage", "typedef")) tag = "typedef"; } if (strstr(eo, tag) == 0) { continue; } } emit_one(c); } return SWIG_OK; } /* Stubs for dispatcher class. We don't do anything by default---up to derived class to fill in traversal code */ int Dispatcher::defaultHandler(Node *) { return SWIG_OK; } int Dispatcher::extendDirective(Node *n) { return defaultHandler(n); } int Dispatcher::applyDirective(Node *n) { return defaultHandler(n); } int Dispatcher::clearDirective(Node *n) { return defaultHandler(n); } int Dispatcher::constantDirective(Node *n) { return defaultHandler(n); } int Dispatcher::fragmentDirective(Node *n) { return defaultHandler(n); } int Dispatcher::importDirective(Node *n) { return defaultHandler(n); } int Dispatcher::includeDirective(Node *n) { return defaultHandler(n); } int Dispatcher::insertDirective(Node *n) { return defaultHandler(n); } int Dispatcher::moduleDirective(Node *n) { return defaultHandler(n); } int Dispatcher::nativeDirective(Node *n) { return defaultHandler(n); } int Dispatcher::pragmaDirective(Node *n) { return defaultHandler(n); } int Dispatcher::typemapDirective(Node *n) { return defaultHandler(n); } int Dispatcher::typemapitemDirective(Node *n) { return defaultHandler(n); } int Dispatcher::typemapcopyDirective(Node *n) { return defaultHandler(n); } int Dispatcher::typesDirective(Node *n) { return defaultHandler(n); } int Dispatcher::cDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::externDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::enumDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::enumvalueDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::enumforwardDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::classDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::templateDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::lambdaDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::classforwardDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::constructorDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::destructorDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::accessDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::usingDeclaration(Node *n) { return defaultHandler(n); } int Dispatcher::namespaceDeclaration(Node *n) { return defaultHandler(n); } Dispatcher::AccessMode Dispatcher::accessModeFromString(String *access) { Dispatcher::AccessMode mode = PUBLIC; if (Cmp(access, "public") == 0) { mode = PUBLIC; } else if (Cmp(access, "private") == 0) { mode = PRIVATE; } else if (Cmp(access, "protected") == 0) { mode = PROTECTED; } else { assert(0); } return mode; } /* Allocators */ Language::Language(): none_comparison(NewString("$arg != 0")), director_ctor_code(NewString("")), director_prot_ctor_code(0), director_multiple_inheritance(1), doxygenTranslator(NULL), symtabs(NewHash()), overloading(0), multiinput(0), cplus_runtime(0) { symbolAddScope(""); // create top level/global symbol table scope argc_template_string = NewString("argc"); argv_template_string = NewString("argv[%d]"); /* Default director constructor code, passed to Swig_ConstructorToFunction */ Printv(director_ctor_code, "if ( $comparison ) { /* subclassed */\n", " $director_new \n", "} else {\n", " $nondirector_new \n", "}\n", NIL); assert(!this_); this_ = this; } Language::~Language() { Delete(symtabs); Delete(director_ctor_code); Delete(none_comparison); this_ = 0; } /* ----------------------------------------------------------------------------- * directorClassName() * ----------------------------------------------------------------------------- */ String *Language::directorClassName(Node *n) { String *dirclassname; String *nspace = NewString(Getattr(n, "sym:nspace")); const char *attrib = "director:classname"; String *classname = getClassPrefix(); Replace(nspace, NSPACE_SEPARATOR, "_", DOH_REPLACE_ANY); if (Len(nspace) > 0) dirclassname = NewStringf("SwigDirector_%s_%s", nspace, classname); else dirclassname = NewStringf("SwigDirector_%s", classname); Setattr(n, attrib, dirclassname); Delete(nspace); return dirclassname; } /* ---------------------------------------------------------------------- emit_one() ---------------------------------------------------------------------- */ int Language::emit_one(Node *n) { int ret; int oldext; if (!n) return SWIG_OK; if (GetFlag(n, "feature:ignore") && !Getattr(n, "feature:onlychildren")) return SWIG_OK; oldext = Extend; if (Getattr(n, "feature:extend")) Extend = 1; line_number = Getline(n); input_file = Getfile(n); /* symtab = Getattr(n,"symtab"); if (symtab) { symtab = Swig_symbol_setscope(symtab); } */ ret = Dispatcher::emit_one(n); /* if (symtab) { Swig_symbol_setscope(symtab); } */ Extend = oldext; return ret; } static Parm *nonvoid_parms(Parm *p) { if (p) { SwigType *t = Getattr(p, "type"); if (SwigType_type(t) == T_VOID) return 0; } return p; } /* ----------------------------------------------------------------------------- * cplus_value_type() * * Returns the alternative value type needed in C++ for class value * types. When swig is not sure about using a plain $ltype value, * since the class doesn't have a default constructor, or it can't be * assigned, you will get back 'SwigValueWrapper<(type)>'. * * ----------------------------------------------------------------------------- */ SwigType *cplus_value_type(SwigType *t) { return SwigType_alttype(t, 0); } static Node *first_nontemplate(Node *n) { while (n) { if (Strcmp(nodeType(n), "template") != 0) return n; n = Getattr(n, "sym:nextSibling"); } return n; } /* -------------------------------------------------------------------------- * swig_pragma() * * Handle swig pragma directives. * -------------------------------------------------------------------------- */ static void swig_pragma(char *lang, char *name, char *value) { if (strcmp(lang, "swig") == 0) { if (strcmp(name, "attributefunction") == 0) { String *nvalue = NewString(value); char *s = strchr(Char(nvalue), ':'); if (!s) { Swig_error(input_file, line_number, "Bad value for attributefunction. Expected \"fmtget:fmtset\".\n"); } else { *s = 0; AttributeFunctionGet = NewString(Char(nvalue)); AttributeFunctionSet = NewString(s + 1); } Delete(nvalue); } else if (strcmp(name, "noattributefunction") == 0) { AttributeFunctionGet = 0; AttributeFunctionSet = 0; } } } /* -------------------------------------------------------------------------- * Language::use_naturalvar_mode() * * Determine whether to use const ref typemaps instead of pointer typemaps * for variable access. * -------------------------------------------------------------------------- */ int Language::use_naturalvar_mode(Node *n) const { if (Getattr(n, "unnamed")) return 0; // The naturalvar feature can be attached to either the variable name or the variable's type // naturalvar on the variable name is more specific and overrides naturalvar on the variable's type String *naturalvar = Getattr(n, "feature:naturalvar"); bool explicitly_off = naturalvar && Strcmp(naturalvar, "0") == 0; int nvar = GetFlag(n, "feature:naturalvar"); if (!explicitly_off && !nvar) { /* look for feature in the class */ SwigType *ty = Getattr(n, "type"); SwigType *fullty = SwigType_typedef_resolve_all(ty); if (SwigType_isclass(fullty)) { SwigType *tys = SwigType_strip_qualifiers(fullty); if (!CPlusPlus) { Replaceall(tys, "struct ", ""); Replaceall(tys, "union ", ""); Replaceall(tys, "class ", ""); } Node *typenode = Swig_symbol_clookup(tys, 0); if (typenode) { naturalvar = Getattr(typenode, "feature:naturalvar"); explicitly_off = naturalvar && Strcmp(naturalvar, "0") == 0; nvar = nvar || GetFlag(typenode, "feature:naturalvar"); } Delete(tys); } Delete(fullty); } nvar = nvar || naturalvar_mode; return explicitly_off ? 0 : nvar ? CWRAP_NATURAL_VAR : 0; } /* ---------------------------------------------------------------------- * Language::top() - Top of parsing tree * ---------------------------------------------------------------------- */ int Language::top(Node *n) { Node *mod = Getattr(n, "module"); if (mod) { Node *options = Getattr(mod, "options"); if (options) { if (Getattr(options, "naturalvar")) { naturalvar_mode = 1; } } } classhash = Getattr(n, "classes"); return emit_children(n); } /* ---------------------------------------------------------------------- * Language::extendDirective() * ---------------------------------------------------------------------- */ int Language::extendDirective(Node *n) { save_value oldam(Extend, CWRAP_EXTEND); save_value oldmode(cplus_mode, PUBLIC); emit_children(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::applyDirective() * ---------------------------------------------------------------------- */ int Language::applyDirective(Node *n) { Parm *pattern = Getattr(n, "pattern"); Node *c = firstChild(n); while (c) { Parm *apattern = Getattr(c, "pattern"); if (ParmList_len(pattern) != ParmList_len(apattern)) { Swig_error(input_file, line_number, "Can't apply (%s) to (%s). Number of arguments don't match.\n", ParmList_str(pattern), ParmList_str(apattern)); } else { if (!Swig_typemap_apply(pattern, apattern)) { Swig_warning(WARN_TYPEMAP_APPLY_UNDEF, input_file, line_number, "Can't apply (%s). No typemaps are defined.\n", ParmList_str(pattern)); } } c = nextSibling(c); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::clearDirective() * ---------------------------------------------------------------------- */ int Language::clearDirective(Node *n) { Node *p; for (p = firstChild(n); p; p = nextSibling(p)) { ParmList *pattern = Getattr(p, "pattern"); Swig_typemap_clear_apply(pattern); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::constantDirective() * ---------------------------------------------------------------------- */ int Language::constantDirective(Node *n) { if (CurrentClass && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; if (!GetFlag(n, "feature:allowexcept")) { UnsetFlag(n, "feature:except"); } if (Getattr(n, "feature:exceptvar")) { Setattr(n, "feature:except", Getattr(n, "feature:exceptvar")); } if (!ImportMode) { Swig_require("constantDirective", n, "name", "?value", NIL); if (!Getattr(n, "value")) { Setattr(n, "value", Getattr(n, "name")); } this->constantWrapper(n); Swig_restore(n); return SWIG_OK; } return SWIG_NOWRAP; } /* ---------------------------------------------------------------------- * Language::fragmentDirective() * ---------------------------------------------------------------------- */ int Language::fragmentDirective(Node *n) { if (!(Getattr(n, "emitonly") && ImportMode)) Swig_fragment_register(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::importDirective() * ---------------------------------------------------------------------- */ int Language::importDirective(Node *n) { int oldim = ImportMode; ImportMode = IMPORT_MODE; emit_children(n); ImportMode = oldim; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::includeDirective() * ---------------------------------------------------------------------- */ int Language::includeDirective(Node *n) { emit_children(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::insertDirective() * ---------------------------------------------------------------------- */ int Language::insertDirective(Node *n) { /* %insert directive */ if ((!ImportMode) || Getattr(n, "generated")) { String *code = Getattr(n, "code"); String *section = Getattr(n, "section"); File *f = 0; if (!section) { /* %{ ... %} */ f = Swig_filebyname("header"); } else { f = Swig_filebyname(section); } if (f) { Printf(f, "%s\n", code); } else { Swig_error(input_file, line_number, "Unknown target '%s' for %%insert directive.\n", section); } return SWIG_OK; } else { return SWIG_NOWRAP; } } /* ---------------------------------------------------------------------- * Language::moduleDirective() * ---------------------------------------------------------------------- */ int Language::moduleDirective(Node *n) { (void) n; /* %module directive */ return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::nativeDirective() * ---------------------------------------------------------------------- */ int Language::nativeDirective(Node *n) { if (!ImportMode) { return nativeWrapper(n); } else { return SWIG_NOWRAP; } } /* ---------------------------------------------------------------------- * Language::pragmaDirective() * ---------------------------------------------------------------------- */ int Language::pragmaDirective(Node *n) { /* %pragma directive */ if (!ImportMode) { String *lan = Getattr(n, "lang"); String *name = Getattr(n, "name"); String *value = Getattr(n, "value"); swig_pragma(Char(lan), Char(name), Char(value)); /* pragma(Char(lan),Char(name),Char(value)); */ return SWIG_OK; } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::typemapDirective() * ---------------------------------------------------------------------- */ int Language::typemapDirective(Node *n) { /* %typemap directive */ String *method = Getattr(n, "method"); String *code = Getattr(n, "code"); Parm *kwargs = Getattr(n, "kwargs"); Node *items = firstChild(n); static int nameerror = 0; if (code && (Strstr(code, "$source") || (Strstr(code, "$target")))) { Swig_error(Getfile(n), Getline(n), "Obsolete typemap feature ($source/$target).\n"); if (!nameerror) { Swig_error(Getfile(n), Getline(n), "The use of $source and $target in a typemap declaration is no longer supported.\n\ For typemaps related to argument input (in,ignore,default,arginit,check), replace\n\ $source by $input and $target by $1. For typemaps related to return values (out,\n\ argout,ret,except), replace $source by $1 and $target by $result. See the file\n\ Doc/Manual/Typemaps.html for complete details.\n"); nameerror = 1; } } if (Strcmp(method, "except") == 0) { Swig_error(Getfile(n), Getline(n), "%%typemap(except) is no longer supported. Use the %%exception directive.\n"); } if (Strcmp(method, "in") == 0) { Hash *k; k = kwargs; while (k) { if (checkAttribute(k, "name", "numinputs")) { if (!multiinput && (GetInt(k, "value") > 1)) { Swig_error(Getfile(n), Getline(n), "Multiple-input typemaps (numinputs > 1) not supported by this target language module.\n"); return SWIG_ERROR; } break; } k = nextSibling(k); } if (!k) { k = NewHash(); Setattr(k, "name", "numinputs"); Setattr(k, "value", "1"); set_nextSibling(k, kwargs); Setattr(n, "kwargs", k); kwargs = k; } } if (Strcmp(method, "ignore") == 0) { Swig_error(Getfile(n), Getline(n), "%%typemap(ignore) is no longer supported. Use %%typemap(in,numinputs=0).\n"); } /* Replace $descriptor() macros */ if (code) { Setfile(code, Getfile(n)); Setline(code, Getline(n)); Swig_cparse_replace_descriptor(code); } while (items) { Parm *pattern = Getattr(items, "pattern"); Parm *parms = Getattr(items, "parms"); if (code) { Swig_typemap_register(method, pattern, code, parms, kwargs); } else { Swig_typemap_clear(method, pattern); } items = nextSibling(items); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::typemapcopyDirective() * ---------------------------------------------------------------------- */ int Language::typemapcopyDirective(Node *n) { String *method = Getattr(n, "method"); Parm *pattern = Getattr(n, "pattern"); Node *items = firstChild(n); int nsrc = 0; nsrc = ParmList_len(pattern); while (items) { ParmList *npattern = Getattr(items, "pattern"); if (nsrc != ParmList_len(npattern)) { Swig_error(input_file, line_number, "Can't copy typemap. Number of types differ.\n"); } else { if (Swig_typemap_copy(method, pattern, npattern) < 0) { Swig_error(input_file, line_number, "Can't copy typemap (%s) %s = %s\n", method, ParmList_str(pattern), ParmList_str(npattern)); } } items = nextSibling(items); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::typesDirective() * ---------------------------------------------------------------------- */ int Language::typesDirective(Node *n) { Parm *parms = Getattr(n, "parms"); String *convcode = Getattr(n, "convcode"); /* optional user supplied conversion code for custom casting */ while (parms) { SwigType *t = Getattr(parms, "type"); String *v = Getattr(parms, "value"); if (!v) { SwigType_remember(t); } else { if (SwigType_issimple(t)) { SwigType_inherit(t, v, 0, convcode); } } parms = nextSibling(parms); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::cDeclaration() * ---------------------------------------------------------------------- */ int Language::cDeclaration(Node *n) { String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); SwigType *decl = Getattr(n, "decl"); String *storage = Getattr(n, "storage"); Node *over; File *f_header = 0; SwigType *ty, *fullty; if (Getattr(n, "feature:onlychildren")) { if (GetFlag(n, "feature:ignore")) { return SWIG_NOWRAP; } else { // Found an unignored templated method that has an empty template instantiation (%template()) // Ignore it unless it has been %rename'd if (Strncmp(symname, "__dummy_", 8) == 0 && Cmp(storage, "typedef") != 0) { SetFlag(n, "feature:ignore"); Swig_warning(WARN_LANG_TEMPLATE_METHOD_IGNORE, input_file, line_number, "%%template() contains no name. Template method ignored: %s\n", Swig_name_decl(n)); return SWIG_NOWRAP; } } } /* discards nodes following the access control rules */ if (cplus_mode != PUBLIC || !is_public(n)) { /* except for friends, they are not affected by access control */ int isfriend = (Strstr(storage, "friend") != NULL); if (!isfriend) { /* Check what the director needs. If the method is pure virtual, it is always needed. * Also wrap non-virtual protected members if asked for (allprotected mode). */ if (!(Swig_directors_enabled() && ((is_member_director(CurrentClass, n) && need_nonpublic_member(n)) || isNonVirtualProtectedAccess(n)))) { return SWIG_NOWRAP; } // Prevent wrapping protected overloaded director methods more than once - // This bit of code is only needed due to the cDeclaration call in classHandler() String *wrapname = NewStringf("nonpublic_%s%s", symname, Getattr(n, "sym:overname")); if (Getattr(CurrentClass, wrapname)) { Delete(wrapname); return SWIG_NOWRAP; } SetFlag(CurrentClass, wrapname); Delete(wrapname); } } if (Cmp(storage, "typedef") == 0) { Swig_save("cDeclaration", n, "type", NIL); SwigType *t = Copy(type); if (t) { SwigType_push(t, decl); Setattr(n, "type", t); typedefHandler(n); } Swig_restore(n); return SWIG_OK; } /* If in import mode, we proceed no further */ if (ImportMode) return SWIG_NOWRAP; /* If we're in extend mode and there is code, replace the $descriptor macros */ if (Extend) { String *code = Getattr(n, "code"); if (code) { Setfile(code, Getfile(n)); Setline(code, Getline(n)); Swig_cparse_replace_descriptor(code); } } /* Overloaded symbol check */ over = Swig_symbol_isoverloaded(n); if (!overloading) { if (over) over = first_nontemplate(over); if (over && (over != n)) { Swig_warning(WARN_LANG_OVERLOAD_DECL, input_file, line_number, "Overloaded declaration ignored. %s\n", Swig_name_decl(n)); Swig_warning(WARN_LANG_OVERLOAD_DECL, Getfile(over), Getline(over), "Previous declaration is %s\n", Swig_name_decl(over)); return SWIG_NOWRAP; } } if (!validIdentifier(symname)) { Swig_warning(WARN_LANG_IDENTIFIER, input_file, line_number, "Can't wrap '%s' unless renamed to a valid identifier.\n", SwigType_namestr(symname)); return SWIG_NOWRAP; } ty = NewString(type); SwigType_push(ty, decl); fullty = SwigType_typedef_resolve_all(ty); if (SwigType_isfunction(fullty)) { if (!SwigType_isfunction(ty)) { Delete(ty); ty = fullty; fullty = 0; ParmList *parms = SwigType_function_parms(ty, n); Setattr(n, "parms", parms); } /* Transform the node into a 'function' node and emit */ if (!CurrentClass) { f_header = Swig_filebyname("header"); if (AddExtern) { if (f_header) { if (Swig_storage_isextern(n) || (ForceExtern && !storage)) { /* we don't need the 'extern' part in the C/C++ declaration, and it produces some problems when namespace and SUN Studio is used. Printf(f_header,"extern %s", SwigType_str(ty,name)); In fact generating extern declarations is quite error prone and is no longer the default. Getting it right seems impossible with namespaces and default arguments and when a method is declared with the various Windows calling conventions - SWIG doesn't understand Windows (non standard) calling conventions in the first place, so can't regenerate them. */ String *str = SwigType_str(ty, name); Printf(f_header, "%s", str); Delete(str); { DOH *t = Getattr(n, "throws"); if (t) { Printf(f_header, " throw("); while (t) { Printf(f_header, "%s", Getattr(t, "type")); t = nextSibling(t); if (t) Printf(f_header, ","); } Printf(f_header, ")"); } } Printf(f_header, ";\n"); } else if (Swig_storage_isexternc(n)) { /* here 'extern "C"' is needed */ String *str = SwigType_str(ty, name); Printf(f_header, "extern \"C\" %s;\n", str); Delete(str); } } } } /* This needs to check qualifiers */ if (SwigType_isqualifier(ty)) { SwigType *qual = SwigType_pop(ty); Setattr(n, "qualifier", qual); Delete(qual); } Delete(SwigType_pop_function(ty)); DohIncref(type); Setattr(n, "type", ty); functionHandler(n); Setattr(n, "type", type); Delete(ty); Delete(type); return SWIG_OK; } else { /* Some kind of variable declaration */ String *declaration = Copy(decl); Delattr(n, "decl"); if (!CurrentClass) { if (Swig_storage_isextern(n) || ForceExtern) { if (AddExtern) { f_header = Swig_filebyname("header"); if (f_header) { String *str = SwigType_str(ty, name); Printf(f_header, "%s %s;\n", Getattr(n, "storage"), str); Delete(str); } } } } DohIncref(type); Setattr(n, "type", ty); variableHandler(n); Setattr(n, "type", type); Setattr(n, "decl", declaration); Delete(ty); Delete(type); Delete(fullty); return SWIG_OK; } } /* ---------------------------------------------------------------------- * Language::functionHandler() * ---------------------------------------------------------------------- */ int Language::functionHandler(Node *n) { String *storage = Getattr(n, "storage"); int isfriend = CurrentClass && Strstr(storage, "friend"); int isstatic = CurrentClass && Swig_storage_isstatic(n) && !(SmartPointer && Getattr(n, "allocate:smartpointeraccess")); Parm *p = Getattr(n, "parms"); if (GetFlag(n, "feature:del")) { /* the method acts like a delete operator, ie, we need to disown the parameter */ if (CurrentClass && !isstatic && !isfriend) { SetFlag(n, "feature:self:disown"); } else { if (p) SetFlag(p, "wrap:disown"); } } if (!CurrentClass) { globalfunctionHandler(n); } else { if (isstatic) { staticmemberfunctionHandler(n); } else if (isfriend) { int oldInClass = InClass; InClass = 0; globalfunctionHandler(n); InClass = oldInClass; } else { // This is a member function, set a flag so the documentation type is correct SetFlag(n, "memberfunction"); Node *explicit_n = 0; if (Swig_directors_enabled() && is_member_director(CurrentClass, n) && !extraDirectorProtectedCPPMethodsRequired()) { bool virtual_but_not_pure_virtual = (!(Cmp(storage, "virtual")) && (Cmp(Getattr(n, "value"), "0") != 0)); if (virtual_but_not_pure_virtual) { // Add additional wrapper which makes an explicit call to the virtual method (ie not a virtual call) explicit_n = Copy(n); String *new_symname = Copy(Getattr(n, "sym:name")); String *suffix = Getattr(parentNode(n), "sym:name"); Printv(new_symname, "SwigExplicit", suffix, NIL); Setattr(explicit_n, "sym:name", new_symname); Delattr(explicit_n, "storage"); Delattr(explicit_n, "override"); Delattr(explicit_n, "hides"); SetFlag(explicit_n, "explicitcall"); Setattr(n, "explicitcallnode", explicit_n); } } memberfunctionHandler(n); if (explicit_n) { memberfunctionHandler(explicit_n); Delattr(explicit_n, "explicitcall"); Delete(explicit_n); } } } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::globalfunctionHandler() * ---------------------------------------------------------------------- */ int Language::globalfunctionHandler(Node *n) { Swig_require("globalfunctionHandler", n, "name", "sym:name", "type", "?parms", NIL); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); ParmList *parms = Getattr(n, "parms"); /* Check for callback mode */ String *cb = GetFlagAttr(n, "feature:callback"); if (cb) { String *cbname = Getattr(n, "feature:callback:name"); if (!cbname) { cbname = NewStringf(cb, symname); Setattr(n, "feature:callback:name", cbname); } callbackfunctionHandler(n); if (Cmp(cbname, symname) == 0) { Delete(cbname); Swig_restore(n); return SWIG_NOWRAP; } Delete(cbname); } Setattr(n, "parms", nonvoid_parms(parms)); String *extendname = Getattr(n, "extendname"); String *call = Swig_cfunction_call(extendname ? extendname : name, parms); String *cres = Swig_cresult(type, Swig_cresult_name(), call); String *friendusing = Getattr(n, "friendusing"); if (friendusing) { // Add a using directive to avoid having to possibly fully qualify the call to the friend function. // Unconventional for SWIG generation, but the alternative is to implement Argument Dependent Lookup // as friend functions are quirky and not visible, except for ADL. An ADL implementation would be needed // in order to work out when the friend function is visible or not, in order to determine whether to // rely on ADL (with no qualification) or to fully qualify the call to the friend function made // visible via a matching declaration at namespace scope. String *action = NewStringf("%s\n%s", friendusing, cres); Setattr(n, "wrap:action", action); Delete(action); } else { Setattr(n, "wrap:action", cres); } Delete(cres); Delete(call); functionWrapper(n); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::callbackfunctionHandler() * ---------------------------------------------------------------------- */ int Language::callbackfunctionHandler(Node *n) { Swig_require("callbackfunctionHandler", n, "name", "*sym:name", "*type", "?value", NIL); String *type = Getattr(n, "type"); String *name = Getattr(n, "name"); String *parms = Getattr(n, "parms"); String *cbname = Getattr(n, "feature:callback:name"); String *calltype = NewStringf("(%s (*)(%s))(%s)", SwigType_str(type, 0), ParmList_str(parms), SwigType_namestr(name)); SwigType *cbty = Copy(type); SwigType_add_function(cbty, parms); SwigType_add_pointer(cbty); Setattr(n, "sym:name", cbname); Setattr(n, "type", cbty); Setattr(n, "value", calltype); Node *ns = symbolLookup(cbname); if (!ns) constantWrapper(n); Delete(cbty); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::memberfunctionHandler() * ---------------------------------------------------------------------- */ int Language::memberfunctionHandler(Node *n) { Swig_require("memberfunctionHandler", n, "*name", "*sym:name", "*type", "?parms", "?value", NIL); String *storage = Getattr(n, "storage"); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); ParmList *parms = Getattr(n, "parms"); String *cb = GetFlagAttr(n, "feature:callback"); if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { IsVirtual = PURE_VIRTUAL; } else { IsVirtual = PLAIN_VIRTUAL; } } else { IsVirtual = 0; } if (cb) { Node *cbn = NewHash(); String *cbname = Getattr(n, "feature:callback:name"); if (!cbname) { cbname = NewStringf(cb, symname); } SwigType *cbty = Copy(type); SwigType_add_function(cbty, parms); SwigType_add_memberpointer(cbty, ClassName); String *cbvalue = NewStringf("&%s::%s", ClassName, name); Setattr(cbn, "sym:name", cbname); Setattr(cbn, "type", cbty); Setattr(cbn, "value", cbvalue); Setattr(cbn, "name", name); Setfile(cbn, Getfile(n)); Setline(cbn, Getline(n)); memberconstantHandler(cbn); Setattr(n, "feature:callback:name", Swig_name_member(NSpace, ClassPrefix, cbname)); Delete(cb); Delete(cbn); Delete(cbvalue); Delete(cbty); Delete(cbname); if (Cmp(cbname, symname) == 0) { Swig_restore(n); return SWIG_NOWRAP; } } String *fname = Swig_name_member(NSpace, ClassPrefix, symname); if (Extend && SmartPointer) { if (!Getattr(n, "extendsmartclassname")) { Setattr(n, "extendsmartclassname", Getattr(CurrentClass, "allocate:smartpointerpointeeclassname")); } } // Set up the type for the cast to this class for use when wrapping const director (virtual) methods. // Note: protected director methods or when allprotected mode turned on. String *director_type = 0; if (!is_public(n) && (is_member_director(CurrentClass, n) || GetFlag(n, "explicitcall") || isNonVirtualProtectedAccess(n))) { director_type = Copy(DirectorClassName); String *qualifier = Getattr(n, "qualifier"); if (qualifier) SwigType_push(director_type, qualifier); SwigType_add_pointer(director_type); } int DirectorExtraCall = 0; if (Swig_directors_enabled() && is_member_director(CurrentClass, n) && !SmartPointer) if (extraDirectorProtectedCPPMethodsRequired()) DirectorExtraCall = CWRAP_DIRECTOR_TWO_CALLS; if (GetFlag(n, "explicitcall")) DirectorExtraCall = CWRAP_DIRECTOR_ONE_CALL; int extendmember = GetFlag(n, "isextendmember") ? Extend : 0; int flags = Getattr(n, "template") ? extendmember | SmartPointer : Extend | SmartPointer | DirectorExtraCall; Swig_MethodToFunction(n, NSpace, ClassType, flags, director_type, is_member_director(CurrentClass, n)); Setattr(n, "sym:name", fname); /* Explicitly save low-level and high-level documentation names */ Setattr(n, "doc:low:name", fname); Setattr(n, "doc:high:name", symname); functionWrapper(n); Delete(director_type); Delete(fname); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::staticmemberfunctionHandler() * ---------------------------------------------------------------------- */ int Language::staticmemberfunctionHandler(Node *n) { Swig_require("staticmemberfunctionHandler", n, "*name", "*sym:name", "*type", NIL); Swig_save("staticmemberfunctionHandler", n, "storage", NIL); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); ParmList *parms = Getattr(n, "parms"); String *cb = GetFlagAttr(n, "feature:callback"); String *cname; String *mrename = Swig_name_member(NSpace, ClassPrefix, symname); if (!(Extend && GetFlag(n, "isextendmember"))) { Node *sb = Getattr(n, "cplus:staticbase"); String *sname = Getattr(sb, "name"); if (isNonVirtualProtectedAccess(n)) cname = NewStringf("%s::%s", DirectorClassName, name); else cname = NewStringf("%s::%s", sname, name); } else { String *classname_str = SwigType_namestr(ClassName); String *mname = Swig_name_mangle_string(classname_str); cname = Swig_name_member(NSpace, mname, name); Delete(mname); Delete(classname_str); String *code = Getattr(n, "code"); String *defaultargs = Getattr(n, "defaultargs"); String *mangled = Swig_name_mangle_string(mrename); Delete(mrename); mrename = mangled; if (code) { // See Swig_MethodToFunction() for the explanation of this code. if (Getattr(n, "sym:overloaded")) { Append(cname, Getattr(defaultargs ? defaultargs : n, "sym:overname")); } else if (UseWrapperSuffix) { Append(cname, "__SWIG"); } if (!defaultargs) { /* Hmmm. An added static member. We have to create a little wrapper for this */ String *mangled_cname = Swig_name_mangle_string(cname); Swig_add_extension_code(n, mangled_cname, parms, type, code, CPlusPlus, 0); Setattr(n, "extendname", mangled_cname); Delete(mangled_cname); } } } Setattr(n, "name", cname); Setattr(n, "sym:name", mrename); /* Explicitly save low-level and high-level documentation names */ Setattr(n, "doc:low:name", mrename); Setattr(n, "doc:high:name", symname); if (cb) { String *cbname = NewStringf(cb, symname); Setattr(n, "feature:callback:name", Swig_name_member(NSpace, ClassPrefix, cbname)); Setattr(n, "feature:callback:staticname", name); } Delattr(n, "storage"); globalfunctionHandler(n); Delete(cname); Delete(mrename); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::variableHandler() * ---------------------------------------------------------------------- */ int Language::variableHandler(Node *n) { /* If not a smart-pointer access or added method. We clear feature:except. There is no way C++ or C would throw an exception merely for accessing a member data. Caveat: Some compilers seem to route attribute access through methods which can generate exceptions. The feature:allowexcept allows this. Also, the feature:exceptvar can be used to match only variables. */ if (!(Extend | SmartPointer)) { if (!GetFlag(n, "feature:allowexcept")) { UnsetFlag(n, "feature:except"); } if (Getattr(n, "feature:exceptvar")) { Setattr(n, "feature:except", Getattr(n, "feature:exceptvar")); } } if (!CurrentClass) { globalvariableHandler(n); } else { Swig_save("variableHandler", n, "feature:immutable", NIL); if (SmartPointer) { /* If a smart-pointer and it's a constant access, we have to set immutable */ if (!Getattr(CurrentClass, "allocate:smartpointermutable")) { SetFlag(n, "feature:immutable"); } } if (Swig_storage_isstatic(n) && !(SmartPointer && Getattr(n, "allocate:smartpointeraccess"))) { staticmembervariableHandler(n); } else { membervariableHandler(n); } Swig_restore(n); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::globalvariableHandler() * ---------------------------------------------------------------------- */ int Language::globalvariableHandler(Node *n) { variableWrapper(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::membervariableHandler() * ---------------------------------------------------------------------- */ int Language::membervariableHandler(Node *n) { Swig_require("membervariableHandler", n, "*name", "*sym:name", "*type", NIL); Swig_save("membervariableHandler", n, "parms", NIL); String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); if (!AttributeFunctionGet) { String *mname = Swig_name_member(0, ClassPrefix, symname); String *mrename_get = Swig_name_get(NSpace, mname); String *mrename_set = Swig_name_set(NSpace, mname); Delete(mname); /* Create a function to set the value of the variable */ int assignable = !is_immutable(n); if (SmartPointer) { if (!Getattr(CurrentClass, "allocate:smartpointermutable")) { assignable = 0; } } if (assignable) { int make_set_wrapper = 1; String *tm = 0; String *target = 0; if (!Extend) { if (SmartPointer) { if (Swig_storage_isstatic(n)) { Node *sn = Getattr(n, "cplus:staticbase"); String *base = Getattr(sn, "name"); target = NewStringf("%s::%s", base, name); } else { String *pname = Swig_cparm_name(0, 0); target = NewStringf("(*%s)->%s", pname, name); Delete(pname); } } else { String *pname = isNonVirtualProtectedAccess(n) ? NewString("darg") : Swig_cparm_name(0, 0); target = NewStringf("%s->%s", pname, name); Delete(pname); } // This is an input type typemap lookup and so it should not use Node n // otherwise qualification is done on the parameter name for the setter function Parm *nin = NewParm(type, name, n); tm = Swig_typemap_lookup("memberin", nin, target, 0); Delete(nin); } int flags = Extend | SmartPointer | use_naturalvar_mode(n); if (isNonVirtualProtectedAccess(n)) flags = flags | CWRAP_ALL_PROTECTED_ACCESS; Swig_MembersetToFunction(n, ClassType, flags); Setattr(n, "memberset", "1"); if (!Extend) { /* Check for a member in typemap here */ if (!tm) { if (SwigType_isarray(type)) { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(type, 0)); make_set_wrapper = 0; } } else { String *pname0 = Swig_cparm_name(0, 0); String *pname1 = Swig_cparm_name(0, 1); Replace(tm, "$input", pname1, DOH_REPLACE_ANY); Replace(tm, "$self", pname0, DOH_REPLACE_ANY); Setattr(n, "wrap:action", tm); Delete(tm); Delete(pname0); Delete(pname1); } Delete(target); } if (make_set_wrapper) { Setattr(n, "sym:name", mrename_set); functionWrapper(n); } else { SetFlag(n, "feature:immutable"); } /* Restore parameters */ Setattr(n, "type", type); Setattr(n, "name", name); Setattr(n, "sym:name", symname); Delattr(n, "memberset"); /* Delete all attached typemaps and typemap attributes */ Iterator ki; for (ki = First(n); ki.key; ki = Next(ki)) { if (Strncmp(ki.key, "tmap:", 5) == 0) Delattr(n, ki.key); } } /* Emit get function */ { int flags = Extend | SmartPointer | use_naturalvar_mode(n); if (isNonVirtualProtectedAccess(n)) flags = flags | CWRAP_ALL_PROTECTED_ACCESS; Swig_MembergetToFunction(n, ClassType, flags); Setattr(n, "sym:name", mrename_get); Setattr(n, "memberget", "1"); functionWrapper(n); Delattr(n, "memberget"); } Delete(mrename_get); Delete(mrename_set); } else { /* This code is used to support the attributefunction directive where member variables are converted automagically to accessor functions */ #if 0 Parm *p; String *gname; SwigType *vty; p = NewParm(type, 0, n); gname = NewStringf(AttributeFunctionGet, symname); if (!Extend) { ActionFunc = Copy(Swig_cmemberget_call(name, type)); cpp_member_func(Char(gname), Char(gname), type, 0); Delete(ActionFunc); } else { String *cname = Swig_name_get(NSpace, name); cpp_member_func(Char(cname), Char(gname), type, 0); Delete(cname); } Delete(gname); if (!GetFlag(n, "feature:immutable")) { gname = NewStringf(AttributeFunctionSet, symname); vty = NewString("void"); if (!Extend) { ActionFunc = Copy(Swig_cmemberset_call(name, type)); cpp_member_func(Char(gname), Char(gname), vty, p); Delete(ActionFunc); } else { String *cname = Swig_name_set(NSpace, name); cpp_member_func(Char(cname), Char(gname), vty, p); Delete(cname); } Delete(gname); } ActionFunc = 0; #endif } Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::staticmembervariableHandler() * ---------------------------------------------------------------------- */ int Language::staticmembervariableHandler(Node *n) { Swig_require("staticmembervariableHandler", n, "*name", "*sym:name", "*type", "?value", NIL); String *value = Getattr(n, "value"); String *classname = !SmartPointer ? (isNonVirtualProtectedAccess(n) ? DirectorClassName : ClassName) : Getattr(CurrentClass, "allocate:smartpointerpointeeclassname"); if (!value || !Getattr(n, "hasconsttype")) { String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); String *cname, *mrename; /* Create the variable name */ mrename = Swig_name_member(0, ClassPrefix, symname); cname = NewStringf("%s::%s", classname, name); Setattr(n, "sym:name", mrename); Setattr(n, "name", cname); /* Wrap as an ordinary global variable */ variableWrapper(n); Delete(mrename); Delete(cname); } else { /* This is a C++ static member declaration with an initializer and it's const. Certain C++ compilers optimize this out so that there is no linkage to a memory address. Example: class Foo { public: static const int x = 3; }; Some discussion of this in section 9.4 of the C++ draft standard. Also, we have to manage the case: class Foo { public: %extend { static const int x = 3; } }; in which there's no actual Foo::x variable to refer to. In this case, the best we can do is to wrap the given value verbatim. */ String *name = Getattr(n, "name"); String *cname = NewStringf("%s::%s", classname, name); if (Extend) { /* the variable is a synthesized one. There's nothing we can do; we just keep the given value */ } else { /* we refer to the value as Foo::x */ String *value = SwigType_namestr(cname); Setattr(n, "value", value); } SwigType *t1 = SwigType_typedef_resolve_all(Getattr(n, "type")); SwigType *t2 = SwigType_strip_qualifiers(t1); Setattr(n, "type", t2); Delete(t1); Delete(t2); SetFlag(n, "wrappedasconstant"); memberconstantHandler(n); Delete(cname); } Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::externDeclaration() * ---------------------------------------------------------------------- */ int Language::externDeclaration(Node *n) { return emit_children(n); } /* ---------------------------------------------------------------------- * Language::enumDeclaration() * ---------------------------------------------------------------------- */ int Language::enumDeclaration(Node *n) { if (CurrentClass && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; String *oldNSpace = NSpace; NSpace = Getattr(n, "sym:nspace"); String *oldEnumClassPrefix = EnumClassPrefix; if (GetFlag(n, "scopedenum")) { assert(Getattr(n, "sym:name")); assert(Getattr(n, "name")); EnumClassPrefix = ClassPrefix ? NewStringf("%s_", ClassPrefix) : NewString(""); Printv(EnumClassPrefix, Getattr(n, "sym:name"), NIL); EnumClassName = Copy(Getattr(n, "name")); } if (!ImportMode) { emit_children(n); } if (GetFlag(n, "scopedenum")) { Delete(EnumClassName); EnumClassName = 0; Delete(EnumClassPrefix); EnumClassPrefix = oldEnumClassPrefix; } NSpace = oldNSpace; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::enumvalueDeclaration() * ---------------------------------------------------------------------- */ int Language::enumvalueDeclaration(Node *n) { if (CurrentClass && (cplus_mode != PUBLIC)) return SWIG_NOWRAP; Swig_require("enumvalueDeclaration", n, "*name", "*sym:name", "?value", NIL); String *value = Getattr(n, "value"); String *name = Getattr(n, "name"); String *tmpValue; if (value) tmpValue = NewString(value); else tmpValue = NewString(name); Setattr(n, "value", tmpValue); Node *parent = parentNode(n); if (GetFlag(parent, "scopedenum")) { String *symname = Swig_name_member(0, Getattr(parent, "sym:name"), Getattr(n, "sym:name")); Setattr(n, "sym:name", symname); Delete(symname); } if (!CurrentClass || !cparse_cplusplus) { Setattr(n, "name", tmpValue); /* for wrapping of enums in a namespace when emit_action is used */ constantWrapper(n); } else { memberconstantHandler(n); } Delete(tmpValue); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::enumforwardDeclaration() * ---------------------------------------------------------------------- */ int Language::enumforwardDeclaration(Node *n) { (void) n; if (GetFlag(n, "enumMissing")) enumDeclaration(n); // Generate an empty enum in target language return SWIG_OK; } /* ----------------------------------------------------------------------------- * Language::memberconstantHandler() * ----------------------------------------------------------------------------- */ int Language::memberconstantHandler(Node *n) { Swig_require("memberconstantHandler", n, "*name", "*sym:name", "value", NIL); if (!GetFlag(n, "feature:allowexcept")) { UnsetFlag(n, "feature:except"); } if (Getattr(n, "feature:exceptvar")) { Setattr(n, "feature:except", Getattr(n, "feature:exceptvar")); } String *enumvalue_symname = Getattr(n, "enumvalueDeclaration:sym:name"); // Only set if a strongly typed enum String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); String *value = Getattr(n, "value"); String *mrename = Swig_name_member(0, EnumClassPrefix, enumvalue_symname ? enumvalue_symname : symname); Setattr(n, "sym:name", mrename); String *new_name = 0; if (Extend) new_name = Copy(value); else if (EnumClassName) new_name = NewStringf("%s::%s", isNonVirtualProtectedAccess(n) ? DirectorClassName : EnumClassName, name); else new_name = NewStringf("%s::%s", isNonVirtualProtectedAccess(n) ? DirectorClassName : ClassName, name); Setattr(n, "name", new_name); constantWrapper(n); Delete(mrename); Delete(new_name); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::typedefHandler() * ---------------------------------------------------------------------- */ int Language::typedefHandler(Node *n) { /* since this is a recurring issue, we are going to remember the typedef pointer, if already it is not a pointer or reference, as in typedef void NT; int func(NT *p); see director_basic.i for example. */ SwigType *name = Getattr(n, "name"); SwigType *decl = Getattr(n, "decl"); Setfile(name, Getfile(n)); Setline(name, Getline(n)); if (!SwigType_ispointer(decl) && !SwigType_isreference(decl)) { SwigType *pname = Copy(name); SwigType_add_pointer(pname); SwigType_remember(pname); Delete(pname); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorMethod() * ---------------------------------------------------------------------- */ int Language::classDirectorMethod(Node *n, Node *parent, String *super) { (void) n; (void) parent; (void) super; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorConstructor() * ---------------------------------------------------------------------- */ int Language::classDirectorConstructor(Node *n) { (void) n; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorDefaultConstructor() * ---------------------------------------------------------------------- */ int Language::classDirectorDefaultConstructor(Node *n) { (void) n; return SWIG_OK; } static String *vtable_method_id(Node *n) { String *nodeType = Getattr(n, "nodeType"); int is_destructor = (Cmp(nodeType, "destructor") == 0); if (is_destructor) return 0; String *name = Getattr(n, "name"); String *decl = Getattr(n, "decl"); String *local_decl = SwigType_typedef_resolve_all(decl); String *tmp = SwigType_pop_function(local_decl); Delete(local_decl); local_decl = tmp; String *method_id = NewStringf("%s|%s", name, local_decl); Delete(local_decl); return method_id; } /* ---------------------------------------------------------------------- * Language::unrollOneVirtualMethod() * ---------------------------------------------------------------------- */ void Language::unrollOneVirtualMethod(String *classname, Node *n, Node *parent, List *vm, int &virtual_destructor, int protectedbase) { if (!checkAttribute(n, "storage", "virtual")) return; if (GetFlag(n, "final")) return; String *nodeType = Getattr(n, "nodeType"); /* we need to add methods(cdecl) and destructor (to check for throw decl) */ int is_destructor = (Cmp(nodeType, "destructor") == 0); if ((Cmp(nodeType, "cdecl") == 0) || is_destructor) { String *decl = Getattr(n, "decl"); /* extra check for function type and proper access */ if (SwigType_isfunction(decl) && (((!protectedbase || dirprot_mode()) && is_public(n)) || need_nonpublic_member(n))) { String *name = Getattr(n, "name"); String *method_id = is_destructor ? NewStringf("~destructor") : vtable_method_id(n); /* Make sure that the new method overwrites the existing: */ int len = Len(vm); const int DO_NOT_REPLACE = -1; int replace = DO_NOT_REPLACE; for (int i = 0; i < len; i++) { Node *item = Getitem(vm, i); String *check_vmid = Getattr(item, "vmid"); if (Strcmp(method_id, check_vmid) == 0) { replace = i; break; } } /* filling a new method item */ String *fqdname = NewStringf("%s::%s", classname, name); Hash *item = NewHash(); Setattr(item, "fqdname", fqdname); Node *m = Copy(n); /* Store the complete return type - needed for non-simple return types (pointers, references etc.) */ SwigType *ty = NewString(Getattr(m, "type")); SwigType_push(ty, decl); if (SwigType_isqualifier(ty)) { Delete(SwigType_pop(ty)); } Delete(SwigType_pop_function(ty)); Setattr(m, "returntype", ty); String *mname = NewStringf("%s::%s", Getattr(parent, "name"), name); /* apply the features of the original method found in the base class */ Swig_features_get(Swig_cparse_features(), 0, mname, Getattr(m, "decl"), m); Setattr(item, "methodNode", m); Setattr(item, "vmid", method_id); if (replace == DO_NOT_REPLACE) Append(vm, item); else Setitem(vm, replace, item); Setattr(n, "directorNode", m); Delete(mname); } if (is_destructor) { virtual_destructor = 1; } } } /* ---------------------------------------------------------------------- * Language::unrollVirtualMethods() * ---------------------------------------------------------------------- */ int Language::unrollVirtualMethods(Node *n, Node *parent, List *vm, int &virtual_destructor, int protectedbase) { bool first_base = false; // recurse through all base classes to build the vtable List *bl = Getattr(n, "bases"); if (bl) { Iterator bi; for (bi = First(bl); bi.item; bi = Next(bi)) { if (first_base && !director_multiple_inheritance) break; unrollVirtualMethods(bi.item, parent, vm, virtual_destructor); first_base = true; } } // recurse through all protected base classes to build the vtable, as needed bl = Getattr(n, "protectedbases"); if (bl) { Iterator bi; for (bi = First(bl); bi.item; bi = Next(bi)) { if (first_base && !director_multiple_inheritance) break; unrollVirtualMethods(bi.item, parent, vm, virtual_destructor, 1); first_base = true; } } // find the methods that need directors String *classname = Getattr(n, "name"); for (Node *ni = firstChild(n); ni; ni = nextSibling(ni)) { /* we only need to check the virtual members */ if (Equal(nodeType(ni), "using")) { for (Node *nn = firstChild(ni); nn; nn = Getattr(nn, "sym:nextSibling")) { unrollOneVirtualMethod(classname, nn, parent, vm, virtual_destructor, protectedbase); } } unrollOneVirtualMethod(classname, ni, parent, vm, virtual_destructor, protectedbase); } /* We delete all the nodirector methods. This prevents the generation of 'empty' director classes. Done once we've collated all the virtual methods into vm. */ if (n == parent) { int len = Len(vm); for (int i = 0; i < len; i++) { Node *item = Getitem(vm, i); Node *m = Getattr(item, "methodNode"); /* retrieve the director features */ int mdir = GetFlag(m, "feature:director"); int mndir = GetFlag(m, "feature:nodirector"); /* 'nodirector' has precedence over 'director' */ int dir = (mdir || mndir) ? (mdir && !mndir) : 1; /* check if the method was found only in a base class */ Node *p = Getattr(m, "parentNode"); if (p != n) { Node *c = Copy(m); Setattr(c, "parentNode", n); int cdir = GetFlag(c, "feature:director"); int cndir = GetFlag(c, "feature:nodirector"); dir = (cdir || cndir) ? (cdir && !cndir) : dir; Delete(c); } if (dir) { /* be sure the 'nodirector' feature is disabled */ if (mndir) Delattr(m, "feature:nodirector"); } else { /* or just delete from the vm, since is not a director method */ Delitem(vm, i); len--; i--; } } } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorDisown() * ---------------------------------------------------------------------- */ int Language::classDirectorDisown(Node *n) { Node *disown = NewHash(); String *mrename; String *symname = Getattr(n, "sym:name"); mrename = Swig_name_disown(NSpace, symname); String *type = NewString(ClassType); String *name = NewString("self"); SwigType_add_pointer(type); Parm *p = NewParm(type, name, n); Delete(name); Delete(type); type = NewString("void"); String *action = NewString(""); Printv(action, "{\n", "Swig::Director *director = SWIG_DIRECTOR_CAST(arg1);\n", "if (director) director->swig_disown();\n", "}\n", NULL); Setfile(disown, Getfile(n)); Setline(disown, Getline(n)); Setattr(disown, "wrap:action", action); Setattr(disown, "name", mrename); Setattr(disown, "sym:name", mrename); Setattr(disown, "type", type); Setattr(disown, "parms", p); Delete(action); Delete(mrename); Delete(type); Delete(p); functionWrapper(disown); Delete(disown); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorConstructors() * ---------------------------------------------------------------------- */ int Language::classDirectorConstructors(Node *n) { String *nodeType; Node *parent = Swig_methodclass(n); int default_ctor = Getattr(parent, "allocate:default_constructor") ? 1 : 0; int protected_ctor = 0; int constructor = 0; /* emit constructors */ List *constructors = NewList(); for (Node *ni = firstChild(n); ni; ni = nextSibling(ni)) { nodeType = Getattr(ni, "nodeType"); if (Equal(nodeType, "constructor")) { Append(constructors, ni); } else if (Equal(nodeType, "using") && GetFlag(ni, "usingctor")) { for (Node *ui = firstChild(ni); ui; ui = nextSibling(ui)) { Append(constructors, ui); } } } for (Iterator it = First(constructors); it.item; it = Next(it)) { Node *ni = it.item; if (GetFlag(ni, "feature:ignore")) continue; Parm *parms = Getattr(ni, "parms"); if (is_public(ni)) { /* emit public constructor */ classDirectorConstructor(ni); constructor = 1; if (default_ctor) default_ctor = !ParmList_numrequired(parms); } else { /* emit protected constructor if needed */ if (need_nonpublic_ctor(ni)) { classDirectorConstructor(ni); constructor = 1; protected_ctor = 1; if (default_ctor) default_ctor = !ParmList_numrequired(parms); } } } Delete(constructors); /* emit default constructor if needed */ if (!constructor) { if (!default_ctor) { /* we get here because the class has no public, protected or default constructor, therefore, the director class can't be created, ie, is kind of abstract. */ Swig_warning(WARN_LANG_DIRECTOR_ABSTRACT, Getfile(n), Getline(n), "Director class '%s' can't be constructed\n", SwigType_namestr(Getattr(n, "name"))); return SWIG_OK; } classDirectorDefaultConstructor(n); default_ctor = 1; } /* this is just to support old java behavior, ie, the default constructor is always emitted, even when protected, and not needed, since there is a public constructor already defined. (scottm) This code is needed here to make the director_abstract + test generate compilable code (Example2 in director_abstract.i). (mmatus) This is very strange, since swig compiled with gcc3.2.3 doesn't need it here.... */ if (!default_ctor && !protected_ctor) { if (Getattr(parent, "allocate:default_base_constructor")) { classDirectorDefaultConstructor(n); } } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorMethods() * ---------------------------------------------------------------------- */ int Language::classDirectorMethods(Node *n) { Node *vtable = Getattr(n, "vtable"); int len = Len(vtable); for (int i = 0; i < len; i++) { Node *item = Getitem(vtable, i); String *method = Getattr(item, "methodNode"); String *fqdname = Getattr(item, "fqdname"); if (GetFlag(method, "feature:nodirector") || GetFlag(method, "final")) continue; String *wrn = Getattr(method, "feature:warnfilter"); if (wrn) Swig_warnfilter(wrn, 1); String *type = Getattr(method, "nodeType"); if (!Cmp(type, "destructor")) { classDirectorDestructor(method); } else { Swig_require("classDirectorMethods", method, "*type", NIL); assert(Getattr(method, "returntype")); Setattr(method, "type", Getattr(method, "returntype")); if (classDirectorMethod(method, n, fqdname) == SWIG_OK) SetFlag(item, "director"); Swig_restore(method); } if (wrn) Swig_warnfilter(wrn, 0); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorInit() * ---------------------------------------------------------------------- */ int Language::classDirectorInit(Node *n) { (void) n; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorDestructor() * ---------------------------------------------------------------------- */ int Language::classDirectorDestructor(Node *n) { /* Always emit the virtual destructor in the declaration and in the compilation unit. Being explicit here can't make any damage, and can solve some nasty C++ compiler problems. */ File *f_directors = Swig_filebyname("director"); File *f_directors_h = Swig_filebyname("director_h"); if (Getattr(n, "noexcept")) { Printf(f_directors_h, " virtual ~%s() noexcept;\n", DirectorClassName); Printf(f_directors, "%s::~%s() noexcept {\n}\n\n", DirectorClassName, DirectorClassName); } else if (Getattr(n, "throw")) { Printf(f_directors_h, " virtual ~%s() throw();\n", DirectorClassName); Printf(f_directors, "%s::~%s() throw() {\n}\n\n", DirectorClassName, DirectorClassName); } else { Printf(f_directors_h, " virtual ~%s();\n", DirectorClassName); Printf(f_directors, "%s::~%s() {\n}\n\n", DirectorClassName, DirectorClassName); } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirectorEnd() * ---------------------------------------------------------------------- */ int Language::classDirectorEnd(Node *n) { (void) n; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDirector() * ---------------------------------------------------------------------- */ int Language::classDirector(Node *n) { Node *module = Getattr(n, "module"); String *classtype = Getattr(n, "classtype"); Hash *directormap = 0; if (module) { directormap = Getattr(module, "wrap:directormap"); if (directormap == 0) { directormap = NewHash(); Setattr(module, "wrap:directormap", directormap); } } List *vtable = NewList(); int virtual_destructor = 0; unrollVirtualMethods(n, n, vtable, virtual_destructor); // Emit all the using base::member statements for non virtual members (allprotected mode) Node *ni; String *using_protected_members_code = NewString(""); for (ni = Getattr(n, "firstChild"); ni; ni = nextSibling(ni)) { String *nodeType = nodeType(ni); if (Cmp(nodeType, "destructor") == 0 && GetFlag(ni, "final")) { String *classtype = Getattr(n, "classtype"); SWIG_WARN_NODE_BEGIN(ni); Swig_warning(WARN_LANG_DIRECTOR_FINAL, input_file, line_number, "Destructor %s is final, %s cannot be a director class.\n", Swig_name_decl(ni), classtype); SWIG_WARN_NODE_END(ni); SetFlag(n, "feature:nodirector"); Delete(vtable); Delete(using_protected_members_code); return SWIG_OK; } Node *nn = ni; bool cdeclaration = Equal(nodeType, "cdecl"); if (!cdeclaration && Equal(nodeType, "using")) { nn = Getattr(ni, "firstChild"); cdeclaration = nn && Equal(nodeType(nn), "cdecl") ? true : false; } if (cdeclaration && !GetFlag(nn, "feature:ignore")) { if (isNonVirtualProtectedAccess(nn)) { Node *overloaded = Getattr(nn, "sym:overloaded"); // emit the using base::member statement (but only once if the method is overloaded) if (!overloaded || (overloaded && (overloaded == nn))) Printf(using_protected_members_code, " using %s::%s;\n", SwigType_namestr(ClassName), Getattr(nn, "name")); } } } if (virtual_destructor || Len(vtable) > 0) { if (!virtual_destructor) { String *classtype = Getattr(n, "classtype"); Swig_warning(WARN_LANG_DIRECTOR_VDESTRUCT, input_file, line_number, "Director base class %s has no virtual destructor.\n", classtype); } Setattr(n, "vtable", vtable); if (directormap != 0) { Setattr(directormap, classtype, n); } classDirectorInit(n); classDirectorConstructors(n); classDirectorMethods(n); File *f_directors_h = Swig_filebyname("director_h"); Printv(f_directors_h, using_protected_members_code, NIL); classDirectorEnd(n); } Delete(vtable); Delete(using_protected_members_code); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classDeclaration() * ---------------------------------------------------------------------- */ int Language::classDeclaration(Node *n) { String *ochildren = Getattr(n, "feature:onlychildren"); if (ochildren) { Setattr(n, "feature:emitonlychildren", ochildren); emit_children(n); Delattr(n, "feature:emitonlychildren"); SetFlag(n, "feature:ignore"); return SWIG_NOWRAP; } // save class local variables for nested classes support int oldInClass = InClass; String *oldClassType = ClassType; String *oldClassPrefix = ClassPrefix; String *oldEnumClassPrefix = EnumClassPrefix; String *oldClassName = ClassName; String *oldDirectorClassName = DirectorClassName; String *oldNSpace = NSpace; Node *oldCurrentClass = CurrentClass; int dir = 0; String *kind = Getattr(n, "kind"); String *name = Getattr(n, "name"); String *tdname = Getattr(n, "tdname"); String *unnamed = Getattr(n, "unnamed"); String *symname = Getattr(n, "sym:name"); int strip = CPlusPlus ? 1 : unnamed && tdname; if (cplus_mode != PUBLIC) return SWIG_NOWRAP; if (!name) { Swig_warning(WARN_LANG_CLASS_UNNAMED, input_file, line_number, "Can't generate wrappers for unnamed struct/class.\n"); return SWIG_NOWRAP; } /* Check symbol name for template. If not renamed. Issue a warning */ if (!validIdentifier(symname)) { Swig_warning(WARN_LANG_IDENTIFIER, input_file, line_number, "Can't wrap class %s unless renamed to a valid identifier.\n", SwigType_namestr(symname)); return SWIG_NOWRAP; } AccessMode oldAccessMode = cplus_mode; Node *outerClass = Getattr(n, "nested:outer"); if (outerClass && oldAccessMode != PUBLIC) return SWIG_NOWRAP; ClassName = Copy(name); ClassPrefix = Copy(symname); if (Cmp(kind, "class") == 0) { cplus_mode = PRIVATE; } else { cplus_mode = PUBLIC; } for (; outerClass; outerClass = Getattr(outerClass, "nested:outer")) { Push(ClassPrefix, "_"); Push(ClassPrefix, Getattr(outerClass, "sym:name")); } EnumClassPrefix = Copy(ClassPrefix); if (strip) { ClassType = Copy(name); } else { ClassType = NewStringf("%s %s", kind, name); } Setattr(n, "classtypeobj", Copy(ClassType)); Setattr(n, "classtype", SwigType_namestr(ClassType)); InClass = 1; CurrentClass = n; NSpace = Getattr(n, "sym:nspace"); int oldAbstract = Abstract; /* Call classHandler() here */ if (!ImportMode) { if (Swig_directors_enabled()) { int ndir = GetFlag(n, "feature:director"); int nndir = GetFlag(n, "feature:nodirector"); /* 'nodirector' has precedence over 'director' */ dir = (ndir || nndir) ? (ndir && !nndir) : 0; } if (dir) { DirectorClassName = directorClassName(n); classDirector(n); } /* check for abstract after resolving directors */ Abstract = abstractClassTest(n); classHandler(n); } else { Abstract = abstractClassTest(n); Language::classHandler(n); } Abstract = oldAbstract; cplus_mode = oldAccessMode; NSpace = oldNSpace; InClass = oldInClass; CurrentClass = oldCurrentClass; Delete(ClassType); ClassType = oldClassType; Delete(EnumClassPrefix); EnumClassPrefix = oldEnumClassPrefix; Delete(ClassPrefix); ClassPrefix = oldClassPrefix; Delete(ClassName); ClassName = oldClassName; if (dir) { Delete(DirectorClassName); } DirectorClassName = oldDirectorClassName; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classHandler() * ---------------------------------------------------------------------- */ int Language::classHandler(Node *n) { save_value oldExtend(Extend); if (Getattr(n, "template")) Extend = 0; bool hasDirector = Swig_directorclass(n) ? true : false; /* Emit all of the class members */ emit_children(n); /* Look for smart pointer handling */ if (Getattr(n, "allocate:smartpointer")) { List *methods = Getattr(n, "allocate:smartpointer"); cplus_mode = PUBLIC; SmartPointer = CWRAP_SMART_POINTER; if (Getattr(n, "allocate:smartpointerconst") && Getattr(n, "allocate:smartpointermutable")) { SmartPointer |= CWRAP_SMART_POINTER_OVERLOAD; } Iterator c; for (c = First(methods); c.item; c = Next(c)) { emit_one(c.item); } SmartPointer = 0; } cplus_mode = PUBLIC; /* emit director disown method */ if (hasDirector) { classDirectorDisown(n); /* Emit additional protected virtual methods - only needed if the language module * codes logic in the C++ layer instead of the director proxy class method - primarily * to catch public use of protected methods by the scripting languages. */ if (dirprot_mode() && extraDirectorProtectedCPPMethodsRequired()) { Node *vtable = Getattr(n, "vtable"); String *symname = Getattr(n, "sym:name"); save_value old_mode(cplus_mode); cplus_mode = PROTECTED; int len = Len(vtable); for (int i = 0; i < len; i++) { Node *item = Getitem(vtable, i); Node *method = Getattr(item, "methodNode"); SwigType *type = Getattr(method, "nodeType"); if (Strcmp(type, "cdecl") != 0) continue; if (GetFlag(method, "feature:ignore")) continue; String *methodname = Getattr(method, "sym:name"); String *wrapname = NewStringf("%s_%s", symname, methodname); if (!symbolLookup(wrapname, "") && (!is_public(method))) { Node *m = Copy(method); Setattr(m, "director", "1"); Setattr(m, "parentNode", n); /* * There is a bug that needs fixing still... * This area of code is creating methods which have not been overridden in a derived class (director methods that are protected in the base) * If the method is overloaded, then Swig_overload_dispatch() incorrectly generates a call to the base wrapper, _wrap_xxx method * See director_protected_overloaded.i - Possibly sym:overname needs correcting here. Printf(stdout, "new method: %s::%s(%s)\n", Getattr(parentNode(m), "name"), Getattr(m, "name"), ParmList_str_defaultargs(Getattr(m, "parms"))); */ cDeclaration(m); Delete(m); } Delete(wrapname); } } } return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::classforwardDeclaration() * ---------------------------------------------------------------------- */ int Language::classforwardDeclaration(Node *n) { (void) n; return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::constructorDeclaration() * ---------------------------------------------------------------------- */ int Language::constructorDeclaration(Node *n) { String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); if (!symname) return SWIG_NOWRAP; if (!CurrentClass) return SWIG_NOWRAP; if (ImportMode) return SWIG_NOWRAP; if (Extend) { /* extend default constructor can be safely ignored if there is already one */ int num_required = ParmList_numrequired(Getattr(n, "parms")); if ((num_required == 0) && Getattr(CurrentClass, "has_default_constructor")) { return SWIG_NOWRAP; } if ((num_required == 1) && Getattr(CurrentClass, "has_copy_constructor")) { String *ccdecl = Getattr(CurrentClass, "copy_constructor_decl"); if (ccdecl && (Strcmp(ccdecl, Getattr(n, "decl")) == 0)) { return SWIG_NOWRAP; } } } /* clean protected overloaded constructors, in case they are not needed anymore */ Node *over = Swig_symbol_isoverloaded(n); if (over && !Getattr(CurrentClass, "sym:cleanconstructor")) { int dirclass = Swig_directorclass(CurrentClass); Node *nn = over; while (nn) { if (!is_public(nn)) { if (!dirclass || !need_nonpublic_ctor(nn)) { SetFlag(nn, "feature:ignore"); } } nn = Getattr(nn, "sym:nextSibling"); } clean_overloaded(over); Setattr(CurrentClass, "sym:cleanconstructor", "1"); } if (!is_public(n)) { /* check only for director classes */ if (!Swig_directorclass(CurrentClass) || !need_nonpublic_ctor(n)) return SWIG_NOWRAP; } // Name adjustment of constructor when a class has been renamed with %rename Swig_save("constructorDeclaration", n, "sym:name", NIL); { String *base = Swig_scopename_last(name); // Note that it is possible for the constructor to have a different name to the class name in // some target languages, where it is wrapped as a factory type function instead of a constructor. if (Equal(base, symname) && !Equal(symname, ClassPrefix)) { // Adjust name, except when the constructor's name comes from a templated constructor, // where the name passed to %template is used instead. if (!Getattr(n, "template")) Setattr(n, "sym:name", ClassPrefix); } Delete(base); } /* Only create a constructor if the class is not abstract */ if (!Abstract) { Node *over; over = Swig_symbol_isoverloaded(n); if (over) over = first_nontemplate(over); if ((over) && (!overloading)) { /* If the symbol is overloaded. We check to see if it is a copy constructor. If so, we invoke copyconstructorHandler() as a special case. */ if (Getattr(n, "copy_constructor") && (!Getattr(CurrentClass, "has_copy_constructor"))) { copyconstructorHandler(n); Setattr(CurrentClass, "has_copy_constructor", "1"); } else { if (Getattr(over, "copy_constructor")) over = Getattr(over, "sym:nextSibling"); if (over != n) { Swig_warning(WARN_LANG_OVERLOAD_CONSTRUCT, input_file, line_number, "Overloaded constructor ignored. %s\n", Swig_name_decl(n)); Swig_warning(WARN_LANG_OVERLOAD_CONSTRUCT, Getfile(over), Getline(over), "Previous declaration is %s\n", Swig_name_decl(over)); } else { constructorHandler(n); } } } else { String *expected_name = ClassName; String *scope = Swig_scopename_check(ClassName) ? Swig_scopename_prefix(ClassName) : 0; String *actual_name = scope ? NewStringf("%s::%s", scope, name) : NewString(name); Delete(scope); if (!Equal(actual_name, expected_name) && !SwigType_istemplate(expected_name) && !SwigType_istemplate(actual_name)) { // Checking templates is skipped but they ought to be checked... they are just somewhat more tricky to check correctly bool illegal_name = true; if (Extend) { // Check for typedef names used as a constructor name in %extend. This is deprecated except for anonymous // typedef structs which have had their symbol names adjusted to the typedef name in the parser. SwigType *name_resolved = SwigType_typedef_resolve_all(actual_name); SwigType *expected_name_resolved = SwigType_typedef_resolve_all(expected_name); if (!CPlusPlus) { if (Strncmp(name_resolved, "struct ", 7) == 0) Replace(name_resolved, "struct ", "", DOH_REPLACE_FIRST); else if (Strncmp(name_resolved, "union ", 6) == 0) Replace(name_resolved, "union ", "", DOH_REPLACE_FIRST); } illegal_name = !Equal(name_resolved, expected_name_resolved); if (!illegal_name) Swig_warning(WARN_LANG_EXTEND_CONSTRUCTOR, input_file, line_number, "Use of an illegal constructor name '%s' in %%extend is deprecated, the constructor name should be '%s'.\n", SwigType_str(Swig_scopename_last(actual_name), 0), SwigType_str(Swig_scopename_last(expected_name), 0)); Delete(name_resolved); Delete(expected_name_resolved); } if (illegal_name) { Swig_warning(WARN_LANG_RETURN_TYPE, input_file, line_number, "Function %s must have a return type. Ignored.\n", Swig_name_decl(n)); Swig_restore(n); return SWIG_NOWRAP; } } constructorHandler(n); } } Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * get_director_ctor_code() * ---------------------------------------------------------------------- */ static String *get_director_ctor_code(Node *n, String *director_ctor_code, String *director_prot_ctor_code, List *&abstracts) { String *director_ctor = director_ctor_code; int use_director = Swig_directorclass(n); if (use_director) { Node *pn = Swig_methodclass(n); abstracts = Getattr(pn, "abstracts"); if (director_prot_ctor_code) { int is_notabstract = GetFlag(pn, "feature:notabstract"); int is_abstract = abstracts && !is_notabstract; if (is_protected(n) || is_abstract) { director_ctor = director_prot_ctor_code; abstracts = Copy(abstracts); Delattr(pn, "abstracts"); } else { if (is_notabstract) { abstracts = Copy(abstracts); Delattr(pn, "abstracts"); } else { abstracts = 0; } } } } return director_ctor; } /* ---------------------------------------------------------------------- * Language::constructorHandler() * ---------------------------------------------------------------------- */ int Language::constructorHandler(Node *n) { Swig_require("constructorHandler", n, "?name", "*sym:name", "?type", "?parms", NIL); String *symname = Getattr(n, "sym:name"); String *mrename = Swig_name_construct(NSpace, symname); String *nodeType = Getattr(n, "nodeType"); int constructor = (!Cmp(nodeType, "constructor")); List *abstracts = 0; String *director_ctor = get_director_ctor_code(n, director_ctor_code, director_prot_ctor_code, abstracts); if (!constructor) { /* if not originally a constructor, still handle it as one */ Setattr(n, "handled_as_constructor", "1"); } int extendmember = GetFlag(n, "isextendmember") ? Extend : 0; int flags = Getattr(n, "template") ? extendmember : Extend; Swig_ConstructorToFunction(n, NSpace, ClassType, none_comparison, director_ctor, CPlusPlus, flags, DirectorClassName); Setattr(n, "sym:name", mrename); functionWrapper(n); Delete(mrename); Swig_restore(n); if (abstracts) Setattr(Swig_methodclass(n), "abstracts", abstracts); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::copyconstructorHandler() * ---------------------------------------------------------------------- */ int Language::copyconstructorHandler(Node *n) { Swig_require("copyconstructorHandler", n, "?name", "*sym:name", "?type", "?parms", NIL); String *symname = Getattr(n, "sym:name"); String *mrename = Swig_name_copyconstructor(NSpace, symname); List *abstracts = 0; String *director_ctor = get_director_ctor_code(n, director_ctor_code, director_prot_ctor_code, abstracts); Swig_ConstructorToFunction(n, NSpace, ClassType, none_comparison, director_ctor, CPlusPlus, Getattr(n, "template") ? 0 : Extend, DirectorClassName); Setattr(n, "sym:name", mrename); functionWrapper(n); Delete(mrename); Swig_restore(n); if (abstracts) Setattr(Swig_methodclass(n), "abstracts", abstracts); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::destructorDeclaration() * ---------------------------------------------------------------------- */ int Language::destructorDeclaration(Node *n) { if (!CurrentClass) return SWIG_NOWRAP; if (cplus_mode != PUBLIC && !Getattr(CurrentClass, "feature:unref")) return SWIG_NOWRAP; if (ImportMode) return SWIG_NOWRAP; Swig_save("destructorDeclaration", n, "name", "sym:name", NIL); char *c = GetChar(n, "sym:name"); if (c && (*c == '~')) { Setattr(n, "sym:name", c + 1); } String *name = Getattr(n, "name"); String *symname = Getattr(n, "sym:name"); if ((Strcmp(name, symname) == 0) || (Strcmp(symname, ClassPrefix) != 0)) { Setattr(n, "sym:name", ClassPrefix); } String *nprefix = 0; String *nlast = 0; String *tprefix; Swig_scopename_split(ClassName, &nprefix, &nlast); tprefix = SwigType_templateprefix(nlast); String *expected_name = NewStringf("~%s", tprefix); String *actual_name = Copy(name); if (!Equal(actual_name, expected_name) && !(Getattr(n, "template"))) { bool illegal_name = true; if (Extend) { // Check for typedef names used as a destructor name in %extend. This is deprecated except for anonymous // typedef structs which have had their symbol names adjusted to the typedef name in the parser. Replace(actual_name, "~", "", DOH_REPLACE_FIRST); Replace(expected_name, "~", "", DOH_REPLACE_FIRST); if (Len(nprefix) > 0) { String *old_actual_name = actual_name; String *old_expected_name = expected_name; actual_name = NewStringf("%s::%s", nprefix, actual_name); expected_name = NewStringf("%s::%s", nprefix, expected_name); Delete(old_expected_name); Delete(old_actual_name); } SwigType *name_resolved = SwigType_typedef_resolve_all(actual_name); SwigType *expected_name_resolved = SwigType_typedef_resolve_all(expected_name); if (!CPlusPlus) { if (Strncmp(name_resolved, "struct ", 7) == 0) Replace(name_resolved, "struct ", "", DOH_REPLACE_FIRST); else if (Strncmp(name_resolved, "union ", 6) == 0) Replace(name_resolved, "union ", "", DOH_REPLACE_FIRST); } illegal_name = !Equal(name_resolved, expected_name_resolved); if (!illegal_name) Swig_warning(WARN_LANG_EXTEND_DESTRUCTOR, input_file, line_number, "Use of an illegal destructor name '%s' in %%extend is deprecated, the destructor name should be '%s'.\n", SwigType_str(Swig_scopename_last(actual_name), 0), SwigType_str(Swig_scopename_last(expected_name), 0)); Delete(name_resolved); Delete(expected_name_resolved); } if (illegal_name) { Swig_warning(WARN_LANG_ILLEGAL_DESTRUCTOR, input_file, line_number, "Illegal destructor name %s. Ignored.\n", Swig_name_decl(n)); Swig_restore(n); Delete(tprefix); Delete(nlast); Delete(nprefix); Delete(expected_name); Delete(actual_name); return SWIG_NOWRAP; } } destructorHandler(n); Setattr(CurrentClass, "has_destructor", "1"); Swig_restore(n); Delete(tprefix); Delete(nlast); Delete(nprefix); Delete(expected_name); Delete(actual_name); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::destructorHandler() * ---------------------------------------------------------------------- */ int Language::destructorHandler(Node *n) { Swig_require("destructorHandler", n, "?name", "*sym:name", NIL); Swig_save("destructorHandler", n, "type", "parms", NIL); String *symname = Getattr(n, "sym:name"); String *mrename; char *csymname = Char(symname); if (*csymname == '~') csymname += 1; mrename = Swig_name_destroy(NSpace, csymname); Swig_DestructorToFunction(n, NSpace, ClassType, CPlusPlus, Extend); Setattr(n, "sym:name", mrename); functionWrapper(n); Delete(mrename); Swig_restore(n); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::accessDeclaration() * ---------------------------------------------------------------------- */ int Language::accessDeclaration(Node *n) { cplus_mode = accessModeFromString(Getattr(n, "kind")); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Language::namespaceDeclaration() * ----------------------------------------------------------------------------- */ int Language::namespaceDeclaration(Node *n) { if (Getattr(n, "alias")) return SWIG_OK; if (Getattr(n, "unnamed")) return SWIG_OK; emit_children(n); return SWIG_OK; } int Language::validIdentifier(String *s) { char *c = Char(s); while (*c) { if (!(isalnum(*c) || (*c == '_'))) return 0; c++; } return 1; } /* ----------------------------------------------------------------------------- * Language::usingDeclaration() * ----------------------------------------------------------------------------- */ int Language::usingDeclaration(Node *n) { if ((cplus_mode == PUBLIC) || (!is_public(n) && dirprot_mode())) { Node *np = Copy(n); Node *c; for (c = firstChild(np); c; c = nextSibling(c)) { /* it seems for some cases this is needed, like A* A::boo() */ if (CurrentClass) Setattr(c, "parentNode", CurrentClass); emit_one(c); } Delete(np); } return SWIG_OK; } /* Stubs. Language modules need to implement these */ /* ---------------------------------------------------------------------- * Language::constantWrapper() * ---------------------------------------------------------------------- */ int Language::constantWrapper(Node *n) { String *name = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *str = SwigType_str(type, name); Printf(stdout, "constantWrapper : %s = %s\n", str, value); Delete(str); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::variableWrapper() * ---------------------------------------------------------------------- */ int Language::variableWrapper(Node *n) { Swig_require("variableWrapper", n, "*name", "*sym:name", "*type", "?parms", "?varset", "?varget", NIL); String *symname = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *name = Getattr(n, "name"); Delattr(n,"varset"); Delattr(n,"varget"); String *newsymname = 0; if (!CurrentClass && EnumClassPrefix) { newsymname = Swig_name_member(0, EnumClassPrefix, symname); symname = newsymname; } /* If no way to set variables. We simply create functions */ int assignable = !is_immutable(n); int flags = use_naturalvar_mode(n); if (!GetFlag(n, "wrappedasconstant")) flags = flags | Extend; if (assignable) { int make_set_wrapper = 1; String *tm = Swig_typemap_lookup("globalin", n, name, 0); Swig_VarsetToFunction(n, flags); String *sname = Swig_name_set(NSpace, symname); Setattr(n, "sym:name", sname); Delete(sname); if (!tm) { if (SwigType_isarray(type)) { Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(type, 0)); make_set_wrapper = 0; } } else { String *pname0 = Swig_cparm_name(0, 0); Replace(tm, "$input", pname0, DOH_REPLACE_ANY); Setattr(n, "wrap:action", tm); Delete(tm); Delete(pname0); } if (make_set_wrapper) { Setattr(n, "varset", "1"); functionWrapper(n); } else { SetFlag(n, "feature:immutable"); } /* Restore parameters */ Setattr(n, "sym:name", symname); Setattr(n, "type", type); Setattr(n, "name", name); Delattr(n, "varset"); /* Delete all attached typemaps and typemap attributes */ Iterator ki; for (ki = First(n); ki.key; ki = Next(ki)) { if (Strncmp(ki.key, "tmap:", 5) == 0) Delattr(n, ki.key); } } Swig_VargetToFunction(n, flags); String *gname = Swig_name_get(NSpace, symname); Setattr(n, "sym:name", gname); Delete(gname); Setattr(n, "varget", "1"); functionWrapper(n); Delattr(n, "varget"); Swig_restore(n); Delete(newsymname); return SWIG_OK; } /* ---------------------------------------------------------------------- * Language::functionWrapper() * ---------------------------------------------------------------------- */ int Language::functionWrapper(Node *n) { String *name = Getattr(n, "sym:name"); SwigType *type = Getattr(n, "type"); ParmList *parms = Getattr(n, "parms"); Printf(stdout, "functionWrapper : %s\n", SwigType_str(type, NewStringf("%s(%s)", name, ParmList_str_defaultargs(parms)))); Printf(stdout, " action : %s\n", Getattr(n, "wrap:action")); return SWIG_OK; } /* ----------------------------------------------------------------------------- * Language::nativeWrapper() * ----------------------------------------------------------------------------- */ int Language::nativeWrapper(Node *n) { (void) n; return SWIG_OK; } void Language::main(int argc, char *argv[]) { (void) argc; (void) argv; } /* ----------------------------------------------------------------------------- * Language::addSymbol() * * Adds a symbol entry into the target language symbol tables. * Returns 1 if the symbol is added successfully. * Prints an error message and returns 0 if a conflict occurs. * The scope is optional for target languages and if supplied must be a fully * qualified scope and the symbol s must not contain any scope qualifiers. * ----------------------------------------------------------------------------- */ int Language::addSymbol(const String *s, const Node *n, const_String_or_char_ptr scope) { //Printf( stdout, "addSymbol: %s %s %s:%d\n", s, scope, Getfile(n), Getline(n) ); Hash *symbols = symbolScopeLookup(scope); if (!symbols) { symbols = symbolAddScope(scope); } else { Node *c = Getattr(symbols, s); if (c && (c != n)) { if (scope && Len(scope) > 0) Swig_error(input_file, line_number, "'%s' is multiply defined in the generated target language module in scope '%s'.\n", s, scope); else Swig_error(input_file, line_number, "'%s' is multiply defined in the generated target language module.\n", s); Swig_error(Getfile(c), Getline(c), "Previous declaration of '%s'\n", s); return 0; } } Setattr(symbols, s, n); return 1; } /* ----------------------------------------------------------------------------- * Language::addInterfaceSymbol() * * Adds a symbol entry into the target language symbol tables - for the interface * feature only. * Returns 1 if the symbol is added successfully. * The scope is as per addSymbol. * ----------------------------------------------------------------------------- */ int Language::addInterfaceSymbol(const String *interface_name, Node *n, const_String_or_char_ptr scope) { if (interface_name) { Node *existing_symbol = symbolLookup(interface_name, scope); if (existing_symbol) { String *proxy_class_name = Getattr(n, "sym:name"); Swig_error(input_file, line_number, "The interface feature name '%s' for proxy class '%s' is already defined in the generated target language module in scope '%s'.\n", interface_name, proxy_class_name, scope); Swig_error(Getfile(existing_symbol), Getline(existing_symbol), "Previous declaration of '%s'\n", interface_name); return 0; } if (!addSymbol(interface_name, n, scope)) return 0; } return 1; } /* ----------------------------------------------------------------------------- * Language::symbolAddScope() * * Creates a scope (symbols Hash) for given name. This method is auxiliary, * you don't have to call it - addSymbol will lazily create scopes automatically. * If scope with given name already exists, then do nothing. * Returns newly created (or already existing) scope. * ----------------------------------------------------------------------------- */ Hash *Language::symbolAddScope(const_String_or_char_ptr scope/*, Node *n*/) { Hash *symbols = symbolScopeLookup(scope); if (!symbols) { // The order in which the following code is executed is important. In the Language // constructor symbolAddScope("") is called to create a top level scope. // Thus we must first add a symbols hash to symtab and only then add pseudo // symbols to the top-level scope. // New scope which has not been added by the target language - lazily created. symbols = NewHash(); Setattr(symtabs, scope, symbols); // Add the new scope as a symbol in the top level scope. // Alternatively the target language must add it in before attempting to add symbols into the scope. const_String_or_char_ptr top_scope = ""; Hash *topscope_symbols = Getattr(symtabs, top_scope); // TODO: // Stop using pseudo scopes, the symbol Node containing the new scope should be passed into this function. // This will require explicit calls to symbolScopeLookup() in each language and removing the call from addSymbol(). // addSymbol() should then instead assert that the scope exists. // All this just to fix up the file/line numbering of the scopes for error reporting. //Node *symbol = n; Node *symbol = Getattr(topscope_symbols, scope); Hash *pseudo_symbol = 0; if (symbol) { pseudo_symbol = symbol; } else { pseudo_symbol = NewHash(); Setattr(pseudo_symbol, "sym:scope", "1"); } Setattr(topscope_symbols, scope, pseudo_symbol); } return symbols; } /* ----------------------------------------------------------------------------- * Language::symbolScopeLookup() * * Lookup and returns a symtable (hash) representing given scope. Hash contains * all symbols in this scope. * ----------------------------------------------------------------------------- */ Hash *Language::symbolScopeLookup(const_String_or_char_ptr scope) { Hash *symbols = Getattr(symtabs, scope ? scope : ""); return symbols; } /* ----------------------------------------------------------------------------- * Language::symbolScopePseudoSymbolLookup() * * For every scope there is a special pseudo-symbol in the top scope (""). It * exists solely to detect name clashes. This pseudo symbol may contain a few properties, * but more could be added. This is also true for the top level scope (""). * It contains a pseudo symbol with name "" (empty). Pseudo symbol contains the * following properties: * sym:scope = "1" - a flag that this is a scope pseudo symbol * * Pseudo symbols are a Hash*, not a Node*. * There is no difference from symbolLookup() method except for signature * and return type. * ----------------------------------------------------------------------------- */ Hash *Language::symbolScopePseudoSymbolLookup(const_String_or_char_ptr scope) { /* Getting top scope */ const_String_or_char_ptr top_scope = ""; Hash *symbols = Getattr(symtabs, top_scope); return Getattr(symbols, scope); } /* ----------------------------------------------------------------------------- * Language::dumpSymbols() * ----------------------------------------------------------------------------- */ void Language::dumpSymbols() { Printf(stdout, "LANGUAGE SYMBOLS start =======================================\n"); Node *table = symtabs; Iterator ki = First(table); while (ki.key) { String *k = ki.key; Printf(stdout, "===================================================\n"); Printf(stdout, "%s -\n", k); { Symtab *symtab = Getattr(table, k); Iterator it = First(symtab); while (it.key) { String *symname = it.key; Printf(stdout, " %s\n", symname); //Printf(stdout, " %s (%s:%d)\n", symname, Getfile(it.item), Getline(it.item)); it = Next(it); } } ki = Next(ki); } Printf(stdout, "LANGUAGE SYMBOLS finish =======================================\n"); } /* ----------------------------------------------------------------------------- * Language::symbolLookup() * ----------------------------------------------------------------------------- */ Node *Language::symbolLookup(const String *s, const_String_or_char_ptr scope) { Hash *symbols = Getattr(symtabs, scope ? scope : ""); if (!symbols) { return NULL; } return Getattr(symbols, s); } /* ----------------------------------------------------------------------------- * Language::classLookup() * * Tries to locate a class from a type definition * ----------------------------------------------------------------------------- */ Node *Language::classLookup(const SwigType *s) { static Hash *classtypes = 0; Node *n = 0; /* Look in hash of cached values */ n = classtypes ? Getattr(classtypes, s) : 0; if (!n) { Symtab *stab = 0; SwigType *ty1 = SwigType_typedef_resolve_all(s); SwigType *ty2 = SwigType_strip_qualifiers(ty1); String *base = SwigType_base(ty2); Replaceall(base, "class ", ""); Replaceall(base, "struct ", ""); Replaceall(base, "union ", ""); if (strncmp(Char(base), "::", 2) == 0) { String *oldbase = base; base = NewString(Char(base) + 2); Delete(oldbase); } String *prefix = SwigType_prefix(ty2); /* Do a symbol table search on the base type */ while (!n) { Hash *nstab; n = Swig_symbol_clookup(base, stab); if (!n) break; if (Strcmp(nodeType(n), "class") == 0) break; Node *sibling = n; while (sibling) { sibling = Getattr(sibling, "csym:nextSibling"); if (sibling && Strcmp(nodeType(sibling), "class") == 0) break; } if (sibling) break; n = parentNode(n); if (!n) break; nstab = Getattr(n, "sym:symtab"); n = 0; if ((!nstab) || (nstab == stab)) { break; } stab = nstab; } if (n) { /* Found a match. Look at the prefix. We only allow the cases where we want a proxy class for the particular type */ bool acceptable_prefix = (Len(prefix) == 0) || // simple type (pass by value) (Strcmp(prefix, "p.") == 0) || // pointer (Strcmp(prefix, "r.") == 0) || // reference (Strcmp(prefix, "z.") == 0) || // rvalue reference SwigType_prefix_is_simple_1D_array(prefix); // Simple 1D array (not arrays of pointers/references) // Also accept pointer by const reference, not non-const pointer reference if (!acceptable_prefix && (Strcmp(prefix, "r.p.") == 0)) { Delete(prefix); prefix = SwigType_prefix(ty1); acceptable_prefix = (Strncmp(prefix, "r.q(const", 9) == 0); } if (acceptable_prefix) { SwigType *cs = Copy(s); if (!classtypes) classtypes = NewHash(); Setattr(classtypes, cs, n); Delete(cs); } else { n = 0; } } Delete(prefix); Delete(base); Delete(ty2); Delete(ty1); } if (n && (GetFlag(n, "feature:ignore") || Getattr(n, "feature:onlychildren"))) { n = 0; } return n; } /* ----------------------------------------------------------------------------- * Language::enumLookup() * * Finds and returns the Node containing the enum declaration for the (enum) * type passed in. * ----------------------------------------------------------------------------- */ Node *Language::enumLookup(SwigType *s) { static Hash *enumtypes = 0; Node *n = 0; /* Look in hash of cached values */ n = enumtypes ? Getattr(enumtypes, s) : 0; if (!n) { Symtab *stab = 0; SwigType *lt = SwigType_ltype(s); SwigType *ty1 = SwigType_typedef_resolve_all(lt); SwigType *ty2 = SwigType_strip_qualifiers(ty1); String *base = SwigType_base(ty2); Replaceall(base, "enum ", ""); String *prefix = SwigType_prefix(ty2); if (strncmp(Char(base), "::", 2) == 0) { String *oldbase = base; base = NewString(Char(base) + 2); Delete(oldbase); } /* Look for type in symbol table */ while (!n) { Hash *nstab; n = Swig_symbol_clookup(base, stab); if (!n) break; if (Equal(nodeType(n), "enum")) break; if (Equal(nodeType(n), "enumforward") && GetFlag(n, "enumMissing")) break; n = parentNode(n); if (!n) break; nstab = Getattr(n, "sym:symtab"); n = 0; if ((!nstab) || (nstab == stab)) { break; } stab = nstab; } if (n) { /* Found a match. Look at the prefix. We only allow simple types. */ if (Len(prefix) == 0) { /* Simple type */ if (!enumtypes) enumtypes = NewHash(); Setattr(enumtypes, Copy(s), n); } else { n = 0; } } Delete(prefix); Delete(base); Delete(ty2); Delete(ty1); Delete(lt); } if (n && (GetFlag(n, "feature:ignore"))) { n = 0; } return n; } /* ----------------------------------------------------------------------------- * Language::allow_overloading() * ----------------------------------------------------------------------------- */ void Language::allow_overloading(int val) { overloading = val; } /* ----------------------------------------------------------------------------- * Language::allow_multiple_input() * ----------------------------------------------------------------------------- */ void Language::allow_multiple_input(int val) { multiinput = val; } /* ----------------------------------------------------------------------------- * Language::enable_cplus_runtime_mode() * ----------------------------------------------------------------------------- */ void Language::enable_cplus_runtime_mode() { cplus_runtime = 1; } /* ----------------------------------------------------------------------------- * Language::cplus_runtime_mode() * ----------------------------------------------------------------------------- */ int Language::cplus_runtime_mode() { return cplus_runtime; } /* ----------------------------------------------------------------------------- * Language::directorLanguage() * ----------------------------------------------------------------------------- */ void Language::directorLanguage(int val) { director_language = val; } /* ----------------------------------------------------------------------------- * Language::allow_directors() * ----------------------------------------------------------------------------- */ void Language::allow_directors(int val) { directors_allowed = val; } /* ----------------------------------------------------------------------------- * Language::allow_dirprot() * ----------------------------------------------------------------------------- */ void Language::allow_dirprot(int val) { director_protected_mode = val; } /* ----------------------------------------------------------------------------- * Language::allow_allprotected() * ----------------------------------------------------------------------------- */ void Language::allow_allprotected(int val) { all_protected_mode = val; } /* ----------------------------------------------------------------------------- * Language::dirprot_mode() * ----------------------------------------------------------------------------- */ int Language::dirprot_mode() const { return Swig_directors_enabled() ? director_protected_mode : 0; } /* ----------------------------------------------------------------------------- * Language::need_nonpublic_ctor() * ----------------------------------------------------------------------------- */ int Language::need_nonpublic_ctor(Node *n) { /* detects when a protected constructor is needed, which is always the case if 'dirprot' mode is used. However, if that is not the case, we will try to strictly emit what is minimal to don't break the generated, while preserving compatibility with java, which always try to emit the default constructor. rules: - when dirprot mode is used, the protected constructors are always needed. - the protected default constructor is always needed. - if dirprot mode is not used, the protected constructors will be needed only if: - there is no any public constructor in the class, and - there is no protected default constructor In that case, all the declared protected constructors are needed since we don't know which one to pick up. Note: given all the complications here, I am always in favor to always enable 'dirprot', since is the C++ idea of protected members, and use %ignore for the method you don't want to add in the director class. */ if (Swig_directors_enabled()) { if (is_protected(n)) { if (dirprot_mode()) { /* when using dirprot mode, the protected constructors are always needed */ return 1; } else { int is_default_ctor = !ParmList_numrequired(Getattr(n, "parms")); if (is_default_ctor) { /* the default protected constructor is always needed, for java compatibility */ return 1; } else { /* check if there is a public constructor */ Node *parent = Swig_methodclass(n); int public_ctor = Getattr(parent, "allocate:default_constructor") || Getattr(parent, "allocate:public_constructor"); if (!public_ctor) { /* if not, the protected constructor will be needed only if there is no protected default constructor declared */ int no_prot_default_ctor = !Getattr(parent, "allocate:default_base_constructor"); return no_prot_default_ctor; } } } } } return 0; } /* ----------------------------------------------------------------------------- * Language::need_nonpublic_member() * ----------------------------------------------------------------------------- */ int Language::need_nonpublic_member(Node *n) { if (Swig_directors_enabled() && DirectorClassName) { if (is_protected(n)) { if (dirprot_mode()) { /* when using dirprot mode, the protected members are always needed. */ return 1; } else { /* if the method is pure virtual, we need it. */ int pure_virtual = (Cmp(Getattr(n, "value"), "0") == 0); return pure_virtual; } } } return 0; } /* ----------------------------------------------------------------------------- * Language::is_smart_pointer() * ----------------------------------------------------------------------------- */ int Language::is_smart_pointer() const { return SmartPointer; } /* ----------------------------------------------------------------------------- * Language::makeParameterName() * * Inputs: * n - Node * p - parameter node * arg_num - parameter argument number * setter - set this flag when wrapping variables * Return: * arg - a unique parameter name * ----------------------------------------------------------------------------- */ String *Language::makeParameterName(Node *n, Parm *p, int arg_num, bool setter) const { String *arg = 0; String *pn = Getattr(p, "name"); // Check if parameter name is a duplicate. int count = 0; Parm *first_duplicate_parm = 0; ParmList *plist = Getattr(n, "parms"); while (plist) { if ((Cmp(pn, Getattr(plist, "name")) == 0)) { if (!first_duplicate_parm) first_duplicate_parm = plist; count++; } plist = nextSibling(plist); } // If the parameter has no name at all or has a non-unique name, replace it with "argN". // On the assumption that p is pointer/element in plist, only replace the 2nd and subsequent duplicates if (!pn || (count > 1 && p != first_duplicate_parm)) { arg = NewStringf("arg%d", arg_num); } else { // Otherwise, try to use the original C name, but modify it if necessary to avoid conflicting with the language keywords. arg = Swig_name_make(p, 0, pn, 0, 0); } if (setter && Cmp(arg, "self") != 0) { // Some languages (C#) insist on calling the input variable "value" while // others (D, Java) could, in principle, use something different but this // would require more work, and so we just use "value" for them too. // For setters the parameter name sometimes includes C++ scope resolution which needs removing. Delete(arg); arg = NewString("value"); } return arg; } /* ----------------------------------------------------------------------------- * Language::() * ----------------------------------------------------------------------------- */ bool Language::isNonVirtualProtectedAccess(Node *n) const { // Ideally is_non_virtual_protected_access() would contain all this logic, see // comments therein about vtable. return DirectorClassName && is_non_virtual_protected_access(n); } /* ----------------------------------------------------------------------------- * Language::extraDirectorProtectedCPPMethodsRequired() * ----------------------------------------------------------------------------- */ bool Language::extraDirectorProtectedCPPMethodsRequired() const { return true; } /* ----------------------------------------------------------------------------- * Language::nestedClassesSupport() * ----------------------------------------------------------------------------- */ Language::NestedClassSupport Language::nestedClassesSupport() const { return NCS_Unknown; } /* ----------------------------------------------------------------------------- * Language::kwargsSupport() * ----------------------------------------------------------------------------- */ bool Language::kwargsSupport() const { return false; } /* ----------------------------------------------------------------------------- * Language::is_wrapping_class() * ----------------------------------------------------------------------------- */ int Language::is_wrapping_class() const { return InClass; } /* ----------------------------------------------------------------------------- * Language::getCurrentClass() * ----------------------------------------------------------------------------- */ Node *Language::getCurrentClass() const { return CurrentClass; } /* ----------------------------------------------------------------------------- * Language::getNSpace() * ----------------------------------------------------------------------------- */ String *Language::getNSpace() const { return NSpace; } /* ----------------------------------------------------------------------------- * Language::getClassName() * ----------------------------------------------------------------------------- */ String *Language::getClassName() const { return ClassName; } /* ----------------------------------------------------------------------------- * Language::getClassPrefix() * ----------------------------------------------------------------------------- */ String *Language::getClassPrefix() const { return ClassPrefix; } /* ----------------------------------------------------------------------------- * Language::getEnumClassPrefix() * ----------------------------------------------------------------------------- */ String *Language::getEnumClassPrefix() const { return EnumClassPrefix; } /* ----------------------------------------------------------------------------- * Language::getClassType() * ----------------------------------------------------------------------------- */ String *Language::getClassType() const { return ClassType; } /* ----------------------------------------------------------------------------- * Dispatcher::abstractClassTest() * ----------------------------------------------------------------------------- */ //#define SWIG_DEBUG int Dispatcher::abstractClassTest(Node *n) { /* check for non public operator new */ if (GetFlag(n, "feature:notabstract")) return 0; if (Getattr(n, "allocate:nonew")) return 1; // A class cannot be instantiated if one of its bases has a private destructor // Note that if the above does not hold the class can be instantiated if its own destructor is private List *bases = Getattr(n, "bases"); if (bases) { for (int i = 0; i < Len(bases); i++) { Node *b = Getitem(bases, i); if (GetFlag(b, "allocate:private_destructor")) return 1; } } /* now check for the rest */ List *abstracts = Getattr(n, "abstracts"); if (!abstracts) return 0; int labs = Len(abstracts); #ifdef SWIG_DEBUG List *allbases = Getattr(n, "allbases"); Printf(stderr, "testing %s %d %d\n", Getattr(n, "name"), labs, Len(allbases)); #endif if (!labs) return 0; /*strange, but need to be fixed */ if (abstracts && !Swig_directors_enabled()) return 1; if (!GetFlag(n, "feature:director")) return 1; Node *dirabstract = 0; Node *vtable = Getattr(n, "vtable"); if (vtable) { #ifdef SWIG_DEBUG Printf(stderr, "vtable %s %d %d\n", Getattr(n, "name"), Len(vtable), labs); #endif for (int i = 0; i < labs; i++) { Node *ni = Getitem(abstracts, i); String *method_id = vtable_method_id(ni); if (!method_id) continue; bool exists_item = false; int len = Len(vtable); for (int i = 0; i < len; i++) { Node *item = Getitem(vtable, i); String *check_item = Getattr(item, "vmid"); if (Strcmp(method_id, check_item) == 0) { exists_item = true; break; } } #ifdef SWIG_DEBUG Printf(stderr, "method %s %d\n", method_id, exists_item ? 1 : 0); #endif Delete(method_id); if (!exists_item) { dirabstract = ni; break; } } if (dirabstract) { if (is_public(dirabstract)) { Swig_warning(WARN_LANG_DIRECTOR_ABSTRACT, Getfile(n), Getline(n), "Director class '%s' is abstract, abstract method '%s' is not accessible, maybe due to multiple inheritance or 'nodirector' feature\n", SwigType_namestr(Getattr(n, "name")), Getattr(dirabstract, "name")); } else { Swig_warning(WARN_LANG_DIRECTOR_ABSTRACT, Getfile(n), Getline(n), "Director class '%s' is abstract, abstract method '%s' is private\n", SwigType_namestr(Getattr(n, "name")), Getattr(dirabstract, "name")); } return 1; } } else { return 1; } return 0; } void Language::setSubclassInstanceCheck(String *nc) { none_comparison = nc; } void Language::setOverloadResolutionTemplates(String *argc, String *argv) { Delete(argc_template_string); argc_template_string = Copy(argc); Delete(argv_template_string); argv_template_string = Copy(argv); } int Language::is_immutable(Node *n) { return GetFlag(n, "feature:immutable"); } String *Language::runtimeCode() { return NewString(""); } String *Language::defaultExternalRuntimeFilename() { return 0; } /* ----------------------------------------------------------------------------- * Language::replaceSpecialVariables() * * Language modules should implement this if special variables are to be handled * correctly in the $typemap(...) special variable macro. * method - typemap method name * tm - string containing typemap contents * parm - a parameter describing the typemap type to be handled * ----------------------------------------------------------------------------- */ void Language::replaceSpecialVariables(String *method, String *tm, Parm *parm) { (void)method; (void)tm; (void)parm; } Language *Language::instance() { return this_; } Hash *Language::getClassHash() const { return classhash; } swig-4.4.0/Source/Modules/main.cxx0000664000175000017500000013121615075443613016704 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * main.cxx * * Main entry point to the SWIG core. * ----------------------------------------------------------------------------- */ #include "swigconfig.h" #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include #endif #include "swigmod.h" #include "swigwarn.h" #include "cparse.h" #include #include #include // for INT_MAX // Global variables static Language *lang = 0; // Language method int CPlusPlus = 0; int Extend = 0; // Extend flag int ForceExtern = 0; // Force extern mode int Verbose = 0; int AddExtern = 0; int NoExcept = 0; extern "C" { int UseWrapperSuffix = 0; // If 1, append suffix to non-overloaded functions too. } /* Suppress warning messages for private inheritance, etc by default. These are enabled by command line option -Wextra. WARN_PARSE_PRIVATE_INHERIT 309 WARN_PARSE_BUILTIN_NAME 321 WARN_PARSE_REDUNDANT 322 WARN_TYPE_ABSTRACT 403 WARN_TYPE_RVALUE_REF_QUALIFIER_IGNORED 405 WARN_LANG_OVERLOAD_CONST 512 */ #define EXTRA_WARNINGS "309,403,405,512,321,322" extern "C" { extern String *ModuleName; extern int ignore_nested_classes; extern int kwargs_supported; } /* usage string split into multiple parts otherwise string is too big for some compilers */ /* naming conventions for commandline options - no underscores, no capital letters, join words together * except when using a common prefix, then use '-' to separate, eg the debug-xxx options */ static const char *usage1 = "\ \nGeneral Options\n\ -addextern - Add extra extern declarations\n\ -c++ - Enable C++ processing\n\ -co - Check out of the SWIG library\n\ -copyctor - Automatically generate copy constructors wherever possible\n\ -cpperraswarn - Treat the preprocessor #error statement as #warning (default)\n\ -cppext - Change file extension of generated C++ files to \n\ (default is cxx)\n\ -copyright - Display copyright notices\n\ -debug-classes - Display information about the classes found in the interface\n\ -debug-module - Display module parse tree at stages 1-4, is a csv list of stages\n\ -debug-symtabs - Display symbol tables information\n\ -debug-symbols - Display target language symbols in the symbol tables\n\ -debug-csymbols - Display C symbols in the symbol tables\n\ -debug-lsymbols - Display target language layer symbols\n\ -debug-quiet - Display less parse tree node debug info when using other -debug options\n\ -debug-tags - Display information about the tags found in the interface\n\ -debug-template - Display information for debugging templates\n\ -debug-top - Display entire parse tree at stages 1-4, is a csv list of stages\n\ -debug-typedef - Display information about the types and typedefs in the interface\n\ -debug-typemap - Display typemap debugging information\n\ -debug-tmsearch - Display typemap search debugging information\n\ -debug-tmused - Display typemaps used debugging information\n\ -directors - Turn on director mode for all the classes, mainly for testing\n\ -dirprot - Turn on wrapping of protected members for director classes (default)\n\ -D[=] - Define symbol (for conditional compilation)\n\ "; static const char *usage2 = "\ -E - Preprocess only, does not generate wrapper code\n\ -external-runtime [file] - Export the SWIG runtime stack\n\ -fakeversion - Make SWIG fake the program version number to \n\ -fcompact - Compile in compact mode\n\ -features - Set global features, where is a comma separated list of\n\ features, eg -features directors,autodoc=1\n\ If no explicit value is given to the feature, a default of 1 is used\n\ -fastdispatch - Enable fast dispatch mode to produce faster overload dispatcher code\n\ -Fmicrosoft - Display error/warning messages in Microsoft format\n\ -Fstandard - Display error/warning messages in commonly used format\n\ -fvirtual - Compile in virtual elimination mode\n\ -help - Display help\n\ -I- - Don't search the current directory\n\ -I

- Look for SWIG files in directory \n\ -ignoremissing - Ignore missing include files\n\ -importall - Follow all #include statements as imports\n\ -includeall - Follow all #include statements\n\ -l - Include SWIG library file \n\ "; static const char *usage3 = "\ -macroerrors - Report errors inside macros\n\ -M - List all dependencies\n\ -MD - Is equivalent to `-M -MF ', except `-E' is not implied\n\ -MF - Generate dependencies into and continue generating wrappers\n\ -MM - List dependencies, but omit files in SWIG library\n\ -MMD - Like `-MD', but omit files in SWIG library\n\ -module - Set module name to \n\ -MP - Generate phony targets for all dependencies\n\ -MT - Set the target of the rule emitted by dependency generation\n\ -nocontract - Turn off contract checking\n\ -nocpperraswarn - Do not treat the preprocessor #error statement as #warning\n\ -nodefaultctor - Do not generate implicit default constructors\n\ -nodefaultdtor - Do not generate implicit default destructors\n\ -nodirprot - Do not wrap director protected members\n\ -noexcept - Do not wrap exception specifiers\n\ -nofastdispatch - Disable fast dispatch mode (default)\n\ -nopreprocess - Skip the preprocessor step\n\ -notemplatereduce - Disable reduction of the typedefs in templates\n\ "; static const char *usage4 = "\ -O - Enable the optimization options:\n\ -fastdispatch -fvirtual\n\ -o - Set name of C/C++ output file to \n\ -oh - Set name of C++ output header file for directors to \n\ -outcurrentdir - Set default output dir to current dir instead of input file's path\n\ -outdir - Set language specific files output directory to \n\ -pcreversion - Display PCRE2 version information\n\ -small - Compile in virtual elimination and compact mode\n\ -std= - Set the C or C++ language for inputs\n\ -swiglib - Report location of SWIG library and exit\n\ -templatereduce - Reduce all the typedefs in templates\n\ -U - Undefine symbol \n\ -v - Run in verbose mode\n\ -version - Display SWIG version number\n\ -Wall - Remove all warning suppression, also implies -Wextra\n\ -Wallkw - Enable keyword warnings for all the supported languages\n\ -Werror - Treat warnings as errors\n\ -Wextra - Adds the following additional warnings: " EXTRA_WARNINGS "\n\ -w - Suppress/add warning messages, eg -w401,+321 - see Warnings.html\n\ -xmlout - Write XML version of the parse tree to after normal processing\n\ \n\ Options can also be defined using the SWIG_FEATURES environment variable, for example:\n\ \n\ $ SWIG_FEATURES=\"-Wall\"\n\ $ export SWIG_FEATURES\n\ $ swig -python interface.i\n\ \n\ is equivalent to:\n\ \n\ $ swig -Wall -python interface.i\n\ \n\ Arguments may also be passed in a file, separated by whitespace. For example:\n\ \n\ $ echo \"-Wall -python interface.i\" > args.txt\n\ $ swig @args.txt\n\ \n"; // Local variables static String *LangSubDir = 0; // Target language library subdirectory static String *SwigLib = 0; // Library directory static String *SwigLibWinUnix = 0; // Extra library directory on Windows static int freeze = 0; static String *lang_config = 0; static const char *hpp_extension = "h"; static const char *cpp_extension = "cxx"; static const char *depends_extension = "d"; static String *outdir = 0; static String *xmlout = 0; static int outcurrentdir = 0; static int help = 0; static int checkout = 0; static int cpp_only = 0; static int no_cpp = 0; static String *outfile_name = 0; static String *outfile_name_h = 0; static int tm_debug = 0; static int dump_symtabs = 0; static int dump_symbols = 0; static int dump_csymbols = 0; static int dump_lang_symbols = 0; static int dump_tags = 0; static int dump_module = 0; static int dump_top = 0; static int dump_typedef = 0; static int dump_classes = 0; static int werror = 0; static int depend = 0; static int depend_only = 0; static int depend_phony = 0; static int memory_debug = 0; static int allkw = 0; static DOH *cpps = 0; static String *dependencies_file = 0; static String *dependencies_target = 0; static int external_runtime = 0; static String *external_runtime_name = 0; enum { STAGE1=1, STAGE2=2, STAGE3=4, STAGE4=8, STAGEOVERFLOW=16 }; static List *libfiles = 0; static List *all_output_files = 0; static const char *stdcpp_define = NULL; static const char *stdc_define = NULL; /* ----------------------------------------------------------------------------- * check_extension() * * Checks the extension of a file to see if we should emit extern declarations. * ----------------------------------------------------------------------------- */ static bool check_extension(String *filename) { bool wanted = false; const char *name = Char(filename); if (!name) return 0; String *extension = Swig_file_extension(name); const char *c = Char(extension); if ((strcmp(c, ".c") == 0) || (strcmp(c, ".C") == 0) || (strcmp(c, ".cc") == 0) || (strcmp(c, ".cxx") == 0) || (strcmp(c, ".c++") == 0) || (strcmp(c, ".cpp") == 0)) { wanted = true; } Delete(extension); return wanted; } /* ----------------------------------------------------------------------------- * decode_numbers_list() * * Decode comma separated list into a binary number of the inputs or'd together * eg list="1,4" will return (2^0 || 2^3) = 0x1001 * ----------------------------------------------------------------------------- */ static unsigned int decode_numbers_list(String *numlist) { unsigned int decoded_number = 0; if (numlist) { List *numbers = Split(numlist, ',', INT_MAX); if (numbers && Len(numbers) > 0) { for (Iterator it = First(numbers); it.item; it = Next(it)) { String *numstring = it.item; // TODO: check that it is a number int number = atoi(Char(numstring)); if (number > 0 && number <= 16) { decoded_number |= (1 << (number-1)); } } } } return decoded_number; } /* ----------------------------------------------------------------------------- * Sets the output directory for language specific (proxy) files from the * C wrapper file if not set and corrects the directory name and adds a trailing * file separator if necessary. * ----------------------------------------------------------------------------- */ static void configure_outdir(const String *c_wrapper_outfile) { // Use the C wrapper file's directory if the output directory has not been set by user if (!outdir || Len(outdir) == 0) outdir = Swig_file_dirname(c_wrapper_outfile); Swig_filename_correct(outdir); // Add trailing file delimiter if not present in output directory name if (Len(outdir) > 0) { const char *outd = Char(outdir); if (strcmp(outd + strlen(outd) - strlen(SWIG_FILE_DELIMITER), SWIG_FILE_DELIMITER) != 0) Printv(outdir, SWIG_FILE_DELIMITER, NIL); } } /* This function sets the name of the configuration file */ void SWIG_config_file(const_String_or_char_ptr filename) { lang_config = NewString(filename); } /* Sets the target language subdirectory name */ void SWIG_library_directory(const char *subdirectory) { LangSubDir = NewString(subdirectory); } // Returns the directory for generating language specific files (non C/C++ files) const String *SWIG_output_directory() { assert(outdir); return outdir; } void SWIG_config_cppext(const char *ext) { cpp_extension = ext; } List *SWIG_output_files() { assert(all_output_files); return all_output_files; } static void SWIG_setfeature(const char *cfeature, const char *cvalue) { Hash *features_hash = Swig_cparse_features(); String *name = NewString(""); String *fname = NewString(cfeature); String *fvalue = NewString(cvalue); Swig_feature_set(features_hash, name, 0, fname, fvalue, 0); Delete(name); Delete(fname); Delete(fvalue); } static void SWIG_setfeatures(const char *c) { char feature[64]; char *fb = feature; char *fe = fb + 63; Hash *features_hash = Swig_cparse_features(); String *name = NewString(""); /* Printf(stderr,"all features %s\n", c); */ while (*c) { char *f = fb; String *fname = NewString("feature:"); String *fvalue = NewString(""); while ((f != fe) && *c != '=' && *c != ',' && *c) { *(f++) = *(c++); } *f = 0; Printf(fname, "%s", feature); if (*c && *(c++) == '=') { char value[64]; char *v = value; char *ve = v + 63; while ((v != ve) && *c != ',' && *c && !isspace(*c)) { *(v++) = *(c++); } *v = 0; Printf(fvalue, "%s", value); } else { Printf(fvalue, "1"); } /* Printf(stderr,"%s %s\n", fname, fvalue); */ Swig_feature_set(features_hash, name, 0, fname, fvalue, 0); Delete(fname); Delete(fvalue); } Delete(name); } /* This function handles the -external-runtime command option */ static void SWIG_dump_runtime() { String *outfile; File *runtime; String *s; outfile = external_runtime_name; if (!outfile) { outfile = lang->defaultExternalRuntimeFilename(); if (!outfile) { Printf(stderr, "*** Please provide a filename for the external runtime\n"); Exit(EXIT_FAILURE); } } runtime = NewFile(outfile, "w", SWIG_output_files()); if (!runtime) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } Swig_banner(runtime); Printf(runtime, "\n"); s = Swig_include_sys("swigcompat.swg"); if (!s) { Printf(stderr, "*** Unable to open 'swigcompat.swg'\n"); Delete(runtime); Exit(EXIT_FAILURE); } Printf(runtime, "%s", s); Delete(s); s = Swig_include_sys("swiglabels.swg"); if (!s) { Printf(stderr, "*** Unable to open 'swiglabels.swg'\n"); Delete(runtime); Exit(EXIT_FAILURE); } Printf(runtime, "%s", s); Delete(s); s = Swig_include_sys("swigerrors.swg"); if (!s) { Printf(stderr, "*** Unable to open 'swigerrors.swg'\n"); Delete(runtime); Exit(EXIT_FAILURE); } Printf(runtime, "%s", s); Delete(s); s = Swig_include_sys("swigrun.swg"); if (!s) { Printf(stderr, "*** Unable to open 'swigrun.swg'\n"); Delete(runtime); Exit(EXIT_FAILURE); } Printf(runtime, "%s", s); Delete(s); s = lang->runtimeCode(); Printf(runtime, "%s", s); Delete(s); s = Swig_include_sys("runtime.swg"); if (!s) { Printf(stderr, "*** Unable to open 'runtime.swg'\n"); Delete(runtime); Exit(EXIT_FAILURE); } Printf(runtime, "%s", s); Delete(s); Delete(runtime); Exit(EXIT_SUCCESS); } static void getoptions(int argc, char *argv[]) { int i; // Get options for (i = 1; i < argc; i++) { if (argv[i] && !Swig_check_marked(i)) { if (strncmp(argv[i], "-I-", 3) == 0) { // Don't push/pop directories Swig_set_push_dir(0); Swig_mark_arg(i); } else if (strncmp(argv[i], "-I", 2) == 0) { // Add a new directory search path Swig_add_directory((String_or_char*)(argv[i] + 2)); Swig_mark_arg(i); } else if (strncmp(argv[i], "-D", 2) == 0) { String *d = NewString(argv[i] + 2); if (Replace(d, "=", " ", DOH_REPLACE_FIRST) == 0) { // Match C preprocessor behaviour whereby -DFOO sets FOO=1. Append(d, " 1"); } // Create a symbol Preprocessor_define(d, 0); Delete(d); Swig_mark_arg(i); } else if (strncmp(argv[i], "-U", 2) == 0) { Preprocessor_undef(argv[i] + 2); Swig_mark_arg(i); } else if (strcmp(argv[i], "-E") == 0) { cpp_only = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-nopreprocess") == 0) { no_cpp = 1; Swig_mark_arg(i); } else if ((strcmp(argv[i], "-verbose") == 0) || (strcmp(argv[i], "-v") == 0)) { Verbose = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-c++") == 0) { CPlusPlus = 1; Swig_cparse_cplusplus(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-c++out") == 0) { // Undocumented Swig_cparse_cplusplusout(1); Swig_mark_arg(i); } else if (strncmp(argv[i], "-std=c", 6) == 0) { const char *std = argv[i] + 6; if (strncmp(std, "++", 2) == 0) { std += 2; if (strcmp(std, "98") == 0 || strcmp(std, "03") == 0) { stdcpp_define = "__cplusplus 199711L"; } else if (strcmp(std, "11") == 0) { stdcpp_define = "__cplusplus 201103L"; } else if (strcmp(std, "14") == 0) { stdcpp_define = "__cplusplus 201402L"; } else if (strcmp(std, "17") == 0) { stdcpp_define = "__cplusplus 201703L"; } else if (strcmp(std, "20") == 0) { stdcpp_define = "__cplusplus 202002L"; } else if (strcmp(std, "23") == 0) { stdcpp_define = "__cplusplus 202302L"; } else { Printf(stderr, "Unrecognised C++ standard version in option '%s'\n", argv[i]); Exit(EXIT_FAILURE); } } else { if (strcmp(std, "89") == 0 || strcmp(std, "90") == 0) { stdc_define = NULL; } else if (strcmp(std, "95") == 0) { stdc_define = "__STDC_VERSION__ 199409L"; } else if (strcmp(std, "99") == 0) { stdc_define = "__STDC_VERSION__ 199901L"; } else if (strcmp(std, "11") == 0) { stdc_define = "__STDC_VERSION__ 201112L"; } else if (strcmp(std, "17") == 0 || strcmp(std, "18") == 0) { // Both GCC and clang accept -std=c18 as well as -std=c17. stdc_define = "__STDC_VERSION__ 201710L"; } else if (strcmp(std, "23") == 0) { stdc_define = "__STDC_VERSION__ 202311L"; } else { Printf(stderr, "Unrecognised C standard version in option '%s'\n", argv[i]); Exit(EXIT_FAILURE); } } Swig_mark_arg(i); } else if (strcmp(argv[i], "-fcompact") == 0) { Wrapper_compact_print_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-fvirtual") == 0) { Wrapper_virtual_elimination_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-fastdispatch") == 0) { Wrapper_fast_dispatch_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nofastdispatch") == 0) { Wrapper_fast_dispatch_mode_set(0); Swig_mark_arg(i); } else if (strcmp(argv[i], "-naturalvar") == 0) { Wrapper_naturalvar_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-directors") == 0) { SWIG_setfeature("feature:director", "1"); Wrapper_director_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-dirprot") == 0) { Wrapper_director_protected_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nodirprot") == 0) { Wrapper_director_protected_mode_set(0); Swig_mark_arg(i); } else if (strcmp(argv[i], "-pcreversion") == 0) { String *version = Swig_pcre_version(); Printf(stdout, "%s\n", version); Delete(version); Swig_mark_arg(i); Exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-small") == 0) { Wrapper_compact_print_mode_set(1); Wrapper_virtual_elimination_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-external-runtime") == 0) { external_runtime = 1; Swig_mark_arg(i); if (argv[i + 1]) { external_runtime_name = NewString(argv[i + 1]); Swig_mark_arg(i + 1); i++; } } else if ((strcmp(argv[i], "-nodefaultctor") == 0)) { SWIG_setfeature("feature:nodefaultctor", "1"); Swig_mark_arg(i); } else if ((strcmp(argv[i], "-nodefaultdtor") == 0)) { SWIG_setfeature("feature:nodefaultdtor", "1"); Swig_mark_arg(i); } else if ((strcmp(argv[i], "-copyctor") == 0)) { SWIG_setfeature("feature:copyctor", "1"); Swig_mark_arg(i); } else if (strcmp(argv[i], "-noexcept") == 0) { NoExcept = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-addextern") == 0) { AddExtern = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-template") == 0) { Swig_cparse_debug_templates(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-templatereduce") == 0) { SWIG_cparse_template_reduce(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-notemplatereduce") == 0) { SWIG_cparse_template_reduce(0); Swig_mark_arg(i); } else if (strcmp(argv[i], "-macroerrors") == 0) { Swig_cparse_follow_locators(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-swiglib") == 0) { Printf(stdout, "%s\n", SwigLib); if (SwigLibWinUnix) Printf(stdout, "%s\n", SwigLibWinUnix); Exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-o") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { outfile_name = NewString(argv[i + 1]); Swig_filename_correct(outfile_name); if (!outfile_name_h || !dependencies_file) { char *ext = strrchr(Char(outfile_name), '.'); String *basename = ext ? NewStringWithSize(Char(outfile_name), (int)(Char(ext) - Char(outfile_name))) : NewString(outfile_name); if (!dependencies_file) { dependencies_file = NewStringf("%s.%s", basename, depends_extension); } if (!outfile_name_h) { Printf(basename, ".%s", hpp_extension); outfile_name_h = NewString(basename); } Delete(basename); } Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-oh") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { outfile_name_h = NewString(argv[i + 1]); Swig_filename_correct(outfile_name_h); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-fakeversion") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { Swig_set_fakeversion(argv[i + 1]); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[1], "--version") == 0) { fprintf(stdout, "\nSWIG Version %s\n", Swig_package_version()); fprintf(stdout, "\nCompiled with %s [%s]\n", SWIG_CXX, SWIG_PLATFORM); fprintf(stdout, "\nConfigured options: %cpcre\n", #ifdef HAVE_PCRE '+' #else '-' #endif ); fprintf(stdout, "\nPlease see %s for reporting bugs and further information\n", PACKAGE_BUGREPORT); Exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-copyright") == 0) { fprintf(stdout, "\nSWIG Version %s\n", Swig_package_version()); fprintf(stdout, "Copyright (c) 1995-1998\n"); fprintf(stdout, "University of Utah and the Regents of the University of California\n"); fprintf(stdout, "Copyright (c) 1998-2005\n"); fprintf(stdout, "University of Chicago\n"); fprintf(stdout, "Copyright (c) 2005-2006\n"); fprintf(stdout, "Arizona Board of Regents (University of Arizona)\n"); Exit(EXIT_SUCCESS); } else if (strncmp(argv[i], "-l", 2) == 0) { // Add a new directory search path Append(libfiles, argv[i] + 2); Swig_mark_arg(i); } else if (strcmp(argv[i], "-co") == 0) { checkout = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-features") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { SWIG_setfeatures(argv[i + 1]); Swig_mark_arg(i + 1); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-freeze") == 0) { freeze = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-includeall") == 0) { Preprocessor_include_all(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-importall") == 0) { Preprocessor_import_all(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-ignoremissing") == 0) { Preprocessor_ignore_missing(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-cpperraswarn") == 0) { Preprocessor_error_as_warning(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocpperraswarn") == 0) { Preprocessor_error_as_warning(0); Swig_mark_arg(i); } else if (strcmp(argv[i], "-cppext") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { SWIG_config_cppext(argv[i + 1]); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-debug-typemap") == 0) { tm_debug = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-tmsearch") == 0) { Swig_typemap_search_debug_set(); Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-tmused") == 0) { Swig_typemap_used_debug_set(); Swig_mark_arg(i); } else if (strcmp(argv[i], "-module") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { ModuleName = NewString(argv[i + 1]); Swig_mark_arg(i + 1); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-M") == 0) { depend = 1; depend_only = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-MM") == 0) { depend = 2; depend_only = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-MF") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { dependencies_file = NewString(argv[i + 1]); Swig_mark_arg(i + 1); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-MD") == 0) { depend = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-MMD") == 0) { depend = 2; Swig_mark_arg(i); } else if (strcmp(argv[i], "-MP") == 0) { depend_phony = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-MT") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { if (!dependencies_target) dependencies_target = NewString(argv[i + 1]); else Printf(dependencies_target, " %s", argv[i + 1]); Swig_mark_arg(i + 1); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-outdir") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { outdir = NewString(argv[i + 1]); Swig_mark_arg(i + 1); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-outcurrentdir") == 0) { Swig_mark_arg(i); outcurrentdir = 1; } else if (strcmp(argv[i], "-Wall") == 0) { Swig_mark_arg(i); Swig_warnall(); } else if (strcmp(argv[i], "-Wallkw") == 0) { allkw = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-Werror") == 0) { werror = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-Wextra") == 0) { Swig_mark_arg(i); Swig_warnfilter(EXTRA_WARNINGS, 0); } else if (strncmp(argv[i], "-w", 2) == 0) { Swig_mark_arg(i); Swig_warnfilter(argv[i] + 2, 1); } else if (strcmp(argv[i], "-debug-quiet") == 0) { Swig_print_quiet(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-symtabs") == 0) { dump_symtabs = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-symbols") == 0) { dump_symbols = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-csymbols") == 0) { dump_csymbols = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-lsymbols") == 0) { dump_lang_symbols = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-tags") == 0) { dump_tags = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-top") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { String *dump_list = NewString(argv[i + 1]); dump_top = decode_numbers_list(dump_list); if (dump_top < STAGE1 || dump_top >= STAGEOVERFLOW) Swig_arg_error(); else Swig_mark_arg(i + 1); Delete(dump_list); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-debug-module") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { String *dump_list = NewString(argv[i + 1]); dump_module = decode_numbers_list(dump_list); if (dump_module < STAGE1 || dump_module >= STAGEOVERFLOW) Swig_arg_error(); else Swig_mark_arg(i + 1); Delete(dump_list); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-xmlout") == 0) { Swig_mark_arg(i); if (argv[i + 1]) { xmlout = NewString(argv[i + 1]); Swig_mark_arg(i + 1); } else { Swig_arg_error(); } } else if (strcmp(argv[i], "-nocontract") == 0) { Swig_mark_arg(i); Swig_contract_mode_set(0); } else if (strcmp(argv[i], "-debug-typedef") == 0) { dump_typedef = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-classes") == 0) { dump_classes = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-debug-memory") == 0) { memory_debug = 1; Swig_mark_arg(i); } else if (strcmp(argv[i], "-Fstandard") == 0) { Swig_error_msg_format(EMF_STANDARD); Swig_mark_arg(i); } else if (strcmp(argv[i], "-Fmicrosoft") == 0) { Swig_error_msg_format(EMF_MICROSOFT); Swig_mark_arg(i); } else if (strcmp(argv[i], "-O") == 0) { Wrapper_virtual_elimination_mode_set(1); Wrapper_fast_dispatch_mode_set(1); Swig_mark_arg(i); } else if (strcmp(argv[i], "-help") == 0) { fputs(usage1, stdout); fputs(usage2, stdout); fputs(usage3, stdout); fputs(usage4, stdout); Swig_mark_arg(i); help = 1; } } } } static void SWIG_exit_handler(int status); int SWIG_main(int argc, char *argv[], const TargetLanguageModule *tlm) { char *c; /* Set function for Exit() to call. */ SetExitHandler(SWIG_exit_handler); /* Initialize the SWIG core */ Swig_init(); // Default warning suppression Swig_warnfilter(EXTRA_WARNINGS, 1); // Initialize the preprocessor Preprocessor_init(); // Set lang to a dummy value if no target language was specified so we // can process options enough to handle -version, etc. lang = tlm ? tlm->fac() : new Language; Swig_contract_mode_set(1); /* Turn off directors mode */ Wrapper_director_mode_set(0); Wrapper_director_protected_mode_set(1); // Inform the parser if the nested classes should be ignored unless explicitly told otherwise via feature:flatnested ignore_nested_classes = lang->nestedClassesSupport() == Language::NCS_Unknown ? 1 : 0; kwargs_supported = lang->kwargsSupport() ? 1 : 0; // Create Library search directories // Check for SWIG_LIB environment variable c = getenv("SWIG_LIB"); if (c == (char *) 0 || *c == 0) { #if defined(_WIN32) char buf[MAX_PATH]; char *p; if (!(GetModuleFileName(0, buf, MAX_PATH) == 0 || (p = strrchr(buf, '\\')) == 0)) { *(p + 1) = '\0'; SwigLib = NewStringf("%sLib", buf); // Native windows installation path } else { SwigLib = NewStringf(""); // Unexpected error } if (Len(SWIG_LIB_WIN_UNIX) > 0) SwigLibWinUnix = NewString(SWIG_LIB_WIN_UNIX); // Unix installation path using a drive letter (for msys/mingw) #else SwigLib = NewString(SWIG_LIB); #endif } else { SwigLib = NewString(c); } libfiles = NewList(); all_output_files = NewList(); getoptions(argc, argv); // Parse language dependent options lang->main(argc, argv); if (help) { Printf(stdout, "\nNote: 'swig - -help' displays options for a specific target language.\n\n"); Exit(EXIT_SUCCESS); // Exit if we're in help mode } // Check all of the options to make sure we're cool. // Don't check for an input file if -external-runtime is passed Swig_check_options(external_runtime ? 0 : 1); if (CPlusPlus && cparse_cplusplusout) { Printf(stderr, "The -c++out option is for C input but C++ input has been requested via -c++\n"); Exit(EXIT_FAILURE); } // Set up some default symbols (available in both SWIG interface files // and C files). Define all predefined symbols after option parsing so // that attempts to use `-U` to undefine them are consistently handled. Preprocessor_define("SWIG 1", 0); Preprocessor_define("__STDC__ 1", 0); if (CPlusPlus) { // Default to C++98. if (!stdcpp_define) stdcpp_define = "__cplusplus 199711L"; Preprocessor_define(stdcpp_define, 0); } else { if (stdcpp_define) { Printf(stderr, "Option -std=c++XX was used without -c++\n"); Exit(EXIT_FAILURE); } } if (!CPlusPlus) { // Default to C90 which didn't define __STDC_VERSION__. if (stdc_define) { Preprocessor_define(stdc_define, 0); } } else { if (stdc_define) { Printf(stderr, "Option -std=cXX was used with -c++\n"); Exit(EXIT_FAILURE); } } String *vers = Swig_package_version_hex(); Preprocessor_define(vers, 0); Delete(vers); // Add language dependent directory to the search path { String *rl = NewString(""); Printf(rl, ".%sswig_lib%s%s", SWIG_FILE_DELIMITER, SWIG_FILE_DELIMITER, LangSubDir); Swig_add_directory(rl); if (SwigLibWinUnix) { rl = NewString(""); Printf(rl, "%s%s%s", SwigLibWinUnix, SWIG_FILE_DELIMITER, LangSubDir); Swig_add_directory(rl); } rl = NewString(""); Printf(rl, "%s%s%s", SwigLib, SWIG_FILE_DELIMITER, LangSubDir); Swig_add_directory(rl); } Swig_add_directory((String *) "." SWIG_FILE_DELIMITER "swig_lib"); if (SwigLibWinUnix) Swig_add_directory((String *) SwigLibWinUnix); Swig_add_directory(SwigLib); if (Verbose) { Printf(stdout, "Language subdirectory: %s\n", LangSubDir); Printf(stdout, "Search paths:\n"); List *sp = Swig_search_path(); Iterator s; for (s = First(sp); s.item; s = Next(s)) { Printf(stdout, " %s\n", s.item); } } // handle the -external-runtime argument if (external_runtime) SWIG_dump_runtime(); // If we made it this far, looks good. go for it.... input_file = NewString(argv[argc - 1]); Swig_filename_correct(input_file); // If the user has requested to check out a file, handle that if (checkout) { DOH *s; String *outfile = input_file; if (outfile_name) outfile = outfile_name; if (Verbose) Printf(stdout, "Handling checkout...\n"); s = Swig_include(input_file); if (!s) { Printf(stderr, "Unable to locate '%s' in the SWIG library.\n", input_file); } else { FILE *f = Swig_open(outfile); if (f) { fclose(f); Printf(stderr, "File '%s' already exists. Checkout aborted.\n", outfile); } else { File *f_outfile = NewFile(outfile, "w", SWIG_output_files()); if (!f_outfile) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } else { if (Verbose) Printf(stdout, "'%s' checked out from the SWIG library.\n", outfile); Printv(f_outfile, s, NIL); Delete(f_outfile); } } } } else { // Run the preprocessor if (Verbose) Printf(stdout, "Preprocessing...\n"); { int i; String *fs = NewString(""); FILE *df = Swig_open(input_file); if (!df) { char *cfile = Char(input_file); if (cfile && cfile[0] == '-') { Printf(stderr, "Unable to find option or file '%s', ", input_file); Printf(stderr, "Use 'swig -help' for more information.\n"); } else { Printf(stderr, "Unable to find file '%s'.\n", input_file); } Exit(EXIT_FAILURE); } if (!tlm) { Printf(stderr, "No target language specified.\n"); Printf(stderr, "Use 'swig -help' for more information.\n"); Exit(EXIT_FAILURE); } if (!no_cpp) { fclose(df); Printf(fs, "%%include \n"); if (allkw) { Printf(fs, "%%include \n"); } if (lang_config) { Printf(fs, "\n%%include <%s>\n", lang_config); } Printf(fs, "%%include(maininput=\"%s\") \"%s\"\n", Swig_filename_escape(input_file), Swig_filename_escape(Swig_last_file())); for (i = 0; i < Len(libfiles); i++) { Printf(fs, "\n%%include \"%s\"\n", Swig_filename_escape(Getitem(libfiles, i))); } Seek(fs, 0, SEEK_SET); cpps = Preprocessor_parse(fs); Delete(fs); } else { cpps = Swig_read_file(df); fclose(df); } if (Swig_error_count()) { Exit(EXIT_FAILURE); } if (cpp_only) { Printf(stdout, "%s", cpps); Exit(EXIT_SUCCESS); } if (depend) { if (!no_cpp) { String *outfile; File *f_dependencies_file = 0; String *inputfile_filename = outcurrentdir ? Swig_file_filename(input_file): Copy(input_file); String *basename = Swig_file_basename(inputfile_filename); if (!outfile_name) { if (CPlusPlus || lang->cplus_runtime_mode()) { outfile = NewStringf("%s_wrap.%s", basename, cpp_extension); } else { outfile = NewStringf("%s_wrap.c", basename); } } else { outfile = NewString(outfile_name); } if (dependencies_file && Len(dependencies_file) != 0) { f_dependencies_file = NewFile(dependencies_file, "w", SWIG_output_files()); if (!f_dependencies_file) { FileErrorDisplay(dependencies_file); Exit(EXIT_FAILURE); } } else if (!depend_only) { String *filename = NewStringf("%s_wrap.%s", basename, depends_extension); f_dependencies_file = NewFile(filename, "w", SWIG_output_files()); if (!f_dependencies_file) { FileErrorDisplay(filename); Exit(EXIT_FAILURE); } } else f_dependencies_file = stdout; if (dependencies_target) { Printf(f_dependencies_file, "%s: ", Swig_filename_escape_space(dependencies_target)); } else { Printf(f_dependencies_file, "%s: ", Swig_filename_escape_space(outfile)); } List *files = Preprocessor_depend(); List *phony_targets = NewList(); for (int i = 0; i < Len(files); i++) { int use_file = 1; if (depend == 2) { if ((Strncmp(Getitem(files, i), SwigLib, Len(SwigLib)) == 0) || (SwigLibWinUnix && (Strncmp(Getitem(files, i), SwigLibWinUnix, Len(SwigLibWinUnix)) == 0))) use_file = 0; } if (use_file) { Printf(f_dependencies_file, "\\\n %s ", Swig_filename_escape_space(Getitem(files, i))); if (depend_phony) Append(phony_targets, Getitem(files, i)); } } Printf(f_dependencies_file, "\n"); if (depend_phony) { for (int i = 0; i < Len(phony_targets); i++) { Printf(f_dependencies_file, "\n%s:\n", Swig_filename_escape_space(Getitem(phony_targets, i))); } } if (f_dependencies_file != stdout) Delete(f_dependencies_file); if (depend_only) Exit(EXIT_SUCCESS); Delete(inputfile_filename); Delete(basename); Delete(phony_targets); } else { Printf(stderr, "Cannot generate dependencies with -nopreprocess\n"); // Actually we could but it would be inefficient when just generating dependencies, as it would be done after Swig_cparse Exit(EXIT_FAILURE); } } Seek(cpps, 0, SEEK_SET); } /* Register a null file with the file handler */ Swig_register_filebyname("null", NewString("")); // Pass control over to the specific language interpreter if (Verbose) { fprintf(stdout, "Starting language-specific parse...\n"); fflush(stdout); } Node *top = Swig_cparse(cpps); if (dump_top & STAGE1) { Printf(stdout, "debug-top stage 1\n"); Swig_print_tree(top); } if (dump_module & STAGE1) { Printf(stdout, "debug-module stage 1\n"); Swig_print_tree(Getattr(top, "module")); } if (!CPlusPlus) { if (Verbose) Printf(stdout, "Processing unnamed structs...\n"); Swig_nested_name_unnamed_c_structs(top); } Swig_extend_unused_check(); if (Verbose) { Printf(stdout, "Processing types...\n"); } Swig_process_types(top); if (dump_top & STAGE2) { Printf(stdout, "debug-top stage 2\n"); Swig_print_tree(top); } if (dump_module & STAGE2) { Printf(stdout, "debug-module stage 2\n"); Swig_print_tree(Getattr(top, "module")); } if (Verbose) { Printf(stdout, "C++ analysis...\n"); } Swig_default_allocators(top); if (CPlusPlus) { if (Verbose) Printf(stdout, "Processing nested classes...\n"); Swig_nested_process_classes(top); } if (dump_top & STAGE3) { Printf(stdout, "debug-top stage 3\n"); Swig_print_tree(top); } if (top && (dump_module & STAGE3)) { Printf(stdout, "debug-module stage 3\n"); Swig_print_tree(Getattr(top, "module")); } if (Verbose) { Printf(stdout, "Generating wrappers...\n"); } if (top && dump_classes) { Hash *classes = Getattr(top, "classes"); if (classes) { Printf(stdout, "Classes\n"); Printf(stdout, "------------\n"); Iterator ki; for (ki = First(classes); ki.key; ki = Next(ki)) { Printf(stdout, "%s\n", ki.key); } } } if (dump_typedef) { SwigType_print_scope(); } if (dump_symtabs) { Swig_symbol_print_tables(Swig_symbol_global_scope()); Swig_symbol_print_tables_summary(); } if (dump_symbols) { Swig_symbol_print_symbols(); } if (dump_csymbols) { Swig_symbol_print_csymbols(); } if (dump_tags) { Swig_print_tags(top, 0); } if (top) { if (!Getattr(top, "name")) { Printf(stderr, "No module name specified using %%module or -module.\n"); Exit(EXIT_FAILURE); } else { /* Set some filename information on the object */ String *infile = scanner_get_main_input_file(); if (!infile) { Printf(stderr, "Missing input file in preprocessed output.\n"); Exit(EXIT_FAILURE); } Setattr(top, "infile", infile); // Note: if nopreprocess then infile is the original input file, otherwise input_file Setattr(top, "inputfile", input_file); String *infile_filename = outcurrentdir ? Swig_file_filename(infile): Copy(infile); String *basename = Swig_file_basename(infile_filename); if (!outfile_name) { if (CPlusPlus || lang->cplus_runtime_mode()) { Setattr(top, "outfile", NewStringf("%s_wrap.%s", basename, cpp_extension)); } else { Setattr(top, "outfile", NewStringf("%s_wrap.c", basename)); } } else { Setattr(top, "outfile", outfile_name); } if (!outfile_name_h) { Setattr(top, "outfile_h", NewStringf("%s_wrap.%s", basename, hpp_extension)); } else { Setattr(top, "outfile_h", outfile_name_h); } configure_outdir(Getattr(top, "outfile")); if (Swig_contract_mode_get()) { Swig_contracts(top); } // Check the extension for a c/c++ file. If so, we're going to declare everything we see as "extern" ForceExtern = check_extension(input_file); if (tlm->status == Experimental) { Swig_warning(WARN_LANG_EXPERIMENTAL, "SWIG", 1, "Experimental target language. " "Target language %s specified by %s is an experimental language. " "See the 'Target Languages' section in the Introduction chapter of the SWIG documentation.\n", tlm->help ? tlm->help : "", tlm->name); } else if (tlm->status == Deprecated) { Swig_warning(WARN_LANG_DEPRECATED, "SWIG", 1, "Deprecated target language. " "Target language %s specified by %s is a deprecated target language. " "It will be removed in the next release of SWIG unless a new maintainer steps forward to bring it up to at least experimental status. " "See the 'Target Languages' section in the Introduction chapter of the SWIG documentation.\n", tlm->help ? tlm->help : "", tlm->name); } lang->top(top); Delete(infile_filename); Delete(basename); } } if (dump_lang_symbols) { lang->dumpSymbols(); } if (dump_top & STAGE4) { Printf(stdout, "debug-top stage 4\n"); Swig_print_tree(top); } if (dump_module & STAGE4) { Printf(stdout, "debug-module stage 4\n"); Swig_print_tree(Getattr(top, "module")); } if (xmlout && top) { delete lang; lang = 0; Swig_print_xml(top, xmlout); } Delete(top); } if (tm_debug) Swig_typemap_debug(); if (memory_debug) DohMemoryDebug(); char *outfiles = getenv("CCACHE_OUTFILES"); if (outfiles) { File *f_outfiles = NewFile(outfiles, "w", 0); if (!f_outfiles) { Printf(stderr, "Failed to write list of output files to the filename '%s' specified in CCACHE_OUTFILES environment variable - ", outfiles); FileErrorDisplay(outfiles); Exit(EXIT_FAILURE); } else { int i; for (i = 0; i < Len(all_output_files); i++) Printf(f_outfiles, "%s\n", Getitem(all_output_files, i)); Delete(f_outfiles); } } // Deletes Delete(libfiles); Preprocessor_delete(); while (freeze) { } delete lang; int error_count = werror ? Swig_warn_count() : 0; error_count += Swig_error_count(); if (error_count != 0) Exit(EXIT_FAILURE); return 0; } /* ----------------------------------------------------------------------------- * SWIG_exit_handler() * * Cleanup and either freeze or exit * ----------------------------------------------------------------------------- */ static void SWIG_exit_handler(int status) { while (freeze) { } if (status > 0) { CloseAllOpenFiles(); /* Remove all generated files */ if (all_output_files) { for (int i = 0; i < Len(all_output_files); i++) { String *filename = Getitem(all_output_files, i); int removed = remove(Char(filename)); if (removed == -1) fprintf(stderr, "On exit, could not delete file %s: %s\n", Char(filename), strerror(errno)); } } } } swig-4.4.0/Source/Modules/.deps/0000775000175000017500000000000015075470612016240 5ustar williamwilliamswig-4.4.0/Source/Modules/php.cxx0000664000175000017500000027067715075443613016566 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * php.cxx * * PHP language module for SWIG. * ----------------------------------------------------------------------------- */ #include "swigmod.h" #include #include #include #include static const char *usage = "\ PHP Options (available with -php7)\n\ -prefix - Prepend to all class names in PHP wrappers\n\ \n"; // How to wrap non-class functions, variables and constants: // FIXME: Make this specifiable and also allow a real namespace. // Wrap as global PHP names. static bool wrap_nonclass_global = true; // Wrap in a class to fake a namespace (for compatibility with SWIG's behaviour // before PHP added namespaces. static bool wrap_nonclass_fake_class = true; static String *module = 0; static String *cap_module = 0; static String *prefix = 0; static File *f_begin = 0; static File *f_runtime = 0; static File *f_runtime_h = 0; static File *f_h = 0; static File *f_directors = 0; static File *f_directors_h = 0; static String *s_header; static String *s_wrappers; static String *s_init; static String *r_init; // RINIT user code static String *s_shutdown; // MSHUTDOWN user code static String *r_shutdown; // RSHUTDOWN user code static String *s_vdecl; static String *s_cinit; // consttab initialization code. static String *s_oinit; static String *s_arginfo; static String *s_entry; static String *cs_entry; static String *all_cs_entry; static String *fake_cs_entry; static String *s_creation; static String *pragma_incl; static String *pragma_code; static String *pragma_phpinfo; static String *pragma_version; static String *class_name = NULL; static String *base_class = NULL; static String *destructor_action = NULL; static String *magic_set = NULL; static String *magic_get = NULL; static String *magic_isset = NULL; // Class used as pseudo-namespace for compatibility. static String *fake_class_name() { static String *result = NULL; if (!result) { result = Len(prefix) ? prefix : module; if (!fake_cs_entry) { fake_cs_entry = NewStringf("static const zend_function_entry class_%s_functions[] = {\n", result); } Printf(s_creation, "static zend_class_entry *SWIG_Php_ce_%s;\n\n",result); Printf(s_oinit, " INIT_CLASS_ENTRY(internal_ce, \"%s\", class_%s_functions);\n", result, result); Printf(s_oinit, " SWIG_Php_ce_%s = zend_register_internal_class(&internal_ce);\n", result); Printf(s_oinit, "\n"); } return result; } static String *swig_wrapped_interface_ce() { static String *result = NULL; if (!result) { result = NewStringf("SWIG_Php_swig_wrapped_interface_ce"); Printf(s_oinit, " INIT_CLASS_ENTRY(%s, \"SWIG\\\\wrapped\", NULL);\n", result); } return result; } /* To reduce code size (generated and compiled) we only want to emit each * different arginfo once, so we need to track which have been used. */ static Hash *arginfo_used; /* Track non-class pointer types we need to to wrap */ static Hash *raw_pointer_types = 0; static int shadow = 1; // These static variables are used to pass some state from Handlers into functionWrapper static enum { standard = 0, memberfn, staticmemberfn, membervar, staticmembervar, constructor, destructor, directorconstructor, directordisown } wrapperType = standard; extern "C" { static void (*r_prevtracefunc) (const SwigType *t, String *mangled, String *clientdata) = 0; } static void SwigPHP_emit_pointer_type_registrations() { if (!raw_pointer_types) return; Iterator ki = First(raw_pointer_types); if (!ki.key) return; Printf(s_wrappers, "/* class object handlers for pointer wrappers */\n"); Printf(s_wrappers, "static zend_object_handlers swig_ptr_object_handlers;\n\n"); Printf(s_wrappers, "/* Object Creation Method for pointer wrapping class */\n"); Printf(s_wrappers, "static zend_object *swig_ptr_object_new(zend_class_entry *ce) {\n"); Printf(s_wrappers, " swig_object_wrapper *obj = (swig_object_wrapper*)zend_object_alloc(sizeof(swig_object_wrapper), ce);\n"); Printf(s_wrappers, " zend_object_std_init(&obj->std, ce);\n"); Printf(s_wrappers, " object_properties_init(&obj->std, ce);\n"); Printf(s_wrappers, " obj->std.handlers = &swig_ptr_object_handlers;\n"); Printf(s_wrappers, " obj->newobject = 0;\n"); Printf(s_wrappers, " return &obj->std;\n"); Printf(s_wrappers, "}\n\n"); Printf(s_wrappers, "/* Implement __toString equivalent, since that worked for the old-style resource wrapped pointers. */\n"); Append(s_wrappers, "#if PHP_MAJOR_VERSION > 8 || PHP_MINOR_VERSION >= 2\n"); Printf(s_wrappers, "static ZEND_RESULT_CODE swig_ptr_cast_object(zend_object *zobj, zval *retval, int type) {\n"); Append(s_wrappers, "#else\n"); Printf(s_wrappers, "static int swig_ptr_cast_object(zend_object *zobj, zval *retval, int type) {\n"); Append(s_wrappers, "#endif\n"); Printf(s_wrappers, " if (type == IS_STRING) {\n"); Printf(s_wrappers, " swig_object_wrapper *obj = swig_php_fetch_object(zobj);\n"); Printv(s_wrappers, " ZVAL_NEW_STR(retval, zend_strpprintf(0, \"SWIGPointer(%p,owned=%d)\", obj->ptr, obj->newobject));\n", NIL); Printf(s_wrappers, " return SUCCESS;\n"); Printf(s_wrappers, " }\n"); Printf(s_wrappers, " return FAILURE;\n"); Printf(s_wrappers, "}\n\n"); Printf(s_oinit, "\n /* Register classes to represent non-class pointer types */\n"); Printf(s_oinit, " swig_ptr_object_handlers = *zend_get_std_object_handlers();\n"); Printf(s_oinit, " swig_ptr_object_handlers.offset = XtOffsetOf(swig_object_wrapper, std);\n"); Printf(s_oinit, " swig_ptr_object_handlers.cast_object = swig_ptr_cast_object;\n"); while (ki.key) { String *type = ki.key; String *swig_wrapped = swig_wrapped_interface_ce(); Printf(s_creation, "/* class entry for pointer to %s */\n", type); Printf(s_creation, "static zend_class_entry *SWIG_Php_ce_%s;\n\n", type); Printf(s_oinit, " INIT_CLASS_ENTRY(internal_ce, \"%s\\\\%s\", NULL);\n", "SWIG", type); Printf(s_oinit, " SWIG_Php_ce_%s = zend_register_internal_class(&internal_ce);\n", type); Printf(s_oinit, " SWIG_Php_ce_%s->create_object = swig_ptr_object_new;\n", type); Printv(s_oinit, " zend_do_implement_interface(SWIG_Php_ce_", type, ", &", swig_wrapped, ");\n", NIL); Printf(s_oinit, " SWIG_TypeClientData(SWIGTYPE%s,SWIG_Php_ce_%s);\n", type, type); Printf(s_oinit, "\n"); ki = Next(ki); } } static Hash *create_php_type_flags() { Hash *h = NewHash(); Setattr(h, "array", "MAY_BE_ARRAY"); Setattr(h, "bool", "MAY_BE_BOOL"); Setattr(h, "callable", "MAY_BE_CALLABLE"); Setattr(h, "float", "MAY_BE_DOUBLE"); Setattr(h, "int", "MAY_BE_LONG"); Setattr(h, "iterable", "MAY_BE_ITERABLE"); Setattr(h, "mixed", "MAY_BE_MIXED"); Setattr(h, "null", "MAY_BE_NULL"); Setattr(h, "object", "MAY_BE_OBJECT"); Setattr(h, "resource", "MAY_BE_RESOURCE"); Setattr(h, "string", "MAY_BE_STRING"); Setattr(h, "void", "MAY_BE_VOID"); return h; } static Hash *php_type_flags = create_php_type_flags(); // php_class + ":" + php_method -> PHPTypes* // ":" + php_function -> PHPTypes* static Hash *all_phptypes = NewHash(); // php_class_name -> php_parent_class_name static Hash *php_parent_class = NewHash(); // Track if a method is directed in a descendent class. // php_class + ":" + php_method -> boolean (using SetFlag()/GetFlag()). static Hash *has_directed_descendent = NewHash(); // Track required return type for parent class methods. // php_class + ":" + php_method -> List of php types. static Hash *parent_class_method_return_type = NewHash(); // Class encapsulating the machinery to add PHP type declarations. class PHPTypes { // List with an entry for each parameter and one for the return type. // // We assemble the types in here before emitting them so for an overloaded // function we combine the type declarations from each overloaded form. List *merged_types; // List with an entry for each parameter which is passed "byref" in any // overloaded form. We use this to pass such parameters by reference in // the dispatch function. If NULL, no parameters are passed by reference. List *byref; // The id string used in the name of the arginfo for this object. String *arginfo_id; // The feature:php:type value: 0, 1 or -1 for "compatibility". int php_type_flag; // Does the node for this have directorNode set? bool has_director_node; // Track if all the overloads of a method are static. // // We should only flag a dispatch method as ACC_STATIC if all the dispatched // to methods are static. If we have both static and non-static methods in // the overloaded set we omit ACC_STATIC, and then all the methods are // callable (though the static ones only via an object). If we set // ACC_STATIC we get a crash on an attempt to call a non-static method. bool all_overloads_static; // Used to clamp the required number of parameters in the arginfo to be // compatible with any parent class version of the method. int num_required; int get_byref(int key) const { return byref && key < Len(byref) && Getitem(byref, key) != None; } int size() const { return std::max(Len(merged_types), Len(byref)); } String *get_phptype(int key, String *classtypes, List *more_return_types = NULL) { Clear(classtypes); // We want to minimise the list of class types by not redundantly listing // a class for which a super-class is also listed. This canonicalisation // allows for more sharing of arginfo (which reduces module size), makes // for a cleaner list if it's shown to the user, and also will speed up // module load a bit. Hash *classes = NewHash(); DOH *types = Getitem(merged_types, key); String *result = NewStringEmpty(); if (more_return_types) { if (types != None) { merge_type_lists(types, more_return_types); } } if (types != None) { SortList(types, NULL); String *prev = NULL; for (Iterator i = First(types); i.item; i = Next(i)) { if (prev && Equal(prev, i.item)) { // Skip duplicates when merging. continue; } String *c = Getattr(php_type_flags, i.item); if (c) { if (Len(result) > 0) Append(result, "|"); Append(result, c); } else { SetFlag(classes, i.item); } prev = i.item; } } // Remove entries for which a super-class is also listed. Iterator i = First(classes); while (i.key) { String *this_class = i.key; // We must advance the iterator early so we don't delete the element it // points to. i = Next(i); String *parent = this_class; while ((parent = Getattr(php_parent_class, parent)) != NULL) { if (GetFlag(classes, parent)) { Delattr(classes, this_class); break; } } } List *sorted_classes = SortedKeys(classes, Strcmp); for (i = First(sorted_classes); i.item; i = Next(i)) { if (Len(classtypes) > 0) Append(classtypes, "|"); Append(classtypes, prefix); Append(classtypes, i.item); } Delete(sorted_classes); // Make the mask 0 if there are only class names specified. if (Len(result) == 0) { Append(result, "0"); } return result; } public: PHPTypes(Node *n) : merged_types(NewList()), byref(NULL), num_required(INT_MAX) { String *php_type_feature = Getattr(n, "feature:php:type"); php_type_flag = 0; if (php_type_feature != NULL) { if (Equal(php_type_feature, "1")) { php_type_flag = 1; } else if (!Equal(php_type_feature, "0")) { php_type_flag = -1; } } arginfo_id = Copy(Getattr(n, "sym:name")); has_director_node = (Getattr(n, "directorNode") != NULL); all_overloads_static = true; } ~PHPTypes() { Delete(merged_types); Delete(byref); } void not_all_static() { all_overloads_static = false; } bool get_all_static() const { return all_overloads_static; } void adjust(int num_required_, bool php_constructor) { num_required = std::min(num_required, num_required_); if (php_constructor) { // Don't add a return type declaration for a PHP __construct method // (because there it has no return type as far as PHP is concerned). php_type_flag = 0; } } String *get_arginfo_id() const { return arginfo_id; } // key is 0 for return type, or >= 1 for parameters numbered from 1 List *process_phptype(Node *n, int key, const String_or_char *attribute_name); // Merge entries from o_merge_list into merge_list, skipping any entries // already present. // // Both merge_list and o_merge_list should be in sorted order. static void merge_type_lists(List *merge_list, List *o_merge_list); void merge_from(const PHPTypes* o); void set_byref(int key) { if (!byref) { byref = NewList(); } while (Len(byref) <= key) { Append(byref, None); } // If any overload takes a particular parameter by reference then the // dispatch function also needs to take that parameter by reference so // we can just set unconditionally here. Setitem(byref, key, ""); // Just needs to be something != None. } void emit_arginfo(DOH *item, String *key) { Setmark(item, 1); char *colon_ptr = Strchr(key, ':'); assert(colon_ptr); int colon = (int)(colon_ptr - Char(key)); if (colon > 0 && Strcmp(colon_ptr + 1, "__construct") != 0) { // See if there's a parent class which implements this method, and if so // emit its arginfo and then merge its PHPTypes into ours as we need to // be compatible with it (whether it is virtual or not). String *this_class = NewStringWithSize(Char(key), colon); String *parent = this_class; while ((parent = Getattr(php_parent_class, parent)) != NULL) { String *k = NewStringf("%s%s", parent, colon_ptr); DOH *item = Getattr(all_phptypes, k); if (item) { PHPTypes *p = (PHPTypes*)Data(item); if (!Getmark(item)) { p->emit_arginfo(item, k); } merge_from(p); Delete(k); break; } Delete(k); } Delete(this_class); } // We want to only emit each different arginfo once, as that reduces the // size of both the generated source code and the compiled extension // module. The parameters at this level are just named arg1, arg2, etc // so the arginfo will be the same for any function with the same number // of parameters and (if present) PHP type declarations for parameters and // return type. // // We generate the arginfo we want (taking care to normalise, e.g. the // lists of types are unique and in sorted order), then use the // arginfo_used Hash to see if we've already generated it. String *out_phptype = NULL; String *out_phpclasses = NewStringEmpty(); // We provide a simple way to generate PHP return type declarations // except for directed methods. The point of directors is to allow // subclassing in the target language, and if the wrapped method has // a return type declaration then an overriding method in user code // needs to have a compatible declaration. // // The upshot of this is that enabling return type declarations for // existing bindings would break compatibility with user code written // for an older version. For parameters however the situation is // different because if the parent class declares types for parameters // a subclass overriding the function will be compatible whether it // declares them or not. // // directorNode being present seems to indicate if this method or one // it inherits from is directed, which is what we care about here. // Using (!is_member_director(n)) would get it wrong for testcase // director_frob. if (php_type_flag && (php_type_flag > 0 || !has_director_node)) { if (!GetFlag(has_directed_descendent, key)) { out_phptype = get_phptype(0, out_phpclasses, Getattr(parent_class_method_return_type, key)); } } // ### in arginfo_code will be replaced with the id once that is known. String *arginfo_code = NewStringEmpty(); if (out_phptype) { if (Len(out_phpclasses)) { Replace(out_phpclasses, "\\", "\\\\", DOH_REPLACE_ANY); Printf(arginfo_code, "ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(swig_arginfo_###, 0, %d, %s, %s)\n", num_required, out_phpclasses, out_phptype); } else { Printf(arginfo_code, "ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(swig_arginfo_###, 0, %d, %s)\n", num_required, out_phptype); } } else { Printf(arginfo_code, "ZEND_BEGIN_ARG_INFO_EX(swig_arginfo_###, 0, 0, %d)\n", num_required); } int phptypes_size = size(); for (int param_count = 1; param_count < phptypes_size; ++param_count) { String *phpclasses = NewStringEmpty(); String *phptype = get_phptype(param_count, phpclasses); int byref = get_byref(param_count); // FIXME: Should we be doing byref for return value as well? if (phptype) { if (Len(phpclasses)) { // We need to double any backslashes (which are PHP namespace // separators) in the PHP class names as they get turned into // C strings by the ZEND_ARG_OBJ_TYPE_MASK macro. Replace(phpclasses, "\\", "\\\\", DOH_REPLACE_ANY); Printf(arginfo_code, " ZEND_ARG_OBJ_TYPE_MASK(%d,arg%d,%s,%s,NULL)\n", byref, param_count, phpclasses, phptype); } else { Printf(arginfo_code, " ZEND_ARG_TYPE_MASK(%d,arg%d,%s,NULL)\n", byref, param_count, phptype); } } else { Printf(arginfo_code, " ZEND_ARG_INFO(%d,arg%d)\n", byref, param_count); } } Printf(arginfo_code, "ZEND_END_ARG_INFO()\n"); String *arginfo_id_same = Getattr(arginfo_used, arginfo_code); if (arginfo_id_same) { Printf(s_arginfo, "#define swig_arginfo_%s swig_arginfo_%s\n", arginfo_id, arginfo_id_same); } else { // Not had this arginfo before. Setattr(arginfo_used, arginfo_code, arginfo_id); arginfo_code = Copy(arginfo_code); Replace(arginfo_code, "###", arginfo_id, DOH_REPLACE_FIRST); Append(s_arginfo, arginfo_code); } Delete(arginfo_code); arginfo_code = NULL; } }; static PHPTypes *phptypes = NULL; class PHP : public Language { public: PHP() { directorLanguage(); } /* ------------------------------------------------------------ * main() * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { SWIG_library_directory("php"); for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; } else { Swig_arg_error(); } } else if ((strcmp(argv[i], "-noshadow") == 0)) { shadow = 0; Swig_mark_arg(i); } else if (strcmp(argv[i], "-help") == 0) { fputs(usage, stdout); } } Preprocessor_define("SWIGPHP 1", 0); Preprocessor_define("SWIGPHP7 1", 0); SWIG_config_file("php.swg"); allow_overloading(); } /* ------------------------------------------------------------ * top() * ------------------------------------------------------------ */ virtual int top(Node *n) { String *filen; /* Check if directors are enabled for this module. */ Node *mod = Getattr(n, "module"); if (mod) { Node *options = Getattr(mod, "options"); if (options && Getattr(options, "directors")) { allow_directors(); } } /* Set comparison with null for ConstructorToFunction */ setSubclassInstanceCheck(NewString("Z_TYPE_P($arg) != IS_NULL")); /* Initialize all of the output files */ String *outfile = Getattr(n, "outfile"); String *outfile_h = Getattr(n, "outfile_h"); /* main output file */ f_begin = NewFile(outfile, "w", SWIG_output_files()); if (!f_begin) { FileErrorDisplay(outfile); Exit(EXIT_FAILURE); } f_runtime = NewStringEmpty(); /* sections of the output file */ s_init = NewStringEmpty(); r_init = NewStringEmpty(); s_shutdown = NewStringEmpty(); r_shutdown = NewStringEmpty(); s_header = NewString("/* header section */\n"); s_wrappers = NewString("/* wrapper section */\n"); s_creation = NewStringEmpty(); /* subsections of the init section */ s_vdecl = NewString("/* vdecl subsection */\n"); s_cinit = NewString(" /* cinit subsection */\n"); s_oinit = NewString(" /* oinit subsection */\n"); pragma_phpinfo = NewStringEmpty(); f_directors_h = NewStringEmpty(); f_directors = NewStringEmpty(); if (Swig_directors_enabled()) { f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { FileErrorDisplay(outfile_h); Exit(EXIT_FAILURE); } } /* Register file targets with the SWIG file handler */ Swig_register_filebyname("begin", f_begin); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", s_init); Swig_register_filebyname("rinit", r_init); Swig_register_filebyname("shutdown", s_shutdown); Swig_register_filebyname("rshutdown", r_shutdown); Swig_register_filebyname("header", s_header); Swig_register_filebyname("wrapper", s_wrappers); Swig_register_filebyname("director", f_directors); Swig_register_filebyname("director_h", f_directors_h); Swig_banner(f_begin); Swig_obligatory_macros(f_runtime, "PHP"); if (Swig_directors_enabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); } // We need to include php.h before string.h gets included, at least with // PHP 8.2. Otherwise string.h is included without _GNU_SOURCE being // included and memrchr() doesn't get declared, and then inline code in // the PHP headers defines _GNU_SOURCE, includes string.h (which is a // no op thanks to the include gaurds), then tries to use memrchr() and // fails. // // We also need to suppress -Wdeclaration-after-statement if enabled // since with PHP 8.2 zend_operators.h contains inline code which triggers // this warning and our testsuite uses with option and -Werror. I don't // see a good way to only do this within our testsuite, but disabling // it globally like this shouldn't be problematic. Append(f_runtime, "\n" "#if defined __GNUC__ && !defined __cplusplus\n" "# if __GNUC__ >= 4\n" "# pragma GCC diagnostic push\n" "# pragma GCC diagnostic ignored \"-Wdeclaration-after-statement\"\n" "# endif\n" "#endif\n" "#include \"php.h\"\n" "#if defined __GNUC__ && !defined __cplusplus\n" "# if __GNUC__ >= 4\n" "# pragma GCC diagnostic pop\n" "# endif\n" "#endif\n\n"); /* Set the module name */ module = Copy(Getattr(n, "name")); cap_module = NewStringf("%(upper)s", module); if (!prefix) prefix = NewStringEmpty(); if (Swig_directors_enabled()) { Swig_banner(f_directors_h); Printf(f_directors_h, "\n"); Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", cap_module); Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", cap_module); String *filename = Swig_file_filename(outfile_h); Printf(f_directors, "\n#include \"%s\"\n\n", filename); Delete(filename); } /* sub-sections of the php file */ pragma_code = NewStringEmpty(); pragma_incl = NewStringEmpty(); pragma_version = NULL; /* Initialize the rest of the module */ /* start the header section */ Printf(s_header, "#define SWIG_name \"%s\"\n", module); Printf(s_header, "#ifdef __cplusplus\n"); Printf(s_header, "extern \"C\" {\n"); Printf(s_header, "#endif\n"); Printf(s_header, "#include \"php_ini.h\"\n"); Printf(s_header, "#include \"ext/standard/info.h\"\n"); Printf(s_header, "#include \"php_%s.h\"\n", module); Printf(s_header, "#ifdef __cplusplus\n"); Printf(s_header, "}\n"); Printf(s_header, "#endif\n\n"); if (Swig_directors_enabled()) { // Insert director runtime Swig_insert_file("director_common.swg", s_header); Swig_insert_file("director.swg", s_header); } /* Create the .h file too */ filen = NewStringEmpty(); Printv(filen, SWIG_output_directory(), "php_", module, ".h", NIL); f_h = NewFile(filen, "w", SWIG_output_files()); if (!f_h) { FileErrorDisplay(filen); Exit(EXIT_FAILURE); } Swig_banner(f_h); Printf(f_h, "\n"); Printf(f_h, "#ifndef PHP_%s_H\n", cap_module); Printf(f_h, "#define PHP_%s_H\n\n", cap_module); Printf(f_h, "extern zend_module_entry %s_module_entry;\n", module); Printf(f_h, "#define phpext_%s_ptr &%s_module_entry\n\n", module, module); Printf(f_h, "#ifdef PHP_WIN32\n"); Printf(f_h, "# define PHP_%s_API __declspec(dllexport)\n", cap_module); Printf(f_h, "#else\n"); Printf(f_h, "# define PHP_%s_API\n", cap_module); Printf(f_h, "#endif\n\n"); /* start the arginfo section */ s_arginfo = NewString("/* arginfo subsection */\n"); arginfo_used = NewHash(); /* start the function entry section */ s_entry = NewString("/* entry subsection */\n"); /* holds all the per-class function entry sections */ all_cs_entry = NewString("/* class entry subsection */\n"); cs_entry = NULL; fake_cs_entry = NULL; Printf(s_entry, "/* Every non-class user visible function must have an entry here */\n"); Printf(s_entry, "static const zend_function_entry module_%s_functions[] = {\n", module); /* Emit all of the code */ Language::top(n); /* Emit all the arginfo. We sort the keys so the output order doesn't depend on * hashkey order. */ { List *sorted_keys = SortedKeys(all_phptypes, Strcmp); for (Iterator k = First(sorted_keys); k.item; k = Next(k)) { DOH *val = Getattr(all_phptypes, k.item); if (!Getmark(val)) { PHPTypes *p = (PHPTypes*)Data(val); p->emit_arginfo(val, k.item); } } Delete(sorted_keys); } SwigPHP_emit_pointer_type_registrations(); Dump(s_creation, s_header); Delete(s_creation); s_creation = NULL; /* start the init section */ { String *s_init_old = s_init; s_init = NewString("/* init section */\n"); Printv(s_init, "zend_module_entry ", module, "_module_entry = {\n", NIL); Printf(s_init, " STANDARD_MODULE_HEADER,\n"); Printf(s_init, " \"%s\",\n", module); Printf(s_init, " module_%s_functions,\n", module); Printf(s_init, " PHP_MINIT(%s),\n", module); if (Len(s_shutdown) > 0) { Printf(s_init, " PHP_MSHUTDOWN(%s),\n", module); } else { Printf(s_init, " NULL, /* No MSHUTDOWN code */\n"); } if (Len(r_init) > 0) { Printf(s_init, " PHP_RINIT(%s),\n", module); } else { Printf(s_init, " NULL, /* No RINIT code */\n"); } if (Len(r_shutdown) > 0) { Printf(s_init, " PHP_RSHUTDOWN(%s),\n", module); } else { Printf(s_init, " NULL, /* No RSHUTDOWN code */\n"); } if (Len(pragma_phpinfo) > 0) { Printf(s_init, " PHP_MINFO(%s),\n", module); } else { Printf(s_init, " NULL, /* No MINFO code */\n"); } if (Len(pragma_version) > 0) { Printf(s_init, " \"%s\",\n", pragma_version); } else { Printf(s_init, " NO_VERSION_YET,\n"); } Printf(s_init, " STANDARD_MODULE_PROPERTIES\n"); Printf(s_init, "};\n\n"); Printf(s_init, "#ifdef __cplusplus\n"); Printf(s_init, "extern \"C\" {\n"); Printf(s_init, "#endif\n"); // We want to write "SWIGEXPORT ZEND_GET_MODULE(%s)" but the definition // of ZEND_GET_MODULE has "extern "C" { ... }" around it so we can't do // that. Printf(s_init, "SWIGEXPORT zend_module_entry *get_module(void) { return &%s_module_entry; }\n", module); Printf(s_init, "#ifdef __cplusplus\n"); Printf(s_init, "}\n"); Printf(s_init, "#endif\n\n"); Printf(s_init, "#define SWIG_php_minit PHP_MINIT_FUNCTION(%s)\n\n", module); Printv(s_init, s_init_old, NIL); Delete(s_init_old); } /* We have to register the constants before they are (possibly) used * by the pointer typemaps. This all needs re-arranging really as * things are being called in the wrong order */ Printf(s_oinit, " /* end oinit subsection */\n"); Printf(s_init, "%s\n", s_oinit); /* Constants generated during top call */ Printf(s_cinit, " /* end cinit subsection */\n"); Printf(s_init, "%s\n", s_cinit); Clear(s_cinit); Delete(s_cinit); Printf(s_init, " return SUCCESS;\n"); Printf(s_init, "}\n\n"); // Now do REQUEST init which holds any user specified %rinit, and also vinit if (Len(r_init) > 0) { Printf(f_h, "PHP_RINIT_FUNCTION(%s);\n", module); Printf(s_init, "PHP_RINIT_FUNCTION(%s)\n{\n", module); Printv(s_init, "/* rinit section */\n", r_init, "\n", NIL); Printf(s_init, " return SUCCESS;\n"); Printf(s_init, "}\n\n"); } Printf(f_h, "PHP_MINIT_FUNCTION(%s);\n", module); if (Len(s_shutdown) > 0) { Printf(f_h, "PHP_MSHUTDOWN_FUNCTION(%s);\n", module); Printv(s_init, "PHP_MSHUTDOWN_FUNCTION(", module, ")\n" "/* shutdown section */\n" "{\n", s_shutdown, " return SUCCESS;\n" "}\n\n", NIL); } if (Len(r_shutdown) > 0) { Printf(f_h, "PHP_RSHUTDOWN_FUNCTION(%s);\n", module); Printf(s_init, "PHP_RSHUTDOWN_FUNCTION(%s)\n{\n", module); Printf(s_init, "/* rshutdown section */\n"); Printf(s_init, "%s\n", r_shutdown); Printf(s_init, " return SUCCESS;\n"); Printf(s_init, "}\n\n"); } if (Len(pragma_phpinfo) > 0) { Printf(f_h, "PHP_MINFO_FUNCTION(%s);\n", module); Printf(s_init, "PHP_MINFO_FUNCTION(%s)\n{\n", module); Printf(s_init, "%s", pragma_phpinfo); Printf(s_init, "}\n"); } Printf(s_init, "/* end init section */\n"); Printf(f_h, "\n#endif /* PHP_%s_H */\n", cap_module); Delete(f_h); String *type_table = NewStringEmpty(); SwigType_emit_type_table(f_runtime, type_table); Printf(s_header, "%s", type_table); Delete(type_table); /* Oh dear, more things being called in the wrong order. This whole * function really needs totally redoing. */ if (Swig_directors_enabled()) { Dump(f_directors_h, f_runtime_h); Printf(f_runtime_h, "\n"); Printf(f_runtime_h, "#endif\n"); Delete(f_runtime_h); } Printf(s_header, "/* end header section */\n"); Printf(s_wrappers, "/* end wrapper section */\n"); Printf(s_vdecl, "/* end vdecl subsection */\n"); Dump(f_runtime, f_begin); Printv(f_begin, s_header, NIL); if (Swig_directors_enabled()) { Dump(f_directors, f_begin); } Printv(f_begin, s_vdecl, s_wrappers, NIL); Printv(f_begin, s_arginfo, "\n\n", all_cs_entry, "\n\n", s_entry, " ZEND_FE_END\n};\n\n", NIL); if (fake_cs_entry) { Printv(f_begin, fake_cs_entry, " ZEND_FE_END\n};\n\n", NIL); Delete(fake_cs_entry); fake_cs_entry = NULL; } Printv(f_begin, s_init, NIL); Delete(s_header); Delete(s_wrappers); Delete(s_init); Delete(s_vdecl); Delete(all_cs_entry); Delete(s_entry); Delete(s_arginfo); Delete(f_runtime); Delete(f_begin); Delete(arginfo_used); if (Len(pragma_incl) > 0 || Len(pragma_code) > 0) { /* PHP module file */ String *php_filename = NewStringEmpty(); Printv(php_filename, SWIG_output_directory(), module, ".php", NIL); File *f_phpcode = NewFile(php_filename, "w", SWIG_output_files()); if (!f_phpcode) { FileErrorDisplay(php_filename); Exit(EXIT_FAILURE); } Printf(f_phpcode, " 0) { Printv(f_phpcode, pragma_incl, "\n", NIL); } if (Len(pragma_code) > 0) { Printv(f_phpcode, pragma_code, "\n", NIL); } Delete(f_phpcode); Delete(php_filename); } return SWIG_OK; } /* Just need to append function names to function table to register with PHP. */ void create_command(String *cname, String *fname, Node *n, bool dispatch, String *modes) { // This is for the single main zend_function_entry record ParmList *l = Getattr(n, "parms"); if (cname && !Strstr(Getattr(n, "storage"), "friend")) { Printf(f_h, "static PHP_METHOD(%s%s,%s);\n", prefix, cname, fname); if (wrapperType != staticmemberfn && wrapperType != staticmembervar && !Equal(fname, "__construct")) { // Skip the first entry in the parameter list which is the this pointer. if (l) l = Getattr(l, "tmap:in:next"); // FIXME: does this throw the phptype key value off? } } else { if (dispatch) { Printf(f_h, "static ZEND_NAMED_FUNCTION(%s);\n", fname); } else { Printf(f_h, "static PHP_FUNCTION(%s);\n", fname); } } phptypes->adjust(emit_num_required(l), Equal(fname, "__construct") ? true : false); String *arginfo_id = phptypes->get_arginfo_id(); String *s = cs_entry; if (!s) s = s_entry; if (cname && !Strstr(Getattr(n, "storage"), "friend")) { Printf(all_cs_entry, " PHP_ME(%s%s,%s,swig_arginfo_%s,%s)\n", prefix, cname, fname, arginfo_id, modes); } else { if (dispatch) { if (wrap_nonclass_global) { Printf(s, " ZEND_NAMED_FE(%(lower)s,%s,swig_arginfo_%s)\n", Getattr(n, "sym:name"), fname, arginfo_id); } if (wrap_nonclass_fake_class) { (void)fake_class_name(); Printf(fake_cs_entry, " ZEND_NAMED_ME(%(lower)s,%s,swig_arginfo_%s,ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)\n", Getattr(n, "sym:name"), fname, arginfo_id); } } else { if (wrap_nonclass_global) { Printf(s, " PHP_FE(%s,swig_arginfo_%s)\n", fname, arginfo_id); } if (wrap_nonclass_fake_class) { String *fake_class = fake_class_name(); Printf(fake_cs_entry, " PHP_ME(%s,%s,swig_arginfo_%s,ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)\n", fake_class, fname, arginfo_id); } } } } /* ------------------------------------------------------------ * dispatchFunction() * ------------------------------------------------------------ */ void dispatchFunction(Node *n, int constructor) { /* Last node in overloaded chain */ int maxargs; bool check_emitted = false; String *tmp = NewStringEmpty(); String *dispatch = Swig_overload_dispatch(n, "%s(INTERNAL_FUNCTION_PARAM_PASSTHRU); return;", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ Wrapper *f = NewWrapper(); String *symname = Getattr(n, "sym:name"); String *wname = NULL; String *modes = NULL; bool constructorRenameOverload = false; if (constructor) { if (!Equal(class_name, Getattr(n, "constructorHandler:sym:name"))) { // Renamed constructor - turn into static factory method constructorRenameOverload = true; wname = Copy(Getattr(n, "constructorHandler:sym:name")); } else { wname = NewString("__construct"); } } else if (class_name) { wname = Getattr(n, "wrapper:method:name"); } else { wname = Swig_name_wrapper(symname); } if (constructor) { modes = NewString("ZEND_ACC_PUBLIC | ZEND_ACC_CTOR"); if (constructorRenameOverload) { Append(modes, " | ZEND_ACC_STATIC"); } } else if (phptypes->get_all_static()) { modes = NewString("ZEND_ACC_PUBLIC | ZEND_ACC_STATIC"); } else { modes = NewString("ZEND_ACC_PUBLIC"); } create_command(class_name, wname, n, true, modes); if (class_name && !Strstr(Getattr(n, "storage"), "friend")) { Printv(f->def, "static PHP_METHOD(", prefix, class_name, ",", wname, ") {\n", NIL); } else { Printv(f->def, "static ZEND_NAMED_FUNCTION(", wname, ") {\n", NIL); } Wrapper_add_local(f, "argc", "int argc"); Printf(tmp, "zval argv[%d]", maxargs); Wrapper_add_local(f, "argv", tmp); Printf(f->code, "argc = ZEND_NUM_ARGS();\n"); Printf(f->code, "zend_get_parameters_array_ex(argc, argv);\n"); Replaceall(dispatch, "$args", "self,args"); Printv(f->code, dispatch, "\n", NIL); Printf(f->code, "zend_throw_exception(zend_ce_type_error, \"No matching function for overloaded '%s'\", 0);\n", symname); Printv(f->code, "fail: SWIGUNUSED;\n", NIL); Printv(f->code, "return;\n", NIL); Printv(f->code, "}\n", NIL); Wrapper_print(f, s_wrappers); DelWrapper(f); Delete(dispatch); Delete(tmp); } /* ------------------------------------------------------------ * functionWrapper() * ------------------------------------------------------------ */ /* Helper function to check if class is wrapped */ bool is_class_wrapped(String *className) { if (!className) return false; Node *n = symbolLookup(className); return n && Getattr(n, "classtype") != NULL; } void generate_magic_property_methods(Node *class_node) { String *swig_base = base_class; if (Equal(swig_base, "Exception") || !is_class_wrapped(swig_base)) { swig_base = NULL; } static bool generated_magic_arginfo = false; if (!generated_magic_arginfo) { // Create arginfo entries for __get, __set and __isset. Append(s_arginfo, "ZEND_BEGIN_ARG_INFO_EX(swig_magic_arginfo_get, 0, 0, 1)\n" " ZEND_ARG_TYPE_MASK(0,arg1,MAY_BE_STRING,NULL)\n" "ZEND_END_ARG_INFO()\n"); Append(s_arginfo, "ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(swig_magic_arginfo_set, 0, 1, MAY_BE_VOID)\n" " ZEND_ARG_TYPE_MASK(0,arg1,MAY_BE_STRING,NULL)\n" " ZEND_ARG_INFO(0,arg2)\n" "ZEND_END_ARG_INFO()\n"); Append(s_arginfo, "ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(swig_magic_arginfo_isset, 0, 1, MAY_BE_BOOL)\n" " ZEND_ARG_TYPE_MASK(0,arg1,MAY_BE_STRING,NULL)\n" "ZEND_END_ARG_INFO()\n"); generated_magic_arginfo = true; } Wrapper *f = NewWrapper(); Printf(f_h, "PHP_METHOD(%s%s,__set);\n", prefix, class_name); Printf(all_cs_entry, " PHP_ME(%s%s,__set,swig_magic_arginfo_set,ZEND_ACC_PUBLIC)\n", prefix, class_name); Printf(f->code, "PHP_METHOD(%s%s,__set) {\n", prefix, class_name); Printf(f->code, " swig_object_wrapper *arg = SWIG_Z_FETCH_OBJ_P(ZEND_THIS);\n"); Printf(f->code, " zval args[2];\n zend_string *arg2 = 0;\n\n"); Printf(f->code, " if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_array_ex(2, args) != SUCCESS) {\n"); Printf(f->code, "\tWRONG_PARAM_COUNT;\n}\n\n"); Printf(f->code, " if (!arg) {\n"); Printf(f->code, " zend_throw_exception(zend_ce_type_error, \"this pointer is NULL\", 0);\n"); Printf(f->code, " return;\n"); Printf(f->code, " }\n"); Printf(f->code, " arg2 = Z_STR(args[0]);\n\n"); Printf(f->code, "if (!arg2) {\n RETVAL_NULL();\n}\n"); if (magic_set) { Append(f->code, magic_set); } Printf(f->code, "\nelse if (strcmp(ZSTR_VAL(arg2),\"thisown\") == 0) {\n"); Printf(f->code, "arg->newobject = zval_get_long(&args[1]);\n"); if (Swig_directorclass(class_node)) { Printv(f->code, "if (arg->newobject == 0) {\n", " Swig::Director *director = SWIG_DIRECTOR_CAST((", Getattr(class_node, "classtype"), "*)(arg->ptr));\n", " if (director) director->swig_disown();\n", "}\n", NIL); } if (swig_base) { Printf(f->code, "} else {\nPHP_MN(%s%s___set)(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n", prefix, swig_base); } else if (GetFlag(class_node, "feature:php:allowdynamicproperties")) { Printf(f->code, "} else {\nadd_property_zval_ex(ZEND_THIS, ZSTR_VAL(arg2), ZSTR_LEN(arg2), &args[1]);\n"); } Printf(f->code, "}\n"); Printf(f->code, "fail: SWIGUNUSED;\n"); Printf(f->code, "return;\n"); Printf(f->code, "}\n\n\n"); Printf(f_h, "PHP_METHOD(%s%s,__get);\n", prefix, class_name); Printf(all_cs_entry, " PHP_ME(%s%s,__get,swig_magic_arginfo_get,ZEND_ACC_PUBLIC)\n", prefix, class_name); Printf(f->code, "PHP_METHOD(%s%s,__get) {\n",prefix, class_name); Printf(f->code, " swig_object_wrapper *arg = SWIG_Z_FETCH_OBJ_P(ZEND_THIS);\n"); Printf(f->code, " zval args[1];\n zend_string *arg2 = 0;\n\n"); Printf(f->code, " if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_array_ex(1, args) != SUCCESS) {\n"); Printf(f->code, "\tWRONG_PARAM_COUNT;\n}\n\n"); Printf(f->code, " if (!arg) {\n"); Printf(f->code, " zend_throw_exception(zend_ce_type_error, \"this pointer is NULL\", 0);\n"); Printf(f->code, " return;\n"); Printf(f->code, " }\n"); Printf(f->code, " arg2 = Z_STR(args[0]);\n\n"); Printf(f->code, "if (!arg2) {\n RETVAL_NULL();\n}\n"); if (magic_get) { Append(f->code, magic_get); } Printf(f->code, "\nelse if (strcmp(ZSTR_VAL(arg2),\"thisown\") == 0) {\n"); Printf(f->code, "if(arg->newobject) {\nRETVAL_LONG(1);\n}\nelse {\nRETVAL_LONG(0);\n}\n}\n\n"); Printf(f->code, "else {\n"); if (swig_base) { Printf(f->code, "PHP_MN(%s%s___get)(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n}\n", prefix, swig_base); } else { // __get is only called if the property isn't set on the zend_object. Printf(f->code, "RETVAL_NULL();\n}\n"); } Printf(f->code, "fail: SWIGUNUSED;\n"); Printf(f->code, "return;\n"); Printf(f->code, "}\n\n\n"); Printf(f_h, "PHP_METHOD(%s%s,__isset);\n", prefix, class_name); Printf(all_cs_entry, " PHP_ME(%s%s,__isset,swig_magic_arginfo_isset,ZEND_ACC_PUBLIC)\n", prefix, class_name); Printf(f->code, "PHP_METHOD(%s%s,__isset) {\n",prefix, class_name); Printf(f->code, " swig_object_wrapper *arg = SWIG_Z_FETCH_OBJ_P(ZEND_THIS);\n"); Printf(f->code, " zval args[1];\n zend_string *arg2 = 0;\n\n"); Printf(f->code, " if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_array_ex(1, args) != SUCCESS) {\n"); Printf(f->code, "\tWRONG_PARAM_COUNT;\n}\n\n"); Printf(f->code, " if(!arg) {\n"); Printf(f->code, " zend_throw_exception(zend_ce_type_error, \"this pointer is NULL\", 0);\n"); Printf(f->code, " return;\n"); Printf(f->code, " }\n"); Printf(f->code, " arg2 = Z_STR(args[0]);\n\n"); Printf(f->code, "if (!arg2) {\n RETVAL_FALSE;\n}\n"); Printf(f->code, "\nelse if (strcmp(ZSTR_VAL(arg2),\"thisown\") == 0) {\n"); Printf(f->code, "RETVAL_TRUE;\n}\n\n"); if (magic_isset) { Append(f->code, magic_isset); } Printf(f->code, "else {\n"); if (swig_base) { Printf(f->code, "PHP_MN(%s%s___isset)(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n}\n", prefix, swig_base); } else { // __isset is only called if the property isn't set on the zend_object. Printf(f->code, "RETVAL_FALSE;\n}\n"); } Printf(f->code, "fail: SWIGUNUSED;\n"); Printf(f->code, "return;\n"); Printf(f->code, "}\n\n\n"); Wrapper_print(f, s_wrappers); DelWrapper(f); f = NULL; Delete(magic_set); Delete(magic_get); Delete(magic_isset); magic_set = NULL; magic_get = NULL; magic_isset = NULL; } String *getAccessMode(String *access) { if (Cmp(access, "protected") == 0) { return NewString("ZEND_ACC_PROTECTED"); } else if (Cmp(access, "private") == 0) { return NewString("ZEND_ACC_PRIVATE"); } return NewString("ZEND_ACC_PUBLIC"); } bool is_setter_method(Node *n) { const char *p = GetChar(n, "sym:name"); if (strlen(p) > 4) { p += strlen(p) - 4; if (strcmp(p, "_set") == 0) { return true; } } return false; } bool is_getter_method(Node *n) { const char *p = GetChar(n, "sym:name"); if (strlen(p) > 4) { p += strlen(p) - 4; if (strcmp(p, "_get") == 0) { return true; } } return false; } virtual int functionWrapper(Node *n) { if (wrapperType == directordisown) { // Handled via __set magic method - no explicit wrapper method wanted. return SWIG_OK; } String *name = GetChar(n, "name"); String *iname = GetChar(n, "sym:name"); SwigType *returntype = Getattr(n, "type"); ParmList *l = Getattr(n, "parms"); String *nodeType = Getattr(n, "nodeType"); int newobject = GetFlag(n, "feature:new"); int constructor = (Cmp(nodeType, "constructor") == 0); Parm *p; int i; int numopt; String *tm; Wrapper *f; String *wname = NewStringEmpty(); String *overloadwname = NULL; int overloaded = 0; String *modes = NULL; bool static_setter = false; bool static_getter = false; modes = getAccessMode(Getattr(n, "access")); if (constructor) { Append(modes, " | ZEND_ACC_CTOR"); } if (wrapperType == staticmemberfn || Cmp(Getattr(n, "storage"), "static") == 0) { Append(modes, " | ZEND_ACC_STATIC"); } if (GetFlag(n, "abstract") && Swig_directorclass(Swig_methodclass(n)) && !is_member_director(n)) Append(modes, " | ZEND_ACC_ABSTRACT"); if (Getattr(n, "sym:overloaded")) { overloaded = 1; overloadwname = NewString(Swig_name_wrapper(iname)); Printf(overloadwname, "%s", Getattr(n, "sym:overname")); } else { if (!addSymbol(iname, n)) return SWIG_ERROR; } if (constructor) { if (!Equal(class_name, Getattr(n, "constructorHandler:sym:name"))) { // Renamed constructor - turn into static factory method wname = Copy(Getattr(n, "constructorHandler:sym:name")); } else { wname = NewString("__construct"); } } else if (wrapperType == membervar) { wname = Copy(Getattr(n, "membervariableHandler:sym:name")); if (is_setter_method(n)) { Append(wname, "_set"); } else if (is_getter_method(n)) { Append(wname, "_get"); } } else if (wrapperType == memberfn) { wname = Getattr(n, "memberfunctionHandler:sym:name"); } else if (wrapperType == staticmembervar) { // Shape::nshapes -> nshapes wname = Getattr(n, "staticmembervariableHandler:sym:name"); /* We get called twice for getter and setter methods. But to maintain compatibility, Shape::nshapes() is being used for both setter and getter methods. So using static_setter and static_getter variables to generate half of the code each time. */ static_setter = is_setter_method(n); if (is_getter_method(n)) { // This is to overcome types that can't be set and hence no setter. if (!Equal(Getattr(n, "feature:immutable"), "1")) static_getter = true; } } else if (wrapperType == staticmemberfn) { wname = Getattr(n, "staticmemberfunctionHandler:sym:name"); } else { if (class_name) { if (Strstr(Getattr(n, "storage"), "friend") && Cmp(Getattr(n, "view"), "globalfunctionHandler") == 0) { wname = iname; } else { wname = Getattr(n, "destructorHandler:sym:name"); } } else { wname = iname; } } if (wrapperType == destructor) { // We don't explicitly wrap the destructor for PHP - Zend manages the // reference counting, and the user can just do `$obj = null;' or similar // to remove a reference to an object. Setattr(n, "wrap:name", wname); (void)emit_action(n); return SWIG_OK; } if (!static_getter) { // Create or find existing PHPTypes. phptypes = NULL; String *key; if (class_name && !Strstr(Getattr(n, "storage"), "friend")) { key = NewStringf("%s:%s", class_name, wname); } else { key = NewStringf(":%s", wname); } PHPTypes *p = (PHPTypes*)GetVoid(all_phptypes, key); if (p) { // We already have an entry so use it. phptypes = p; Delete(key); } else { phptypes = new PHPTypes(n); SetVoid(all_phptypes, key, phptypes); } if (!(wrapperType == staticmemberfn || Cmp(Getattr(n, "storage"), "static") == 0)) { phptypes->not_all_static(); } } f = NewWrapper(); if (static_getter) { Printf(f->def, "{\n"); } String *outarg = NewStringEmpty(); String *cleanup = NewStringEmpty(); if (!overloaded) { if (!static_getter) { if (class_name && !Strstr(Getattr(n, "storage"), "friend")) { Printv(f->def, "static PHP_METHOD(", prefix, class_name, ",", wname, ") {\n", NIL); } else { if (wrap_nonclass_global) { Printv(f->def, "static PHP_METHOD(", fake_class_name(), ",", wname, ") {\n", " PHP_FN(", wname, ")(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n", "}\n\n", NIL); } if (wrap_nonclass_fake_class) { Printv(f->def, "static PHP_FUNCTION(", wname, ") {\n", NIL); } } } } else { Printv(f->def, "static ZEND_NAMED_FUNCTION(", overloadwname, ") {\n", NIL); } emit_parameter_variables(l, f); /* Attach standard typemaps */ emit_attach_parmmaps(l, f); if (wrapperType == memberfn || wrapperType == membervar) { // Assign "this" to arg1 and remove first entry from ParmList l. Printf(f->code, "arg1 = (%s)SWIG_Z_FETCH_OBJ_P(ZEND_THIS)->ptr;\n", SwigType_lstr(Getattr(l, "type"), "")); l = nextSibling(l); } // wrap:parms is used by overload resolution. Setattr(n, "wrap:parms", l); int num_arguments = emit_num_arguments(l); int num_required = emit_num_required(l); numopt = num_arguments - num_required; if (num_arguments > 0) { String *args = NewStringEmpty(); Printf(args, "zval args[%d]", num_arguments); Wrapper_add_local(f, "args", args); Delete(args); args = NULL; } if (wrapperType == directorconstructor) { Wrapper_add_local(f, "arg0", "zval *arg0 = ZEND_THIS"); } // This generated code may be called: // 1) as an object method, or // 2) as a class-method/function (without a "this_ptr") // Option (1) has "this_ptr" for "this", option (2) needs it as // first parameter // NOTE: possible we ignore this_ptr as a param for native constructor if (numopt > 0) { // membervariable wrappers do not have optional args Wrapper_add_local(f, "arg_count", "int arg_count"); Printf(f->code, "arg_count = ZEND_NUM_ARGS();\n"); Printf(f->code, "if(arg_count<%d || arg_count>%d ||\n", num_required, num_arguments); Printf(f->code, " zend_get_parameters_array_ex(arg_count,args)!=SUCCESS)\n"); Printf(f->code, "\tWRONG_PARAM_COUNT;\n\n"); } else if (static_setter || static_getter) { if (num_arguments == 0) { Printf(f->code, "if(ZEND_NUM_ARGS() == 0) {\n"); } else { Printf(f->code, "if(ZEND_NUM_ARGS() == %d && zend_get_parameters_array_ex(%d, args) == SUCCESS) {\n", num_arguments, num_arguments); } } else { if (num_arguments == 0) { Printf(f->code, "if(ZEND_NUM_ARGS() != 0) {\n"); } else { Printf(f->code, "if(ZEND_NUM_ARGS() != %d || zend_get_parameters_array_ex(%d, args) != SUCCESS) {\n", num_arguments, num_arguments); } Printf(f->code, "WRONG_PARAM_COUNT;\n}\n\n"); } /* Now convert from PHP to C variables */ // At this point, argcount if used is the number of deliberately passed args // not including this_ptr even if it is used. // It means error messages may be out by argbase with error // reports. We can either take argbase into account when raising // errors, or find a better way of dealing with _thisptr. // I would like, if objects are wrapped, to assume _thisptr is always // _this and not the first argument. // This may mean looking at Language::memberfunctionHandler for (i = 0, p = l; i < num_arguments; i++) { /* Skip ignored arguments */ while (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); } /* Check if optional */ if (i >= num_required) { Printf(f->code, "\tif(arg_count > %d) {\n", i); } tm = Getattr(p, "tmap:in"); if (!tm) { SwigType *pt = Getattr(p, "type"); Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0)); p = nextSibling(p); continue; } phptypes->process_phptype(p, i + 1, "tmap:in:phptype"); if (GetFlag(p, "tmap:in:byref")) phptypes->set_byref(i + 1); String *source = NewStringf("args[%d]", i); Replaceall(tm, "$input", source); Setattr(p, "emit:input", source); Printf(f->code, "%s\n", tm); if (i == 0 && Getattr(p, "self")) { Printf(f->code, "\tif(!arg1) {\n"); Printf(f->code, "\t zend_throw_exception(zend_ce_type_error, \"this pointer is NULL\", 0);\n"); Printf(f->code, "\t return;\n"); Printf(f->code, "\t}\n"); } if (i >= num_required) { Printf(f->code, "}\n"); } p = Getattr(p, "tmap:in:next"); Delete(source); } if (is_member_director(n)) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); Append(f->code, "director = SWIG_DIRECTOR_CAST(arg1);\n"); Wrapper_add_local(f, "upcall", "bool upcall = false"); Printf(f->code, "upcall = (director && (director->swig_get_self()==Z_OBJ_P(ZEND_THIS)));\n"); } Swig_director_emit_dynamic_cast(n, f); /* Insert constraint checking code */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:check"))) { Printv(f->code, tm, "\n", NIL); p = Getattr(p, "tmap:check:next"); } else { p = nextSibling(p); } } /* Insert cleanup code */ for (i = 0, p = l; p; i++) { if ((tm = Getattr(p, "tmap:freearg"))) { Printv(cleanup, tm, "\n", NIL); p = Getattr(p, "tmap:freearg:next"); } else { p = nextSibling(p); } } /* Insert argument output code */ for (i = 0, p = l; p; i++) { if ((tm = Getattr(p, "tmap:argout")) && Len(tm)) { Replaceall(tm, "$result", "return_value"); Replaceall(tm, "$arg", Getattr(p, "emit:input")); Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { p = nextSibling(p); } } if (!overloaded) { Setattr(n, "wrap:name", wname); } else { Setattr(n, "wrap:name", overloadwname); } Setattr(n, "wrapper:method:name", wname); bool php_constructor = (constructor && Cmp(class_name, Getattr(n, "constructorHandler:sym:name")) == 0); /* emit function call */ String *actioncode = emit_action(n); if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { Replaceall(tm, "$input", Swig_cresult_name()); Replaceall(tm, "$result", php_constructor ? "ZEND_THIS" : "return_value"); Replaceall(tm, "$owner", newobject ? "1" : "0"); Printf(f->code, "%s\n", tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(returntype, 0), name); } emit_return_variable(n, returntype, f); List *return_types = phptypes->process_phptype(n, 0, "tmap:out:phptype"); if (class_name && !Strstr(Getattr(n, "storage"), "friend")) { if (is_member_director(n)) { String *parent = class_name; while ((parent = Getattr(php_parent_class, parent)) != NULL) { // Mark this method name as having no return type declaration for all // classes we're derived from. SetFlag(has_directed_descendent, NewStringf("%s:%s", parent, wname)); } } else if (return_types) { String *parent = class_name; while ((parent = Getattr(php_parent_class, parent)) != NULL) { String *key = NewStringf("%s:%s", parent, wname); // The parent class method needs to have a superset of the possible // return types of methods with the same name in subclasses. List *v = Getattr(parent_class_method_return_type, key); if (!v) { // New entry. Setattr(parent_class_method_return_type, key, Copy(return_types)); } else { // Update existing entry. PHPTypes::merge_type_lists(v, return_types); } } } } if (outarg) { Printv(f->code, outarg, NIL); } if (static_setter && cleanup) { Printv(f->code, cleanup, NIL); } /* Look to see if there is any newfree cleanup code */ if (GetFlag(n, "feature:new")) { if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } } /* See if there is any return cleanup code */ if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) { Printf(f->code, "%s\n", tm); Delete(tm); } if (static_getter) { Printf(f->code, "}\n"); } if (static_setter || static_getter) { Printf(f->code, "}\n"); } if (!static_setter) { Printf(f->code, "fail: SWIGUNUSED;\n"); Printv(f->code, cleanup, NIL); Printf(f->code, "return;\n"); Printf(f->code, "}\n"); } Replaceall(f->code, "$cleanup", cleanup); bool isvoid = !Cmp(returntype, "void"); Replaceall(f->code, "$isvoid", isvoid ? "1" : "0"); Replaceall(f->code, "$symname", iname); Wrapper_print(f, s_wrappers); DelWrapper(f); f = NULL; if (overloaded) { if (!Getattr(n, "sym:nextSibling")) { dispatchFunction(n, constructor); } } else { if (!static_setter) { create_command(class_name, wname, n, false, modes); } } return SWIG_OK; } /* ------------------------------------------------------------ * globalvariableHandler() * ------------------------------------------------------------ */ /* PHP doesn't support intercepting reads and writes to global variables * (nor static property reads and writes so we can't wrap them as static * properties on a dummy class) so just let SWIG do its default thing and * wrap them as name_get() and name_set(). */ //virtual int globalvariableHandler(Node *n) { //} /* ------------------------------------------------------------ * constantWrapper() * ------------------------------------------------------------ */ virtual int constantWrapper(Node *n) { String *name = GetChar(n, "name"); String *iname = GetChar(n, "sym:name"); SwigType *type = Getattr(n, "type"); String *value = Getattr(n, "value"); String *tm; if (!addSymbol(iname, n)) return SWIG_ERROR; SwigType_remember(type); String *wrapping_member_constant = Getattr(n, "memberconstantHandler:sym:name"); if (!wrapping_member_constant) { { tm = Swig_typemap_lookup("consttab", n, name, 0); Replaceall(tm, "$value", value); if (Getattr(n, "tmap:consttab:rinit")) { Printf(r_init, "%s\n", tm); } else { Printf(s_cinit, "%s\n", tm); } } { tm = Swig_typemap_lookup("classconsttab", n, name, 0); Replaceall(tm, "$class", fake_class_name()); Replaceall(tm, "$const_name", iname); Replaceall(tm, "$value", value); if (Getattr(n, "tmap:classconsttab:rinit")) { Printf(r_init, "%s\n", tm); } else { Printf(s_cinit, "%s\n", tm); } } } else { tm = Swig_typemap_lookup("classconsttab", n, name, 0); Replaceall(tm, "$class", class_name); Replaceall(tm, "$const_name", wrapping_member_constant); Replaceall(tm, "$value", value); if (Getattr(n, "tmap:classconsttab:rinit")) { Printf(r_init, "%s\n", tm); } else { Printf(s_cinit, "%s\n", tm); } } wrapperType = standard; return SWIG_OK; } /* * PHP::pragma() * * Pragma directive. * * %pragma(php) code="String" # Includes a string in the .php file * %pragma(php) include="file.php" # Includes a file in the .php file */ virtual int pragmaDirective(Node *n) { if (!ImportMode) { String *lang = Getattr(n, "lang"); String *type = Getattr(n, "name"); String *value = Getattr(n, "value"); if (Strcmp(lang, "php") == 0) { if (Strcmp(type, "code") == 0) { if (value) { Printf(pragma_code, "%s\n", value); } } else if (Strcmp(type, "include") == 0) { if (value) { Printf(pragma_incl, "include '%s';\n", value); } } else if (Strcmp(type, "phpinfo") == 0) { if (value) { Printf(pragma_phpinfo, "%s\n", value); } } else if (Strcmp(type, "version") == 0) { if (value) { pragma_version = value; } } else { Swig_warning(WARN_PHP_UNKNOWN_PRAGMA, input_file, line_number, "Unrecognized pragma <%s>.\n", type); } } } return Language::pragmaDirective(n); } /* ------------------------------------------------------------ * classDeclaration() * ------------------------------------------------------------ */ virtual int classDeclaration(Node *n) { return Language::classDeclaration(n); } /* ------------------------------------------------------------ * classHandler() * ------------------------------------------------------------ */ virtual int classHandler(Node *n) { String *symname = Getattr(n, "sym:name"); class_name = symname; base_class = NULL; destructor_action = NULL; Printf(all_cs_entry, "static const zend_function_entry class_%s_functions[] = {\n", class_name); // namespace code to introduce namespaces into wrapper classes. //if (nameSpace != NULL) //Printf(s_oinit, "INIT_CLASS_ENTRY(internal_ce, \"%s\\\\%s\", class_%s_functions);\n", nameSpace, class_name, class_name); //else Printf(s_oinit, " INIT_CLASS_ENTRY(internal_ce, \"%s%s\", class_%s_functions);\n", prefix, class_name, class_name); if (shadow) { char *rename = GetChar(n, "sym:name"); if (!addSymbol(rename, n)) return SWIG_ERROR; /* Deal with inheritance */ List *baselist = Getattr(n, "bases"); if (baselist) { Iterator base = First(baselist); while (base.item) { if (!GetFlag(base.item, "feature:ignore")) { if (!base_class) { base_class = Getattr(base.item, "sym:name"); } else { /* Warn about multiple inheritance for additional base class(es) */ String *proxyclassname = SwigType_str(Getattr(n, "classtypeobj"), 0); String *baseclassname = SwigType_str(Getattr(base.item, "name"), 0); Swig_warning(WARN_PHP_MULTIPLE_INHERITANCE, input_file, line_number, "Warning for %s, base %s ignored. Multiple inheritance is not supported in PHP.\n", proxyclassname, baseclassname); } } base = Next(base); } } } if (GetFlag(n, "feature:exceptionclass") && Getattr(n, "feature:except")) { /* PHP requires thrown objects to be instances of or derived from * Exception, so that really needs to take priority over any * explicit base class. */ if (base_class) { String *proxyclassname = SwigType_str(Getattr(n, "classtypeobj"), 0); Swig_warning(WARN_PHP_MULTIPLE_INHERITANCE, input_file, line_number, "Warning for %s, base %s ignored. Multiple inheritance is not supported in PHP.\n", proxyclassname, base_class); } base_class = NewString("Exception"); } if (!base_class) { Printf(s_oinit, " SWIG_Php_ce_%s = zend_register_internal_class(&internal_ce);\n", class_name); } else if (Equal(base_class, "Exception")) { Printf(s_oinit, " SWIG_Php_ce_%s = zend_register_internal_class_ex(&internal_ce, zend_ce_exception);\n", class_name); } else if (is_class_wrapped(base_class)) { Printf(s_oinit, " SWIG_Php_ce_%s = zend_register_internal_class_ex(&internal_ce, SWIG_Php_ce_%s);\n", class_name, base_class); Setattr(php_parent_class, class_name, base_class); } else { Printf(s_oinit, " {\n"); Printf(s_oinit, " swig_type_info *type_info = SWIG_MangledTypeQueryModule(swig_module.next, &swig_module, \"_p_%s\");\n", base_class); Printf(s_oinit, " SWIG_Php_ce_%s = zend_register_internal_class_ex(&internal_ce, (zend_class_entry*)(type_info ? type_info->clientdata : NULL));\n", class_name); Printf(s_oinit, " }\n"); } if (Getattr(n, "abstracts") && !GetFlag(n, "feature:notabstract")) { Printf(s_oinit, " SWIG_Php_ce_%s->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;\n", class_name); } if (GetFlag(n, "feature:php:allowdynamicproperties")) { Append(s_oinit, "#ifdef ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES\n"); Printf(s_oinit, " SWIG_Php_ce_%s->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;\n", class_name); Append(s_oinit, "#endif\n"); } else { Append(s_oinit, "#ifdef ZEND_ACC_NO_DYNAMIC_PROPERTIES\n"); Printf(s_oinit, " SWIG_Php_ce_%s->ce_flags |= ZEND_ACC_NO_DYNAMIC_PROPERTIES;\n", class_name); Append(s_oinit, "#endif\n"); } String *swig_wrapped = swig_wrapped_interface_ce(); Printv(s_oinit, " zend_do_implement_interface(SWIG_Php_ce_", class_name, ", &", swig_wrapped, ");\n", NIL); { Node *node = NewHash(); Setattr(node, "type", Getattr(n, "name")); Setfile(node, Getfile(n)); Setline(node, Getline(n)); String *interfaces = Swig_typemap_lookup("phpinterfaces", node, "", 0); Replaceall(interfaces, " ", ""); if (interfaces && Len(interfaces) > 0) { // It seems we need to wait until RINIT time to look up class entries // for interfaces by name. The downside is that this then happens for // every request. // // Most pre-defined interfaces are accessible via zend_class_entry* // variables declared in the PHP C API - these we can use at MINIT // time, so we special case them. This will also be a little faster // than looking up by name. Printv(s_header, "#ifdef __cplusplus\n", "extern \"C\" {\n", "#endif\n", NIL); String *r_init_prefix = NewStringEmpty(); List *interface_list = Split(interfaces, ',', -1); int num_interfaces = Len(interface_list); for (int i = 0; i < num_interfaces; ++i) { String *interface = Getitem(interface_list, i); // We generate conditional code in both minit and rinit - then we or the user // just need to define SWIG_PHP_INTERFACE_xxx_CE (and optionally // SWIG_PHP_INTERFACE_xxx_HEADER) to handle interface `xxx` at minit-time. Printv(s_header, "#ifdef SWIG_PHP_INTERFACE_", interface, "_HEADER\n", "# include SWIG_PHP_INTERFACE_", interface, "_HEADER\n", "#endif\n", NIL); Printv(s_oinit, "#ifdef SWIG_PHP_INTERFACE_", interface, "_CE\n", " zend_do_implement_interface(SWIG_Php_ce_", class_name, ", SWIG_PHP_INTERFACE_", interface, "_CE);\n", "#endif\n", NIL); Printv(r_init_prefix, "#ifndef SWIG_PHP_INTERFACE_", interface, "_CE\n", " {\n", " zend_class_entry *swig_interface_ce = zend_lookup_class(zend_string_init(\"", interface, "\", sizeof(\"", interface, "\") - 1, 0));\n", " if (swig_interface_ce)\n", " zend_do_implement_interface(SWIG_Php_ce_", class_name, ", swig_interface_ce);\n", " else\n", " zend_throw_exception(zend_ce_error, \"Interface \\\"", interface, "\\\" not found\", 0);\n", " }\n", "#endif\n", NIL); } // Handle interfaces at the start of rinit so that they're added // before any potential constant objects, etc which might be created // later in rinit. Insert(r_init, 0, r_init_prefix); Delete(r_init_prefix); Printv(s_header, "#ifdef __cplusplus\n", "}\n", "#endif\n", NIL); } Delete(interfaces); } Language::classHandler(n); static bool emitted_base_object_handlers = false; if (!emitted_base_object_handlers) { Printf(s_creation, "static zend_object_handlers Swig_Php_base_object_handlers;\n\n"); // Set up a base zend_object_handlers structure which we can use as-is // for classes without a destructor, and copy as the basis for other // classes. Printf(s_oinit, " Swig_Php_base_object_handlers = *zend_get_std_object_handlers();\n"); Printf(s_oinit, " Swig_Php_base_object_handlers.offset = XtOffsetOf(swig_object_wrapper, std);\n"); Printf(s_oinit, " Swig_Php_base_object_handlers.clone_obj = NULL;\n"); emitted_base_object_handlers = true; } Printf(s_creation, "static zend_class_entry *SWIG_Php_ce_%s;\n\n", class_name); if (Getattr(n, "has_destructor")) { if (destructor_action ? Equal(destructor_action, "free((char *) arg1);") : !CPlusPlus) { // We can use a single function if the destructor action calls free() // (either explicitly or as the default in C-mode) since free() doesn't // care about the object's type. We currently only check for the exact // code that Swig_cdestructor_call() emits. static bool emitted_common_cdestructor = false; if (!emitted_common_cdestructor) { Printf(s_creation, "static zend_object_handlers Swig_Php_common_c_object_handlers;\n\n"); Printf(s_creation, "static void SWIG_Php_common_c_free_obj(zend_object *object) {free(SWIG_Php_free_obj(object));}\n\n"); Printf(s_creation, "static zend_object *SWIG_Php_common_c_create_object(zend_class_entry *ce) {return SWIG_Php_do_create_object(ce, &Swig_Php_common_c_object_handlers);}\n"); Printf(s_oinit, " Swig_Php_common_c_object_handlers = Swig_Php_base_object_handlers;\n"); Printf(s_oinit, " Swig_Php_common_c_object_handlers.free_obj = SWIG_Php_common_c_free_obj;\n"); emitted_common_cdestructor = true; } Printf(s_oinit, " SWIG_Php_ce_%s->create_object = SWIG_Php_common_c_create_object;\n", class_name); } else { Printf(s_creation, "static zend_object_handlers %s_object_handlers;\n", class_name); Printf(s_creation, "static zend_object *SWIG_Php_create_object_%s(zend_class_entry *ce) {return SWIG_Php_do_create_object(ce, &%s_object_handlers);}\n", class_name, class_name); Printf(s_creation, "static void SWIG_Php_free_obj_%s(zend_object *object) {",class_name); String *type = Getattr(n, "classtype"); // Special case handling the delete call generated by // Swig_cppdestructor_call() and generate simpler code. if (destructor_action && !Equal(destructor_action, "delete arg1;")) { Printv(s_creation, "\n" " ", type, " *arg1 = (" , type, " *)SWIG_Php_free_obj(object);\n" " if (arg1) {\n" " ", destructor_action, "\n" " }\n", NIL); } else { Printf(s_creation, "delete (%s *)SWIG_Php_free_obj(object);", type); } Printf(s_creation, "}\n\n"); Printf(s_oinit, " SWIG_Php_ce_%s->create_object = SWIG_Php_create_object_%s;\n", class_name, class_name); Printf(s_oinit, " %s_object_handlers = Swig_Php_base_object_handlers;\n", class_name); Printf(s_oinit, " %s_object_handlers.free_obj = SWIG_Php_free_obj_%s;\n", class_name, class_name); } } else { static bool emitted_destructorless_create_object = false; if (!emitted_destructorless_create_object) { emitted_destructorless_create_object = true; Printf(s_creation, "static zend_object *SWIG_Php_create_object(zend_class_entry *ce) {return SWIG_Php_do_create_object(ce, &Swig_Php_base_object_handlers);}\n", class_name); } Printf(s_oinit, " SWIG_Php_ce_%s->create_object = SWIG_Php_create_object;\n", class_name); } // If not defined we aren't wrapping any functions which use this type as a // parameter or return value, in which case we don't need the clientdata // set. Printf(s_oinit, "#ifdef SWIGTYPE_p%s\n", SwigType_manglestr(Getattr(n, "classtypeobj"))); Printf(s_oinit, " SWIG_TypeClientData(SWIGTYPE_p%s,SWIG_Php_ce_%s);\n", SwigType_manglestr(Getattr(n, "classtypeobj")), class_name); Printf(s_oinit, "#endif\n"); Printf(s_oinit, "\n"); generate_magic_property_methods(n); Printf(all_cs_entry, " ZEND_FE_END\n};\n\n"); class_name = NULL; base_class = NULL; return SWIG_OK; } /* ------------------------------------------------------------ * memberfunctionHandler() * ------------------------------------------------------------ */ virtual int memberfunctionHandler(Node *n) { wrapperType = memberfn; Language::memberfunctionHandler(n); wrapperType = standard; return SWIG_OK; } /* ------------------------------------------------------------ * membervariableHandler() * ------------------------------------------------------------ */ virtual int membervariableHandler(Node *n) { if (magic_set == NULL) { magic_set = NewStringEmpty(); magic_get = NewStringEmpty(); magic_isset = NewStringEmpty(); } String *v_name = GetChar(n, "name"); Printf(magic_set, "\nelse if (strcmp(ZSTR_VAL(arg2),\"%s\") == 0) {\n", v_name); Printf(magic_set, "zend_string *swig_funcname = ZSTR_INIT_LITERAL(\"%s_set\", 0);\n", v_name); Append(magic_set, "zend_function *swig_zend_func = zend_std_get_method(&Z_OBJ_P(ZEND_THIS), swig_funcname, NULL);\n"); Append(magic_set, "zend_string_release(swig_funcname);\n"); Printf(magic_set, "zend_call_known_instance_method(swig_zend_func, Z_OBJ_P(ZEND_THIS), return_value, 1, &args[1]);\n"); Printf(magic_set, "}\n"); Printf(magic_get, "\nelse if (strcmp(ZSTR_VAL(arg2),\"%s\") == 0) {\n", v_name); Printf(magic_get, "zend_string *swig_funcname = ZSTR_INIT_LITERAL(\"%s_get\", 0);\n", v_name); Append(magic_get, "zend_function *swig_zend_func = zend_std_get_method(&Z_OBJ_P(ZEND_THIS), swig_funcname, NULL);\n"); Append(magic_get, "zend_string_release(swig_funcname);\n"); Printf(magic_get, "zend_call_known_instance_method(swig_zend_func, Z_OBJ_P(ZEND_THIS), return_value, 0, NULL);\n"); Printf(magic_get, "}\n"); Printf(magic_isset, "\nelse if (strcmp(ZSTR_VAL(arg2),\"%s\") == 0) {\n", v_name); Printf(magic_isset, "RETVAL_TRUE;\n}\n"); wrapperType = membervar; Language::membervariableHandler(n); wrapperType = standard; return SWIG_OK; } /* ------------------------------------------------------------ * staticmembervariableHandler() * ------------------------------------------------------------ */ virtual int staticmembervariableHandler(Node *n) { wrapperType = staticmembervar; Language::staticmembervariableHandler(n); wrapperType = standard; return SWIG_OK; } /* ------------------------------------------------------------ * staticmemberfunctionHandler() * ------------------------------------------------------------ */ virtual int staticmemberfunctionHandler(Node *n) { wrapperType = staticmemberfn; Language::staticmemberfunctionHandler(n); wrapperType = standard; return SWIG_OK; } int abstractConstructorHandler(Node *) { return SWIG_OK; } /* ------------------------------------------------------------ * constructorHandler() * ------------------------------------------------------------ */ virtual int constructorHandler(Node *n) { if (Swig_directorclass(n)) { String *ctype = GetChar(Swig_methodclass(n), "classtype"); String *sname = GetChar(Swig_methodclass(n), "sym:name"); String *args = NewStringEmpty(); ParmList *p = Getattr(n, "parms"); int i; for (i = 0; p; p = nextSibling(p), i++) { if (i) { Printf(args, ", "); } if (Strcmp(GetChar(p, "type"), SwigType_str(GetChar(p, "type"), 0))) { SwigType *t = Getattr(p, "type"); Printf(args, "%s", SwigType_rcaststr(t, 0)); if (SwigType_isreference(t)) { Append(args, "*"); } } Printf(args, "arg%d", i+1); } /* director ctor code is specific for each class */ Delete(director_ctor_code); director_ctor_code = NewStringEmpty(); director_prot_ctor_code = NewStringEmpty(); Printf(director_ctor_code, "if (Z_OBJCE_P(arg0) == SWIG_Php_ce_%s) { /* not subclassed */\n", class_name); Printf(director_prot_ctor_code, "if (Z_OBJCE_P(arg0) == SWIG_Php_ce_%s) { /* not subclassed */\n", class_name); Printf(director_ctor_code, " %s = new %s(%s);\n", Swig_cresult_name(), ctype, args); Printf(director_prot_ctor_code, " zend_throw_exception(zend_ce_type_error, \"accessing abstract class or protected constructor\", 0);\n" " return;\n"); if (i) { Insert(args, 0, ", "); } Printf(director_ctor_code, "} else {\n %s = (%s *)new SwigDirector_%s(arg0%s);\n}\n", Swig_cresult_name(), ctype, sname, args); Printf(director_prot_ctor_code, "} else {\n %s = (%s *)new SwigDirector_%s(arg0%s);\n}\n", Swig_cresult_name(), ctype, sname, args); Delete(args); wrapperType = directorconstructor; } else { wrapperType = constructor; } Language::constructorHandler(n); wrapperType = standard; return SWIG_OK; } /* ------------------------------------------------------------ * destructorHandler() * ------------------------------------------------------------ */ virtual int destructorHandler(Node *n) { wrapperType = destructor; Language::destructorHandler(n); destructor_action = Getattr(n, "wrap:action"); wrapperType = standard; return SWIG_OK; } int classDirectorInit(Node *n) { String *declaration = Swig_director_declaration(n); Printf(f_directors_h, "%s\n", declaration); Printf(f_directors_h, "public:\n"); Delete(declaration); return Language::classDirectorInit(n); } int classDirectorEnd(Node *n) { Printf(f_directors_h, "};\n"); return Language::classDirectorEnd(n); } int classDirectorConstructor(Node *n) { Node *parent = Getattr(n, "parentNode"); String *decl = Getattr(n, "decl"); String *supername = Swig_class_name(parent); String *classname = NewStringEmpty(); Printf(classname, "SwigDirector_%s", supername); /* insert self parameter */ Parm *p; ParmList *superparms = Getattr(n, "parms"); ParmList *parms = CopyParmList(superparms); String *type = NewString("zval"); SwigType_add_pointer(type); p = NewParm(type, NewString("self"), n); set_nextSibling(p, parms); parms = p; if (!Getattr(n, "defaultargs")) { // There should always be a "self" parameter first. assert(ParmList_len(parms) > 0); /* constructor */ { Wrapper *w = NewWrapper(); String *call; String *basetype = Getattr(parent, "classtype"); String *target = Swig_method_decl(0, decl, classname, parms, 0); call = Swig_csuperclass_call(0, basetype, superparms); Printf(w->def, "%s::%s: %s, Swig::Director(self) {", classname, target, call); Append(w->def, "}"); Delete(target); Wrapper_print(w, f_directors); Delete(call); DelWrapper(w); } /* constructor header */ { String *target = Swig_method_decl(0, decl, classname, parms, 1); Printf(f_directors_h, " %s;\n", target); Delete(target); } } return Language::classDirectorConstructor(n); } int classDirectorMethod(Node *n, Node *parent, String *super) { int is_void = 0; int is_pointer = 0; String *decl = Getattr(n, "decl"); SwigType *returntype = Getattr(n, "type"); String *name = Getattr(n, "name"); String *classname = Getattr(parent, "sym:name"); String *c_classname = Getattr(parent, "name"); String *symname = Getattr(n, "sym:name"); String *declaration = NewStringEmpty(); ParmList *l = Getattr(n, "parms"); Wrapper *w = NewWrapper(); String *tm; String *wrap_args = NewStringEmpty(); String *value = Getattr(n, "value"); String *storage = Getattr(n, "storage"); bool pure_virtual = false; int status = SWIG_OK; int idx; bool ignored_method = GetFlag(n, "feature:ignore") ? true : false; if (Cmp(storage, "virtual") == 0) { if (Cmp(value, "0") == 0) { pure_virtual = true; } } /* determine if the method returns a pointer */ is_pointer = SwigType_ispointer_return(decl); is_void = (Cmp(returntype, "void") == 0 && !is_pointer); /* virtual method definition */ String *target; String *pclassname = NewStringf("SwigDirector_%s", classname); String *qualified_name = NewStringf("%s::%s", pclassname, name); SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type"); target = Swig_method_decl(rtype, decl, qualified_name, l, 0); Printf(w->def, "%s", target); Delete(qualified_name); Delete(target); /* header declaration */ target = Swig_method_decl(rtype, decl, name, l, 1); Printf(declaration, " virtual %s", target); Delete(target); // Get any exception classes in the throws typemap if (Getattr(n, "noexcept")) { Append(w->def, " noexcept"); Append(declaration, " noexcept"); } ParmList *throw_parm_list = 0; if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) { Parm *p; int gencomma = 0; Append(w->def, " throw("); Append(declaration, " throw("); if (throw_parm_list) Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { if (gencomma++) { Append(w->def, ", "); Append(declaration, ", "); } String *str = SwigType_str(Getattr(p, "type"), 0); Append(w->def, str); Append(declaration, str); Delete(str); } } Append(w->def, ")"); Append(declaration, ")"); } Append(w->def, " {"); Append(declaration, ";\n"); /* declare method return value * if the return value is a reference or const reference, a specialized typemap must * handle it, including declaration of c_result ($result). */ if (!is_void && (!ignored_method || pure_virtual)) { if (!SwigType_isclass(returntype)) { if (!(SwigType_ispointer(returntype) || SwigType_isreference(returntype))) { String *construct_result = NewStringf("= SwigValueInit< %s >()", SwigType_lstr(returntype, 0)); Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), construct_result, NIL); Delete(construct_result); } else { Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), "= 0", NIL); } } else { String *cres = SwigType_lstr(returntype, "c_result"); Printf(w->code, "%s;\n", cres); Delete(cres); } } if (ignored_method) { if (!pure_virtual) { if (!is_void) Printf(w->code, "return "); String *super_call = Swig_method_call(super, l); Printf(w->code, "%s;\n", super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name)); } } else { /* attach typemaps to arguments (C/C++ -> PHP) */ Swig_director_parms_fixup(l); /* remove the wrapper 'w' since it was producing spurious temps */ Swig_typemap_attach_parms("in", l, 0); Swig_typemap_attach_parms("directorin", l, w); Swig_typemap_attach_parms("directorargout", l, w); Parm *p; /* build argument list and type conversion string */ idx = 0; p = l; while (p) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { p = Getattr(p, "tmap:in:next"); continue; } String *pname = Getattr(p, "name"); String *ptype = Getattr(p, "type"); if ((tm = Getattr(p, "tmap:directorin")) != 0) { String *parse = Getattr(p, "tmap:directorin:parse"); if (!parse) { String *input = NewStringf("&args[%d]", idx++); Setattr(p, "emit:directorinput", input); Replaceall(tm, "$input", input); Delete(input); Replaceall(tm, "$owner", "0"); Printv(wrap_args, tm, "\n", NIL); } else { Setattr(p, "emit:directorinput", pname); Replaceall(tm, "$input", pname); Replaceall(tm, "$owner", "0"); if (Len(tm) == 0) Append(tm, pname); } p = Getattr(p, "tmap:directorin:next"); continue; } else if (Cmp(ptype, "void")) { Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_NOWRAP; break; } p = nextSibling(p); } if (!idx) { Printf(w->code, "zval *args = NULL;\n"); } else { Printf(w->code, "zval args[%d];\n", idx); } // typemap_directorout testcase requires that 0 can be assigned to the // variable named after the result of Swig_cresult_name(), so that can't // be a zval - make it a pointer to one instead. Printf(w->code, "zval swig_zval_result;\n"); Printf(w->code, "zval * SWIGUNUSED %s = &swig_zval_result;\n", Swig_cresult_name()); /* wrap complex arguments to zvals */ Append(w->code, wrap_args); const char *funcname = GetChar(n, "sym:name"); Append(w->code, "{\n"); Printf(w->code, "zend_string *swig_funcname = zend_string_init(\"%s\", %d, 0);\n", funcname, strlen(funcname)); Append(w->code, "zend_function *swig_zend_func = zend_std_get_method(&Z_OBJ(swig_self), swig_funcname, NULL);\n"); Append(w->code, "zend_string_release(swig_funcname);\n"); Printf(w->code, "if (swig_zend_func) zend_call_known_instance_method(swig_zend_func, Z_OBJ(swig_self), &swig_zval_result, %d, args);\n", idx); /* exception handling */ tm = Swig_typemap_lookup("director:except", n, Swig_cresult_name(), 0); if (!tm) { tm = Getattr(n, "feature:director:except"); if (tm) tm = Copy(tm); } if (!tm || Len(tm) == 0 || Equal(tm, "1")) { // Skip marshalling the return value as there isn't one. tm = NewString("if ($error) SWIG_fail;"); } Replaceall(tm, "$error", "EG(exception)"); Printv(w->code, Str(tm), "\n}\n{\n", NIL); Delete(tm); /* marshal return value from PHP to C/C++ type */ String *cleanup = NewStringEmpty(); String *outarg = NewStringEmpty(); idx = 0; /* marshal return value */ if (!is_void) { tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w); if (tm != 0) { Replaceall(tm, "$input", Swig_cresult_name()); char temp[24]; sprintf(temp, "%d", idx); Replaceall(tm, "$argnum", temp); /* TODO check this */ if (Getattr(n, "wrap:disown")) { Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); } else { Replaceall(tm, "$disown", "0"); } Replaceall(tm, "$result", "c_result"); Printv(w->code, tm, "\n", NIL); Delete(tm); } else { Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), SwigType_namestr(name)); status = SWIG_ERROR; } } /* marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { Replaceall(tm, "$result", Swig_cresult_name()); Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); Printv(w->code, tm, "\n", NIL); p = Getattr(p, "tmap:directorargout:next"); } else { p = nextSibling(p); } } Append(w->code, "}\n"); Delete(cleanup); Delete(outarg); } Append(w->code, "fail: SWIGUNUSED;\n"); if (!is_void) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { Printf(w->code, "return (%s) c_result;\n", rettype); } else { Printf(w->code, "return (%s) *c_result;\n", rettype); } Delete(rettype); } } Append(w->code, "}\n"); // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method String *inline_extra_method = NewStringEmpty(); if (dirprot_mode() && !is_public(n) && !pure_virtual) { Printv(inline_extra_method, declaration, NIL); String *extra_method_name = NewStringf("%sSwigPublic", name); Replaceall(inline_extra_method, name, extra_method_name); Replaceall(inline_extra_method, ";\n", " {\n "); if (!is_void) Printf(inline_extra_method, "return "); String *methodcall = Swig_method_call(super, l); Printv(inline_extra_method, methodcall, ";\n }\n", NIL); Delete(methodcall); Delete(extra_method_name); } /* emit the director method */ if (status == SWIG_OK) { Replaceall(w->code, "$isvoid", is_void ? "1" : "0"); if (!Getattr(n, "defaultargs")) { Replaceall(w->code, "$symname", symname); Wrapper_print(w, f_directors); Printv(f_directors_h, declaration, NIL); Printv(f_directors_h, inline_extra_method, NIL); } } /* clean up */ Delete(wrap_args); Delete(pclassname); DelWrapper(w); return status; } int classDirectorDisown(Node *n) { wrapperType = directordisown; int result = Language::classDirectorDisown(n); wrapperType = standard; return result; } }; /* class PHP */ static PHP *maininstance = 0; List *PHPTypes::process_phptype(Node *n, int key, const String_or_char *attribute_name) { while (Len(merged_types) <= key) { Append(merged_types, NewList()); } String *phptype = Getattr(n, attribute_name); if (!phptype || Len(phptype) == 0) { // There's no type declaration, so any merged version has no type declaration. // // Use a DOH None object as a marker to indicate there's no type // declaration for this parameter/return value (you can't store NULL as a // value in a DOH List). Setitem(merged_types, key, None); return NULL; } DOH *merge_list = Getitem(merged_types, key); if (merge_list == None) return NULL; List *types = Split(phptype, '|', -1); String *first_type = Getitem(types, 0); if (Char(first_type)[0] == '?') { if (Len(types) > 1) { Printf(stderr, "warning: Invalid phptype: '%s' (can't use ? and | together)\n", phptype); } // Treat `?foo` just like `foo|null`. Append(types, "null"); Setitem(types, 0, NewString(Char(first_type) + 1)); } SortList(types, NULL); String *prev = NULL; for (Iterator i = First(types); i.item; i = Next(i)) { if (prev && Equal(prev, i.item)) { Printf(stderr, "warning: Invalid phptype: '%s' (duplicate entry for '%s')\n", phptype, i.item); continue; } if (key > 0 && Equal(i.item, "void")) { // Reject void for parameter type. Printf(stderr, "warning: Invalid phptype: '%s' ('%s' can't be used as a parameter phptype)\n", phptype, i.item); continue; } if (Equal(i.item, "SWIGTYPE")) { String *type = Getattr(n, "type"); Node *class_node = maininstance->classLookup(type); if (class_node) { // FIXME: Prefix classname with a backslash to prevent collisions // with built-in types? Or are non of those valid anyway and so will // have been renamed at this point? Append(merge_list, Getattr(class_node, "sym:name")); } else { // SWIG wraps a pointer to a non-object type as an object in a PHP // class named based on the SWIG-mangled C/C++ type. // // FIXME: We should check this is actually a known pointer to // non-object type so we complain about `phptype="SWIGTYPE"` being // used for PHP types like `int` or `string` (currently this only // fails at runtime and the error isn't very helpful). We could // check the condition // // raw_pointer_types && Getattr(raw_pointer_types, SwigType_manglestr(type)) // // except that raw_pointer_types may not have been fully filled in when // we are called. Append(merge_list, NewStringf("SWIG\\%s", SwigType_manglestr(type))); } } else { Append(merge_list, i.item); } prev = i.item; } SortList(merge_list, NULL); return merge_list; } void PHPTypes::merge_type_lists(List *merge_list, List *o_merge_list) { int i = 0, j = 0; while (j < Len(o_merge_list)) { String *candidate = Getitem(o_merge_list, j); while (i < Len(merge_list)) { int cmp = Cmp(Getitem(merge_list, i), candidate); if (cmp == 0) goto handled; if (cmp > 0) break; ++i; } Insert(merge_list, i, candidate); ++i; handled: ++j; } } void PHPTypes::merge_from(const PHPTypes* o) { num_required = std::min(num_required, o->num_required); if (o->byref) { if (byref == NULL) { byref = Copy(o->byref); } else { int len = std::min(Len(byref), Len(o->byref)); // Start at 1 because we only want to merge parameter types, and key 0 is // the return type. for (int key = 1; key < len; ++key) { if (Getitem(byref, key) == None && Getitem(o->byref, key) != None) { Setitem(byref, key, ""); } } for (int key = len; key < Len(o->byref); ++key) { Append(byref, Getitem(o->byref, key)); } } } int len = std::min(Len(merged_types), Len(o->merged_types)); for (int key = 0; key < len; ++key) { DOH *merge_list = Getitem(merged_types, key); // None trumps anything else in the merge. if (merge_list == None) continue; DOH *o_merge_list = Getitem(o->merged_types, key); if (o_merge_list == None) { Setitem(merged_types, key, None); continue; } merge_type_lists(merge_list, o_merge_list); } // Copy over any additional entries. for (int key = len; key < Len(o->merged_types); ++key) { Append(merged_types, Copy(Getitem(o->merged_types, key))); } } // Collect non-class pointer types from the type table so we can set up PHP // classes for them later. // // NOTE: it's a function NOT A PHP::METHOD extern "C" { static void typetrace(const SwigType *ty, String *mangled, String *clientdata) { if (maininstance->classLookup(ty) == NULL) { // a non-class pointer if (!raw_pointer_types) { raw_pointer_types = NewHash(); } Setattr(raw_pointer_types, mangled, mangled); } if (r_prevtracefunc) (*r_prevtracefunc) (ty, mangled, clientdata); } } /* ----------------------------------------------------------------------------- * new_swig_php() - Instantiate module * ----------------------------------------------------------------------------- */ static Language *new_swig_php() { maininstance = new PHP; if (!r_prevtracefunc) { r_prevtracefunc = SwigType_remember_trace(typetrace); } else { Printf(stderr, "php Typetrace vector already saved!\n"); assert(0); } return maininstance; } extern "C" Language *swig_php(void) { return new_swig_php(); } swig-4.4.0/Source/Makefile.am0000664000175000017500000001161415075443613015657 0ustar williamwilliam## Process this file with automake to produce Makefile.in # subdir-objects generates object files using the directory structure of the source files. AUTOMAKE_OPTIONS = foreign nostdinc subdir-objects 1.7.2 SOURCE_DIR=$(top_srcdir)/Source BUILD_SOURCE_DIR=$(top_builddir)/Source AM_CPPFLAGS = -I$(BUILD_SOURCE_DIR)/Include \ -I$(BUILD_SOURCE_DIR)/CParse \ -I$(SOURCE_DIR)/Include \ -I$(SOURCE_DIR)/DOH \ -I$(SOURCE_DIR)/CParse \ -I$(SOURCE_DIR)/Doxygen \ -I$(SOURCE_DIR)/Preprocessor \ -I$(SOURCE_DIR)/Swig \ -I$(SOURCE_DIR)/Modules AM_CXXFLAGS = AM_YFLAGS = -d -Wall -Werror BUILT_SOURCES = CParse/parser.c CParse/parser.h eswig_SOURCES = CParse/cscanner.c \ CParse/parser.c \ CParse/templ.c \ CParse/util.c \ DOH/base.c \ DOH/file.c \ DOH/fio.c \ DOH/hash.c \ DOH/list.c \ DOH/memory.c \ DOH/string.c \ DOH/void.c \ Doxygen/csharpdoc.cxx \ Doxygen/csharpdoc.h \ Doxygen/doxyentity.cxx \ Doxygen/doxyentity.h \ Doxygen/doxyparser.cxx \ Doxygen/doxyparser.h \ Doxygen/doxytranslator.cxx \ Doxygen/doxytranslator.h \ Doxygen/javadoc.cxx \ Doxygen/javadoc.h \ Doxygen/pydoc.cxx \ Doxygen/pydoc.h \ Modules/allocate.cxx \ Modules/contract.cxx \ Modules/c.cxx \ Modules/csharp.cxx \ Modules/d.cxx \ Modules/directors.cxx \ Modules/emit.cxx \ Modules/go.cxx \ Modules/guile.cxx \ Modules/interface.cxx \ Modules/java.cxx \ Modules/javascript.cxx \ Modules/lang.cxx \ Modules/lua.cxx \ Modules/main.cxx \ Modules/nested.cxx \ Modules/ocaml.cxx \ Modules/octave.cxx \ Modules/overload.cxx \ Modules/perl5.cxx \ Modules/php.cxx \ Modules/python.cxx \ Modules/r.cxx \ Modules/ruby.cxx \ Modules/scilab.cxx \ Modules/swigmain.cxx \ Modules/tcl8.cxx \ Modules/typepass.cxx \ Modules/utils.cxx \ Modules/xml.cxx \ Preprocessor/cpp.c \ Preprocessor/expr.c \ Swig/cwrap.c \ Swig/deprecate.c \ Swig/error.c \ Swig/extend.c \ Swig/fragment.c \ Swig/getopt.c \ Swig/include.c \ Swig/misc.c \ Swig/naming.c \ Swig/parms.c \ Swig/scanner.c \ Swig/stype.c \ Swig/symbol.c \ Swig/tree.c \ Swig/typemap.c \ Swig/typeobj.c \ Swig/typesys.c \ Swig/wrapfunc.c bin_PROGRAMS = eswig # Use GNU make's grouped targets to avoid repeated bison execution for # each target. CParse/parser.c CParse/parser.h &: CParse/parser.y $(BISON) $(AM_YFLAGS) $(YFLAGS) --output=CParse/parser.c $(srcdir)/CParse/parser.y # The executable is copied to the root directory for installation and running the test-suite. # This occurs on each invocation of make and is a step towards providing support for multiple # build directories. all-local: eswig@EXEEXT@ cp -f $(top_builddir)/Source/eswig@EXEEXT@ $(top_builddir)/swig@EXEEXT@ clean-local: rm -f $(top_builddir)/swig@EXEEXT@ rm -f core @EXTRA_CLEAN@ distclean-local: rm -f $(top_builddir)/Source/Include/swigconfig.h rm -f $(top_builddir)/Source/Include/stamp-h1 # Beautify the code. # Note that this works well on C code, but does some odd joining of lines for C++ code. # Compiling with -DNDEBUG and no optimisations will allow one to do a binary diff of the # swig executable as a way of checking before and after the 'beautifying'. # Single files can be beautified with the beautify-file target, eg: 'make beautify-file INDENTFILE=chosenfile.c' SWIGTYPEDEFS=-T bool -T File -T DohObjInfo -T Parm -T Language -T List -T TargetLanguageModule -T Typetab -T ModuleFactory -T ErrorMessageFormat -T Symtab -T Hash -T Scanner -T String -T DohBase -T Node -T String_or_char -T SwigType -T Dispatcher -T Wrapper -T DohStringMethods -T DohFileMethods -T DohListMethods -T DohHashMethods -T DOH -T DohIterator -T ParmList -T FILE -T HashNode -T DOHObj_or_char -T DOHFile -T DOHString -T DOHString_or_char -T UpcallData -T DoxygenEntity -T string INDENTBAKSDIR=../IndentBaks beautify: rm -rf $(INDENTBAKSDIR) mkdir $(INDENTBAKSDIR) mkdir $(INDENTBAKSDIR)/CParse mkdir $(INDENTBAKSDIR)/DOH mkdir $(INDENTBAKSDIR)/Modules mkdir $(INDENTBAKSDIR)/Preprocessor mkdir $(INDENTBAKSDIR)/Swig mkdir $(INDENTBAKSDIR)/Include (csources=`find . -name "*.c"` && \ hsources=`find . -name "*.h"` && \ cxxsources=`find . -name "*.cxx"` && \ for file in $$csources $$hsources $$cxxsources; do \ $(MAKE) beautify-file INDENTFILE=$$file; \ done; ) beautify-file: test -e $(INDENTBAKSDIR) || (echo $(INDENTBAKSDIR) directory does not exist && exit 1;) test -n "$(INDENTFILE)" || (echo INDENTFILE not defined && exit 1;) test -e $(INDENTFILE) || (echo File does not exist: $(INDENTFILE) && exit 1;) cp $(INDENTFILE) $(INDENTBAKSDIR)/$(INDENTFILE); indent -kr --honour-newlines --line-length160 --indent-level2 --braces-on-func-def-line --leave-optional-blank-lines $(SWIGTYPEDEFS) $(INDENTFILE) -o $(INDENTFILE).tmp; cat $(INDENTFILE).tmp | sed -e 's/const const /const /' > $(INDENTFILE); rm $(INDENTFILE).tmp; swig-4.4.0/Source/Include/0000775000175000017500000000000015075470612015202 5ustar williamwilliamswig-4.4.0/Source/Include/swigwarn.h0000664000175000017500000003502515075443613017222 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * swigwarn.h * * SWIG warning message numbers * This file serves as the main registry of warning message numbers. Some of these * numbers are used internally in the C/C++ source code of SWIG. However, some * of the numbers are used in SWIG configuration files (swig.swg and others). * * The numbers are roughly organized into a few different classes by functionality. * * Even though symbolic constants are used in the SWIG source, this is * not always the case in SWIG interface files. Do not change the * numbers in this file. * * This file is used as the input for generating Lib/swigwarn.swg. * ----------------------------------------------------------------------------- */ #ifndef SWIG_SWIGWARN_H #define SWIG_SWIGWARN_H #define WARN_NONE 0 /* -- Deprecated features -- */ /* Unused since 4.2.0: #define WARN_DEPRECATED_EXTERN 101 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_VAL 102 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_OUT 103 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_DISABLEDOC 104 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_ENABLEDOC 105 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_DOCONLY 106 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_STYLE 107 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_LOCALSTYLE 108 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_TITLE 109 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_SECTION 110 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_SUBSECTION 111 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_SUBSUBSECTION 112 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_ADDMETHODS 113 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_READONLY 114 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_READWRITE 115 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_EXCEPT 116 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_NEW 117 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_EXCEPT_TM 118 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_IGNORE_TM 119 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_OPTC 120 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_NAME 121 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_NOEXTERN 122 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_NODEFAULT 123 */ /* Unused since 4.1.0: #define WARN_DEPRECATED_TYPEMAP_LANG 124 */ /* Unused since 4.2.0: #define WARN_DEPRECATED_INPUT_FILE 125 */ /* Unused since 4.3.0: #define WARN_DEPRECATED_NESTED_WORKAROUND 126 */ /* -- Preprocessor -- */ #define WARN_PP_MISSING_FILE 201 #define WARN_PP_EVALUATION 202 #define WARN_PP_INCLUDEALL_IMPORTALL 203 #define WARN_PP_CPP_WARNING 204 #define WARN_PP_CPP_ERROR 205 #define WARN_PP_UNEXPECTED_TOKENS 206 /* -- C/C++ Parser -- */ #define WARN_PARSE_CLASS_KEYWORD 301 #define WARN_PARSE_REDEFINED 302 #define WARN_PARSE_EXTEND_UNDEF 303 #define WARN_PARSE_UNSUPPORTED_VALUE 304 #define WARN_PARSE_BAD_VALUE 305 /* Unused since 1.3.32: #define WARN_PARSE_PRIVATE 306 */ /* Unused since 4.2.0: #define WARN_PARSE_BAD_DEFAULT 307 */ #define WARN_PARSE_NAMESPACE_ALIAS 308 #define WARN_PARSE_PRIVATE_INHERIT 309 /* Unused since 1.3.18: #define WARN_PARSE_TEMPLATE_REPEAT 310 */ /* Unused since 1.3.18: #define WARN_PARSE_TEMPLATE_PARTIAL 311 */ #define WARN_PARSE_UNNAMED_NESTED_CLASS 312 #define WARN_PARSE_UNDEFINED_EXTERN 313 #define WARN_PARSE_KEYWORD 314 #define WARN_PARSE_USING_UNDEF 315 /* Unused since 1.3.18: #define WARN_PARSE_MODULE_REPEAT 316 */ #define WARN_PARSE_TEMPLATE_SP_UNDEF 317 #define WARN_PARSE_TEMPLATE_AMBIG 318 #define WARN_PARSE_NO_ACCESS 319 #define WARN_PARSE_EXPLICIT_TEMPLATE 320 #define WARN_PARSE_BUILTIN_NAME 321 #define WARN_PARSE_REDUNDANT 322 #define WARN_PARSE_REC_INHERITANCE 323 #define WARN_PARSE_NESTED_TEMPLATE 324 #define WARN_PARSE_NAMED_NESTED_CLASS 325 #define WARN_PARSE_EXTEND_NAME 326 #define WARN_PARSE_EXTERN_TEMPLATE 327 #define WARN_PARSE_ASSIGNED_VALUE 328 #define WARN_PARSE_USING_CONSTRUCTOR 329 #define WARN_PARSE_TEMPLATE_FORWARD 330 #define WARN_PARSE_TEMPLATE_NESTED 331 #define WARN_CPP11_LAMBDA 340 /* Unused since 3.0.11: #define WARN_CPP11_ALIAS_DECLARATION 341 */ /* Unused since 3.0.11: #define WARN_CPP11_ALIAS_TEMPLATE 342 */ /* Unused since 4.2.0: #define WARN_CPP11_VARIADIC_TEMPLATE 343 */ #define WARN_CPP11_DECLTYPE 344 #define WARN_CPP14_AUTO 345 #define WARN_CPP11_AUTO 346 #define WARN_IGNORE_OPERATOR_NEW 350 /* new */ #define WARN_IGNORE_OPERATOR_DELETE 351 /* delete */ #define WARN_IGNORE_OPERATOR_PLUS 352 /* + */ #define WARN_IGNORE_OPERATOR_MINUS 353 /* - */ #define WARN_IGNORE_OPERATOR_MUL 354 /* * */ #define WARN_IGNORE_OPERATOR_DIV 355 /* / */ #define WARN_IGNORE_OPERATOR_MOD 356 /* % */ #define WARN_IGNORE_OPERATOR_XOR 357 /* ^ */ #define WARN_IGNORE_OPERATOR_AND 358 /* & */ #define WARN_IGNORE_OPERATOR_OR 359 /* | */ #define WARN_IGNORE_OPERATOR_NOT 360 /* ~ */ #define WARN_IGNORE_OPERATOR_LNOT 361 /* ! */ #define WARN_IGNORE_OPERATOR_EQ 362 /* = */ #define WARN_IGNORE_OPERATOR_LT 363 /* < */ #define WARN_IGNORE_OPERATOR_GT 364 /* > */ #define WARN_IGNORE_OPERATOR_PLUSEQ 365 /* += */ #define WARN_IGNORE_OPERATOR_MINUSEQ 366 /* -= */ #define WARN_IGNORE_OPERATOR_MULEQ 367 /* *= */ #define WARN_IGNORE_OPERATOR_DIVEQ 368 /* /= */ #define WARN_IGNORE_OPERATOR_MODEQ 369 /* %= */ #define WARN_IGNORE_OPERATOR_XOREQ 370 /* ^= */ #define WARN_IGNORE_OPERATOR_ANDEQ 371 /* &= */ #define WARN_IGNORE_OPERATOR_OREQ 372 /* |= */ #define WARN_IGNORE_OPERATOR_LSHIFT 373 /* << */ #define WARN_IGNORE_OPERATOR_RSHIFT 374 /* >> */ #define WARN_IGNORE_OPERATOR_LSHIFTEQ 375 /* <<= */ #define WARN_IGNORE_OPERATOR_RSHIFTEQ 376 /* >>= */ #define WARN_IGNORE_OPERATOR_EQUALTO 377 /* == */ #define WARN_IGNORE_OPERATOR_NOTEQUAL 378 /* != */ #define WARN_IGNORE_OPERATOR_LTEQUAL 379 /* <= */ #define WARN_IGNORE_OPERATOR_GTEQUAL 380 /* >= */ #define WARN_IGNORE_OPERATOR_LAND 381 /* && */ #define WARN_IGNORE_OPERATOR_LOR 382 /* || */ #define WARN_IGNORE_OPERATOR_PLUSPLUS 383 /* ++ */ #define WARN_IGNORE_OPERATOR_MINUSMINUS 384 /* -- */ #define WARN_IGNORE_OPERATOR_COMMA 385 /* , */ #define WARN_IGNORE_OPERATOR_ARROWSTAR 386 /* ->* */ #define WARN_IGNORE_OPERATOR_ARROW 387 /* -> */ #define WARN_IGNORE_OPERATOR_CALL 388 /* () */ #define WARN_IGNORE_OPERATOR_INDEX 389 /* [] */ #define WARN_IGNORE_OPERATOR_UPLUS 390 /* + */ #define WARN_IGNORE_OPERATOR_UMINUS 391 /* - */ #define WARN_IGNORE_OPERATOR_UMUL 392 /* * */ #define WARN_IGNORE_OPERATOR_UAND 393 /* & */ #define WARN_IGNORE_OPERATOR_NEWARR 394 /* new [] */ #define WARN_IGNORE_OPERATOR_DELARR 395 /* delete [] */ #define WARN_IGNORE_OPERATOR_REF 396 /* operator *() */ #define WARN_IGNORE_OPERATOR_LTEQUALGT 397 /* <=> */ /* please leave 350-399 free for WARN_IGNORE_OPERATOR_* */ /* -- Type system and typemaps -- */ #define WARN_TYPE_UNDEFINED_CLASS 401 #define WARN_TYPE_INCOMPLETE 402 #define WARN_TYPE_ABSTRACT 403 #define WARN_TYPE_REDEFINED 404 #define WARN_TYPE_RVALUE_REF_QUALIFIER_IGNORED 405 #define WARN_TYPE_NSPACE_SETTING 406 /* Unused since 4.1.0: #define WARN_TYPEMAP_SOURCETARGET 450 */ #define WARN_TYPEMAP_CHARLEAK 451 /* Unused since 1.3.32: #define WARN_TYPEMAP_SWIGTYPE 452 */ #define WARN_TYPEMAP_APPLY_UNDEF 453 #define WARN_TYPEMAP_SWIGTYPELEAK 454 #define WARN_TYPEMAP_WCHARLEAK 455 #define WARN_TYPEMAP_IN_UNDEF 460 #define WARN_TYPEMAP_OUT_UNDEF 461 #define WARN_TYPEMAP_VARIN_UNDEF 462 #define WARN_TYPEMAP_VAROUT_UNDEF 463 #define WARN_TYPEMAP_CONST_UNDEF 464 #define WARN_TYPEMAP_UNDEF 465 #define WARN_TYPEMAP_VAR_UNDEF 466 #define WARN_TYPEMAP_TYPECHECK 467 #define WARN_TYPEMAP_THROW 468 #define WARN_TYPEMAP_DIRECTORIN_UNDEF 469 #define WARN_TYPEMAP_THREAD_UNSAFE 470 /* mostly used in directorout typemaps */ #define WARN_TYPEMAP_DIRECTOROUT_UNDEF 471 #define WARN_TYPEMAP_TYPECHECK_UNDEF 472 #define WARN_TYPEMAP_DIRECTOROUT_PTR 473 #define WARN_TYPEMAP_OUT_OPTIMAL_IGNORED 474 #define WARN_TYPEMAP_OUT_OPTIMAL_MULTIPLE 475 #define WARN_TYPEMAP_INITIALIZER_LIST 476 #define WARN_TYPEMAP_DIRECTORTHROWS_UNDEF 477 /* -- Fragments -- */ #define WARN_FRAGMENT_NOT_FOUND 490 /* -- General code generation -- */ #define WARN_LANG_OVERLOAD_DECL 501 #define WARN_LANG_OVERLOAD_CONSTRUCT 502 #define WARN_LANG_IDENTIFIER 503 #define WARN_LANG_RETURN_TYPE 504 #define WARN_LANG_VARARGS 505 #define WARN_LANG_VARARGS_KEYWORD 506 #define WARN_LANG_NATIVE_UNIMPL 507 #define WARN_LANG_DEREF_SHADOW 508 #define WARN_LANG_OVERLOAD_SHADOW 509 #define WARN_LANG_FRIEND_IGNORE 510 /* No longer issued */ #define WARN_LANG_OVERLOAD_KEYWORD 511 #define WARN_LANG_OVERLOAD_CONST 512 #define WARN_LANG_CLASS_UNNAMED 513 #define WARN_LANG_DIRECTOR_VDESTRUCT 514 #define WARN_LANG_DISCARD_CONST 515 #define WARN_LANG_OVERLOAD_IGNORED 516 #define WARN_LANG_DIRECTOR_ABSTRACT 517 #define WARN_LANG_PORTABILITY_FILENAME 518 #define WARN_LANG_TEMPLATE_METHOD_IGNORE 519 #define WARN_LANG_SMARTPTR_MISSING 520 #define WARN_LANG_ILLEGAL_DESTRUCTOR 521 #define WARN_LANG_EXTEND_CONSTRUCTOR 522 #define WARN_LANG_EXTEND_DESTRUCTOR 523 #define WARN_LANG_EXPERIMENTAL 524 #define WARN_LANG_DIRECTOR_FINAL 525 #define WARN_LANG_USING_NAME_DIFFERENT 526 #define WARN_LANG_DEPRECATED 527 /* -- Doxygen comments -- */ #define WARN_DOXYGEN_UNKNOWN_COMMAND 560 #define WARN_DOXYGEN_UNEXPECTED_END_OF_COMMENT 561 #define WARN_DOXYGEN_COMMAND_EXPECTED 562 #define WARN_DOXYGEN_HTML_ERROR 563 #define WARN_DOXYGEN_COMMAND_ERROR 564 #define WARN_DOXYGEN_UNKNOWN_CHARACTER 565 #define WARN_DOXYGEN_UNEXPECTED_ITERATOR_VALUE 566 /* -- Reserved (600-699) -- */ /* -- Language module specific warnings (700 - 899) -- */ /* Feel free to claim any number in this space that's not currently being used. Just make sure you add an entry here */ #define WARN_D_TYPEMAP_CTYPE_UNDEF 700 #define WARN_D_TYPEMAP_IMTYPE_UNDEF 701 #define WARN_D_TYPEMAP_DTYPE_UNDEF 702 #define WARN_D_MULTIPLE_INHERITANCE 703 #define WARN_D_TYPEMAP_CLASSMOD_UNDEF 704 #define WARN_D_TYPEMAP_DBODY_UNDEF 705 #define WARN_D_TYPEMAP_DOUT_UNDEF 706 #define WARN_D_TYPEMAP_DIN_UNDEF 707 #define WARN_D_TYPEMAP_DDIRECTORIN_UNDEF 708 #define WARN_D_TYPEMAP_DCONSTRUCTOR_UNDEF 709 #define WARN_D_EXCODE_MISSING 710 #define WARN_D_CANTHROW_MISSING 711 #define WARN_D_NO_DIRECTORCONNECT_ATTR 712 #define WARN_D_NAME_COLLISION 713 /* please leave 700-719 free for D */ #define WARN_SCILAB_TRUNCATED_NAME 720 /* please leave 720-739 free for Scilab */ #define WARN_PYTHON_INDENT_MISMATCH 740 /* please leave 740-749 free for Python */ /* Unused since 4.2.0: #define WARN_R_MISSING_RTYPECHECK_TYPEMAP 750 */ #define WARN_R_TYPEMAP_RTYPECHECK_UNDEF 751 /* please leave 750-759 free for R */ #define WARN_C_TYPEMAP_CTYPE_UNDEF 760 #define WARN_C_UNSUPPORTTED 761 /* please leave 760-779 free for C */ #define WARN_RUBY_WRONG_NAME 801 #define WARN_RUBY_MULTIPLE_INHERITANCE 802 /* please leave 800-809 free for Ruby */ #define WARN_JAVA_TYPEMAP_JNI_UNDEF 810 #define WARN_JAVA_TYPEMAP_JTYPE_UNDEF 811 #define WARN_JAVA_TYPEMAP_JSTYPE_UNDEF 812 #define WARN_JAVA_MULTIPLE_INHERITANCE 813 #define WARN_JAVA_TYPEMAP_GETCPTR_UNDEF 814 #define WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF 815 #define WARN_JAVA_TYPEMAP_JAVABODY_UNDEF 816 #define WARN_JAVA_TYPEMAP_JAVAOUT_UNDEF 817 #define WARN_JAVA_TYPEMAP_JAVAIN_UNDEF 818 #define WARN_JAVA_TYPEMAP_JAVADIRECTORIN_UNDEF 819 #define WARN_JAVA_TYPEMAP_JAVADIRECTOROUT_UNDEF 820 #define WARN_JAVA_TYPEMAP_INTERFACECODE_UNDEF 821 #define WARN_JAVA_COVARIANT_RET 822 #define WARN_JAVA_TYPEMAP_JAVACONSTRUCT_UNDEF 823 #define WARN_JAVA_TYPEMAP_DIRECTORIN_NODESC 824 #define WARN_JAVA_NO_DIRECTORCONNECT_ATTR 825 #define WARN_JAVA_NSPACE_WITHOUT_PACKAGE 826 #define WARN_JAVA_TYPEMAP_INTERFACEMODIFIERS_UNDEF 827 /* please leave 810-829 free for Java */ #define WARN_CSHARP_TYPEMAP_CTYPE_UNDEF 830 #define WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF 831 #define WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF 832 #define WARN_CSHARP_MULTIPLE_INHERITANCE 833 #define WARN_CSHARP_TYPEMAP_GETCPTR_UNDEF 834 #define WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF 835 #define WARN_CSHARP_TYPEMAP_CSBODY_UNDEF 836 #define WARN_CSHARP_TYPEMAP_CSOUT_UNDEF 837 #define WARN_CSHARP_TYPEMAP_CSIN_UNDEF 838 #define WARN_CSHARP_TYPEMAP_CSDIRECTORIN_UNDEF 839 #define WARN_CSHARP_TYPEMAP_CSDIRECTOROUT_UNDEF 840 #define WARN_CSHARP_TYPEMAP_INTERFACECODE_UNDEF 841 #define WARN_CSHARP_COVARIANT_RET 842 #define WARN_CSHARP_TYPEMAP_CSCONSTRUCT_UNDEF 843 #define WARN_CSHARP_EXCODE 844 #define WARN_CSHARP_CANTHROW 845 #define WARN_CSHARP_NO_DIRECTORCONNECT_ATTR 846 #define WARN_CSHARP_TYPEMAP_INTERFACEMODIFIERS_UNDEF 847 /* please leave 830-849 free for C# */ /* 850-860 were used by Modula 3 (removed in SWIG 4.1.0) - avoid reusing for now */ #define WARN_PHP_MULTIPLE_INHERITANCE 870 #define WARN_PHP_UNKNOWN_PRAGMA 871 /* Unused since 4.1.0: define WARN_PHP_PUBLIC_BASE 872 */ /* please leave 870-889 free for PHP */ #define WARN_GO_NAME_CONFLICT 890 /* please leave 890-899 free for Go */ /* -- User defined warnings (900 - 999) -- */ #endif swig-4.4.0/Source/Include/swigconfig.h.in0000664000175000017500000000507315075470576020135 0ustar williamwilliam/* Source/Include/swigconfig.h.in. Generated from configure.ac by autoheader. */ /* define if the Boost library is available */ #undef HAVE_BOOST /* define if the compiler supports basic C++11 syntax */ #undef HAVE_CXX11 /* define if the compiler supports basic C++14 syntax */ #undef HAVE_CXX14 /* define if the compiler supports basic C++17 syntax */ #undef HAVE_CXX17 /* define if the compiler supports basic C++20 syntax */ #undef HAVE_CXX20 /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H /* Define to 1 if you have the `dl' library (-ldl). */ #undef HAVE_LIBDL /* Define to 1 if you have the `dld' library (-ldld). */ #undef HAVE_LIBDLD /* Define if you have PCRE2 library */ #undef HAVE_PCRE /* Define to 1 if you have the header file. */ #undef HAVE_STDINT_H /* Define to 1 if you have the header file. */ #undef HAVE_STDIO_H /* Define to 1 if you have the header file. */ #undef HAVE_STDLIB_H /* Define to 1 if you have the header file. */ #undef HAVE_STRINGS_H /* Define to 1 if you have the header file. */ #undef HAVE_STRING_H /* Define to 1 if you have the header file. */ #undef HAVE_SYS_STAT_H /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TYPES_H /* Define to 1 if you have the header file. */ #undef HAVE_UNISTD_H /* Name of package */ #undef PACKAGE /* Define to the address where bug reports for this package should be sent. */ #undef PACKAGE_BUGREPORT /* Define to the full name of this package. */ #undef PACKAGE_NAME /* Define to the full name and version of this package. */ #undef PACKAGE_STRING /* Define to the one symbol short name of this package. */ #undef PACKAGE_TARNAME /* Define to the home page for this package. */ #undef PACKAGE_URL /* Define to the version of this package. */ #undef PACKAGE_VERSION /* Define to 1 if all of the C90 standard headers exist (not just the ones required in a freestanding environment). This macro is provided for backward compatibility; new code need not use it. */ #undef STDC_HEADERS /* Compiler that built SWIG */ #undef SWIG_CXX /* Directory for SWIG system-independent libraries */ #undef SWIG_LIB /* Directory for SWIG system-independent libraries (Unix install on native Windows) */ #undef SWIG_LIB_WIN_UNIX /* Platform that SWIG is built for */ #undef SWIG_PLATFORM /* Version number of package */ #undef VERSION /* Deal with attempt by Microsoft to deprecate C standard runtime functions */ #if defined(_MSC_VER) # define _CRT_SECURE_NO_DEPRECATE #endif swig-4.4.0/Source/Doxygen/0000775000175000017500000000000015075470607015240 5ustar williamwilliamswig-4.4.0/Source/Doxygen/pydoc.h0000664000175000017500000001614415075443613016532 0ustar williamwilliam/* ----------------------------------------------------------------------------- * This file is part of SWIG, which is licensed as a whole under version 3 * (or any later version) of the GNU General Public License. Some additional * terms also apply to certain portions of SWIG. The full details of the SWIG * license and copyrights can be found in the LICENSE and COPYRIGHT files * included with the SWIG source code as distributed by the SWIG developers * and at https://www.swig.org/legal.html. * * pydoc.h * * Module to return documentation for nodes formatted for PyDoc * ----------------------------------------------------------------------------- */ #ifndef SWIG_PYDOC_H #define SWIG_PYDOC_H #include #include #include "swig.h" #include "doxyentity.h" #include "doxytranslator.h" #define DOC_STRING_LENGTH 64 // characters per line allowed #define DOC_PARAM_STRING_LENGTH 30 // characters reserved for param name / type class PyDocConverter : public DoxygenTranslator { public: PyDocConverter(int flags = 0); String *makeDocumentation(Node *node); protected: size_t m_tableLineLen; bool m_prevRowIsTH; std::string m_url; /* * Translate every entity in a tree, also manages sections * display. Prints title for every group of tags that have * a section title associated with them */ std::string translateSubtree(DoxygenEntity &doxygenEntity); /* * Translate one entity with the appropriate handler, according * to the tagHandlers */ void translateEntity(DoxygenEntity &doxyEntity, std::string &translatedComment); /* * Typedef for the function that handles one tag * arg - some string argument to easily pass it through lookup table */ typedef void (PyDocConverter::*tagHandler) (DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Wrap the command data with the some string * arg - string to wrap with, like '_' or '*' */ void handleTagWrap(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Just prints new line */ void handleNewLine(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Print the name of tag to the output, used for escape-commands */ void handleTagChar(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Format the contents of the \exception tag or its synonyms. */ void handleTagException(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Print only the content and strip original tag */ void handleParagraph(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg = std::string()); /* * Handle Doxygen verbatim tag */ void handleVerbatimBlock(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg = std::string()); /* * Handle one of the Doxygen formula-related tags. */ void handleMath(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Handle a code snippet. */ void handleCode(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Print only data part of code */ void handlePlainString(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /** * Copies verbatim args of the tag to output, used for commands like \f$, ... */ void handleTagVerbatim(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Print the if-elseif-else-endif section */ void handleTagIf(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Prints the specified message, than the contents of the tag * arg - message */ void handleTagMessage(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Insert 'Title: ...' */ void handleTagPar(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Insert 'Image: ...' */ void handleTagImage(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Format nice param description with type information */ void handleTagParam(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Format the contents of the \return tag or its synonyms. */ void handleTagReturn(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* * Writes text for \ref tag. */ void handleTagRef(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg); /* Handles HTML tags recognized by Doxygen, like ,