Commit Graph

333 Commits

Author SHA1 Message Date
William S Fulton bc0b8b558d Improve handling and documentation of missing enum base type information.
Closes #2828
2024-03-11 08:12:50 +00:00
William S Fulton 44e6e288c0 Correct node stored in language symbol tables for main module 2024-02-03 14:29:01 +00:00
William S Fulton e6846ac8f3 Fix assert handling enums with same name in different namespaces. 2024-02-03 14:29:01 +00:00
William S Fulton 3be670e8db Fix assertion handling upcasting when using %shared_ptr on some templates.
A different approach is taken for supporting casting smart pointers up the
inheritance hierarchy. We no longer try to replace the underlying pointer type,
provided in the 'feature:smartptr', with the base class type. Such as morphing
'std::shared_ptr<(Derived)>' into 'std::shared_ptr<(Base)>'. Instead, we simply
use 'feature:smartptr' from the base class. This is more reliable than trying to
pattern match the pointer type in the feature. The base class must of course
also have the 'feature:smartptr' set, and this is still checked for as before.
The feature is now parsed in one place and stored in the parse tree in the
new 'smart' attribute for handling by the target languages.

Fix also improves the handling of the type parsed in 'feature:smartptr' in that
the type is now normalized and resolved in the scope of the class it is attached
to.

Closes #2768
2024-01-30 22:24:42 +00:00
William S Fulton 605a768497 Minor tweaks and docs for the cs:defaultargs feature 2023-12-20 19:45:27 +00:00
Chris Hilton 867b2396f4 feature("cs:defaultargs") review changes
Expanding test cases, which found a bug with static methods and global
functions (they needed their own special handling).

Fixing code to match swig style rules and some other code quality.

And documentation fixes.
2023-12-01 10:17:24 -06:00
Chris Hilton b6409b584f feature("cs:defaultargs")
We need a way to specify default arguments for functions.  Sometimes
we can just take the arguments straight from c++ literals.  Sometimes
you can't, and need to specify those by hand.

So to make this work we:

 1. Shutoff handling all "defaultargs" variants of methods and constructors
    which have cs:defaultargs defined for their node.  This gets us a single
    constructor or method and skips the variants.
 2. As we emit arguments in the function declaration, check if there is a
    definition for it it from cs:defaultargs, if so, use that.  If not,
    and the method has a cs:defaultargs declaration, then fall back to the
    literal expression from c++.
2023-11-30 13:09:18 -06: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
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 af72fbb08b C++11 enum base type is now used for C# underlying enum type 2023-10-18 19:56:08 +01:00
Olly Betts 4a618cd646 Fix misindented line 2023-10-11 08:54:37 +13:00
William S Fulton 84542f6b59 Replace Language::directorsEnabled() with Swig_directors_enabled()
For use outside of the target languages for forthcoming commits
which move adding default constructors/destructors from Language
to Allocate.
2023-07-12 18:44:42 +01:00
William S Fulton 777fd2c280 Minor refactor of D, C#, Java director code
for overloaded methods. Fixes regression (crash) in director_ignore D
testcase since string mangling names change.
2022-11-12 16:30:25 +00:00
William S Fulton 2acdfd77e9 SwigType * handling corrections
Further corrections to pass SwigType * to methods expecting types
instead of passing readable type strings.

Required reworking code that adds a fake inheritance for smart pointers
using the smartptr feature. Swig_smartptr_upcast() added as a support
function for this.
2022-11-09 22:11:27 +00:00
William S Fulton 46f7501d94 Cleanup SWIG_VERSION definition
Add Swig_obligatory_macros which must be called by each
target language to define SWIG_VERSION correctly
in the generated code, as well as the language specific
macro SWIGXXX where XXX is the target language name.

Drop the #ifdef SWIGXXX that was previously generated -
I can't see the point of this and if users are defining
this macro somehow, then users will need to change this

Closes #1050
2022-10-13 19:47:43 +01:00
Olly Betts 631b41ae7b Use https for swig.org links 2022-10-06 13:16:39 +13:00
William S Fulton 7592722e39 Order of interfaces generated for %interface macros
Ensure the order of interfaces generated in proxy interfaces for the
%interface family of macros is the same as that parsed from the bases in C++.
2022-07-20 19:50:53 +01:00
William S Fulton 34c219b5f7 Merge branch 'imfunc'
* imfunc:
  Add special variable imfuncname expansion for C# and D
  Test and document imfuncname special variable expansion
  Update docs.
  Also expose  in proxyClassFunctionHandler
  Expose  to javaout typemaps.

Conflicts:
	CHANGES.current
2022-05-30 19:45:28 +01:00
William S Fulton 954f29b032 Add special variable imfuncname expansion for C# and D
Same functionality as Java
2022-05-30 19:43:04 +01:00
William S Fulton b4cc35ddbc Merge branch 'csharp-strenghten-overload-check'
* csharp-strenghten-overload-check:
  Fix expanded director_basic test.
  Improve correctness of SwigDerivedClassHasMethod() by making sure only methods that have `override` are used connected by director.
2022-05-26 08:23:18 +01:00
William S Fulton 6939d91e4c Header file tidyup
Fix Visual C++ warning in scilab.cxx:
  warning C4996: 'strtok': This function or variable may be unsafe.
2022-05-07 11:56:06 +01:00
William S Fulton 598736a6b2 %interface family of macros no longer add variable wrappers
The getter/setter methods used for wrapping methods are no longer
added to the interface class.

Closes #1524

Also add in testcase for enums.
2022-03-12 23:04:24 +00:00
William S Fulton a87e722834 Use GetFlag instead of Getattr for feature:interface 2022-03-12 23:04:24 +00:00
William S Fulton c88a9436bc Fix segfault in C# layer handling using declarations
Segfault was actually avoided in previous commit ab23cb29.
This commit makes handling more robust in the event of
using %ignore just on the derived method, not tested as it is not
what one should do with directors, and possibly other cases.

Go still segfaults with the new testcase director_using_member_scopes.i.

Issue #1441.
2022-03-10 22:18:23 +00:00
Olly Betts 55377bdc08 Add DOH Exit() and SetExitHandler()
Exit() is a wrapper for exit() by default, but SetExitHandler() allows
specifying a function to call instead.

This means that failures within DOH (e.g. Malloc() failing due to lack
of memory) will now perform cleanup such as removing output files.

This commit also cleans up exit statuses so SWIG should now reliably
exit with status 0 if the run was successful and status 1 if there was
an error (or a warning and -Werror was in effect).

Previously in some situations SWIG would try to exit with the status set
to the number of errors encountered, but that's problematic - for
example if there were 256 errors this would result in exit status 0 on
most platforms.  Also some error statuses have special meanings e.g.
those defined by <sysexits.h>.

Also SWIG/Javascript tried to exit with status -1 in a few places (which
typically results in exit status 255).
2022-03-06 12:33:54 +13:00
Olly Betts 9ddc9dceb7 Remove support for $source and $target
These were officially deprecated in 2001, and attempts to use them have
resulted in a warning (including a pointer to what to update them to)
for most if not all of that time.

Fixes #1984
2021-04-30 10:20:14 +12:00
William S Fulton c1b004f4fa Add access modifier support for interface feature
Add ability to change the modifiers for the C# and Java
interface generated when using the %interface macros.

For C# use the 'csinterfacemodifiers' typemap.
For Java use the 'javainterfacemodifiers' typemap.

For example:

  %typemap(csinterfacemodifiers) X "internal interface"

Closes #1874
2020-09-25 18:54:25 +01:00
William S Fulton b3da344765 Add some comments about shared_ptr upcast code 2020-01-24 19:46:39 +00:00
William S Fulton 564a91864a Refactor upcasts code
In preparation for possible improvement in usage of
SwigType_typedef_resolve_all - a SwigType* should be used,
not a readable name in a String*.
2020-01-23 19:30:03 +00:00
William S Fulton 6910e5f09c Merge branch 'shared-ptr-template-upcast'
* shared-ptr-template-upcast:
  comments
  Applying shared_ptr template upcast fix to CSharp, adding CSharp test, and cleanup
  Adding test case demonstrating issue where SWIG does not generate a correctly typed, upcasted shared_ptr for a template instantiation deriving from a base class
  WIP - Use the non-encoded type string for upcasting a shared_ptr of a derived type to a shared_ptr of the base type
  comments
  Applying shared_ptr template upcast fix to CSharp, adding CSharp test, and cleanup
  Adding test case demonstrating issue where SWIG does not generate a correctly typed, upcasted shared_ptr for a template instantiation deriving from a base class
  WIP - Use the non-encoded type string for upcasting a shared_ptr of a derived type to a shared_ptr of the base type
2020-01-17 18:45:30 +00:00
William S Fulton 18b2dcd222 C# 'out' or 'ref' removal improvements in director typemaps.
- Add support to DOH Replace for not replacing inside C comments
- Fix removing 'out' or 'ref' when these are present in C comments
  in cstype typemaps.

Closes #1628
2019-11-26 19:39:28 +00:00
William S Fulton 6fb345feb2 'out' or 'ref' usage in a cstype typemap in directors
'out' / 'ref' was not always stripped out in parts of the
director code generation.

Issue #1628
2019-11-26 19:32:34 +00:00
Rokas Kupstys 06345e2aa5 Fix invalid code generated for "%constant enum EnumType ..." and add a test. 2019-08-01 08:55:07 +03:00
Isaac Pascual Monells 63497c8d0e Fix class name for nested classes 2019-07-08 12:49:00 +02:00
etse 3359b4ccab Applying shared_ptr template upcast fix to CSharp, adding CSharp test, and cleanup 2019-05-06 15:30:54 -04:00
William S Fulton 06462acdf9 Fix C# CA1063 warning by implementing the recommended Dispose methods.
Previously just the Dispose() method was generated.
Now the Dispose() and Dispose(bool disposing) methods are generated.
Changes are required if custom "csfinalize", "csdestruct" or "csdestruct_derived"
typemaps are being used. Details in #421 on Github. SWIG will error out if one of
the "csfinalize, "csdestruct" or "csdestruct_derived" typemaps are found. Example
error message:

  foo.h:60: Error: A deprecated csfinalize typemap was found for Foo, please remove
  it and replace all csdestruct, csdestruct_derived and csfinalize typemaps by the
  csdispose, csdispose_derived, csdisposing and csdisposing_derived typemaps.

Closes #421
2019-04-08 19:20:45 +01:00
Rokas Kupstys 15f9403e06 Fix expanded director_basic test.
Since we are mixing `new` and `override` a class instance may have multiple methods with the same name. in case of director_basic test one method comes from `MyClassMiddle` (one that participates in dynamic dispatch) and one comes from `MyClassEnd` (one with `new` keyword). Therefore all methods have to be checked.
2018-09-26 12:20:38 +03:00
Rokas Kupstys 82a6fe02e3 Improve correctness of SwigDerivedClassHasMethod() by making sure only methods that have `override` are used connected by director.
C# does not treat `virtual` methods of child classes as overriding (ass opposed to c++). In order to override a method it must have `override` specified.

Previous version of this method treated `virtual void foo()` or `void foo()` in a subclass as methods that override virtual method of parent class. This resulted in `SwigDirectorConnect()` creating delegates and connecting them to a native class. Presence of such methods caused needless roundtrip through managed layer while in the end correct native function was called.

This is the old code flow:

```cpp
void SwigDirector_MyClass::nonOverride() {
  if (!swig_callbacknonOverride) {  // 0. swig_callbacknonOverride is set
    MyClass::nonOverride();
    return;
  } else {
    swig_callbacknonOverride();     // 1. this is called because wrapper mistakenly assumes overriding
  }
}

SWIGEXPORT void SWIGSTDCALL CSharp_director_basicNamespace_MyClass_nonOverrideSwigExplicitMyClass(void * jarg1) {
  MyClass *arg1 = (MyClass *) 0 ;

  arg1 = (MyClass *)jarg1;
  (arg1)->MyClass::nonOverride();   // 5. Correct method called in the end
}
```

```cs
  private void SwigDirectornonOverride() {
    nonOverride();                  // 2.
  }

  public virtual void nonOverride() {
    if (SwigDerivedClassHasMethod("nonOverride", swigMethodTypes4))                // 3. This returns `true`
        director_basicPINVOKE.MyClass_nonOverrideSwigExplicitMyClass(swigCPtr);    // 4. Native method of director class called explicitly
    else
        director_basicPINVOKE.MyClass_nonOverride(swigCPtr);
  }

```

This is the new code flow:

```cpp
void SwigDirector_MyClass::nonOverride() {
  if (!swig_callbacknonOverride) {  // 0. swig_callbacknonOverride is not set
    MyClass::nonOverride();         // 1. Calls correct method immediately
    return;
  } else {
    swig_callbacknonOverride();
  }
}
```
2018-09-26 09:57:51 +03:00
Rokas Kupstys 0a102e2846 Fix class having method `Connect()` creating a name collision with `SwigDirectorConnect()`.
Issue is fixed by prepending director methods with `SwigDirectorMethod` instead of `SwigDirector`.
2018-09-06 12:43:51 +03:00
luz.paz 60dfa31a67 Misc. typos
found via `codespell` and `grep`
2018-05-17 10:04:23 -04:00
William S Fulton 63a5a8af88 Fix lookup of csconstruct, dconstructor and javaconstruct typemaps
The C++ namespace was previously ignored in the lookup type
2018-05-15 22:26:35 +01:00
William S Fulton ee17f8d04f C#, D, Java methodmodifiers on destructors
Add support so that the %csmethodmodifiers, %dmethodmodifiers,
%javamethodmodifiers can modify the method modifiers for the destructor wrappers
in the proxy class: dispose, Dispose, delete. With this feature, it is now possible
to make a C# proxy class sealed, eg when wrapping a class X, the virtual method modifiers
can be removed using:

  %typemap(csclassmodifiers) X "public sealed class"
  %csmethodmodifiers X::~X "public /*virtual*/";
2018-05-11 18:09:51 +01:00
William S Fulton 9fb996b83f Consistent spacing in generated exception specifications 2018-05-06 09:46:37 +01:00
William S Fulton 60ee821bca Correct C# warning for missing cstype typemap 2018-03-09 06:58:08 +00:00
William S Fulton dafe2d6949 Add director typemaps for pointer const ref types 2017-10-24 23:47:40 +01:00
William S Fulton ae044c1c2f Generation of director method declarations fixes
- Fixes generation of director method declarations containing C++11 ref-qualifiers.
- Fixes generation of director method declarations returning more complex types such
  as const ref pointers.
- Rewrite Swig_method_call to use more up to date code in the core.
2017-10-23 18:55:14 +01:00
William S Fulton 3f8b8fe734 Enhancements for directorin typemaps
The directorin typemaps will now generate a temporary variable
(specified after the type), such as:
%typemap(directorin) MyType (MyType *temp) { ... use temp ... }

The shared_ptr director typemaps have been fixed for use in functions
that take more than one parameter.
2017-10-16 19:28:27 +01:00
William S Fulton 05badeb1a4 Remove undocumented features used in directors
The jsdowncast and csdowncast features are not documented and I think
these are a relic of something that was never finished.
2017-05-21 17:43:06 +01:00
William S Fulton b90a8d7444 Remove unnecessary dynamic_cast in C#/D/Java directors
Also revert the removal of dynamic_cast in the csdowncast feature.
2017-05-21 17:42:27 +01:00
William S Fulton 7f2849cb0a Merge branch 'Sghirate-master'
* Sghirate-master:
  [C#] static_cast for native directors
2017-05-21 00:22:17 +01:00