Apple Swift杂乱无章的反复背景规则

发布于 2025-01-24 08:27:53 字数 1158 浏览 3 评论 0 原文

在带有Swift 5.6的MacOS上,给定从这里具有以下快速代码:

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

foo is _ $ s4test3carc3foocadyyf (目标名称name test ),我无法理解为什么<为什么<代码> foo 给出了索引代码 ad ,我期望分配 ac

AA   AB  AC 
TEST.Car.Foo.Foo

在其他示例中:

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

杂交名称为 _ $ S4TESTAAC3CARC3FOOCACCAEYYF ,同样是我要分配的值:

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

为什么 foo 分配给 ae> ae 之后,第一个长期编码 3foo ?为什么 AB AD 被跳过且未使用?请注意,这仅在字符串中的复发项目中,如果我可以愉快地编码所有经常性项目,则只有。

On MacOS with Swift 5.6 and given the mangling rules from here with the following Swift code:

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:

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:

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

Why Foo is assigned to AE after the first long form encoding 3Foo? Why AB and AD 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2025-01-31 08:27:53

在第一种情况下,替换指数分配如下:

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

您可以通过使用这个问题。将方法的被弄糊糊的名称删除,而没有 f 后缀 - 这是为了允许我们添加任何 a 替换我们选择的替代。 (我还删除了 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
*/

因此事实证明,当Mangling Algorithm Mangles CAR CAR 时,它也会“记住”替换 test.car (因为它是标称类型),而不仅仅是 car 自己。无论是否以后使用替换,都会发生这种情况,可能会使脱孔更容易(?),

时,您也可以看到这一点。

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

当您将代码更改为:现在:现在方法 foo 名称为 $ s6output3carc3foocadcyf ,使用 ac

作为一种心理模型,您可以将其视为与字母 c 相关联的替代指数,以及&lt; number&gt;&gt;&gt;&lt; didentifier&gt; 零件,像这样:

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

在第二种情况下发生了类似的事情。 ab test.test ad is test.test.car

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. (I've also removed the yy to reduce noise in the output)

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. 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.

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:

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

A similar thing happens in the second case. AB is TEST.TEST and AD is TEST.TEST.Car.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文