Could #Self / #type be implemented as a macro? I gave it a try but my macro-fu needs improvements.
Macro attempt that doesn't work
public struct TypeNameMacro: ExpressionMacro {
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
var currentNode: Syntax? = Syntax(node)
while let node = currentNode {
if let structDecl = node.as(StructDeclSyntax.self) {
return ExprSyntax(StringLiteralExprSyntax(content: structDecl.name.text))
}
if let classDecl = node.as(ClassDeclSyntax.self) {
return ExprSyntax(StringLiteralExprSyntax(content: classDecl.name.text))
}
if let enumDecl = node.as(EnumDeclSyntax.self) {
return ExprSyntax(StringLiteralExprSyntax(content: enumDecl.name.text))
}
currentNode = node.parent
}
return ExprSyntax(StringLiteralExprSyntax(content: "<global>"))
}
}