title: Apple Swift mangling rules for recurring context
body: <p>On MacOS with Swift 5.6 and given the <a href="https://github.com/apple/swift/blob/main/docs/ABI/Mangling.rst" rel="nofollow noreferrer">mangling rules from here</a> with the following Swift code:</p>
  <pre><code>class Car //AB
  {
      class Foo //AD
      {
          func Foo() -&gt; Void //AD
          {
                 
          }
      }
  }
  </code></pre>
  <p>The resulting mangled name for <code>Foo</code> is <code>_$s4TEST3CarC3FooCADyyF</code> (Target name <code>TEST</code>), I cannot understand why <code>Foo</code> is given the index code <code>AD</code>, I would expect <code>AC</code> to be assigned:</p>
  <pre><code>AA   AB  AC 
  TEST.Car.Foo.Foo
  </code></pre>
  <p>In this other example:</p>
  <pre><code>class TEST //AA
  {
      class Car //AC
      {
          class Foo //AE
          {
              class Car //AC
              {
                  func Foo() -&gt; Void //AE
                  {
                      
                  }
              }
          }
      }
  }
  </code></pre>
  <p>The mangled name is <code>_$s4TESTAAC3CarC3FooCACCAEyyF</code>, again those are the values I would assign:</p>
  <pre><code>AA        AB  AC
  TEST.TEST.Car.Foo.Car.Foo
  </code></pre>
  <p>Why <code>Foo</code> is assigned to <code>AE</code> after the first long form encoding <code>3Foo</code>? Why <code>AB</code> and <code>AD</code> are skipped and not used? Note that this is only in regards to recurrent items in the string, if the are no recurrent items i can encode all happily.</p>