Ch03 Names Scopes and Bindings 4e
Ch03 Names Scopes and Bindings 4e
1
Contents
2
Name, Scope, and Binding
3
Binding
4
Binding
5
Binding
6
Binding
7
Binding
• In general, early binding times are associated
with greater efficiency
• Later binding times are associated with greater
flexibility
• Compiled languages tend to have early
binding times
• Interpreted languages tend to have later
binding times
• Today we talk about the binding of identifiers
to the variables they name
8
Binding
9
Lifetime and Storage Management
• Key events
– creation of objects
– creation of bindings
– references to variables (which use bindings)
– (temporary) deactivation of bindings
– reactivation of bindings
– destruction of bindings
– destruction of objects
10
Lifetime and Storage Management
• The period of time from creation to destruction is
called the LIFETIME of a binding
– If object outlives binding it's garbage
– If binding outlives object it's a dangling
reference
• The textual region of the program in which the
binding is active is its scope
• In addition to talking about the scope of a binding,
we sometimes use the word scope as a noun all by
itself, without an indirect object
11
Lifetime and Storage Management
14
Lifetime and Storage Management
15
Lifetime and Storage Management
16
Lifetime and Storage Management
17
Scope Rules
• On subroutine exit:
– destroy bindings for local variables
– reactivate bindings for global variables that were
deactivated
• Algol 68:
– ELABORATION = process of creating bindings when
entering a scope
• Ada (re-popularized the term elaboration):
– storage may be allocated, tasks started, even exceptions
propagated as a result of the elaboration of declarations
19
Scope Rules
20
Scope Rules
23
Scope Rules
24
Scope Rules
25
Scope Rules
27
Scope Rules
Example: Static vs. Dynamic
28
Scope Rules
Example: Static vs. Dynamic
30
Scope Rules
Example: Static vs. Dynamic
31
The Meaning of Names within a Scope
• Aliasing
– What are aliases good for? (consider uses of
FORTRAN equivalence)
• space saving - modern data allocation methods are
better
• multiple representations - unions are better
• linked data structures - legit
– Also, aliases arise in parameter passing as an
unfortunate side effect
• Euclid scope rules are designed to prevent this
32
The Meaning of Names within a Scope
• Overloading
– some overloading happens in almost all
languages
• integer + vs. real +
• read and write in Pascal
• function return in Pascal
– some languages get into overloading in a big
way
• Ada
• C++
33
The Meaning of Names within a Scope
• It's worth distinguishing between some closely
related concepts
– overloaded functions - two different things with
the same name; in C++
• overload norm
int norm (int a){return a>0 ? a : -a;)
complex norm (complex c ) { // ...
– polymorphic functions -- one thing that works in
more than one way
• in Modula-2: function min (A : array of integer); …
• in Smalltalk
34
The Meaning of Names within a Scope
35
Binding of Referencing Environments
36
Binding of Referencing Environments
37
Binding of Referencing Environments
38
Binding of Referencing Environments
• REFERENCING ENVIRONMENT of a
statement at run time is the set of active
bindings
• A referencing environment corresponds to a
collection of scopes that are examined (in
order) to find a binding
39
Binding of Referencing Environments
40
Separate Compilation
43
Conclusions
44
Contents
45