stackotter / swift-macro-toolkit

A powerful toolkit for creating concise and expressive Swift macros
Apache License 2.0
246 stars 15 forks source link

Implement `Class`, `Actor`, `Extension`, and `Protocol` syntax wrappers #9

Closed stackotter closed 6 months ago

stackotter commented 1 year ago

So far the only declaration group wrappers in the toolkit are Enum and Struct. By declaration group I mean a declaration with an attached block which itself contains declarations. For example, a function isn't a declaration group because its block contains statements instead of declarations, whereas a class is a declaration group because its block contains 'member's which are all declarations (and could themselves also be classes).

The remaining declaration groups are class, actor, extension, and protocol. Implementing wrappers for these would unlock quite a few more use cases for the toolkit.

Implementing new wrappers

The wrappers themselves are pretty cookie cutter: simply create a new file (matching the new of the wrapper) and copy an existing wrapper for another declaration group and then modify it.

Bonus points!

This isn't strictly necessary but it would be a very welcome extension to this task; a DeclGroupProtocol protocol. This would enable macros to generically operate over the members of a declaration group without having to actually know what specific type of declaration group it is. As a rough idea, the protocol would probably look something like this;

public protocol DeclGroupProtocol {
    var inheritanceClause: [Type] { get }
    var members: [Decl] { get }
}

Also, if DeclProtocol exists by the time someone gets to this, DeclGroupProtocol should inherit from DeclProtocol too.