简体   繁体   English

Apple Swift 重复上下文的修改规则

[英]Apple Swift mangling rules for recurring context

On MacOS with Swift 5.6 and given the mangling rules from here with the following Swift code:在带有 Swift 5.6 的 MacOS 上,并使用以下 Swift 代码 从此处给出了修改规则

class Car //AB
{
    class Foo //AD
    {
        func Foo() -> Void //AD
        {
               
        }
    }
}

The resulting mangled name for Foo is _$s4TEST3CarC3FooCADyyF (Target name TEST ), I cannot understand why Foo is given the index code AD , I would expect AC to be assigned: Foo的结果损坏名称是_$s4TEST3CarC3FooCADyyF (目标名称TEST ),我不明白为什么Foo被赋予索引代码AD ,我希望分配AC

AA   AB  AC 
TEST.Car.Foo.Foo

In this other example:在另一个例子中:

class TEST //AA
{
    class Car //AC
    {
        class Foo //AE
        {
            class Car //AC
            {
                func Foo() -> Void //AE
                {
                    
                }
            }
        }
    }
}

The mangled name is _$s4TESTAAC3CarC3FooCACCAEyyF , again those are the values I would assign:损坏的名称是_$s4TESTAAC3CarC3FooCACCAEyyF ,这些也是我要分配的值:

AA        AB  AC
TEST.TEST.Car.Foo.Car.Foo

Why Foo is assigned to AE after the first long form encoding 3Foo ?为什么Foo在第一个长格式编码3Foo之后被分配给AE Why AB and AD are skipped and not used?为什么ABAD被跳过而不被使用? Note that this is only in regards to recurrent items in the string, if the are no recurrent items i can encode all happily.请注意,这仅适用于字符串中的经常性项目,如果没有经常性项目,我可以愉快地进行编码。

In the first case, the substitution indices are assigned as follows:在第一种情况下,替代指数分配如下:

AA TEST
AB Car
AC TEST.Car
AD Foo
AE TEST.Car.Foo

You can get these results by using the demangling code from this question .您可以使用此问题中的分解代码获得这些结果。 Demangle the mangled name of the method, just without the F suffix - this is to allow us to add any A substitution of our choice to the end. Demangle 方法的损坏名称,只是没有F后缀 - 这是为了允许我们将我们选择的任何A替换添加到末尾。 (I've also removed the yy to reduce noise in the output) (我还删除了yy以减少输出中的噪音)

print(swift_demangle("$s4TEST3CarC3FooCAdA")!)
print(swift_demangle("$s4TEST3CarC3FooCAdB")!)
print(swift_demangle("$s4TEST3CarC3FooCAdC")!)
print(swift_demangle("$s4TEST3CarC3FooCAdD")!)
print(swift_demangle("$s4TEST3CarC3FooCAdE")!)
/*
TEST.Car.FooFooTEST
TEST.Car.FooFooCar
TEST.Car.FooFooTEST.Car
TEST.Car.FooFooFoo
TEST.Car.FooFooTEST.Car.Foo
*/

So it turns out that, when the mangling algorithm mangles Car , it also "remembers" a substitution for TEST.Car (because it is a nominal type, perhaps), not just Car on its own.所以事实证明,当处理算法处理Car时,它也“记住”了TEST.Car的替代(因为它可能是名义类型),而不仅仅是Car本身。 This happens regardless of whether the substitution is used later on or not, probably to make demangling easier (?)无论以后是否使用替换都会发生这种情况,可能是为了更容易进行分解(?)

You can also see this in effect when you change the code to:当您将代码更改为:

class Car
{
    class Foo
    {
        func Foo() -> Car
        {
            fatalError("")
        }
    }
}

Now the method Foo 's mangled name is $s6output3CarC3FooCAdCyF , making use of AC .现在,方法Foo的损坏名称是$s6output3CarC3FooCAdCyF ,使用了AC

As a mental model, you can think of it as the substitution indices being associated with the letter C in the mangled name as well as the <number><identifier> parts, like this:作为一个心理 model,您可以将其视为与错位名称中的字母C以及<number><identifier>部分相关联的替代索引,如下所示:

$s4TEST3CarC3FooCADyyF
   ^     ^ ^  ^ ^
   |     | |  | |
  AA     ABAC ADAE

A similar thing happens in the second case.第二种情况发生了类似的事情。 AB is TEST.TEST and AD is TEST.TEST.Car . ABTEST.TESTADTEST.TEST.Car

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM