diff --git a/Doc/Manual/C.html b/Doc/Manual/C.html index c381cb3e1..7a4b646b1 100644 --- a/Doc/Manual/C.html +++ b/Doc/Manual/C.html @@ -108,18 +108,18 @@ Note that -c is the option specifying the target language and

-This will generate an example_wrap.c file or, in the latter case, example_wrap.cxx file, along with example_proxy.h and example_proxy.c files. The name of the file is derived from the name of the input file. To change this, you can use the -o option common to all language modules. +This will generate an example_wrap.c file or, in the latter case, example_wrap.cxx file, along with example_wrap.h (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 -o and -oh options common to all language modules.

-The wrap 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 proxy header file contains the interface we can use in C application code. The additional .c file contains calls to the wrapper functions, allowing us to preserve names of the original functions. +The xxx_wrap.c 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 xxx_wrap.h header file contains the declarations of these functions as well as global variables.

36.2.2 Command line options

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

@@ -131,11 +131,6 @@ $ swig -c -help
 C specific options
 
 
-
--noproxy
-do not generate proxy files (i.e. filename_proxy.h and filename_proxy.c)
-
-
 
 -noexcept
 generate wrappers with no support of exception handling; see Exceptions chapter for more details 
@@ -153,8 +148,7 @@ The next step is to build a dynamically loadable module, which we can link to ou
 
 $ 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
 

@@ -164,8 +158,7 @@ Or, for C++ input:

 $ 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
 

@@ -176,7 +169,7 @@ Now the shared library module is ready to use. Note that the name of the generat

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

@@ -184,14 +177,14 @@ $ gcc runme.c -L. -lexample -o runme
 

-This will compile the application code (runme.c), along with the proxy code and link it against the generated shared module. Following the -L 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 /usr/lib, and additionally we can set the environment variable LD_LIBRARY_PATH (Unix) or PATH (Windows) for other directories. +This will compile the application code (runme.c) and link it against the generated shared module. Following the -L 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 /usr/lib, and additionally we can set the environment variable LD_LIBRARY_PATH (Unix) or PATH (Windows) for other directories.

36.3 Basic C wrapping

-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 check typemap or %extend 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 check typemap or %extend directive.

36.3.1 Functions

@@ -221,16 +214,6 @@ int _wrap_gcd(int arg1, int arg2) { }
-

-Then again, this wrapper function is usually called from C using helper function declared in proxy file, preserving the original name: -

- -
-int gcd(int arg1, int arg2) {
-  return _wrap_gcd(arg1,arg2);
-}
-
-

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:

@@ -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:
-//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);
 
-When we generate the bindings, we generate code for two translation units: - -We need 2 translation units to be able to have C types with the same names as the original C++ types. -

The Wrapper

-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.
 SWIGEXPORTC SwigObj * _wrap_someFunction(SwigObj * carg1, int carg2) {
@@ -593,7 +569,7 @@ int arg2 ;
 
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.
-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).
 cppouttype
@@ -639,11 +615,12 @@ return result;
 

The Proxy

-Compared to the wrapper code generation, the proxy code is very simple.
-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.
+Basically it contains just the declarations corresponding to the definitions +above.
-//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);
 
-For shortness sake, we'll only examine one function that uses all proxy typemaps, as the other functions are analogous. - -
-SomeClass* someFunction(SomeIntTemplateClass* carg1, int carg2) {
-  return (SomeClass*)_wrap_someFunction((SwigObj *)carg1, (int)carg2);
-}
-
- -Again, let's first examine the protype: -
-ctype                   ctype                        ctype
-----------              ---------------------        ---
-SomeClass* someFunction(SomeIntTemplateClass* carg1, int carg2);
-
- -In the body of this function, we'll reuse the 'proxycouttype' and 'ctype' to cast the return value and arguments of the wrapper function. - -
-        ctype                          cmodtype          cmodtype
-        ----------                     ---------         ___
-return (SomeClass*)_wrap_someFunction((SwigObj *)carg1, (int)carg2);
-
-

36.5 Exception handling

- - diff --git a/Examples/Makefile.in b/Examples/Makefile.in index 542b237d8..a091b7343 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -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) diff --git a/Examples/c/class/runme.c b/Examples/c/class/runme.c index ddb1cd725..4c1f33ca0 100644 --- a/Examples/c/class/runme.c +++ b/Examples/c/class/runme.c @@ -1,6 +1,6 @@ #include -#include "example_proxy.h" +#include "example_wrap.h" int main(int argc, char **argv) { printf("Creating some objects:\n"); diff --git a/Examples/c/exception/runme.c b/Examples/c/exception/runme.c index 853f64246..6480f3d50 100644 --- a/Examples/c/exception/runme.c +++ b/Examples/c/exception/runme.c @@ -4,7 +4,7 @@ #include -#include "example_proxy.h" +#include "example_wrap.h" int main() { Test *t = new_Test(); diff --git a/Examples/c/simple/runme.c b/Examples/c/simple/runme.c index 1c9891713..fff159dfc 100644 --- a/Examples/c/simple/runme.c +++ b/Examples/c/simple/runme.c @@ -1,11 +1,11 @@ #include -#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; diff --git a/Examples/c/std_vector/runme.c b/Examples/c/std_vector/runme.c index 0dc91969b..ec43f7505 100644 --- a/Examples/c/std_vector/runme.c +++ b/Examples/c/std_vector/runme.c @@ -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); -} \ No newline at end of file +} diff --git a/Examples/test-suite/c/Makefile.in b/Examples/test-suite/c/Makefile.in index f67f5c2ff..ffb0e78ed 100644 --- a/Examples/test-suite/c/Makefile.in +++ b/Examples/test-suite/c/Makefile.in @@ -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 diff --git a/Examples/test-suite/c/abstract_access_runme.c b/Examples/test-suite/c/abstract_access_runme.c index b2fe97b4e..e27c9b363 100644 --- a/Examples/test-suite/c/abstract_access_runme.c +++ b/Examples/test-suite/c/abstract_access_runme.c @@ -1,4 +1,4 @@ -#include "abstract_access/abstract_access_proxy.h" +#include "abstract_access/abstract_access_wrap.h" #include int main(int argc, const char *argv[]) { diff --git a/Examples/test-suite/c/abstract_change_runme.c b/Examples/test-suite/c/abstract_change_runme.c index 005dc0623..125fad7e9 100644 --- a/Examples/test-suite/c/abstract_change_runme.c +++ b/Examples/test-suite/c/abstract_change_runme.c @@ -1,4 +1,4 @@ -#include "abstract_typedef2/abstract_change_proxy.h" +#include "abstract_typedef2/abstract_change_wrap.h" #include int main(int argc, const char *argv[]) { diff --git a/Examples/test-suite/c/abstract_typedef_runme.c b/Examples/test-suite/c/abstract_typedef_runme.c index b30bde634..c9c699ba8 100644 --- a/Examples/test-suite/c/abstract_typedef_runme.c +++ b/Examples/test-suite/c/abstract_typedef_runme.c @@ -1,4 +1,4 @@ -#include "abstract_typedef/abstract_typedef_proxy.h" +#include "abstract_typedef/abstract_typedef_wrap.h" #include #include diff --git a/Examples/test-suite/c/abstract_virtual_runme.c b/Examples/test-suite/c/abstract_virtual_runme.c index fedf8c643..77dca7f88 100644 --- a/Examples/test-suite/c/abstract_virtual_runme.c +++ b/Examples/test-suite/c/abstract_virtual_runme.c @@ -1,4 +1,4 @@ -#include "abstract_virtual/abstract_virtual_proxy.h" +#include "abstract_virtual/abstract_virtual_wrap.h" #include int main(int argc, const char *argv[]) { diff --git a/Examples/test-suite/c/add_link_runme.c b/Examples/test-suite/c/add_link_runme.c index 1faec097d..c9080dbf9 100644 --- a/Examples/test-suite/c/add_link_runme.c +++ b/Examples/test-suite/c/add_link_runme.c @@ -1,4 +1,4 @@ -#include "add_link/add_link_proxy.h" +#include "add_link/add_link_wrap.h" #include int main(int argc, const char *argv[]) { diff --git a/Examples/test-suite/c/anonymous_bitfield_runme.c b/Examples/test-suite/c/anonymous_bitfield_runme.c index d7eb2447d..a9e0e731d 100644 --- a/Examples/test-suite/c/anonymous_bitfield_runme.c +++ b/Examples/test-suite/c/anonymous_bitfield_runme.c @@ -1,4 +1,4 @@ -#include "abstract_typedef2/abstract_change_proxy.h" +#include "abstract_typedef2/abstract_change_wrap.h" #include int main(int argc, const char *argv[]) { diff --git a/Examples/test-suite/c/c_backend_cpp_exception_runme.c b/Examples/test-suite/c/c_backend_cpp_exception_runme.c index 92a4624bc..314a9ece7 100644 --- a/Examples/test-suite/c/c_backend_cpp_exception_runme.c +++ b/Examples/test-suite/c/c_backend_cpp_exception_runme.c @@ -1,5 +1,5 @@ #include -#include "c_backend_cpp_exception/c_backend_cpp_exception_proxy.h" +#include "c_backend_cpp_exception/c_backend_cpp_exception_wrap.h" int main() { diff --git a/Examples/test-suite/c/c_backend_cpp_natural_std_string_runme.c b/Examples/test-suite/c/c_backend_cpp_natural_std_string_runme.c index 2cd9e7d65..f4ddc4836 100644 --- a/Examples/test-suite/c/c_backend_cpp_natural_std_string_runme.c +++ b/Examples/test-suite/c/c_backend_cpp_natural_std_string_runme.c @@ -1,7 +1,7 @@ #include #include #include -#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() { diff --git a/Examples/test-suite/c/cast_operator_runme.c b/Examples/test-suite/c/cast_operator_runme.c index b9a14aea9..66ccf984a 100644 --- a/Examples/test-suite/c/cast_operator_runme.c +++ b/Examples/test-suite/c/cast_operator_runme.c @@ -1,6 +1,6 @@ #include -#include "cast_operator/cast_operator_proxy.h" +#include "cast_operator/cast_operator_wrap.h" int main() { A *a = new_A(); diff --git a/Examples/test-suite/c/char_strings_runme.c b/Examples/test-suite/c/char_strings_runme.c index 4ddc217d0..bb10749c1 100644 --- a/Examples/test-suite/c/char_strings_runme.c +++ b/Examples/test-suite/c/char_strings_runme.c @@ -1,7 +1,7 @@ #include #include -#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."; diff --git a/Examples/test-suite/c/cpp_basic_class_enum_runme.c b/Examples/test-suite/c/cpp_basic_class_enum_runme.c index 1b84d5943..08634c7ba 100644 --- a/Examples/test-suite/c/cpp_basic_class_enum_runme.c +++ b/Examples/test-suite/c/cpp_basic_class_enum_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_class_method_runme.c b/Examples/test-suite/c/cpp_basic_class_method_runme.c index d85e36278..93fb4e190 100644 --- a/Examples/test-suite/c/cpp_basic_class_method_runme.c +++ b/Examples/test-suite/c/cpp_basic_class_method_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_class_runme.c b/Examples/test-suite/c/cpp_basic_class_runme.c index aa4d481b5..0b835ee21 100644 --- a/Examples/test-suite/c/cpp_basic_class_runme.c +++ b/Examples/test-suite/c/cpp_basic_class_runme.c @@ -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[]) { diff --git a/Examples/test-suite/c/cpp_basic_class_var_pub_member_built_in_runme.c b/Examples/test-suite/c/cpp_basic_class_var_pub_member_built_in_runme.c index 3cfb658ba..747e2c864 100644 --- a/Examples/test-suite/c/cpp_basic_class_var_pub_member_built_in_runme.c +++ b/Examples/test-suite/c/cpp_basic_class_var_pub_member_built_in_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_class_var_pub_member_class_runme.c b/Examples/test-suite/c/cpp_basic_class_var_pub_member_class_runme.c index 4be7d2a54..cc4127d9e 100644 --- a/Examples/test-suite/c/cpp_basic_class_var_pub_member_class_runme.c +++ b/Examples/test-suite/c/cpp_basic_class_var_pub_member_class_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_class_virtual_method_runme.c b/Examples/test-suite/c/cpp_basic_class_virtual_method_runme.c index 1a6f332a3..a5cabe8a7 100644 --- a/Examples/test-suite/c/cpp_basic_class_virtual_method_runme.c +++ b/Examples/test-suite/c/cpp_basic_class_virtual_method_runme.c @@ -1,5 +1,5 @@ #include -#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() { diff --git a/Examples/test-suite/c/cpp_basic_global_enum_runme.c b/Examples/test-suite/c/cpp_basic_global_enum_runme.c index 9620619e7..43805679f 100644 --- a/Examples/test-suite/c/cpp_basic_global_enum_runme.c +++ b/Examples/test-suite/c/cpp_basic_global_enum_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_global_var_built_in_runme.c b/Examples/test-suite/c/cpp_basic_global_var_built_in_runme.c index 80540bda1..4ef992da0 100644 --- a/Examples/test-suite/c/cpp_basic_global_var_built_in_runme.c +++ b/Examples/test-suite/c/cpp_basic_global_var_built_in_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_global_var_class_runme.c b/Examples/test-suite/c/cpp_basic_global_var_class_runme.c index a620e58b3..12a586485 100644 --- a/Examples/test-suite/c/cpp_basic_global_var_class_runme.c +++ b/Examples/test-suite/c/cpp_basic_global_var_class_runme.c @@ -1,5 +1,5 @@ #include -#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[]) { diff --git a/Examples/test-suite/c/cpp_basic_namespaced_class_runme.c b/Examples/test-suite/c/cpp_basic_namespaced_class_runme.c index e06f8b128..95a1ce5c3 100644 --- a/Examples/test-suite/c/cpp_basic_namespaced_class_runme.c +++ b/Examples/test-suite/c/cpp_basic_namespaced_class_runme.c @@ -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[]) { diff --git a/Examples/test-suite/c/cpp_basic_runme.c b/Examples/test-suite/c/cpp_basic_runme.c index 32079f3d2..ec73ff704 100644 --- a/Examples/test-suite/c/cpp_basic_runme.c +++ b/Examples/test-suite/c/cpp_basic_runme.c @@ -1,4 +1,4 @@ -#include "cpp_basic/cpp_basic_proxy.h" +#include "cpp_basic/cpp_basic_wrap.h" #include #include diff --git a/Examples/test-suite/c/cpp_basic_template_class_runme.c b/Examples/test-suite/c/cpp_basic_template_class_runme.c index 0f3bc8d1e..f2aac8cd5 100644 --- a/Examples/test-suite/c/cpp_basic_template_class_runme.c +++ b/Examples/test-suite/c/cpp_basic_template_class_runme.c @@ -1,5 +1,5 @@ #include -#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(); diff --git a/Examples/test-suite/c/cpp_basic_template_function_runme.c b/Examples/test-suite/c/cpp_basic_template_function_runme.c index 24dee571a..10943d5e7 100644 --- a/Examples/test-suite/c/cpp_basic_template_function_runme.c +++ b/Examples/test-suite/c/cpp_basic_template_function_runme.c @@ -1,5 +1,5 @@ #include -#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); diff --git a/Examples/test-suite/c/cpp_enum_runme.c b/Examples/test-suite/c/cpp_enum_runme.c index 8c3d3b759..fa88134cb 100644 --- a/Examples/test-suite/c/cpp_enum_runme.c +++ b/Examples/test-suite/c/cpp_enum_runme.c @@ -1,4 +1,4 @@ -#include "cpp_enum/cpp_enum_proxy.h" +#include "cpp_enum/cpp_enum_wrap.h" #include #include diff --git a/Examples/test-suite/c/enums_runme.c b/Examples/test-suite/c/enums_runme.c index 374e55b06..a98815dc5 100644 --- a/Examples/test-suite/c/enums_runme.c +++ b/Examples/test-suite/c/enums_runme.c @@ -1,6 +1,6 @@ #include -#include "enums/enums_proxy.h" +#include "enums/enums_wrap.h" int main() { bar2(1); diff --git a/Examples/test-suite/c/exception_order_runme.c b/Examples/test-suite/c/exception_order_runme.c index a409db13e..2aadbfeb1 100644 --- a/Examples/test-suite/c/exception_order_runme.c +++ b/Examples/test-suite/c/exception_order_runme.c @@ -1,6 +1,6 @@ #include -#include "exception_order/exception_order_proxy.h" +#include "exception_order/exception_order_wrap.h" int main() { A* a = new_A(); diff --git a/Examples/test-suite/c/operator_overload_runme.c b/Examples/test-suite/c/operator_overload_runme.c index 2409d8df3..0a5a3c9b8 100644 --- a/Examples/test-suite/c/operator_overload_runme.c +++ b/Examples/test-suite/c/operator_overload_runme.c @@ -1,6 +1,6 @@ #include -#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); } diff --git a/Source/Modules/c.cxx b/Source/Modules/c.cxx index 0b7b21502..6b97f45bc 100644 --- a/Source/Modules/c.cxx +++ b/Source/Modules/c.cxx @@ -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"; diff --git a/Source/Swig/naming.c b/Source/Swig/naming.c index 29d530d67..2430ab407 100644 --- a/Source/Swig/naming.c +++ b/Source/Swig/naming.c @@ -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() * diff --git a/Source/Swig/stype.c b/Source/Swig/stype.c index 415b27146..a57222745 100644 --- a/Source/Swig/stype.c +++ b/Source/Swig/stype.c @@ -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() * diff --git a/Source/Swig/swig.h b/Source/Swig/swig.h index 417c02fda..35a67640f 100644 --- a/Source/Swig/swig.h +++ b/Source/Swig/swig.h @@ -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);