Skip to content

Swift: use structured generated C++ classes in DeclVisitor#9230

Merged
AlexDenisov merged 10 commits into
mainfrom
redsun82/swift-use-generated-classes
Jun 21, 2022
Merged

Swift: use structured generated C++ classes in DeclVisitor#9230
AlexDenisov merged 10 commits into
mainfrom
redsun82/swift-use-generated-classes

Conversation

@redsun82
Copy link
Copy Markdown
Contributor

@redsun82 redsun82 commented May 20, 2022

This PR introduces using structured generated C++ classes in DeclVisitor.

The coding style moves to translation methods taking in a swift entity and filling a trap class instance, a bit in the style of gRPC method implementations. Shortcuts are provided to fill optional and repeated properties (fetchOptionalLabel and fetchRepeatedLabels). No explicit mention of trap entries is needed any more, but they could still be used alongside this higher-level interface.

Also, the Binding template class has been removed and aggregate initialization of generated C++ classes disallowed.

Binding was meant to allow aggregate initialization of generated C++ entries having the label id as first argument, before fields from superclasses.

As aggregate initialization turned out to be undesirable (names of fields are not explicit, and {} must be inserted for empty
superclasses), Binding is removed it and aggregate initialization are disallowed by defining empty constructors for generated
classes.

@github-actions github-actions Bot added the Swift label May 20, 2022
@redsun82 redsun82 force-pushed the redsun82/swift-use-generated-classes branch from 17e8be0 to e84cef8 Compare May 20, 2022 08:16
Copy link
Copy Markdown
Contributor

@AlexDenisov AlexDenisov left a comment

Choose a reason for hiding this comment

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

Love it! ❤️

Comment thread swift/extractor/visitors/DeclVisitor.h Outdated
@redsun82 redsun82 force-pushed the redsun82/swift-type-visitor branch from 314dd2e to 685ffc9 Compare May 20, 2022 10:15
Base automatically changed from redsun82/swift-type-visitor to redsun82/swift-visitors May 20, 2022 10:15
@redsun82 redsun82 force-pushed the redsun82/swift-visitors branch from 9d03e87 to b0668ee Compare May 20, 2022 13:43
@redsun82 redsun82 force-pushed the redsun82/swift-use-generated-classes branch from e84cef8 to f985003 Compare May 20, 2022 14:10
Base automatically changed from redsun82/swift-visitors to main May 20, 2022 14:38
@redsun82 redsun82 force-pushed the redsun82/swift-use-generated-classes branch 3 times, most recently from 000aaf0 to 1ab095a Compare May 23, 2022 15:16
@redsun82 redsun82 changed the title Swift: showcase C++ entry style visitor Swift: use structured generated C++ classes in DeclVisitor May 23, 2022
@redsun82 redsun82 marked this pull request as ready for review May 23, 2022 15:17
@redsun82 redsun82 requested a review from a team as a code owner May 23, 2022 15:17
@redsun82 redsun82 requested a review from AlexDenisov May 23, 2022 15:17
@redsun82
Copy link
Copy Markdown
Contributor Author

I made the PR a bit smaller than before and added some comments to the new functions.

With respect to the previous version of this PR, I undid the is_unknown move. It's still kind of an issue, as there will be repeated is_unknown fields in the classes layouts, but it does not prevent the current PR from working. There can be future changes to address that (of which I prefer the possibility to optionally skip C++ generation for some optional or repeated fields).

I also moved fetchOptionalLabel and fetchRepeatedLabels to the dispatcher, as it was unclear yet what belonged to the dispatcher and what to VisitorBase. Maybe we can reorganize the classes later to move all methods relevant to the visitors to VisitorBase.

@redsun82 redsun82 added the no-change-note-required This PR does not need a change note label May 23, 2022
@redsun82 redsun82 force-pushed the redsun82/swift-use-generated-classes branch from 1ab095a to 43888e7 Compare May 24, 2022 10:54
Copy link
Copy Markdown
Contributor

@sashabu sashabu left a comment

Choose a reason for hiding this comment

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

I've reviewed the "interesting" parts of this PR, but:

  1. I haven't done a thorough review of DeclVisitor.h since it looks fairly mechanical and
  2. I'm not familiar with Mustache syntax, so I only commented on what I think the resulting C++ will look like

since Alex is the main reviewer. Let me know if you'd like a more thorough review from me.

Comment thread swift/codegen/templates/cpp_classes.mustache Outdated
Comment thread swift/extractor/SwiftDispatcher.h Outdated
Comment thread swift/extractor/SwiftDispatcher.h Outdated
Comment thread swift/extractor/visitors/VisitorBase.h Outdated
Comment thread swift/extractor/visitors/VisitorBase.h Outdated
Comment thread swift/extractor/visitors/VisitorBase.h Outdated
@redsun82 redsun82 force-pushed the redsun82/swift-use-generated-classes branch from 43888e7 to c65462e Compare May 31, 2022 09:14
Copy link
Copy Markdown
Contributor

@sashabu sashabu left a comment

Choose a reason for hiding this comment

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

LGTM with the same caveats that:

  1. I haven't done a thorough review of DeclVisitor.h since it looks fairly mechanical and
  2. I'm not familiar with Mustache syntax, so I only commented on what I think the resulting C++ will look like

Comment thread swift/extractor/SwiftDispatcher.h Outdated
redsun82 added 6 commits June 1, 2022 08:41
That class was meant to allow aggregate initialization of generated
C++ entries having the label `id` as first argument.

As aggregate initialization turned out to be undesirable (names of
fields are not explicit, and `{}` must be inserted for empty
superclasses), this commit removes it and disallows aggregate
initialization altogether by defining empty constructors for generated
classes.
Translation now takes const references to swift entities and return
trap entries (instead of taking apointer as an out parameter).
This disallows completely calling the disabled function (which could
be done by explicitly providing `void` otherwise).
@redsun82 redsun82 force-pushed the redsun82/swift-use-generated-classes branch from 68716ee to 6e44a12 Compare June 1, 2022 06:48
redsun82 added 2 commits June 1, 2022 11:26
While we need universal references to catch different value categories,
we don't need perfect forwarding as `fetchLabel` does not behave
differently on lvalue and rvalues.
@redsun82 redsun82 changed the base branch from main to alexdenisov/xref-decls June 17, 2022 15:06
@redsun82 redsun82 marked this pull request as draft June 17, 2022 15:06
@redsun82
Copy link
Copy Markdown
Contributor Author

@AlexDenisov I've merged your deduplication branch into this and selected it as a target. Once we merge that, I can rebase this and put it back to review.

Base automatically changed from alexdenisov/xref-decls to main June 20, 2022 08:43
@redsun82 redsun82 marked this pull request as ready for review June 20, 2022 16:02
@AlexDenisov AlexDenisov merged commit 687b9af into main Jun 21, 2022
@AlexDenisov AlexDenisov deleted the redsun82/swift-use-generated-classes branch June 21, 2022 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Swift

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants