E852 Possible to avoid code generation when involved type is missing? · Issue #1296 · swig/swig · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Possible to avoid code generation when involved type is missing? #1296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rokups opened this issue Jul 29, 2018 · 4 comments
Closed

Possible to avoid code generation when involved type is missing? #1296

rokups opened this issue Jul 29, 2018 · 4 comments

Comments

@rokups
Copy link
Contributor
rokups commented Jul 29, 2018

I am using swig to wrap a pretty big library and one of requirements is to expose API that follows target language naming conventions and idioms. I am approaching problem by using a external python script (using pyclang) to generate interface files for renaming identifiers and generating C# properties from getters/setters.

The Problem

Since external tool is used it is not aware of types that are ignored in manually written interface files. This causes code unwanted code generation for ignored types.

As python script walks all the headers it generates interface like this:

%csmethodmodifiers Urho3D::Object::GetTasks "private";
%typemap(cscode) Urho3D::Object %{
  public $typemap(cstype, Urho3D::Tasks *) Tasks {
    get { return GetTasks(); }
  }
%}

However main interface file ignores Urho3D::Object::GetTasks as it should not be wrapped. This causes swig to create SWIGTYPE_p_Urho3D__Tasks and reference missing method GetTasks() as it was ignored.

The Question

Would it be possible to somehow remedy such situations by preventing %typemap(cscode) emitting any code if type used by $typemap macro is not wrapped? I am not sure what approach would work best here. I do not expect this to be automatic either, although it would be nice.

I have not seen anything that would allow this in documentation so i suspect this is a new feature request. In this case i have no problem implementing this myself, however some guidance would be extremely useful both in deciding how to approach this problem and how/where to implement it as swig's codebase is completely unknown to me.

Thanks!

@rokups
Copy link
Contributor Author
rokups commented Jul 30, 2018

I had an idea to use something like:

%typemap(csout) Urho3D::Tasks* Urho3D::Object::GetTasks {
    $typemap(csout, Urho3D::Tasks*)
  }
  public $typemap(cstype, Urho3D::Tasks *) Tasks {
    get { return GetTasks(); }
}

This allows me to latch on to visibility of Urho3D::Object::GetTasks and avoid property generation when said method is ignored. This is of course a fragile solution as getter may be ignored while setter is not, this still would create situation where property is generated accessing ignored method. However this is good enough for my usecase.

If you think a proper solution would be good for swig then maybe we should still explore this topic. If you think that there is no point in adding feature like this to swig then please feel free to close the issue 👍

@wsfulton
Copy link
Member

Instead of parsing the .h files for identifiers, use swig -xml and process the resulting xml. The resulting xml will contain the methods in the interface and it will also show which identifiers have been ignored.

There is the %attribute family of macros in attribute.i for turning get/set methods into properties a.k.a attributes. See attribute.swg for more on this. Here is an example that might work for you:

%include "attribute.i"

// uncomment next line to ignore the added attribute/property that comes from %attibute2 below
// %ignore Urho3D::Object::Tasks; 

%attribute2(Urho3D::Object, Urho3D::Tasks, Tasks, GetTasks);

%inline %{
namespace Urho3D {
  struct Tasks {
	int Number;
  };
  struct Object {
  private:
	Tasks m_t;
  public:
	const Tasks& GetTasks() { 
		return m_t;
	}
	Object() : m_t() {
	  m_t.Number = 123;
	}
  };
}
%}

If you look at %attribute2, you'll see that it is simply a combination of %ignore and %extend. It might be easier to use swig -E to understand the resulting code from the macros.

@rokups
Copy link
Contributor Author
rokups commented Jul 30, 2018

Ah this module is very interesting! As i understand to ignore attribute i would have to do %ignore Class::AttributeName instead of ignoring getter/setter and it has benefit of working in all supported languages unlike my hack. I am definitely going to use this.

Looks like swig has support for just about every usecase 👍 I guess nothing to do here then, ill close the issue.

P.S. Whats the best way to ask random questions about swig? I tried mailing list but it appears to be dead. At least you are looking at github issues though, but it feels wrong to spam issue tracker with questions that arent exactly swig issues.

@rokups rokups closed this as completed Jul 30, 2018
@wsfulton
Copy link
Member

Mailing list is usually better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0