# Full entity name from DeclGroupSyntax

**URL:** <https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990>\
**Category:** Macros\
**Created:** [February 25, 2026, 3:18pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990 "2026-02-25T15:18:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [February 25, 2026, 3:18pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/1 "2026-02-25T15:18:25Z")

</div>

I'm having a hard time figuring out how to infer the fully qualified name of a nested declaration, as in with the parents prepended. Example:

```swift
struct GrandParent {
    struct Parent {
        @MyMacro
        struct Child {
        }
    }
}

```

In other words, I need to get `GrandParent.Parent.Child` within the `MyMacro` expansion code.

Reason: I need to inject protocol conformances into `Child`, which requires the full entity name.

```swift
extension GrandParent.Parent.Child: MyProtocol {}

```

This is the ultimate goal, so I don't mind alternative solutions.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [February 25, 2026, 3:52pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/2 "2026-02-25T15:52:16Z")

</div>

> [@keeshux](#):
>
> In other words, I need to get `GrandParent.Parent.Child` within the `MyMacro` expansion code.

I haven't tried this, but from reading the docs the `some MacroExpansionContext` parameter that gets passed into your expansion method should have a [`.lexicalContext: [any Syntax]` property](https://swiftpackageindex.com/swiftlang/swift-syntax/602.0.0/documentation/swiftsyntaxmacros/macroexpansioncontext/lexicalcontext) from which you should (I think!) be able to extract this information by walking the hierarchy upward. Have you tried something like this?

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [February 25, 2026, 3:54pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/3 "2026-02-25T15:54:33Z")

</div>

I will. LLMs have had no damn clue so far lol.

---

<div class="post-metadata">

**Author:** ![patryk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/patryk/32/39534_2.png) [@patryk](https://forums.swift.org/u/patryk)\
**Post date:** [February 25, 2026, 3:55pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/4 "2026-02-25T15:55:35Z")

</div>

I assumed that `@MyMacro` in above example conforms to `AttachedMacro`, however it should be possible as long as you have access to `MacroExpansionContext`, specifically `lexicalContext` property as iterating over it will provide you with enough information to create fully qualified name of a nested declaration

```swift
enum MyMacro: MemberMacro {
  static func expansion(
    of node: AttributeSyntax,
    providingMembersOf declaration: some DeclGroupSyntax,
    in context: some MacroExpansionContext
  ) throws -> [DeclSyntax] {
    for lexicalContext in context.lexicalContext {
      // Based on your example, it contains elements in such order,
      StructDeclSyntax // (First element)
      ├─attributes: AttributeListSyntax
      ├─modifiers: DeclModifierListSyntax
      ├─structKeyword: keyword(SwiftSyntax.Keyword.struct)
      ├─name: identifier("Parent")
      ╰─memberBlock: MemberBlockSyntax
        ├─leftBrace: leftBrace
        ├─members: MemberBlockItemListSyntax
        ╰─rightBrace: rightBrace (from macro 'MyMacro')

      StructDeclSyntax // (Second element)
      ├─attributes: AttributeListSyntax
      ├─modifiers: DeclModifierListSyntax
      ├─structKeyword: keyword(SwiftSyntax.Keyword.struct)
      ├─name: identifier("GrandParent")
      ╰─memberBlock: MemberBlockSyntax
        ├─leftBrace: leftBrace
        ├─members: MemberBlockItemListSyntax
        ╰─rightBrace: rightBrace (from macro 'MyMacro')
    }

    // And then, you can extract the name of "Child" to which MyMacro has been attached via declaration argument, and it should look like this
    StructDeclSyntax
    ├─attributes: AttributeListSyntax
    │ ╰─[0]: AttributeSyntax
    │ ├─atSign: atSign
    │ ╰─attributeName: IdentifierTypeSyntax
    │ ╰─name: identifier("MyMacro")
    ├─modifiers: DeclModifierListSyntax
    ├─structKeyword: keyword(SwiftSyntax.Keyword.struct)
    ├─name: identifier("Child")
    ╰─memberBlock: MemberBlockSyntax
      ├─leftBrace: leftBrace
      ├─members: MemberBlockItemListSyntax
      ╰─rightBrace: rightBrace (from macro 'MyMacro')

    // return some syntax
  }
}

```

What's left is to either convert extracted syntax into `MemberAccessExprSyntax` or interpolate names to produce fully qualified name as a `String`

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [February 25, 2026, 3:58pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/5 "2026-02-25T15:58:16Z")

</div>

> [@keeshux](#):
>
> Reason: I need to inject protocol conformances into `Child`, which requires the full entity name.
> 
> ```swift
> extension GrandParent.Parent.Child: MyProtocol {}
> 
> ```

PS: Regarding your actual goal, I don't remember if what you want (have a macro declare a possibly arbitrary extension at the top level) is supported, but an extension macro should be able to add a conformance to a nested type out of the box without you having to construct the fully qualified name.

Quoting [SE-0402](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0402-extension-macros.md):

> Swift only allows `extension` declarations at the top level in a file. Despite this, extension macros can be applied to a nested type:
> 
> ```swift
> @attached(extension, conformances: MyProtocol, names: named(requirement)) macro MyProtocol = #externalMacro(...) struct Outer { @MyProtocol struct Inner {} }
> 
> ```
> 
> In this situation, the macro expansion containing the `extension` is inserted at the top level of the file, instead of immediately where the macro is invoked, where the `extension` would be invalid.

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [February 25, 2026, 4:15pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/6 "2026-02-25T16:15:58Z")

</div>

Okay, it was easier than expected. Below is the signature of the ExtensionMacro requirement:

```swift
public static func expansion(
    of node: AttributeSyntax,
    attachedTo declaration: some DeclGroupSyntax,
    providingExtensionsOf type: some TypeSyntaxProtocol,
    conformingTo protocols: [TypeSyntax],
    in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax]

```

I was focusing on `declaration`, but it's rather `type` that comes with the fully qualified type name, as mentioned in the @ole SE-402 reference:

> Each `ExtensionDeclSyntax` in the resulting array must use the `providingExtensionsOf` parameter as the extended type, which is a qualified type name.

Therefore, it was as trivial as using `type.description`.

Thanks a lot guys!

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [February 25, 2026, 4:18pm UTC](https://forums.swift.org/t/full-entity-name-from-declgroupsyntax/84990/7 "2026-02-25T16:18:06Z")

</div>

For the record: [Handle conformance on nested entities by keeshux · Pull Request #14 · keeshux/swon · GitHub](https://github.com/keeshux/swon/pull/14)
