Commit Graph

5672 Commits

Author SHA1 Message Date
William S Fulton ef0bbcd6ed Document Python stable ABI support
Remove -stable-abi option: going forwards enabling the stable ABI
simply requires setting Py_LIMITED_ABI when compiling the wrappers.
2023-12-18 23:30:36 +00:00
William S Fulton a75b87d318 Rename -py3-stable-abi option to -stable-abi
Since this was first added, the -py3 option has been dropped
and the Stable ABI is known as exactly that, not the Python3 Stable ABI.

Document the new option and turn on c++20 testing for it to cover
std::filesystem and std::string_view testing which are affected
2023-12-06 08:01:22 +00:00
William S Fulton 265065b677 Merge conflict fixes and consistent error reporting in stable abi checks 2023-12-04 19:18:19 +00:00
William S Fulton 1451607eda Merge branch 'py3-stable-abi'
* py3-stable-abi:
  Add CI build using -py3-stable-abi option
  Make Python buffer typemaps compatible with limited API
  Don't use PyUnicode_AsUTF8() when python limited API is used
  Make directors implementation for Python work with limited API
  Support using stable Python ABI

Conflicts:
	.github/workflows/ci.yml
	Lib/python/pyhead.swg
	Lib/python/pyrun.swg
	Source/Modules/python.cxx
2023-12-01 18:38:07 +00:00
Olly Betts ddb8791f92 Update comment referencing removed GH API 2023-12-01 11:40:20 +13:00
Olly Betts ab6397a891 Removed unused T_ENUM code
As best I can make out T_ENUM has never been used since it was added by
an apparently unrelated commit in 2000:
bcc6cbfb16
2023-12-01 08:59:30 +13:00
Olly Betts da018ed6f7 Support type deduction of enums
Fixes #2715
2023-12-01 08:57:41 +13:00
Olly Betts bf99d0f28a Address decltype(foo(0)) segfault better
The underlying problem here is that NewSwigType() returns an empty
string for T_USER and some other codes, so instead check for that
in deduce_type() and map to NULL instead.

This should fix any other such cases which currently segfault.
2023-12-01 08:24:40 +13:00
Olly Betts d2c0105d72 Fix segfault on decltype(foo(0))
We don't yet handle inferring the type of a function call, but we
shouldn't segfault.

No regression test as I couldn't see how to write one (if I use
%ignore the segfault isn't triggered) but hopefully we can teach
SWIG to actually deduce the type in this case.
2023-11-30 18:03:28 +13:00
William S Fulton 8d17973f08 Add support for C++20 constexpr destructors
In the parser, cpp_end and cpp_vend are very similar. cpp_end is removed
and instead replaced by cpp_vend and additional checks that ensure a
non-virtual destructor does not have a pure specifier.
2023-11-24 20:12:02 +00:00
Olly Betts b5ca500b40 Deduce type of TYPE(EXPRESSION)
This is actually an easy case - e.g. the type of `double(4)` is
`double`.
2023-11-19 22:08:25 +13:00
Olly Betts 967dd4e5c1 Fix dropping of parentheses around < and > expressions
We were dropping parentheses in these cases, which can change the value
of the expression.
2023-11-19 16:28:13 +13:00
Olly Betts 25654e4ad9 Fix deduced result type of bitwise not
This wasn't getting integer promotion applied to it.
2023-11-19 16:27:17 +13:00
Olly Betts 997616e652 Improve type deduction
Create a new T_UNKNOWN type code and use this for the cases where
we previously abused T_INT.  This means we can now reliably deduce
`int` when we see T_INT.

Fix the deduced result types of unary plus and unary minus which weren't
getting integer promotion applied to them.

Fix the deduced result type of the C++ logical not operator which was
`int` but should be `bool`.
2023-11-18 18:27:29 +13:00
William S Fulton 7b4e562dc1 Fix for closing off C# property declaration
Consistently handle variables as constants or as variable wrappers
to match code in Language::staticmembervariableHandler(). This fixes
the following:

  #define constexpr
  %immutable Foo::Constant;
  struct Foo {
      static size_t constexpr ConstantA = 22;
      static constexpr size_t ConstantB = 64;
  };

which is actually invalid C++, but being done to workaround a SWIG
parser limitation for parsing ConstantA (ConstantB is okay) if constexpr
is left in.

Closes #2573
2023-11-17 07:47:43 +00:00
Olly Betts 3ce0174a0c Fix random doubled spaces in code 2023-11-17 09:49:36 +13:00
Christopher Chavez c7e1f591c2
[Tcl] Migrate `CONST` to `const`
`CONST`from tcl.h was for compatibility with systems lacking `const`.
It is to be deprecated in Tcl 8.7, and removed in Tcl 9.0.
2023-11-15 06:28:26 -06:00
Olly Betts 13eca97013 Support -std= command line option
SWIG now supports command line options -std=cXX and -std=c++XX to
specify the C/C++ standards version.  The only effect of these options
is to set appropriate values for __STDC_VERSION__ and __cplusplus
respectively, which is useful if you're wrapping headers which have
preprocessor checks based on their values.

Closes #2591
2023-11-09 12:27:44 +13:00
William S Fulton 3cfaae48ef Add csbegin, dbegin, javabegin for %module
[C#] Support nullable reference types. A generic C# option to the
%module directive allows one to add in any code at the beginning of every
C# file. This can add the #nullable enable preprocessor directive at the beginning
of every C# file in order to enable nullable reference types as follows:

  %module(csbegin="#nullable enable\n") mymodule

Closes #2681

[D, Java] Add the dbegin option to the %module directive for generating code at
the beginning of every D file. Similarly javabegin for Java. This enables one
to add a common comment at the start of each D/Java file.
2023-11-06 19:12:35 +00:00
William S Fulton 68b0fd809f $typemap() fix for handling variable overrides
Fix when variable override contains a pointer dereference ->.
This enables use of $1.x as a variable override value as SWIG
replaces $1.x with a pointer dereference expression, such as
(&arg1)->x.

Added a testcase showing how Python typemaps could alternatively
be written using $typemap() instead of using C++ templates as used in
the UTL. I'm not convinced this is fully reliable or even a good idea,
so the variable replacements in $typemap() remain undocumented.
2023-11-03 09:07:51 +00:00
William S Fulton e8d3c74590 Improvements to $typemap() special variable replacement overrides
In $typemap(), the $n special variables are replaced by the appropriate
value for the type associated with the typemap calling $typemap().
The (undocumented) special variable overrides now also support
controlling what the $n special variables are replaced with from the
calling typemap, for example:

  %typemap(in) std::pair<std::string, int> {
    int& input_value_second = $1.second;
    $typemap(in, int, 1=input_value_second);
    ...
  }

replaces $1 in the int typemap with input_value_second instead of
whatever is the default for the target language (a variable that holds
the int value after marshalling from the target language).

This additional functionality might make it possible to replace the C++
templates used in the UTL with a much simpler system of typemaps utilising
$typemap(). See follow on commit to typemaps.c.
2023-11-03 09:07:51 +00:00
William S Fulton d701f0b4b2 Correct the implementation of Python PEP 207.
Don't convert all exceptions into a NotImplemented return
when wrapping operators which are marked with %pythonmaybecall.

Closes #1783
2023-10-21 09:25:10 +01:00
William S Fulton b354e5b871 Merge branch 'part3'
* part3:
  Correct initialization order in Language
  Remove redundant initialization code
2023-10-19 20:11:35 +01:00
William S Fulton 5e5549929c Merge branch 'part2'
* part2:
  Use single characters instead of strings where possible
2023-10-19 20:11:31 +01:00
William S Fulton 9a691f44c7 Correct initialization order in Language 2023-10-19 08:47:45 +01:00
William S Fulton 5eb3810dac C++11 enum base type is now used for D enum base type 2023-10-18 19:57:20 +01:00
William S Fulton af72fbb08b C++11 enum base type is now used for C# underlying enum type 2023-10-18 19:56:08 +01:00
Rose 5932fff4e2 Use single characters instead of strings where possible
This makes the code clearer and faster
2023-10-16 17:51:48 -04:00
Rose c4a4717a28 Remove redundant initialization code
Some of this code is redundant and taken care of by the initializer.
2023-10-16 16:48:04 -04:00
William S Fulton 791d0a5e17 Further using declarations fix for inheritance hierarchies
more than two deep and the using declarations are overloaded.
Using declarations from a base class' base were not available for use
in the target language when the using declaration was before a method
declaration.

Closes #2687
2023-10-16 20:05:34 +01:00
William S Fulton 40ed95c4f7 Swig_symbol_clookup_check bug fix
Don't drop the checkfunc when processing using nodes.
I can't find any use cases for this fix, but this
fix seems like the right thing to do.
2023-10-16 20:05:34 +01:00
Olly Betts 4a618cd646 Fix misindented line 2023-10-11 08:54:37 +13:00
Momtchil Momtchev 6b5ae59b03 to run the CI 2023-09-13 10:19:40 +02:00
William S Fulton 68ec78b37a Add test for C++ boolean types in preprocessor
preproc_cpptest was missing in common.mk

Issue #2394
2023-09-11 20:41:11 +01:00
Mario Emmenlauer 9ed26fcf66 Source/Preprocessor/expr.c: Support boolean values in preprocessor expressions 2023-09-11 19:18:12 +01:00
William S Fulton 64c9720a7a Complete transition to rtypecheck typemaps from hard coded logic
See 973590ff91.

The rtypecheck typemaps implement typechecking in R for each function
parameter using functions such as is.numeric, is.character, is.logical,
is.null etc.

Closes #2605
2023-09-11 08:16:02 +01:00
William S Fulton a28b8cfed4 Redundant code removal in nested.cxx
c96c04c5ae
Replicate changes in this commit to add_symbols() for add_symbols_c().
Setting feature:immutable is now done in Allocate:cDeclaration.
Setting hasconsttype for static const variables (or constexpr) doesn't
look possible in this situation (C code for anonymous structs) - no
valid C syntax that I can figure out.

Anonymous nested structs are necessarily read-only (and set as
immutable in Swig_nested_name_unnamed_c_structs).

From https://sourceforge.net/p/swig/bugs/793/ ...

The union variable intRep below has a named type IntRepType.

typedef struct Object {
  int objtype;
  union IntRepType {
    double dvalue;
  } intRep;
} Object;

void tester() {
  /* approach (1) */
  obj.intRep.dvalue = 1.23;

  /* approach (2) */
  union IntRepType irt;
  irt.dvalue = 2.34;
  obj.intRep = irt;
}

Using C code there are two approaches to setting a value in intRep. Equivalents in perl are:

my $obj = example::Object->new();

 # approach (1)
$obj->{intRep}->{dvalue} = 3.45;

 # approach (2)
my $irt = example::IntRepType->new();
$irt->{dvalue} = 4.56;
$obj->{intRep} = $irt;

An Object_intRep_set wrapper is generated which takes type IntRepType as desired.

However, with an anonymous union:

typedef struct Object {
  int objtype;
  union IntRepType {
    double dvalue;
  } intRep;
} Object;

intRep does not have any named type name and is anonymous. It is thus not possible to create a type outside of Object to assign to intRep.
Only approach (1) is available in C and so SWIG can only provide approach (1) via wrappers too.
There is no Object_intRep_set wrapper, intRep is immutable.
2023-09-10 12:51:42 +01:00
William S Fulton 650aad82ed Fix incorrect variable setters being generated when wrapping arrays
A setter is no longer generated if the type of the array members
are non-assignable.
2023-09-09 19:15:15 +01:00
William S Fulton d4abe14f7e Non-assignable detection fixes when wrapping const 2D arrays
Const member variables such as the following are non-assignable by
by default:

  const int x[2][2];

Variable setters are not generated when wrapping these non-assignable
variables and classes containing such non-assignable variables.
2023-09-09 12:31:33 +01:00
William S Fulton c96c04c5ae Non-assignable detection fixes when wrapping const member variables
Const member variables such as the following are non-assignable by
by default:

  char * const x;
  const int x;
  const int x[1];

but not:

  const char * x;

Variable setters are not generated when wrapping these non-assignable
variables and classes containing such non-assignable variables.
2023-09-09 06:24:14 +01:00
William S Fulton ec53b06b8a Restore missing variable setters for types containing non-assignable static members
A struct/class that contains a non-assignable member variable is
actually assignable itself. Only non-static members, not static members,
contribute to the containing class being non-assignable.

Recent regression fix from a few commits back.
2023-09-08 07:49:01 +01:00
William S Fulton 275764c7bb Non-assignable detection fixes when wrapping rvalue reference variables
Rvalue reference variables such as the following are non-assignable by
by default:

  X &&v;

Variable setters are not generated when wrapping these non-assignable
variables and classes containing such non-assignable variables.
2023-09-07 07:02:35 +01:00
William S Fulton 912c535add Non-assignable detection fixes when wrapping reference variables
Reference variables such as the following are non-assignable by
by default:

  int &v;

Variable setters are not generated when wrapping these non-assignable
variables and classes containing such non-assignable variables.
2023-09-07 07:02:35 +01:00
William S Fulton f3e12fbd47 Assignment operator detection fixes when wrapping static member variables
Also add tests for global variables are immutable via assignment.
2023-09-07 07:02:35 +01:00
William S Fulton c0b36721a3 Replace Language::is_assignable with Language::is_immutable
Avoids confusion with newly created Allocate::is_assignable.
Language::is_immutable is just a wrapper around the
"feature:immutable" flag since previous commit.

is_mutable rename wip
2023-09-07 07:01:37 +01:00
William S Fulton e07957ad4c Implicit assignment operator detection fixes.
A class that does not have an explicit assignment operator does not
have an implicit assignment operator if a member variable is not
assignable. Similarly should one of the base classes also not be
assignable. Detection of these scenarios has been fixed so that when
wrapping a variable that is not assignable, a variable setter is not
generated in order to avoid a compiler error.

Template instantiation via %template is required in order for this to
work for templates that are not assignable.

Closes #1416
2023-09-07 04:41:52 +01:00
William S Fulton f11bffcb19 Variable setters for non-assignable types
Fix incorrect variable setters being generated when the type of the
variable is not assignable, due to variable type inheriting a private
assignment operator further up the inheritance chain (further up than
the immediate base).
2023-09-04 07:34:06 +01:00
William S Fulton abd299c6f4 Guile, Ocaml, Perl - add missing use of is_assignment() for variable wrappers
Don't attempt to generate a setter when wrapping
variables which have a private assignment operator as assignment is not
possible. This now matches the behaviour of all the other target languages.
2023-09-03 17:55:36 +01:00
William S Fulton bf330ba524 C++11 deleted destructors fixes
Fix problems wrapping deleted destructors. Derived classes are not
constructible, so don't attempt to generate default constructor or
copy constructor wrappers.

  struct StackOnly1 {
    // Only constructible on the stack
    ~StackOnly1() = delete;
  };
  struct StackOnlyDerived1 : StackOnly1 {
    // this class is not constructible due to deleted base destructor
  };
2023-09-02 22:55:58 +01:00
William S Fulton 1f8d4f8eb0 Tidy up internal "has_copy_constructor" attribute 2023-09-02 18:12:41 +01:00