Commit graph

95 commits

Author SHA1 Message Date
Vadim Zeitlin
208980f4de Avoid gcc warnings for nested anonymous enums in the generated code
Output anonymous enums defined inside C structs in the global scope, this
doesn't change the semantics as the constants defined inside a struct still
have global scope in C, but avoids gcc warnings given when compiling the
generated code.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
6813c7eacf No changes, just write emit_c_struct_def() in a more natural way
Don't force the caller to pass firstChild() of the struct node to this
function, just pass it the struct node itself and iterate over all of its
children inside it.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
c8e4abd16e Use wrapper functions for global variables of non-C types
Don't try to export the variables whose type can't be represented in C
directly, this can't work.

Instead, just let use the default implementation to generate wrapper functions
for them.

Update "cpp_basic_global_var_class" unit test accordingly and remove it from
the list of the failing tests.
2016-04-20 18:45:42 +02:00
Vadim Zeitlin
200ea0e37d Don't output "decl" attribute into the wrapper declarations
This doesn't make any sense as "decl" is a SWIG type string and not a C
declaration.
2016-04-18 20:30:49 +02:00
Vadim Zeitlin
1f6ce1ed64 Reuse existing wrapper declarations code for C functions too
We just copied the function prototype without any changes from the original C
function to the wrappers header when in C mode, but this wasn't correct, e.g.
the original function could use typedefs not available in the wrapper.

Fix this by applying the typemaps in C mode too, but without the C++-specific
parts.

This also makes C and C++ code paths slightly less different from each other,
the long-term goal is to make them identical.
2016-04-18 20:30:49 +02:00
Vadim Zeitlin
c4036fdc58 Fix handling of forward-declared enums
Don't output the enum body if it just forward-declared, empty enums are
invalid in C.
2016-04-18 01:09:29 +02:00
Vadim Zeitlin
44d68197d6 Hack for "true" and "false" used as enum element values
This is not a full solution by far, but should address the most common case.

This also allows "cpp_enum" unit test to pass.
2016-04-16 00:45:58 +02:00
Vadim Zeitlin
3d21bb2c96 Hack around parser bug with char enum element values
Enum element somehow lose the single quotes around them, compensate for it in
C module code (other modules use module-specific constvalue feature to work
around it, but it seems better to do it in SWIG itself rather than leaving the
user code to deal with it).

This finally makes the "enums" unit test pass.
2016-04-16 00:25:50 +02:00
Vadim Zeitlin
168e16ec67 Hack to support variables of anonymous enum types
Declare the variable as int as this is how variables of enum types are stored
(although this would have to be revised when support for C++11 base enum type
is added) and, although ugly, this at least allows the generated code to
compile.

This fixes some (but not all yet) errors in the "enums" and "cpp_enum" unit
tests.
2016-04-16 00:25:10 +02:00
Vadim Zeitlin
fb4d700279 Represent enums as themselves in generated code
Define enum type and enum values as the elements of this enum instead of using
preprocessor #defines for them.

This fixes a couple of enum-related unit tests.
2016-04-16 00:10:48 +02:00
Vadim Zeitlin
dbb9cc250c No real changes, just narrow scope for a variable
Initialize "baselist" just where it is needed.
2016-04-16 00:10:47 +02:00
Vadim Zeitlin
4995e96009 Fix double delete in C mode resulting in crashes later on
This was broken by f1ec1a26 which removed a call to Copy() but not the
matching call to Delete().
2016-04-16 00:10:22 +02:00
Vadim Zeitlin
f9a2d81f29 Use a constant for the indentation in the generated C code
Instead of hard coding two spaces, use "cindent" constant to make it more
clear what is the intended indentation of the generated code and also to make
it possible to change it later easily if desired.
2016-04-16 00:10:20 +02:00
Vadim Zeitlin
c05f03d485 Fix esoteric regression in the previous commit
Implement the check for duplicate SWIG_derives_from<> specializations which
could happen if a template type with default parameters was exported both with
implicit default values used for these parameters and the same values
specified explicitly.

This makes the code more complicated and is very rarely needed (in addition to
the above, the template class must also have a base class to trigger the bug),
but it does happen and used to work, so restore this after breaking it in the
last commit and reenable the template_default2 unit test.
2016-04-16 00:06:35 +02:00
Vadim Zeitlin
818b399c9e Drastically simplify and optimize object creation and destruction
Don't populate the typenames array for each and every created object and don't
search it when deleting every object, this is O(N) in number of classes and is
completely impractical for any big library where N can easily be several
thousands. Use destructor function which will correctly destroy the object
instead.

Also don't store each created objects in the global registry, there doesn't
seem to be any point in it.

And, in fact, don't bother with the typenames at all, just use another
pseudo-virtual function for checking whether a class inherits from a class
with the given name.

There is unfortunately one problem with the new approach: it doesn't work when
the same C++ type is wrapped under different names as this results in multiple
specializations of SWIG_derives_from<> for this type. But this is a relatively
rare situation (but which does arise in template_default2 unit test, which had
to be disabled) and could be fixed in the future by completely resolving the
type, including the default template parameters values, and checking if
SWIG_derives_from had been already specialized for it. In the meanwhile, this
regression is not a big deal compared to the advantages of the new approach.
2016-04-15 23:25:32 +02:00
Vadim Zeitlin
8ff7941503 Don't generate delete functions if dtor is not accessible
This fixes failures in virtual_dtor and using_protected unit tests.
2016-04-15 23:01:20 +02:00
Vadim Zeitlin
4962871cd9 No real changes, just remove an unused variable
"sobj" in C::classHandler() was not used at all.
2016-04-14 02:44:46 +02:00
Vadim Zeitlin
62230758e4 Output declarations of all types before the functions declarations
This fixes compilation of the generated code when a function uses a forward
declared type as was the case in e.g. std_vector example.
2016-04-14 02:44:46 +02:00
Vadim Zeitlin
3d6880aad1 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.
2016-04-14 02:44:45 +02:00
Vadim Zeitlin
a1fedd5202 Minor cleanup: remove commented out code
No real changes, just remove commented out code and comments that didn't make
sense.
2016-04-13 18:10:36 +02:00
Vadim Zeitlin
5cfb66c1fb Remove unused functionWrapperCPPSpecificWrapperSetReturnType()
This function was never used anywhere since its addition back in
f1ec1a26a7.
2016-04-13 18:10:36 +02:00
Vadim Zeitlin
6bbe375a37 Remove unused SWIGPROTECT
It doesn't make any sense to use protected visibility for the symbols in the
proxy code, the only use for it would be if it could be applied to the
original functions being wrapped, to prevent them from being shadowed by the
proxy versions, but this cannot be done, in general.

Apparently this was already understood as the code using SWIGPROTECT was
commented out, but ef85d0d43f didn't give any
reason for doing this, nor for leaving this code.

Assume there was none and just remove it completely now to avoid confusion.
2016-04-13 18:10:35 +02:00
Vadim Zeitlin
7c402ad148 Merge branch 'master' into C 2016-04-08 17:29:16 +02:00
Leif Middelschulte
4ca26c150e Revert "Fix shadowing variable declaration", because it breaks c++ return value type casting in wrapper functions.
This reverts commit 818a9939ecf10ee6cde3e4e389eaf748ec3999d6.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13709 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-20 18:55:51 +00:00
Leif Middelschulte
d906d3c73e Reuse 'cmodtype' typemap and remove 'couttype' typemap completely.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13681 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-20 13:23:18 +00:00
Leif Middelschulte
5aa23f9bdb Reuse 'ctype' typemap and remove 'proxycouttype' typemap.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13680 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-20 13:23:03 +00:00
Leif Middelschulte
a894545c9a Rename 'proxy' to 'ctype' as it's the typemap used for the C API.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13679 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-20 13:22:48 +00:00
Leif Middelschulte
fcecbf17a2 Rename 'ctype' typemap to 'cmodtype' since it's only used for internal (wrapper) stuff.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13678 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-20 13:22:33 +00:00
Leif Middelschulte
b25f28938b Reuse 'ctype' typemap to cast wrapper calls. Reduces number of typemaps for the cost of unnecessary casts (e.g. 'int' to 'int').
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13640 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-17 17:22:06 +00:00
Leif Middelschulte
0dbfbb79ac use correct typemap for wrapper function.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13603 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-13 16:21:36 +00:00
Leif Middelschulte
8538e6c1db Fix wrong type.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13584 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-10 14:49:49 +00:00
Leif Middelschulte
a95e257fe8 unused variable--
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13566 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-08 16:06:23 +00:00
Leif Middelschulte
b6d00028bb Remove name_mangle approach. Broke template function names. Should work like classes, etc. based on "nspace" attribute of node, which is for some reason missing for this kind.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13565 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-08 16:06:09 +00:00
Leif Middelschulte
725c7f395a remove debug printfs
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13553 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-07 20:30:00 +00:00
Leif Middelschulte
d6641336f8 Initial work for templates. Mostly adopted from C#.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13552 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-07 20:18:21 +00:00
Leif Middelschulte
4bc1223c1a Fix segfault of swig due to virtual methods
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13402 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-07-25 14:30:46 +00:00
Leif Middelschulte
e16672f90f Fix shadowing variable declaration
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13401 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-07-25 14:30:34 +00:00
Leif Middelschulte
1c4c1b13d8 Fix proxy return type namespacing
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13341 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-07-23 10:35:15 +00:00
Leif Middelschulte
433a1743ab Namespace functions as well; Remove :: from tl elements
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13332 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-07-18 23:35:22 +00:00
Leif Middelschulte
cfabfd92d2 Add initial namespace support for C backend.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13323 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-07-15 23:25:29 +00:00
Leif Middelschulte
122931dc59 Add 'struct' to typedef of SwibObj_Class declaration.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13186 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-06-19 17:18:39 +00:00
Leif Middelschulte
ae5413ff74 Remove macros "IS_EQUAL" and "IS_SET";Stay in line
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13170 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-06-16 18:58:10 +00:00
Leif Middelschulte
ed7e3143ce Use different types to generate typesafe API.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13169 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-06-16 18:57:55 +00:00
Leif Middelschulte
ef85d0d43f Remove SWIGPROTECT from generated code for now.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13168 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-06-16 18:57:41 +00:00
Leif Middelschulte
f1ec1a26a7 Use seperate typemaps for wrapper and proxy.Refactor bits.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13167 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-06-16 18:57:27 +00:00
Leif Middelschulte
a351c3441c Refactor the c module; initial split of wrapper/proxy
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13164 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-06-12 00:31:09 +00:00
Leif Middelschulte
162ed6931a Fix attribute definition.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13103 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-05-22 17:14:59 +00:00
William S Fulton
ee2979b362 Add some changes that are needed in all modules for the runtime banner
From: William S Fulton <wsf@fultondesigns.co.uk>

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@13040 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-05-07 17:02:21 +00:00
William S Fulton
ffe2a11294 Compile time fixes for C module since merge from trunk
From: William S Fulton <wsf@fultondesigns.co.uk>

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@13039 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-05-07 17:02:00 +00:00
William S Fulton
cebb2aaa93 Re-apply c.cxx changes from r11187
From: William S Fulton <wsf@fultondesigns.co.uk>

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@13038 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-05-07 17:01:40 +00:00