Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is name mangling, and how does it work?

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.

like image 338
Stefano Borini Avatar asked Aug 22 '09 00:08

Stefano Borini


People also ask

What is meant by name mangling?

The need for name mangling arises where the language allows different entities to be named with the same identifier as long as they occupy a different namespace (typically defined by a module, class, or explicit namespace directive) or have different signatures (such as in function overloading).

What is the purpose of name mangling in Python?

The name mangling process helps to access the class variables from outside the class. The class variables can be accessed by adding _classname to it. The name mangling is closest to private not exactly private.

What is name mangling in Java?

Name mangling is a term that denotes the process of mapping a name that is valid in a particular programming language to a name that is valid in the CORBA Interface Definition Language (IDL).

What is name mangling with respect to overloaded functions )?

Given a function name of a set of parameters, it will always generate a unique name. If parameters (number of params, type of params or order of params) change then it will generate another name even if the original C++ function name is the same. This process of encoding the function name is known as name mangling.


1 Answers

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

Some examples:

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

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

Multiple types and multiple modules cover the usual contexts.

Typical strategies:

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

  • 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

from

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

like image 169
Norman Ramsey Avatar answered Nov 09 '22 18:11

Norman Ramsey