Commit graph

22,709 commits

Author SHA1 Message Date
Vadim Zeitlin
f7f59f4648 Rename module_prefix to just module_name
It is used as a prefix, but it's really just the name of the module, so
the new name is less confusing.

There also doesn't seem to be any need to copy/delete it, just keep a
pointer, it's not going to change.

No real changes.
2021-11-27 00:19:05 +01:00
Vadim Zeitlin
43c7aefeb1 Don't leak namespace prefix variable
No real changes, just delete a local variable.
2021-11-27 00:19:05 +01:00
Vadim Zeitlin
f42b0eaa71 Use different executable names for C and C++ examples
Trying to create the same "runme" executable for both C and C++ examples
could result in errors when using parallel make, as one of the targets
could fail to write to the file being currently executed.

Using separate names works around this problem.
2021-11-27 00:19:05 +01:00
Vadim Zeitlin
d2546e23ff Don't use exception specification with C++ compiler in example
Support for the exception specifications using types was removed in
C++17 (and "throw ()" in C++20), so don't use them when using the C++
compiler any longer, as this broke the example with recent g++ versions
that use C++17 by default.

We still need them for SWIG, however, so use SWIG_THROW macro, defined
differently for SWIG and the compiler, to preserve the existing
behaviour.

Using %except might be a better idea, but would require more changes.
2021-11-24 20:16:42 +01:00
Vadim Zeitlin
43276f22a7 Enable testing some C++ wrappers code in the test suite
C++ wrappers still don't compile for about a hundred tests, but they do
compile for almost 500 more of them, so it's still valuable to be able
to check that there are no regressions for those that do work now.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
adb96f8b7a Always pass wrapper objects by const reference in C++ wrappers
Passing them by value doesn't seem to have any advantages, as the copy
will be made internally anyhow if the original function took them by
value and doing an extra one doesn't seem necessary, but does have a big
drawback of not compiling if the class doesn't define a copy ctor.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
3f3438093d Define move ctor and assignment operator for C++ wrappers
This can be done naturally and there doesn't seem to be any reason not
to do it.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
79e9d43c1b Disable broken default-generated copy ctor in C++ wrappers
We can't rely on the default-generated copy ctor because it simply
copies the (possibly) owned pointer, resulting in double destruction if
it's ever used, so disable it completely unless it is explicitly
declared in the original class, in which case we can wrap it using the C
function wrapper defined for it.

Note that we probably should also wrap default-generated copy ctor of
the original class, if it has any, but currently we don't do it even at
C wrappers level, and so can't do it for C++ wrappers neither.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
03c6099ac1 Define "classname" in cxx_class_wrapper dtor too
No changes, just a tiny refactoring before the upcoming commit.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
8d1fbbacb3 Use correct base class C proxy name even if it's not set yet
Replace C::getProxyName() with global get_c_proxy_name() and use it in
cxx_class_wrapper rather than relying on "proxyname" attribute being
already set because this might not be the case if the base class is
defined in another SWIG module just %import'ed from the current one: in
this case, the class is known, but no wrappers had been generated for it
yet, so "proxyname" for it is not set.

Note that, unlike getProxyName(), the new function doesn't bother with
always copying the string, as we may be sure it remains valid as it's
referenced by the node, as one of its attributes.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
b0c9991164 Separate forward declarations and implementations of C++ wrappers
This allows the generated code to compile when different classes depend
on each other, as the class must be forward-declared to be mentioned at
all (as either parameter or return type) and fully defined in order to
be used in either role, as we need to use its ctor or call swig_self()
on it.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
a13da1f8a9 Refactor code to use cxx_wrappers struct
No real changes, just prepare for adding more File objects to be used
for C++ wrappers generation: we can't generate all of them in a single
pass because there may be dependencies between classes.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
a980c036d6 Fix $typemap expansion for C++ wrappers
This required slightly changing the approach used for resolving types in
emit_member_function(), as we can't resolve the type and the casts
to/from it independently any more, but need to do both at once, as this
is required when it's done indirectly due to $typemap() expansion, and
we can't make "return" part of the returned wrapper as we could end with
multiple "returns".

So replace expr_wrapper with type_desc containing both the wrappers and
the type itself and add "return" in emit_member_function() itself for
non-void functions. Also use IIFE so that we can always just prepend
this "return" in the beginning.

Also connect things up so that C++-specific type resolution function is
used while C++ wrappers are being generated.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
e78f5dac45 Add temp_ptr_setter and use it with cxx_class_wrapper
The new helper class will be also useful in other places, so extract it
from cxx_class_wrapper.

No real changes, this is just a refactoring.
2021-11-24 02:41:33 +01:00
Vadim Zeitlin
7c6fb542d3 Convert li_boost_shared_ptr runtime test to C++ from C
It's simpler to write tests in C++ rather than C and checking the
generated C++ API also checks the C API it uses underneath, so there is
no need to have both.
2021-11-23 01:29:26 +01:00
Vadim Zeitlin
c434b867d7 Strip namespaces from parameter types
This fixes e.g. li_boost_shared_ptr unit test, which uses Space::Klass
that should be known as just Klass (inside the outer namespace) in the
generated C++ wrappers.
2021-11-23 01:29:26 +01:00
Vadim Zeitlin
036e3eb08c Correctly skip ignored base classes in C++ wrappers
Don't refuse to generate C++ wrapper class if there is just one real
base class and the other one(s) are ignored.
2021-11-23 01:29:26 +01:00
Vadim Zeitlin
0483e4017c Start adding C++ wrappers generation
Disable them for the test suite, as plenty things don't work yet, but
there is already more than enough code to not want to add even more
fixes to the same commit.
2021-11-23 01:29:26 +01:00
Vadim Zeitlin
606c1c6ea0 Make sure gcc gives error for passing scoped_dohptr to Printv()
After making this mistake too many times already, make sure it doesn't
happen again by requiring gcc to give an error for any attempt to pass
a non-trivial object to vararg functions such as Printv(), Printf(),
Swig_error() etc.
2021-11-21 16:42:11 +01:00
Vadim Zeitlin
7cbbfab198 Show using C++ wrapper API for the "class" example
This almost exactly mirrors the existing C examples, but modify both
examples slightly to make their output show which language is used.
2021-11-19 02:04:41 +01:00
Vadim Zeitlin
55d7db6b27 Get rid of an unnecessary intermediate string
Write the wrapper types and declarations directly to f_wrappers_h,
without passing by an intermediate f_wrappers_h_body which just
complicated things unnecessarily.

This means that "extern "C" { ... }" is now around the functions
declarations only, and not around the types and the declarations, but
this seems like a desirable side effect.

No real changes.
2021-11-19 00:08:44 +01:00
Vadim Zeitlin
8d1faa5b3b Rename various files used in C backend code
Also explain better what each of them is used for and use scoped_dohptr
instead of deleting them manually.

Stop writing everything to f_begin, which didn't make any sense, and use
a separate f_wrappers_cxx object for the contents of the cxx output file.

No real changes.
2021-11-19 00:08:44 +01:00
Vadim Zeitlin
2454efb989 Use Dump() instead of Wrapper_pretty_print()
This should be faster and we don't really need to prettify the wrappers
code which is already quite decent.
2021-11-19 00:08:44 +01:00
Vadim Zeitlin
afc02cbc01 Remove unused f_wrappers_cxx
This variable wasn't used since a long time and removing it doesn't
affect generated code.
2021-11-19 00:08:44 +01:00
Vadim Zeitlin
2ab549611b Syntax check generated headers using both C and C++ compilers
For C++ tests, check header compilation using C++ compiler too, as this
detects constructs valid in C but invalid in C++ and will also be useful
for checking C++-specific parts of the headers that will be generated in
the future.
2021-11-19 00:08:44 +01:00
Vadim Zeitlin
0f734e1164 Don't generate empty enum declarations
They are invalid in C++ and there shouldn't actually be any in valid
input, but they do occur in SWIG test suite.

Don't generate anything for them in the output, especially because doing
this is not really more complicated than using "enum_is_empty" field
that we had previously and might even be a tad simpler, as it doesn't
split the "{" and "}" output between two different functions.
2021-11-19 00:08:43 +01:00
Vadim Zeitlin
e7fa72fc06 Don't generate invalid forward enum declarations
Such declarations are invalid without specifying the underlying type
(which is only possible since C++11) and are useless anyhow, so just
don't bother emitting them.
2021-11-19 00:08:43 +01:00
Vadim Zeitlin
3765a08743 Allow having C++ test cases for C backend
Support compiling and running either _runme.c or _runme.cxx files for
the given test (but not both).

Add a simple C++ test file to check that it actually works.
2021-11-19 00:08:43 +01:00
Vadim Zeitlin
5219338160 Fix previously unused access_change unit test runme file
This file had a wrong name and so wasn't used at all.

Do use it now, after renaming it to the required name and fixing its
compilation.
2021-11-10 02:17:07 +01:00
Vadim Zeitlin
e8f9bdba80 Remove wrapper aliases generation and use -namespace in the tests
The -namespace option provides a better way of using the wrapped API, so
drop the optional wrapper generation, which is useless when this option
is used and just generates many lines of unwanted junk in the header.

Update the test suite and the examples to compensate to not rely on
being able to define SWIG_DEFINE_WRAPPER_ALIASES and add -namespace
option to all C++ tests, as it's done for C# test suite, and update them
to use the correct prefix and also use the accessors for the global
variables rather than using them directly, as this is impossible when
namespace prefix is used (it would have been possible to define a
preprocessor symbol corresponding to the real variable name, but it's
arguably not worth it).

fixup! Remove wrapper aliases generation and use -namespace in the tests
2021-11-10 02:11:59 +01:00
Vadim Zeitlin
3ebf1c1769 Remove redundant cpp_basic_xxx tests
These tests duplicate the other, more complex, existing unit tests, so
it's just not useful to have them at all any more (they could have been
useful at the very beginning of C backend development, when none of the
other tests worked).
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
11bd97653f Inline functions used only once in functionWrapperCPPSpecific()
No real changes, just keep everything in a single function because it is
more clear than having both it and functionWrapperCPPSpecificWrapper()
doing the same thing.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
2f4a09fb21 Avoid calling getFunctionWrapperName() twice for each function
Pass the wrapper name to emit_wrapper_func_decl() to save on an extra
call to this function.

No real changes, this is just an optimization.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
fbf976768a Get rid of "c:globalfun" attribute
Use getCurrentClass() and other existing attributes to check if the
current node is inside a class or not (and hence is global), this is
more explicit and avoids confusion for static member functions that were
marked as being global in spite of being nested inside a class.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
8594a8def6 Use SWIG-specific suffix for non-overloaded %extend functions too
This avoids conflicts between functions synthesized by %extend when
adding static methods to an existing class, and the actual wrapper
functions generated by the backend.

This shouldn't result in any user-visible changes.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
70309fcb0a Refactor code in Language::staticmemberfunctionHandler()
No real changes, just move the test for "code" to the outer scope to
facilitate the upcoming changes.

This commit is best viewed ignoring whitespace-only changes.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
6833ead6ed Add -namespace option to use unique prefix for all wrappers
Using this option allows to prefix all exported symbols (functions,
enums and enum elements) with a prefix based on the given namespace.

Note that (global) variables can't be exported directly when using the
global namespace prefix, even if they are of C-compatible type.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
54af856d09 Fix typemaps for arrays of objects and object pointers
These typemaps never worked, but this went unnoticed until now because
the nonsensical "ctype" expansion generated by them still compiled.

With types possibly having a namespace prefix, they didn't any longer,
so define them correctly for the objects using "$resolved_type" and
define the typemap for "void*[]" separately, as "$resolved_type" can't
be used for it.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
5bf1497d7b Allow customizing type mangling in SWIG preprocessor
Use the "type" naming format for the types mangled by "#@" and "##@"
preprocessor operators, in order to allow customizing them for a
particular backend.

This isn't used by any backend yet, so this doesn't change anything so
far.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
cda93bd903 Fix enum value translation
Don't rely on hack with checking the value length and looking it up,
just check its type instead.
2021-11-07 02:29:01 +01:00
Vadim Zeitlin
f4ee8e5369 Fix names of enums and their elements
Use the containing class name as prefix, and add the name of the enum
itself to the prefix for the scoped enums, instead of doing something
strange and semi-random that we did before, with prefixes including the
namespace (which should never be the case without "nspace" feature), but
not the scoped enum.

This also fixes renaming of enum elements, as a side effect, which
didn't work before, add run test for enum_rename unit test to prove it.
2021-11-07 02:14:16 +01:00
Vadim Zeitlin
81d1fec13c Remove code setting unused "c:retval" attribute
This became unused after the changes of fd3e76365 (Streamline and fix
returning objects by value, 2019-08-06) and allows to simplify code
further as we don't need to override static and non-static member
function handlers at all any more now.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
ec4cfd41de Return scoped_dohptr from get_wrapper_func_return_type()
Ensure that the returned pointer is deleted automatically instead of
doing it manually.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
07e7086b39 Generate wrapper function name in a single place
Rename getGlobalWrapperName() to getFunctionWrapperName() and make it
work for all functions, not just global ones.

This means that we can test whether the function is global or not in a
single place instead of doing it in two different ones.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
7e5e6a4cf0 Avoid unnecessary allocations of function name
Use maybe_owned_dohptr in function wrapper code to avoid making a copy
unnecessarily just to be able to Delete() this pointer later.

No real changes.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
4801a3154e Delete never used variables in function wrapper code
No real changes, just don't bother allocating and deleting strings that
are never used at all.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
ad3db88e44 Add maybe_owned_dohptr helper
This class allows to avoid allocating a new DOH object unnecessarily if
we can reuse an existing one.

It is not used yet, but will be soon.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
ca56f14aa5 Refactor scoped_dohptr() to reuse its reset()
No real changes, just avoid some code duplication and add an optional
argument to reset() to make it more compatible with std::unique_ptr<>
and also more flexible.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
8014af974d Revert accidental change to Python example
This was done way back in f84342a30 (Modified parameter handling using
typemaps. 'Reference' example. Visibility hint now applies only to the
global functions., 2008-06-28), surely accidentally.
2021-11-04 01:31:06 +01:00
Vadim Zeitlin
63a229a40f Don't disable experimental warning for C in GHA workflow any more
This is not necessary any longer after the previous commit and is better
than setting SWIG_FEATURES in the workflow file, as this resulted in
showing "-w524" in the name of C CI build since the changes of 04a6ad3bb
(Add SWIG_FEATURES into GHA name, 2021-10-21), which was a bit ugly.
2021-11-02 17:51:14 +01:00