Skip to content

[Swift projection tooling] Explore possibility of replacing custom build Swift5Demangler with swift-demangle tool #3073

@matouskozak

Description

@matouskozak

Currently, the projection tooling is relying on custom build demangler for Swift symbols.
This proposal aims to discuss benefits from switching to Apple provided swift-demangle tool.

Capabilities of swift-demangle

The swift-demangle can be used from command line with the following syntax:

swift-demangle [options] [mangled name...]

where options are:

  --classify             - Display symbol classification characters
  --compact              - Compact mode (only emit the demangled names)
  --expand               - Expand mode (show node structure of the demangling)
  --no-sugar             - No sugar mode (disable common language idioms such as ? and [] from the output)
  --remangle-new         - Remangle the symbol with new mangling scheme
  --remangle-objc-rt     - Remangle to the ObjC runtime name mangling scheme
  --simplified           - Don't display module names or implicit self types
  --strip-specialization - Remangle the origin of a specialized function
  --test-remangle        - Remangle test mode (show the remangled string)
  --tree-only            - Tree-only mode (do not show the demangled string)

Sample output without any options is:

$s12GenericTests015AcceptsMultipleA19ParamsWithProtocols1a1bSix_q_tAA13MultiplicableRzAA8SummableRzAA9DividableR_AA12SubtractableR_r0_lF ---> GenericTests.AcceptsMultipleGenericParamsWithProtocols<A, B where A: GenericTests.Multiplicable, A: GenericTests.Summable, B: GenericTests.Dividable, B: GenericTests.Subtractable>(a: A, b: B) -> Swift.Int

giving us the full signature of the symbol.

When using the --expand option:

Demangling for $s12GenericTests015AcceptsMultipleA19ParamsWithProtocols1a1bSix_q_tAA13MultiplicableRzAA8SummableRzAA9DividableR_AA12SubtractableR_r0_lF
kind=Global
  kind=Function
    kind=Module, text="GenericTests"
    kind=Identifier, text="AcceptsMultipleGenericParamsWithProtocols"
    kind=LabelList
      kind=Identifier, text="a"
      kind=Identifier, text="b"
    kind=Type
      kind=DependentGenericType
        kind=DependentGenericSignature
          kind=DependentGenericParamCount, index=2
          kind=DependentGenericConformanceRequirement
            kind=Type
              kind=DependentGenericParamType
                kind=Index, index=0
                kind=Index, index=0
            kind=Type
              kind=Protocol
                kind=Module, text="GenericTests"
                kind=Identifier, text="Multiplicable"
          kind=DependentGenericConformanceRequirement
            kind=Type
              kind=DependentGenericParamType
                kind=Index, index=0
                kind=Index, index=0
            kind=Type
              kind=Protocol
                kind=Module, text="GenericTests"
                kind=Identifier, text="Summable"
          kind=DependentGenericConformanceRequirement
            kind=Type
              kind=DependentGenericParamType
                kind=Index, index=0
                kind=Index, index=1
            kind=Type
              kind=Protocol
                kind=Module, text="GenericTests"
                kind=Identifier, text="Dividable"
          kind=DependentGenericConformanceRequirement
            kind=Type
              kind=DependentGenericParamType
                kind=Index, index=0
                kind=Index, index=1
            kind=Type
              kind=Protocol
                kind=Module, text="GenericTests"
                kind=Identifier, text="Subtractable"
        kind=Type
          kind=FunctionType
            kind=ArgumentTuple
              kind=Type
                kind=Tuple
                  kind=TupleElement
                    kind=Type
                      kind=DependentGenericParamType
                        kind=Index, index=0
                        kind=Index, index=0
                  kind=TupleElement
                    kind=Type
                      kind=DependentGenericParamType
                        kind=Index, index=0
                        kind=Index, index=1
            kind=ReturnType
              kind=Type
                kind=Structure
                  kind=Module, text="Swift"
                  kind=Identifier, text="Int"
$s12GenericTests015AcceptsMultipleA19ParamsWithProtocols1a1bSix_q_tAA13MultiplicableRzAA8SummableRzAA9DividableR_AA12SubtractableR_r0_lF ---> GenericTests.AcceptsMultipleGenericParamsWithProtocols<A, B where A: GenericTests.Multiplicable, A: GenericTests.Summable, B: GenericTests.Dividable, B: GenericTests.Subtractable>(a: A, b: B) -> Swift.Int

we get the full node structure.

Additionally, the tool support batch demangling by chaining the mangled names like:

swift-demangle \
s12GenericTests015AcceptsMultipleA19ParamsWithProtocols1a1bSix_q_tAA13MultiplicableRzAA8SummableRzAA9DividableR_AA12SubtractableR_r0_lF \
s12GenericTests015AcceptsMultipleA40ParamsOfTheSameTypeConstrainedByProtocol1a1bSix_xtAA8SummableRzlF
...

Returning demangled results in sequential order.

Advantages

  • Support for all Swift symbols.
  • Using the --expand option, we can get node structure.
    This could lead to future changes where we will not need to parse abi.json and only rely on tbd (or dylib) files.

Disadvantages

  • Introducing an external dependency.
  • Invoking external tool, possibly slowing down the projection tooling generation.
  • Additional work required to write a parser of swift-demangle output (text format).
  • Uncertainty about future support policy.

Open Questions

  • Support for older Swift versions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions