Skip to content
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

Basic structure for composing stub code #87

Merged
merged 14 commits into from
Sep 16, 2020

Conversation

elinor-fung
Copy link
Member

  • Switch to using Roslyn APIs for syntax generation
  • Add basic structure for stub code generation
    • IMarshallingGenerator - called for each stage of generation
    • StubCodeContext - context for what marshalling code should be generated
    • TypePositionInfo - type to be marshalled
  • GenerateSyntax calls into the marshalling generators and uses the returned statements to create the stub code
  • Basic marshallers and forwarder to run through the structure
  • Test cases (just validates they compile)

@AaronRobinsonMSFT
Copy link
Member

@elinor-fung Can we also get a Pipeline.md with a description in the designs/?

@elinor-fung
Copy link
Member Author

Added a Pipeline.md with the basic structure. We can fill in more as we go.


Generation of the stub code happens in stages. The marshalling generator for each parameter and return is called to generate code for each stage of the stub. The statements and syntax provided by each marshalling generator for each stage combine to form the full stub implementation.

### Stages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stages look good. One question is regarding positional and implicit arguments. For example LCID creates an argument to pass and PreserveSig adds an out param to the method signature. Is this scheme able to handle that efficiently? Do we have an example/pattern for how handle those cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking most of the handling would be on creating all the TypePositionInfos during processing of symbols/metadata - setting Managed/UnmanagedIndex properties accordingly and creating ones to represent an LCID argument / out param / HResult if needed. We could have specific marshalling generators for the LCID argument and HResult checking.

Then, going through the stages should not be affected other than a bit of extra cruft around declaring variables for the return value of the stub and the invoke (if needed) and determining identifier names.

I didn't want to deal with actually handling those cases yet. I can add some placeholder TODOs to indicate what I had in mind.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LCID argument

FWIW, LCID arguments is an obscure legacy feature. I would be perfectly happy to not support it for the source generators if it were to add even a bit of extra complexity anywhere.

@jkoritzinsky
Copy link
Member

One thing I've noticed with this design is the marshaler selector will need to know about the particular target scenario (P/Invoke, Reverse P/Invoke, Struct) to correctly support marshalers that either pin or allocate depending on the scenario. I'm ok with that, but I just wanted to make sure that we expected that.

@elinor-fung
Copy link
Member Author

I thought that would be something that could go on the context that gets passed to the IMarshallingGenerator instances? Or am I missing something?

@jkoritzinsky
Copy link
Member

You are correct. I misread how the context was created/passed in. This should work for those cases then.

Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments around places we'll have to change in the future as we add features. We can fix these now or modify them in the future when we'll need the changes.

Other than that, this looks great!

@jkoritzinsky jkoritzinsky added the area-DllImportGenerator Source Generated stubs for P/Invokes in C# label Sep 15, 2020
Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one comment. Other than that, LGTM!

@elinor-fung elinor-fung merged commit e787027 into dotnet:DllImportGenerator Sep 16, 2020
@elinor-fung elinor-fung deleted the basicStructure branch September 16, 2020 05:09
scalablecory pushed a commit that referenced this pull request Sep 22, 2020
jkoritzinsky pushed a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
MichalStrehovsky added a commit to MichalStrehovsky/runtimelab that referenced this pull request Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants