Skip to content

[Macros] Implement a type wrapper transformation as a macro. #1225

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

Merged
merged 1 commit into from
Jan 14, 2023

Conversation

hborla
Copy link
Member

@hborla hborla commented Jan 12, 2023

The type wrapper macro struct conforms to MemberDeclarationMacro, MemberAttributeMacro, and AccessorDeclarationMacro. Each macro capability provides a different part of the type wrapper transform:

  • MemberDeclarationMacro adds the backing var _storage variable.
  • MemberAttributeMacro applies macro attributes to each stored property inside the wrapped type, which are recursively expanded.
  • AccessorDeclarationMacro uses the macros applied to stored properties to add get and set accessors, turning those stored properties into computed properties that indirect access through _storage.

@hborla hborla requested a review from DougGregor as a code owner January 12, 2023 16:21
"""

get {
_storage[wrappedKeyPath: \\.\(identifier)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Just FYI that type wrapper is a bit more then just wrappedKeyPath:, it also detects whether it’s attached to a reference type and if so, adds wrappedSelf: argument and always storageKeyPath: as the last argument which points to \$Storage.<prop-name>.

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 know, this is just a proof of concept. The macro can generate whatever it wants in the accessors. For example, if the macro is attached to an actor or class, it could apply a different macro attribute to each of the stored properties to direct the recursive expansion to generate special accessors in that case. It could also just choose to put whatever code is in the subscript implementation directly into the accessors

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good!

@hborla
Copy link
Member Author

hborla commented Jan 13, 2023

@swift-ci please test

@hborla hborla force-pushed the type-wrapper-macro branch from 4f76356 to 470bd40 Compare January 13, 2023 21:09
@hborla
Copy link
Member Author

hborla commented Jan 13, 2023

@swift-ci please test

…orms

to MemberDeclarationMacro, MemberAttributeMacro, and AccessorDeclarationMacro.
@hborla hborla force-pushed the type-wrapper-macro branch from 470bd40 to 0842f41 Compare January 14, 2023 19:06
@hborla
Copy link
Member Author

hborla commented Jan 14, 2023

@swift-ci please test

@hborla hborla merged commit 37d0027 into swiftlang:main Jan 14, 2023
@hborla hborla deleted the type-wrapper-macro branch January 14, 2023 21:05
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

Successfully merging this pull request may close these issues.

2 participants