Start removing proxy layer, just use the wrapped functions directly

The proxy layer, and all the extra complexity associated with it, seemed to be
only necessary in order to try to allow using the same name for the wrapped
global functions as were used for them in the original C or C++ code being
wrapped. However this could simply never work in all cases, notably it didn't
work at all when using ELF shared libraries under Unix as the functions with
the same name defined in the main program were interposed and replaced the
functions defined in the shared library, meaning that the proxy function foo()
called wrapper function _wrap_foo() which called back into proxy function
foo() itself again, resulting in guaranteed stack overflow. The only possible
way to fix this would be to use "protected" ELF visibility for the original
functions, but this is not always possible (e.g. if the sources of the
original library are not available) and not simple even when it is and,
besides, protected visibility has its own problems -- notably by making it
impossible to hook the library functions when you actually want to do it.
Besides, proxy-based approach simply couldn't work at all when using static
linking as it resulted in two copies of the function with the same name

Most importantly, however, the main task of this module is to wrap C++
classes, not C functions, and renaming them in the wrapper is not necessary at
all in this case as there is no conflict with the original names in this case.
So simply drop the idea of generating a separate proxy header and generate a
header declaring the functions declared in the wrapper instead and, also, do
not give them "_wrap_" prefix whenever possible, i.e. only do it for the
global functions.

This simplifies SWIG code itself and makes it simpler to use its output as
it's not necessary to link both with the wrapper (dynamically) and with the
proxy (statically) and it's not enough to link with the wrapper only and it
can be done in any way (i.e. either statically or dynamically).

As a side effect of this change, Swig_name_proxy() is not necessary any more
and was removed, eliminating the only difference with the master branch in any
source file other than c.cxx itself.
This commit is contained in:
Vadim Zeitlin 2016-04-12 23:25:42 +02:00
commit 3d6880aad1
38 changed files with 195 additions and 299 deletions

View file

@ -108,18 +108,18 @@ Note that <tt>-c</tt> is the option specifying the <b>target</b> language and <t
<p>
<p>
This will generate an <tt>example_wrap.c</tt> file or, in the latter case, <tt>example_wrap.cxx</tt> file, along with <tt>example_proxy.h</tt> and <tt>example_proxy.c</tt> files. The name of the file is derived from the name of the input file. To change this, you can use the <tt>-o</tt> option common to all language modules.
This will generate an <tt>example_wrap.c</tt> file or, in the latter case, <tt>example_wrap.cxx</tt> file, along with <tt>example_wrap.h</tt> (the same extension is used in both C and C++ cases for the last one). The names of the files are derived from the name of the input file by default, but can be changed using the <tt>-o</tt> and <tt>-oh</tt> options common to all language modules.
</p>
<p>
The <tt>wrap</tt> file contains the wrapper functions, which perform the main functionality of SWIG: it translates the input arguments from C to C++, makes calls to the original functions and marshalls C++ output back to C data. The <tt>proxy</tt> header file contains the interface we can use in C application code. The additional <tt>.c</tt> file contains calls to the wrapper functions, allowing us to preserve names of the original functions.
The <tt>xxx_wrap.c</tt> file contains the wrapper functions, which perform the main functionality of SWIG: each of the wrappers translates the input arguments from C to C++, makes calls to the original functions and marshals C++ output back to C data. The <tt>xxx_wrap.h</tt> header file contains the declarations of these functions as well as global variables.
</p>
<H3><a name="C_commandline"></a>36.2.2 Command line options</H3>
<p>
The following table list the additional commandline options available for the C module. They can also be seen by using:
The following table list the additional command line options available for the C module. They can also be seen by using:
</p>
<div class="shell"><pre>
@ -131,11 +131,6 @@ $ swig -c -help
<th>C specific options</th>
</tr>
<tr>
<td>-noproxy</td>
<td>do not generate proxy files (i.e. <i>filename</i><tt>_proxy.h</tt> and <i>filename</i><tt>_proxy.c</tt>)</td>
</tr>
<tr>
<td>-noexcept</td>
<td>generate wrappers with no support of exception handling; see <a href="#C_exceptions">Exceptions</a> chapter for more details </td>
@ -153,8 +148,7 @@ The next step is to build a dynamically loadable module, which we can link to ou
<div class="shell"><pre>
$ swig -c example.i
$ gcc -c example_wrap.c
$ gcc -c example_proxy.c
$ gcc -shared example_wrap.o example_proxy.o -o libexample.so
$ gcc -shared example_wrap.o -o libexample.so
</pre></div>
<p>
@ -164,8 +158,7 @@ Or, for C++ input:
<div class="shell"><pre>
$ swig -c++ -c example.i
$ g++ -c example_wrap.cxx
$ gcc -c example_proxy.c
$ g++ -shared example_proxy.o example_wrap.o -o libexample.so
$ g++ -shared example_wrap.o -o libexample.so
</pre></div>
<p>
@ -176,7 +169,7 @@ Now the shared library module is ready to use. Note that the name of the generat
<p>
The simplest way to use the generated shared module is to link it to the application code during the compilation stage. We have to compile the proxy file as well. The process is usually similar to this:
The simplest way to use the generated shared module is to link it to the application code during the compilation stage. The process is usually similar to this:
</p>
<div class="shell"><pre>
@ -184,14 +177,14 @@ $ gcc runme.c -L. -lexample -o runme
</pre></div>
<p>
This will compile the application code (<tt>runme.c</tt>), along with the proxy code and link it against the generated shared module. Following the <tt>-L</tt> option is the path to the directory containing the shared module. The output executable is ready to use. The last thing to do is to supply to the operating system the information of location of our module. This is system dependant, for instance Unix systems look for shared modules in certain directories, like <tt>/usr/lib</tt>, and additionally we can set the environment variable <tt>LD_LIBRARY_PATH</tt> (Unix) or <tt>PATH</tt> (Windows) for other directories.
This will compile the application code (<tt>runme.c</tt>) and link it against the generated shared module. Following the <tt>-L</tt> option is the path to the directory containing the shared module. The output executable is ready to use. The last thing to do is to supply to the operating system the information of location of our module. This is system dependant, for instance Unix systems look for shared modules in certain directories, like <tt>/usr/lib</tt>, and additionally we can set the environment variable <tt>LD_LIBRARY_PATH</tt> (Unix) or <tt>PATH</tt> (Windows) for other directories.
</p>
<H2><a name="C_basic_c_wrapping"></a>36.3 Basic C wrapping</H2>
<p>
Wrapping C functions and variables is obviously performed in a straightforward way. There is no need to perform type conversions, and all language constructs can be preserved in their original form. However, SWIG allows you to enchance the code with some additional elements, for instance using <tt>check</tt> typemap or <tt>%extend</tt> directive.
Wrapping C functions and variables is obviously performed in a straightforward way. There is no need to perform type conversions, and all language constructs can be preserved in their original form. However, SWIG allows you to enhance the code with some additional elements, for instance using <tt>check</tt> typemap or <tt>%extend</tt> directive.
</p>
<H3><a name="C_functions"></a>36.3.1 Functions</H3>
@ -221,16 +214,6 @@ int _wrap_gcd(int arg1, int arg2) {
}
</pre></div>
<p>
Then again, this wrapper function is usually called from C using helper function declared in proxy file, preserving the original name:
</p>
<div class="targetlang"><pre>
int gcd(int arg1, int arg2) {
return _wrap_gcd(arg1,arg2);
}
</pre></div>
<p>
Now one might think, what's the use of creating such functions in C? The answer is, you can apply special rules to the generated code. Take for example constraint checking. You can write a "check" typemap in your interface file:
</p>
@ -501,7 +484,7 @@ Let's assume we have the following C++ interface file, we'd like to generate cod
What we would like to generate as a C interface of this function would be something like this:
<div class="targetlang"><pre>
//proxy header file
// wrapper header file
typedef struct SwigObj_SomeClass SomeClass;
SomeClass * new_SomeClass();
@ -518,15 +501,8 @@ SomeIntTemplateClass * new_SomeIntTemplateClass();
void delete_SomeIntTemplateClass(SomeIntTemplateClass * carg1);
</pre></div>
When we generate the bindings, we generate code for two translation units:
<ul>
<li>The proxy</li>
<li>The wrapper</li>
</ul>
We need 2 translation units to be able to have C types with the same names as the original C++ types.
<H4>The Wrapper</H4>
Since the proxy embeds a call to the wrapper function, we'll examine the generation of the wrapper function first.
We'll examine the generation of the wrapper function first.
<div class="targetlang"><pre>
SWIGEXPORTC SwigObj * _wrap_someFunction(SwigObj * carg1, int carg2) {
@ -593,7 +569,7 @@ int arg2 ;
</pre></div>
If it's a C++ function that is wrapped (in this case it is), another variable is emitted for the 'original' return value of the C++ function.</br>
At this point, we simply 'inject' behavior if it's a C++ function that is wrapped (in this cas it obviously is).
At this point, we simply 'inject' behavior if it's a C++ function that is wrapped (in this case it obviously is).
<div class="targetlang"><pre>
cppouttype
@ -639,11 +615,12 @@ return result;
</pre></div>
<H4>The Proxy</H4>
Compared to the wrapper code generation, the proxy code is very simple.</br>
Basically it just casts types for the wrapper call. Let's have a look at the corresponding proxy header file code of the interface above.
Compared to the wrapper code generation, the header code is very simple.</br>
Basically it contains just the declarations corresponding to the definitions
above.
<div class="targetlang"><pre>
//proxy header file
// wrapper header file
typedef struct SwigObj_SomeClass SomeClass;
SomeClass * new_SomeClass();
@ -660,33 +637,8 @@ SomeIntTemplateClass * new_SomeIntTemplateClass();
void delete_SomeIntTemplateClass(SomeIntTemplateClass * carg1);
</pre></div>
For shortness sake, we'll only examine one function that uses all proxy typemaps, as the other functions are analogous.
<div class="targetlang"><pre>
SomeClass* someFunction(SomeIntTemplateClass* carg1, int carg2) {
return (SomeClass*)_wrap_someFunction((SwigObj *)carg1, (int)carg2);
}
</pre></div>
Again, let's first examine the protype:
<div class="targetlang"><pre>
ctype ctype ctype
---------- --------------------- ---
SomeClass* someFunction(SomeIntTemplateClass* carg1, int carg2);
</pre></div>
In the body of this function, we'll reuse the 'proxycouttype' and 'ctype' to cast the return value and arguments of the wrapper function.
<div class="targetlang"><pre>
ctype cmodtype cmodtype
---------- --------- ___
return (SomeClass*)_wrap_someFunction((SwigObj *)carg1, (int)carg2);
</pre></div>
<H2><a name="C_exceptions"></a>36.5 Exception handling</H2>
</body>
</html>

View file

@ -1734,23 +1734,18 @@ C_LDSHARED = @C_LDSHARED@
CXX_LDSHARED = @CXX_LDSHARED@
C_SO = @C_SO@
PROXYSRC := $(INTERFACE:.i=_proxy.c)
PROXYOBJ := $(PROXYSRC:.c=.@OBJEXT@)
c: $(SRCDIR_SRCS)
$(SWIG) -c $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH)
$(CC) -c $(CCSHARED) -I$(SRCDIR) $(CFLAGS) $(ISRCS) $(SRCDIR_SRCS) $(INCLUDES)
$(CC) -c $(CCSHARED) $(CFLAGS) $(PROXYSRC) $(INCLUDES)
$(C_LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(CLIBPREFIX)$(TARGET)$(C_SO)
c_cpp: $(SRCDIR_SRCS)
$(SWIG) -c++ -c $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH)
$(CXX) -c $(CCSHARED) -I$(SRCDIR) $(CXXFLAGS) $(ICXXSRCS) $(SRCDIR_CXXSRCS) $(INCLUDES)
$(CC) -c $(CCSHARED) $(CFLAGS) $(PROXYSRC) $(INCLUDES)
$(CXX_LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) $(CPP_DLLIBS) -o $(CLIBPREFIX)$(TARGET)$(C_SO)
c_compile: $(SRCDIR)$(RUNME).c $(PROXYOBJ)
$(COMPILETOOL) $(CC) -o $(RUNME) -I. $^ -L. -l$(TARGET)
c_compile: $(SRCDIR)$(RUNME).c
$(COMPILETOOL) $(CC) -o $(RUNME) -I. $< -L. -l$(TARGET)
# -----------------------------------------------------------------
# Run C example
@ -1771,7 +1766,7 @@ c_version:
# -----------------------------------------------------------------
c_clean:
rm -f *_wrap.c *_wrap.cxx *_proxy.[ch]
rm -f *_wrap.c *_wrap.cxx
rm -f core @EXTRA_CLEAN@
rm -f *.@OBJEXT@ *@SO@
rm -f $(RUNME)

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include "example_proxy.h"
#include "example_wrap.h"
int main(int argc, char **argv) {
printf("Creating some objects:\n");

View file

@ -4,7 +4,7 @@
#include <stdio.h>
#include "example_proxy.h"
#include "example_wrap.h"
int main() {
Test *t = new_Test();

View file

@ -1,11 +1,11 @@
#include <stdio.h>
#include "example_proxy.h"
#include "example_wrap.h"
int main(int argc, char **argv) {
int a = 42;
int b = 105;
int g = gcd(a, b);
int g = _wrap_gcd(a, b);
printf("The gcd of %d and %d is %d\n", a, b, g);
printf("Foo = %f\n", Foo);
Foo = 3.1415926;

View file

@ -1,4 +1,4 @@
#include "example_proxy.h"
#include "example_wrap.h"
int main() {
Klass *klass = new_Klass();
@ -34,4 +34,4 @@ int main() {
}
SWIG_exit(0);
}
}

View file

@ -165,4 +165,4 @@ run_testcase = \
fi;
clean:
@rm -f *_wrap.* *_proxy.* *~ *.exe *.dll *.so *.out *runme
@rm -f *_wrap.* *~ *.exe *.dll *.so *.out *runme

View file

@ -1,4 +1,4 @@
#include "abstract_access/abstract_access_proxy.h"
#include "abstract_access/abstract_access_wrap.h"
#include <assert.h>
int main(int argc, const char *argv[]) {

View file

@ -1,4 +1,4 @@
#include "abstract_typedef2/abstract_change_proxy.h"
#include "abstract_typedef2/abstract_change_wrap.h"
#include <assert.h>
int main(int argc, const char *argv[]) {

View file

@ -1,4 +1,4 @@
#include "abstract_typedef/abstract_typedef_proxy.h"
#include "abstract_typedef/abstract_typedef_wrap.h"
#include <stdbool.h>
#include <assert.h>

View file

@ -1,4 +1,4 @@
#include "abstract_virtual/abstract_virtual_proxy.h"
#include "abstract_virtual/abstract_virtual_wrap.h"
#include <assert.h>
int main(int argc, const char *argv[]) {

View file

@ -1,4 +1,4 @@
#include "add_link/add_link_proxy.h"
#include "add_link/add_link_wrap.h"
#include <assert.h>
int main(int argc, const char *argv[]) {

View file

@ -1,4 +1,4 @@
#include "abstract_typedef2/abstract_change_proxy.h"
#include "abstract_typedef2/abstract_change_wrap.h"
#include <assert.h>
int main(int argc, const char *argv[]) {

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "c_backend_cpp_exception/c_backend_cpp_exception_proxy.h"
#include "c_backend_cpp_exception/c_backend_cpp_exception_wrap.h"
int main()
{

View file

@ -1,7 +1,7 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "c_backend_cpp_natural_std_string/c_backend_cpp_natural_std_string_proxy.h"
#include "c_backend_cpp_natural_std_string/c_backend_cpp_natural_std_string_wrap.h"
int main()
{

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include "cast_operator/cast_operator_proxy.h"
#include "cast_operator/cast_operator_wrap.h"
int main() {
A *a = new_A();

View file

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "char_strings/char_strings_proxy.h"
#include "char_strings/char_strings_wrap.h"
int main() {
char *CPLUSPLUS_MSG = "A message from the deep dark world of C++, where anything is possible.";

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_class_enum/cpp_basic_class_enum_proxy.h"
#include "cpp_basic_class_enum/cpp_basic_class_enum_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_class_method/cpp_basic_class_method_proxy.h"
#include "cpp_basic_class_method/cpp_basic_class_method_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,4 +1,4 @@
#include "cpp_basic_class/cpp_basic_class_proxy.h"
#include "cpp_basic_class/cpp_basic_class_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_class_var_pub_member_atom/cpp_basic_class_var_pub_member_atom_proxy.h"
#include "cpp_basic_class_var_pub_member_atom/cpp_basic_class_var_pub_member_atom_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_class_var_pub_member_class/cpp_basic_class_var_pub_member_class_proxy.h"
#include "cpp_basic_class_var_pub_member_class/cpp_basic_class_var_pub_member_class_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_class_virtual_method/cpp_basic_class_virtual_method_proxy.h"
#include "cpp_basic_class_virtual_method/cpp_basic_class_virtual_method_wrap.h"
int main()
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_global_enum/cpp_basic_global_enum_proxy.h"
#include "cpp_basic_global_enum/cpp_basic_global_enum_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_global_var_atom/cpp_basic_global_var_atom_proxy.h"
#include "cpp_basic_global_var_atom/cpp_basic_global_var_atom_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_global_var_class/cpp_basic_global_var_class_proxy.h"
#include "cpp_basic_global_var_class/cpp_basic_global_var_class_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,4 +1,4 @@
#include "cpp_basic_namespaced_class/cpp_basic_namespaced_class_proxy.h"
#include "cpp_basic_namespaced_class/cpp_basic_namespaced_class_wrap.h"
int main(int argc, const char *argv[])
{

View file

@ -1,4 +1,4 @@
#include "cpp_basic/cpp_basic_proxy.h"
#include "cpp_basic/cpp_basic_wrap.h"
#include <assert.h>
#include <stdio.h>

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_template_class/cpp_basic_template_class_proxy.h"
#include "cpp_basic_template_class/cpp_basic_template_class_wrap.h"
int main() {
MyTemplateClass_Int *ci = new_MyTemplateClass_Int();

View file

@ -1,5 +1,5 @@
#include <assert.h>
#include "cpp_basic_template_function/cpp_basic_template_function_proxy.h"
#include "cpp_basic_template_function/cpp_basic_template_function_wrap.h"
int main() {
assert(GetMaxInt(3, 5) == 5);

View file

@ -1,4 +1,4 @@
#include "cpp_enum/cpp_enum_proxy.h"
#include "cpp_enum/cpp_enum_wrap.h"
#include <assert.h>
#include <stdio.h>

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include "enums/enums_proxy.h"
#include "enums/enums_wrap.h"
int main() {
bar2(1);

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include "exception_order/exception_order_proxy.h"
#include "exception_order/exception_order_wrap.h"
int main() {
A* a = new_A();

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include "operator_overload/operator_overload_proxy.h"
#include "operator_overload/operator_overload_wrap.h"
#define assert(x,msg) if (!x) { printf("%d: %s\n", x, msg); SWIG_exit(0); }

View file

@ -32,6 +32,68 @@ int SwigType_isbuiltin(SwigType *t) {
return 0;
}
// Private helpers, could be made public and reused from other language modules in the future.
namespace
{
// 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"
)
{
}
};
// Return the public name to use for the given class.
//
// It basically just prepends the namespace, if any, to the class name, and mangles the result.
String *make_public_class_name(String* nspace, String* classname) {
String *s = nspace ? NewStringf("%s_", nspace) : NewString("");
Append(s, classname);
return s;
}
} // anonymous namespace
class C:public Language {
static const char *usage;
@ -39,13 +101,8 @@ class C:public Language {
File *f_runtime;
File *f_header;
File *f_wrappers;
File *f_wrappers_decl;
File *f_init;
File *f_proxy_c;
File *f_proxy_h;
String *f_proxy_code_init;
String *f_proxy_code_body;
String *f_proxy_header;
String *empty_string;
String *int_string;
@ -53,7 +110,6 @@ class C:public Language {
String *destroy_object;
String *tl_namespace; // optional top level namespace
bool proxy_flag;
bool except_flag;
public:
@ -68,7 +124,6 @@ public:
create_object(0),
destroy_object(0),
tl_namespace(NULL),
proxy_flag(true),
except_flag(true) {
}
@ -86,10 +141,8 @@ public:
String *nspace = Getattr(n, "sym:nspace");
if (nspace) {
Replaceall(nspace, ".", "_"); // Classes' namespaces get dotted -> replace; FIXME in core!
proxyname = Swig_name_proxy(nspace, symname);
if (tl_namespace)
proxyname = Swig_name_proxy(tl_namespace, proxyname);
// FIXME: using namespace as class name is a hack.
proxyname = Swig_name_member(tl_namespace, nspace, symname);
} else {
proxyname = symname;
}
@ -111,7 +164,7 @@ public:
Node *n = NULL;
t = SwigType_typedef_resolve_all(t);
if (!proxy_flag || !t || !(n = classLookup(t)))
if (!t || !(n = classLookup(t)))
return NULL;
return getNamespacedName(n);
@ -243,12 +296,6 @@ public:
if (argv[i]) {
if (strcmp(argv[i], "-help") == 0) {
Printf(stdout, "%s\n", usage);
} else if ((strcmp(argv[i], "-proxy") == 0) || (strcmp(argv[i], "-proxy") == 0)) {
proxy_flag = true;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-noproxy") == 0) {
proxy_flag = false;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-noexcept") == 0) {
except_flag = false;
Swig_mark_arg(i);
@ -344,75 +391,65 @@ public:
Swig_banner(f_begin);
// generate proxy files if enabled
if (proxy_flag) {
f_proxy_code_init = NewString("");
f_proxy_code_body = NewString("");
f_proxy_header = NewString("");
// create proxy files with appropriate name
String *proxy_code_filename = NewStringf("%s%s_proxy.c", SWIG_output_directory(), Char(module));
if ((f_proxy_c = NewFile(proxy_code_filename, "w", SWIG_output_files())) == 0) {
FileErrorDisplay(proxy_code_filename);
// Open the file where all wrapper declarations will be written to in the end.
String* const outfile_h = Getattr(n, "outfile_h");
File* const f_wrappers_h = NewFile(outfile_h, "w", SWIG_output_files());
if (!f_wrappers_h) {
FileErrorDisplay(outfile_h);
SWIG_exit(EXIT_FAILURE);
}
String *proxy_header_filename = NewStringf("%s%s_proxy.h", SWIG_output_directory(), Char(module));
if ((f_proxy_h = NewFile(proxy_header_filename, "w", SWIG_output_files())) == 0) {
FileErrorDisplay(proxy_header_filename);
SWIG_exit(EXIT_FAILURE);
}
Swig_register_filebyname("proxy_code_init", f_proxy_code_init);
Swig_register_filebyname("proxy_code_body", f_proxy_code_body);
Swig_register_filebyname("proxy_header", f_proxy_header);
Swig_banner(f_proxy_code_init);
Swig_banner(f_proxy_header);
Printf(f_proxy_code_init, "#include \"%s\"\n\n", proxy_header_filename);
Printf(f_proxy_header, "#ifndef _%s_proxy_H_\n#define _%s_proxy_H_\n\n", Char(module), Char(module));
}
Swig_banner(f_wrappers_h);
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("proxy_header", f_wrappers_h);
Swig_name_register("proxyname", "%n_%v");
{
// Create a string to which the wrapper declarations will be appended one by one.
f_wrappers_decl = NewString("");
Printf(f_wrappers, "#ifdef __cplusplus\n");
Printf(f_wrappers, "extern \"C\" {\n");
Printf(f_wrappers, "#endif\n\n");
String* const include_guard_name = NewStringf("SWIG_%s_WRAP_H_", Char(module));
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
);
if (except_flag) {
start_create_object();
start_destroy_object();
}
// emit code for children
Language::top(n);
begin_end_output_guard
include_guard_wrappers_h(f_wrappers_h, include_guard_begin, include_guard_end);
if (except_flag) {
Append(f_header, finish_create_object());
Append(f_header, finish_destroy_object());
}
{
cplusplus_output_guard
cplusplus_guard_wrappers(f_wrappers),
cplusplus_guard_wrappers_h(f_wrappers_decl);
Printf(f_wrappers, "#ifdef __cplusplus\n");
Printf(f_wrappers, "}\n");
Printf(f_wrappers, "#endif\n");
if (except_flag) {
start_create_object();
start_destroy_object();
}
// finalize generating proxy file
if (proxy_flag) {
Printv(f_proxy_c, f_proxy_code_init, "\n", NIL);
Printv(f_proxy_c, f_proxy_code_body, "\n", NIL);
Printv(f_proxy_h, f_proxy_header, "\n#endif /* _", Char(module), "_proxy_H_ */\n", NIL);
Delete(f_proxy_c);
Delete(f_proxy_h);
Delete(f_proxy_code_init);
Delete(f_proxy_header);
}
// emit code for children
Language::top(n);
if (except_flag) {
Append(f_header, finish_create_object());
Append(f_header, finish_destroy_object());
}
} // close extern "C" guards
Dump(f_wrappers_decl, f_wrappers_h);
Delete(f_wrappers_decl);
} // close wrapper header guard
// write all to the file
Dump(f_header, f_runtime);
@ -424,6 +461,7 @@ public:
Delete(f_begin);
Delete(f_header);
Delete(f_wrappers);
Delete(f_wrappers_h);
Delete(f_init);
Delete(f_runtime);
@ -435,8 +473,6 @@ public:
* ------------------------------------------------------------------------ */
virtual int globalvariableHandler(Node *n) {
if (!proxy_flag)
return SWIG_OK;
String *name = Getattr(n, "name");
SwigType *type = Getattr(n, "type");
String *type_str = Copy(SwigType_str(type, 0));
@ -445,11 +481,11 @@ public:
char *c = Char(type_str);
c[Len(type_str) - Len(dims) - 1] = '\0';
String *bare_type = NewStringf("%s", c);
Printv(f_proxy_header, "SWIGIMPORT ", bare_type, " ", name, "[];\n\n", NIL);
Printv(f_wrappers_decl, "SWIGIMPORT ", bare_type, " ", name, "[];\n\n", NIL);
Delete(bare_type);
}
else
Printv(f_proxy_header, "SWIGIMPORT ", type_str, " ", name, ";\n\n", NIL);
Printv(f_wrappers_decl, "SWIGIMPORT ", type_str, " ", name, ";\n\n", NIL);
Delete(type_str);
return SWIG_OK;
}
@ -633,29 +669,14 @@ ready:
Printf(wrapper->code, "return result;\n");
Printf(wrapper->code, "}");
if (proxy_flag) // take care of proxy function
{
SwigType *proxy_type = Getattr(n, "c:stype"); // use proxy-type for return type if supplied
SwigType *proxy_type = Getattr(n, "c:stype"); // use proxy-type for return type if supplied
if (proxy_type) {
return_type = SwigType_str(proxy_type, 0);
}
if (proxy_type) {
return_type = SwigType_str(proxy_type, 0);
}
// emit proxy functions prototypes
// print wrapper prototype into proxy body for later use within proxy
// body
Printv(f_proxy_code_init, return_type, " ", wname, "(", proto, ");\n", NIL);
// print actual proxy code into proxy .c file
Printv(f_proxy_code_body, return_type, " ", name, "(", proto, ") {\n", NIL);
// print the call of the wrapper function
Printv(f_proxy_code_body, " return ", wname, "(", arg_names, ");\n}\n", NIL);
// add function declaration to the proxy header file
Printv(f_proxy_header, return_type, " ", name, "(", proto, ");\n\n", NIL);
}
// add function declaration to the header file
Printv(f_wrappers_decl, return_type, " ", wname, "(", proto, ");\n\n", NIL);
Wrapper_print(wrapper, f_wrappers);
@ -922,9 +943,8 @@ ready:
{
// C++ function wrapper proxy code
ParmList *parms = Getattr(n, "parms");
String *wname = Swig_name_wrapper(name);
String *wname = IS_SET_TO_ONE(n, "c:globalfun") ? Swig_name_wrapper(name) : Copy(name);
SwigType *preturn_type = functionWrapperCPPSpecificProxyReturnTypeGet(n);
String *wproto = Getattr(n, "wrap:proto");
String *pproto = functionWrapperCPPSpecificProxyPrototypeGet(n, parms);
String *wrapper_call = NewString("");
SwigType *proxy_type = Getattr(n, "c:stype"); // use proxy-type for return type if supplied
@ -933,28 +953,11 @@ ready:
preturn_type = SwigType_str(proxy_type, 0);
}
// emit proxy functions prototypes
// print wrapper prototype into proxy body for later use within proxy
// body
Printv(f_proxy_code_init, wproto, "\n", NIL);
// print actual proxy code into proxy .c file
Printv(f_proxy_code_body, preturn_type, " ", name, "(", pproto, ") {\n", NIL);
// print the call of the wrapper function
//Printv(f_proxy_code_body, " return ", wname, "(", proxy_wrap_args, ");\n}\n", NIL);
// Add cast if necessary
if (SwigType_type(preturn_type) != T_VOID) {
Printf(wrapper_call, "(%s)", preturn_type);
}
Printv(wrapper_call, functionWrapperCPPSpecificProxyWrapperCallGet(n, wname, parms), NIL);
Printv(f_proxy_code_body, " return ", wrapper_call, ";\n}\n", NIL);
// add function declaration to the proxy header file
Printv(f_proxy_header, preturn_type, " ", name, "(", pproto, ");\n\n", NIL);
Printv(f_wrappers_decl, preturn_type, " ", wname, "(", pproto, ");\n\n", NIL);
// cleanup
Delete(wname);
Delete(pproto);
Delete(wrapper_call);
Delete(preturn_type);
@ -968,7 +971,7 @@ ready:
SwigType *type = Getattr(n, "type");
SwigType *otype = Copy(type);
SwigType *return_type = functionWrapperCPPSpecificWrapperReturnTypeGet(n);
String *wname = Swig_name_wrapper(name);
String *wname = IS_SET_TO_ONE(n, "c:globalfun") ? Swig_name_wrapper(name) : Copy(name);
String *arg_names = NewString("");
ParmList *parms = Getattr(n, "parms");
Parm *p;
@ -1072,12 +1075,6 @@ ready:
}
Printv(wrapper->def, ")", NIL);
//Create prototype for proxy file
String *wrap_proto = Copy(wrapper->def);
//Declare function as extern so only the linker has to find it
Replaceall(wrap_proto, "SWIGEXPORTC", "extern");
Printv(wrap_proto, ";", NIL);
Setattr(n, "wrap:proto", wrap_proto);
Printv(wrapper->def, " {", NIL);
if (Cmp(nodeType(n), "destructor") != 0) {
@ -1249,9 +1246,7 @@ ready:
// C++ function wrapper
functionWrapperCPPSpecificWrapper(n, name);
if (proxy_flag) // take care of proxy function
functionWrapperCPPSpecificProxy(n, name);
functionWrapperCPPSpecificProxy(n, name);
Delete(name);
}
@ -1317,7 +1312,7 @@ ready:
if ((Cmp(kind, "variable") == 0) || (Cmp(kind, "function") == 0)) {
String* type = NewString("");
Printv(type, Getattr(node, "decl"), Getattr(node, "type"), NIL);
Printv(f_proxy_header, " ", SwigType_str(type, 0), " ", Getattr(node, "name"), ";\n", NIL);
Printv(f_wrappers_decl, " ", SwigType_str(type, 0), " ", Getattr(node, "name"), ";\n", NIL);
Delete(type);
}
// WARNING: proxy delaration can be different than original code
@ -1381,8 +1376,7 @@ ready:
}
// declare type for specific class in the proxy header
if (proxy_flag)
Printv(f_proxy_header, "\ntypedef struct SwigObj_", name, " ", name, ";\n\n", NIL);
Printv(f_wrappers_decl, "\ntypedef struct SwigObj_", name, " ", name, ";\n\n", NIL);
Delete(sobj);
Delete(name);
@ -1390,20 +1384,18 @@ ready:
}
else if (Cmp(Getattr(n, "kind"), "struct") == 0) {
// this is C struct, just declare it in the proxy
if (proxy_flag) {
String *storage = Getattr(n, "storage");
int usetd = storage && Cmp(storage, "typedef") == 0;
if (usetd)
Append(f_proxy_header, "typedef struct {\n");
else
Printv(f_proxy_header, "struct ", name, " {\n", NIL);
Node *node = firstChild(n);
emit_c_struct_def(node);
if (usetd)
Printv(f_proxy_header, "} ", name, ";\n\n", NIL);
else
Append(f_proxy_header, "};\n\n");
}
String *storage = Getattr(n, "storage");
int usetd = storage && Cmp(storage, "typedef") == 0;
if (usetd)
Append(f_wrappers_decl, "typedef struct {\n");
else
Printv(f_wrappers_decl, "struct ", name, " {\n", NIL);
Node *node = firstChild(n);
emit_c_struct_def(node);
if (usetd)
Printv(f_wrappers_decl, "} ", name, ";\n\n", NIL);
else
Append(f_wrappers_decl, "};\n\n");
Delete(sobj);
Delete(name);
@ -1571,7 +1563,7 @@ ready:
SwigType_add_pointer(ctype);
Setattr(n, "type", ctype);
Setattr(n, "c:objstruct", "1");
stype = Swig_name_proxy(nspace, newclassname);
stype = make_public_class_name(nspace, newclassname);
SwigType_add_pointer(stype);
Setattr(n, "c:stype", stype);
@ -1630,7 +1622,7 @@ ready:
SwigType_add_pointer(ctype);
Setattr(n, "type", ctype);
Setattr(n, "c:objstruct", "1");
stype = Swig_name_proxy(nspace, newclassname);
stype = make_public_class_name(nspace, newclassname);
SwigType_add_pointer(stype);
Setattr(n, "c:stype", stype);
@ -1687,7 +1679,7 @@ ready:
SwigType_add_pointer(ctype);
p = NewParm(ctype, "self", n);
Setattr(p, "lname", "arg1");
stype = Swig_name_proxy(nspace, newclassname);
stype = make_public_class_name(nspace, newclassname);
SwigType_add_pointer(stype);
Setattr(p, "c:stype", stype);
Setattr(p, "c:objstruct", "1");
@ -1742,7 +1734,7 @@ ready:
String *name = Getattr(n, "value");
String *value = Getattr(n, "enumvalueex");
value = value ? value : Getattr(n, "enumvalue");
Printv(f_proxy_header, "#define ", Swig_name_mangle(name), " ", value, "\n", NIL);
Printv(f_wrappers_decl, "#define ", Swig_name_mangle(name), " ", value, "\n", NIL);
Swig_restore(n);
return SWIG_OK;
}
@ -1752,11 +1744,9 @@ ready:
* --------------------------------------------------------------------- */
virtual int constantWrapper(Node *n) {
if (!proxy_flag)
return SWIG_OK;
String *name = Getattr(n, "sym:name");
String *value = Getattr(n, "value");
Printv(f_proxy_header, "#define ", name, " ", value, "\n", NIL);
Printv(f_wrappers_decl, "#define ", name, " ", value, "\n", NIL);
return SWIG_OK;
}
@ -1811,7 +1801,6 @@ extern "C" Language *swig_c(void) {
const char *C::usage = (char *) "\
C Options (available with -c)\n\
-noproxy - do not generate proxy interface\n\
-noexcept - do not generate exception handling code\n\
\n";

View file

@ -224,31 +224,6 @@ String *Swig_name_member(const_String_or_char_ptr nspace, const_String_or_char_p
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_proxy()
*
* Returns the name of a proxy function.
* ----------------------------------------------------------------------------- */
String *Swig_name_proxy(const_String_or_char_ptr nspace, const_String_or_char_ptr fname) {
String *r;
String *f;
r = NewStringEmpty();
if (!naming_hash)
naming_hash = NewHash();
f = Getattr(naming_hash, "proxyname");
if (!f) {
Append(r, "%n_%v");
} else {
Append(r, f);
}
Replace(r, (nspace ? "%n" : "%n_"), nspace, DOH_REPLACE_ANY);
Replace(r, "%v", fname, DOH_REPLACE_ANY);
name_mangle(r);
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_get()
*

View file

@ -286,20 +286,6 @@ int SwigType_issimple(const SwigType *t) {
return 1;
}
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;
}
/* -----------------------------------------------------------------------------
* SwigType_default_create()
*

View file

@ -274,7 +274,6 @@ extern int ParmList_is_compactdefargs(ParmList *p);
extern String *Swig_name_mangle(const_String_or_char_ptr 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_proxy(const_String_or_char_ptr nspace, const_String_or_char_ptr fname);
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);