什么是名称修改,它是如何工作的? [英] What is name mangling, and how does it work?

查看:80
本文介绍了什么是名称修改,它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释什么是名称修饰,其工作原理,解决的问题以及使用的上下文和语言.名称修饰策略(例如,编译器选择了什么名称以及为什么选择)是一个加号.

Please explain what is name mangling, how it works, what problem it solves, and in which contexts and languages is used. Name mangling strategies (e.g. what name is chosen by the compiler and why) a plus.

推荐答案

在您选择的编程语言中,如果标识符是从单独编译的单元中导出的,则它需要一个在链接时可被识别的名称.改名解决了编程语言中标识符过载的问题. (如果在多个上下文中或具有多个含义的情况下使用相同的名称,则该标识符为重载".)

In the programming language of your choice, if an identifier is exported from a separately compiled unit, it needs a name by which it is known at link time. Name mangling solves the problem of overloaded identifiers in programming languages. (An identifier is "overloaded" if the same name is used in more than one context or with more than one meaning.)

一些例子:

  • 在C ++中,函数或方法get可能在多种类型中重载.

  • In C++, function or method get may be overloaded at multiple types.

在Ada或Modula-3中,功能get可能会出现在多个模块中.

In Ada or Modula-3, function get may appear in multiple modules.

多种类型和多种模块涵盖了通常的上下文.

Multiple types and multiple modules cover the usual contexts.

典型策略:

  • 将每种类型映射为一个字符串,并使用组合的高级标识符和类型字符串"作为链接时名称.在C ++(特别容易,因为仅允许对函数/方法进行重载并且仅对参数类型进行重载)和Ada(在其中也可以重载结果类型)中很常见.

  • Map each type to a string and use the combined high-level identifier and "type string" as the link-time name. Common in C++ (especially easy since overloading is permitted only for functions/methods and only on argument types) and Ada (where you can overload result types as well).

如果在多个模块或名称空间中使用了标识符,则将模块名称与标识符名称连接起来,例如,用List_get代替List.get.

If an identifier is used in more than one module or namespace, join the name of the module with the name of the identifier, e.g., List_get instead of List.get.

根据链接时名称中哪些字符合法,您可能需要进行其他处理;例如,可能需要使用下划线作为转义字符,以便您区分

Depending on what characters are legal in link-time names, you may have to do additional mangling; for example, it may be necessary to use the underscore as an 'escape' character, so you can distinguish

  • List_my.get-> List__my_get
  • List_my.get -> List__my_get

来自

  • List.my_get-> List_my__get
  • List.my_get -> List_my__get

(诚然,这个例子已经到了,但是作为一名编译器作者,我必须保证源代码中的唯一标识符映射到不同的链接时名称.这就是命名的全部原因和目的.搞砸了.)

(Admittedly this example is reaching, but as a compiler writer, I have to guarantee that distinct identifiers in the source code map to distinct link-time names. That's the whole reason and purpose for name mangling.)

这篇关于什么是名称修改,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆