Commit graph

22,845 commits

Author SHA1 Message Date
Vadim Zeitlin
faa5d8bbf5 Change value of WARN_C_UNSUPPORTTED for consistency
Use consecutive value for the C-specific warnings instead of reserving
the first and last values of the range for consistency with the other
modules.
2022-01-15 16:40:46 +01:00
Vadim Zeitlin
c1fcf99ea7 Revert change to handling "#@" in SWIG preprocessor
The extra flexibility in expanding "#@" was considered inappropriate,
see the discussion in #2096, so revert the preprocessor part of the
changes of 5bf1497d7 (Allow customizing type mangling in SWIG
preprocessor, 2021-11-10).

This unfortunately means that the macros used in <attribute.i> don't
work for C backend any longer, so a previously passed unit test had to
be disabled.
2022-01-15 16:35:21 +01:00
Vadim Zeitlin
7eb67cecca Restore the use of matrix.desc in GHA workflow file
This is not used currently, but keep it there as this change is not
related to C backend addition.
2022-01-04 01:33:55 +01:00
Vadim Zeitlin
9a2e48b447 Clean up class example header
Apply changes of b115c984a (More cleaning up of the class examples,
2014-05-05) and other similar commits to the header in this branch too.
2022-01-04 01:32:25 +01:00
Vadim Zeitlin
cb974ef662 Use COMPILETOOL with C_LDSHARED and CXX_LDSHARED
This is consistent with the way this is done for the other backends and
allows to avoid overriding these variables for partialcheck target.

Co-Authored-By: Olly Betts <olly@survex.com>
2022-01-04 01:29:21 +01:00
Vadim Zeitlin
92c3b37337 Avoid defining custom assert() macro in one of the C tests
This was inconsistent with all the other tests and the macro suffered
from several problems. Just drop it and use the standard macro with the
same name instead.
2022-01-04 01:26:40 +01:00
Vadim Zeitlin
3b7f772dde Remove extra trailing blank line in examples makefile
This was removed in master but somehow survived in this branch.
2022-01-04 01:24:12 +01:00
Vadim Zeitlin
78e09d5372 Remove commented out code from c/std_except.i
This was added in 32e03aa13 (Many major improvements. Almost all
testsuite compiles now., 2009-04-15) and never really used.
2022-01-04 01:10:43 +01:00
Vadim Zeitlin
0fcdec302c Revert some insignificant changes done in historical C branch
This undoes parts of a2dc2756c (Several major fixes for: arrays, static
members, member func.ptrs., exceptions, ... Lots of tests runs ok now.,
2009-04-13) that shouldn't have been done in the first place.
2022-01-04 01:10:16 +01:00
Vadim Zeitlin
6a96b8d477 Disable warnings about unsupported features in C test suite
There are too many of them currently for them to be useful.
2021-12-09 00:50:26 +01:00
Vadim Zeitlin
e15d3e17ac Allow skipping creating C++ wrappers with cxxignore feature
This can be useful to avoid errors due to features not supported at C++
level in some tests.
2021-12-09 00:50:18 +01:00
Vadim Zeitlin
f06bba320a Make it possible to customize swig_check() definition
Move this function definition to cexcept.swg from the code and only
define it if SWIG_swig_check_DEFINED is not defined yet, which both
simplifies the code in C.cxx and makes exception handling code more
flexible, as it's now possible to predefine SWIG_swig_check_DEFINED
in the code injected into the "cxxheader" section to replace the default
implementation. Show an example of doing this in the documentation and
document handling exceptions with C API too.

The changes above required adding a new "cxxcode" section, corresponding
to the "implementation" part of C++ wrappers and defining a new
SWIG_CXX_WRAPPERS preprocessor symbol to allow only adding C++-specific
code when C++ wrappers are actually generated. Also improve the
documentation of the C-specific sections in the manual.
2021-12-09 00:31:14 +01:00
Vadim Zeitlin
86dbf6bcb5 Generate C++ wrappers for the global functions too
This ensures that global functions can also use C++ classes, enums etc
for their parameters and return types.

C++ wrappers for a couple of tests had to be disabled, but this is not
really a regression as wrapping global functions just made apparent
problems that were not visible before because the corresponding wrappers
were not created at all.
2021-12-08 03:29:09 +01:00
Vadim Zeitlin
1fd4c647b8 Add support for const references to pointers
They're used in some unit tests and we can support them by handling them
just as pointers in C++ wrappers (this was already the case for C
wrappers).
2021-12-08 03:28:44 +01:00
Vadim Zeitlin
3d0fe23e29 Move setting parameter/return type inside do_resolve_type()
No real changes, this just prepares for more changes later.
2021-12-08 03:09:28 +01:00
Vadim Zeitlin
bec35fa0c2 Define "equivalent" typecheck typemap attribute for shared_ptr<>
This is required to handle overloaded functions taking shared_ptr or raw
type correctly and is checked in cpp11_shared_ptr_overload unit test.
2021-12-08 03:09:28 +01:00
Vadim Zeitlin
74e8a6e729 Reenable C++ wrappers for some of the previously failing tests
Document the reason for failure for some of those that still fail
without -nocxx.
2021-12-08 03:09:28 +01:00
Vadim Zeitlin
edff57c2ad Don't generate C++ setters and getters inline
The type of the member may be incomplete, so generate the definitions of
these functions in a separate section, after declaring all the wrapper
classes, just as it was already done for the other functions.

This fixes compiling some of the previously failing tests in C++ mode.
2021-12-08 03:09:28 +01:00
Vadim Zeitlin
cc76e3a1d7 Fix crash for classes with ignored base class
This fixes class_ignore.cpptest.
2021-12-08 00:56:50 +01:00
Vadim Zeitlin
c5cd287ec6 Enable some C++ tests by just using a different namespace
Using "typename" or "dynamic_cast" as C++ namespace names unsurprisingly
resulted in compilation errors, so don't do this.
2021-12-08 00:56:13 +01:00
Vadim Zeitlin
184e620151 Fix $resolvedType expansion for references to pointers
Use the base type, not the full type, to deal with the special case of
references to pointers which are blacklisted by classLookup(), for some
reason, but can be represented at C wrappers level.
2021-12-07 23:55:10 +01:00
Vadim Zeitlin
26810a5f02 Fix support for "numinputs" in typemap argument in C++ wrappers
Ignore the parameters with numinputs==0 for C++ wrappers too, just as it
was already done for C ones.
2021-12-07 23:53:43 +01:00
Vadim Zeitlin
57ba3a7245 Factor out C++ function generation into cxx_function_wrapper
No real changes, just factor out the code for (non-special) functions
generation from emit_member_function() into a separate
cxx_function_wrapper class, so that it could be reused for the global
functions generation too.
2021-12-07 23:24:41 +01:00
Vadim Zeitlin
730384834d Fix dealing with class typedefs in do_resolve_type()
We must use the string for the resolved type, i.e. after replacing
typedefs with their expansions, as otherwise typedefs could leak into
the generated header.

In principle it might be possible to actually use the original typedefs
in the C++ part of the wrappers, but then we'd need to propagate the
typedef definitions there too and we don't do it currently.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
09f22c7453 Remove unnecessary stripped_type variable from do_resolve_type()
It's unnecessary to strip qualifiers ourselves when classLookup()
already does it internally anyhow.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
f3b0b42145 Add cxx_wrappers::is_exception_support_enabled()
No real changes, just add a helper function to make the code more clear.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
a2bd9eec05 Don't generate C++ wrappers for functions using unsupported types
This is far from ideal, but better than generating uncompilable code in
this case.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
a40b7b6d1c Change lookup_cxx_parm_type() to return bool too
This is more consistent with lookup_cxx_ret_type() and makes using it
simpler.

Also move error reporting into this function itself as it could fail for
other reasons than missing "ctype" typemap too.

No real changes.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
f4ecc3c06a Move type-resolving code to cxx_wrappers
This is also not specific to class wrappers, but can, and will be, used
for the global functions, so move it to a place where it can be reused.

No changes yet.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
9f80bc8113 Move void check and error reporting to lookup_cxx_ret_type()
This code doesn't belong to emit_member_function() as it's relevant for
all functions, not just the member ones.

No real changes.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
45feb91551 Move cxx_wrappers definition after cxx_{r,p}type_desc
No changes, just prepare for starting to use the "desc" classes in
cxx_wrappers.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
b072b2dc87 Represent empty shared pointers by null pointers at C level
This is much more convenient and allows checking if the shared pointer
is empty easily, unlike before, when it couldn't be done and adding
support for it would have required adding extra functions.

Also add a way to check whether an object is null in C++ wrappers of the
classes handled via shared pointers and static null() method for
creating null objects of such classes.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
8d9f3e88b2 Expand special variables in cxxcode typemap
This makes the typemap much more useful, as it can now reference the
name of the class it is being expanded for and the type of the C pointer
used by it (in principle, the latter could be recovered by using
decltype(), but this results in rather verbose code inside static
methods where a dummy pointer to the type must be constructed first, so
using another special variable seems like a simpler solution).
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
f9b4ea2841 Define a local variable for sym:name attribute
No changes, this is just a tiny simplification.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
e2d32f33b2 Add support for "cxxcode" typemap
This allows injecting arbitrary code into the generated C++ classes and
can be useful for defining extra constructors or operators, for example.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
fd11a591a3 Add cxx{in,out}type typemaps and use them for std::string
This makes using returning strings much simpler to use from C++ code as
the returned pointers don't have to be deleted manually -- although, of
course, this does require an extra allocation and copy and so should be
avoided for the very long strings.

Add a new runtime test showing how simple and convenient it is to use
the functions working with string using the C++ wrappers now.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
8b871b10fe Replace type_desc with cxx_ptype_desc
This is similar to the last two commits for cxx_rtype_desc and uses a
more appropriate and more specific class for holding parameter type
description.

This is just a refactoring, no real changes yet.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
e3a1301698 Rename cxx_rtype_desc::set_return_xxx() to apply_out_typemap()
Instead of using ad hoc terminology, just call the string used here a
typemap because this is what it actually is.

Also keep just a single function and prepend "$result =" to it
automatically if necessary, as this is more convenient.

Still no real changes, but this will make it simpler to add support for
user-specified cxxout typemap.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
4ab28437a7 Separate cxx_rtype_desc from type_desc class
Maintaining just wrapper start/end fragments is too limiting, at least
for the return type, so allow making the "return action" a formatted
string in which the return value can be inserted at any place.

This will allow making return types more customizable in the upcoming
commits, but there are no changes yet, this is just a refactoring.
2021-12-06 16:50:17 +01:00
Vadim Zeitlin
7c46ff1b6e Add "cxxheader" section to allow injecting extra C++ declarations
This can also be used to include extra C++ headers.

Document this section as well as the already existing "cheader" one.
2021-12-06 16:50:17 +01:00
Vadim Zeitlin
a335fff2ec Avoid errors due to generating identical overloads
Add typecheck typemaps for primitive types and string and call
Swig_overload_check() to ensure that we don't generate two wrappers
functions taking the same "const char*" type if we have overloads taking
it and "std::string" (or a reference) in the original code.
2021-12-05 20:49:20 +01:00
Vadim Zeitlin
4a3ae6f8d3 Don't generate C++ wrappers for overridden virtual functions
This is harmless, but also useless, as the implementation is the same as
the existing one in the base class anyhow.
2021-12-05 20:24:38 +01:00
Vadim Zeitlin
aacc930023 Streamline and improve std::string typemaps
Avoid unnecessary heap allocations, just use temporary variables.

Actually update the string parameters passed by pointer/non-const
reference. This requires the pointers passed to actually be non-const,
so update the C-specific unit test runme to use a char buffer instead of
a literal string.

Also simplify the code copying the string contents to just use strdup()
(if there are ever any platforms where this POSIX functions is not
available, we could just define it ourselves once instead of using
strlen() + malloc() + memcpy() manually twice).
2021-12-05 03:31:35 +01:00
Vadim Zeitlin
628e2cf6d2 Make std::{set,map} has() and has_key() methods const
They don't modify the object, so should be callable on const objects.
2021-12-04 17:47:27 +01:00
Vadim Zeitlin
c4187f495f Fix definition of move assignment operator for wrapper classes
Classes not deriving from another class in the hierarchy must take care
of freeing their current pointer before reassigning it.

This should have been part of 3f3438093 (Define move ctor and assignment
operator for C++ wrappers, 2021-11-24).
2021-12-03 03:53:36 +01:00
Vadim Zeitlin
bfc6623bbd Make SWIG_CException dtor public to fix memory leak
The actual dtor was never called when the wrapper object was destroyed
before, due to dtor being private.
2021-12-03 03:38:33 +01:00
Vadim Zeitlin
19e0ecbb9d Don't leak the pointer returned by SWIG_CException::get_pending()
This pointer is always new (if non-null) and so must be deleted.
2021-12-03 03:38:04 +01:00
Vadim Zeitlin
d33e76e045 Fix bug with deleting even non-owned pointers in C++ wrappers
Only take ownership of the objects returned from functions that are
explicitly annotated with %newobject or from functions returning objects
by value -- as in this case nothing else can own the returned object
anyhow.

This required changing the code slightly to let do_resolve_type() access
the function node pointer, as the information we need is only available
in it and not in the dummy node passed to us when we're called from
inside Swig_typemap_lookup() due to $typemap() expansion.
2021-12-03 03:00:17 +01:00
Vadim Zeitlin
efdfad6565 Avoid redundant casts for function result in generated code
No real changes, just avoid somewhat ridiculously looking consecutive
casts to the same type in the generated code.
2021-12-02 00:31:33 +01:00
Vadim Zeitlin
e4bb84f3bd Represent scoped enums as scoped enums in C++ wrappers
This is better than turning them into unscoped enums with a prefix for
the elements.
2021-11-30 02:30:46 +01:00