HOME

TheInfoList



OR:

In
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming l ...
s, name resolution is the resolution of the
token Token may refer to: Arts, entertainment, and media * Token, a game piece or counter, used in some games * The Tokens, a vocal music group * Tolkien Black, a recurring character on the animated television series ''South Park,'' formerly known as ...
s within program expressions to the intended program components.


Overview

Expressions in computer programs reference variables, data types, functions, classes, objects, libraries, packages and other entities by name. In that context, name resolution refers to the association of those not-necessarily-unique names with the intended program entities. The
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
s that determine what those identifiers refer to in specific contexts are part of the language definition. The complexity of these algorithms is influenced by the sophistication of the language. For example, name resolution in assembly language usually involves only a single simple
table lookup In computer science, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation. The process is termed as "direct addressing" and LUTs differ from hash tables in a way that, to retrieve a value v wi ...
, while name resolution in C++ is extremely complicated as it involves: *
namespace In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
s, which make it possible for an identifier to have different meanings depending on its associated namespace; *
scope Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * CinemaS ...
s, which make it possible for an identifier to have different meanings at different scope levels, and which involves various scope overriding and hiding rules. At the most basic level name resolution usually attempts to find the binding in the smallest enclosing scope, so that for example local variables supersede global variables; this is called ''
shadowing Shadowing may refer to: * Shadow fading in wireless communication, caused by obstacles * File shadowing, to provide an exact copy of or to mirror a set of data * Job shadowing, learning tasks by first-hand observation of daily behavior * Project ...
''. * ''visibility rules'', which determine whether identifiers from specific namespaces or scopes are visible from the current context; * overloading, which makes it possible for an identifier to have different meanings depending on how it is used, even in a single namespace or scope; * ''accessibility'', which determines whether identifiers from an otherwise visible scope are actually accessible and participate in the name resolution process.


Static versus dynamic

In
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming l ...
s, name resolution can be performed either at
compile time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
or at runtime. The former is called static name resolution, the latter is called dynamic name resolution. A somewhat common misconception is that
dynamic typing In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
implies dynamic name resolution. For example, Erlang is dynamically typed but has static name resolution. However, static typing does imply static name resolution. Static name resolution catches, at compile time, use of variables that are not in scope; preventing programmer errors. Languages with dynamic scope resolution sacrifice this safety for more flexibility; they can typically set and get variables in the same scope at runtime. For example, in the Python interactive REPL: >>> number = 99 >>> first_noun = "problems" >>> second_noun = "hound" >>> # Which variables to use are decided at runtime >>> print(f"I got but a ain't one.") I got 99 problems but a hound ain't one. However, relying on dynamic name resolution in code is discouraged by the Python community. The feature also may be removed in a later version of Python. Examples of languages that use static name resolution include C, C++, E, Erlang, Haskell,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, Pascal, Scheme, and
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by ...
. Examples of languages that use dynamic name resolution include some
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispi ...
dialects,
Perl Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
,
PHP PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
, Python, REBOL, and Tcl.


Name masking

Masking occurs when the same identifier is used for different entities in overlapping lexical scopes. At the level of variables (rather than names), this is known as variable shadowing. An identifier I' (for variable X') masks an identifier I (for variable X) when two conditions are met # I' has the same name as I # I' is defined in a scope which is a subset of the scope of I The outer variable X is said to be ''shadowed'' by the inner variable X'. For example, the parameter "foo" shadows the local variable "foo" in this common pattern: private int foo; // Name "foo" is declared in the outer scope public void setFoo(int foo) public int getFoo() Name masking can cause complications in function overloading, due to overloading not happening across scopes in some languages, notably C++, thus requiring all overloaded functions to be redeclared or explicitly imported into a given namespace.


Alpha renaming to make name resolution trivial

In programming languages with lexical scoping that do not reflect over variable names,
α-conversion Lambda calculus (also written as ''λ''-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation t ...
(or α-renaming) can be used to make name resolution easy by finding a substitution that makes sure that no variable name masks another name in a containing scope. Alpha-renaming can make
static code analysis In computer science, static program analysis (or static analysis) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs during their execution. The term ...
easier since only the alpha renamer needs to understand the language's scoping rules. For example, in this code: class Point within the constructor, the
class variable In class-based, object-oriented programming, a class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. A class variable is not an instance variable. It is a special ...
s and are shadowed by local variables of the same name. This might be alpha-renamed to: class Point In the new version, there is no masking, so it is immediately obvious which uses correspond to which declarations.


See also

*
Namespace (programming) In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
* Scope (programming) * Naming collision


References

{{DEFAULTSORT:Name resolution Computer libraries Compiler construction