Download presentation
Presentation is loading. Please wait.
1
Compilation 2007 Scopes and Environments Michael I. Schwartzbach BRICS, University of Aarhus
2
2 Scopes and Environments Which Declaration Defines a Name? class A { public int x; public boolean y; public void m(int y) { x = y+1; } class B extends A { String z; public void m(boolean b) { y = b; { int y = 42; x = y; (new B()).m(y); } { String y = "foo"; y = y + z; }
3
3 Scopes and Environments Bindings from Names to Declarations class A { public int x; public boolean y; public void m(int y) { x = y+1; } class B extends A { String z; public void m(boolean b) { y = b; { int y = 42; x = y; (new B()).m(y); } { String y = "foo"; y = y + z; } this way to java.lang.String
4
4 Scopes and Environments Environments and Scopes An environment is a map from names to declarations A scope is the area of a program where a given declaration has effect Scopes may be illustrated by coloring the areas of a program which use the same environment
5
5 Scopes and Environments An Environment class A { public int x; public boolean y; public void m(int y) { x = y+1; } class B extends A { String z; public void m(boolean b) { y = b; { int y = 42; x = y; (new B()).m(y); } { String y = "foo"; y = y + z; } int x int y String z boolean b class A class B void m(boolean) void m(int) class String
6
6 Scopes and Environments class A { public int x; public boolean y; public void m(int y) { x = y+1; } class B extends A { String z; public void m(boolean b) { y = b; { int y = 42; x = y; (new B()).m(y); } { String y = "foo"; y = y + z; } A Scope
7
7 Scopes and Environments class A { public int x; public boolean y; public void m(int y) { x = y+1; } class B extends A { String z; public void m(boolean b) { y = b; { int y = 42; x = y; (new B()).m(y); } { String y = "foo"; y = y + z; } A Scope Extends to Subclasses
8
8 Scopes and Environments Static Nested Scopes A B G C H D I E F J ABCDEFIJABCDEFIJ
9
9 Scopes and Environments Most Closely Nested A1A1 B G C H D I A2A2 F A3A3 A3BCDFIA3BCDFI
10
10 Scopes and Environments Stack-Like Behavior A B G C H D I E F J ABCD ABCD | EF ABCD | EF | G ABCD | EF | G | H ABCD | EF | G ABCD | EF ABCD | EF | IJ ABCD | EF ABCD
11
11 Scopes and Environments Implementing Static Nested Scopes An environment for each scope attached to the appropriate AST node implemented as a hash map A search path out through the nested scopes linking the appropriate AST nodes together
12
12 Scopes and Environments When Defining a Name If the name is already in the local environment: identifier already declared Otherwise, insert the name in the environment
13
13 Scopes and Environments When Looking Up a Name Look for the name in the local environment If it is found, we are done Otherwise, repeat from the next environment on the search path If there are no more environments: identifier not declared
14
14 Scopes and Environments Distinct Namespaces Different kinds of identifiers may reside in separate environments: int x = 0; int x(int x) { return x+1; } x = x(x); This requires that a syntactic distinction of the different kinds is possible
15
15 Scopes and Environments Dynamic Scope Look up identifiers on the call stack: int x = 0; int f() { return x; } int g() { int x = 1; return f(); } Static scope: g()==0 Dynamic scope: g()==1 Used in LaTeX, Perl, Lisp,...
16
16 Scopes and Environments Uses and Definitions in Java Every name occurring in a Java program must be linked to a declaration of either: a type (class or interface) a local variable a formal argument a static or non-static field a static or non-static method Names in Java are of the form A.B.C.D
17
17 Scopes and Environments Environments and Scopes in Java Quite complicated! The analysis requires several phases: 1.building a global class environment 2.resolving uses of types 3.checking well-formedness of the class hierarchy 4.disambiguating uses of names 5.resolving uses of locals, formals, and static fields 6.type checking 7.resolving uses of methods and non-static fields
18
18 Scopes and Environments The Class Environment A collection of (fully qualified) class and interface names (types) The class environment must include the standard libraries
19
19 Scopes and Environments Resolving Type Uses Fully qualified names are easy Unqualified names are handled by these rules: 1.try the enclosing class or interface 2.try any single-type-import ( A.B.C.D ) 3.try the same package 4.try any import-on-demand package ( A.B.C.* ) Each of the above must not produce ambiguities This defines a type environment for each class and interface
20
20 Scopes and Environments Prefix Requirement For any type T in such a type environment: if T = A 1.A 2.A 3....A n (n ≥3) then every A 1.A 2....A j (2≤j<n) must not exist in the class environment
21
21 Scopes and Environments Well-Formed Hierarchies A class environment must describe a well-formed class hierarchy Well-formedness consists of a (long) list of surprisingly complex constraints...
22
22 Scopes and Environments Some Simple Constraints 1)A type mentioned in extends must be a class 2)A type mentioned in implements must be an interface 3)An interface must not be mentioned twice in implements 4)A class must not extend a final class 5)Two constructors in a class must not have the same argument types
23
23 Scopes and Environments Descriptions of classes: S I 1 I 2... I k C C is a class that extends S and implements I j SUPER (C) = {S, I 1, I 2,..., I k } By default, S = Object A Class Hierarchy Model (1/4)
24
24 Scopes and Environments A Class Hierarchy Model (2/4) Descriptions of interfaces: I 1 I 2... I k I I is an interface that extends I j SUPER (I) = {I 1, I 2,..., I k } Methods are public abstract, fields are static
25
25 Scopes and Environments A Class Hierarchy Model (3/4) S <T means that S is a strict subtype of T: T SUPER (S) T': S<T' T'<T S T means S < T S = T T = Object DECLARE (T) is the set of methods and fields that are declared in the class or interface T
26
26 Scopes and Environments A Class Hierarchy Model (4/4) For a class or interface T we know: mods(T):the set of modifiers For a method m DECLARE (T) we know: mods(m):the set of modifiers name(m): the name sig(m):the signature (includes the name) type(m):the return type except(m):the set of checked exceptions decl(m):the declaring class or interface (T) For a field f DECLARE (T) we know: name(f):the name type(f):the type decl(f):the declaring class or interface (T) { public, abstract } for interfaces
27
27 Scopes and Environments Derived Sets Given a class hierarchy, we derive some sets: INHERIT (T): the set of methods and fields that T inherits CONTAIN (T) DECLARE (T) INHERIT (T) REPLACE : the set of pairs (m,m') where m replaces m' REPLAC ing is often called hiding for fields and static methods, and overriding otherwise
28
28 Scopes and Environments Inductive Definitions (1/4) The sets INHERIT and REPLACE are populated through the following rules: m DECLARE (T) S SUPER (T) m' CONTAIN (S) sig(m)=sig(m') (m,m') REPLACE
29
29 Scopes and Environments Inductive Definitions (2/4) S SUPER (T) m CONTAIN (S) nodecl(T,m) abstract mods(m) m INHERIT (T) S SUPER (T) m CONTAIN (S) nodecl(T,m) abstract mods(m) allabs(T,m) m INHERIT (T)
30
30 Scopes and Environments Inductive Definitions (3/4) S SUPER (T) f CONTAIN (S) f' DECLARE (T): name(f') name(f) f INHERIT (T) S SUPER (T) m CONTAIN (S) S' SUPER (T) m' CONTAIN (S') nodecl(T,m) sig(m)=sig(m') abstract mods(m) abstract mods(m') (m,m') REPLACE
31
31 Scopes and Environments Inductive Definitions (4/4) We have used the abbreviations: nodecl(T,m) m' DECLARE (T): sig(m) sig(m') allabs(T,m) S SUPER (T): m' CONTAIN (S): sig(m)=sig(m') abstract mods(m')
32
32 Scopes and Environments Populating Interfaces Interfaces without superinterfaces implicitly include abstract versions of all public methods in the class Object
33
33 Scopes and Environments Well-Formedness Constraints (1/3) 1) T: T < T 2) m,m' DECLARE (T): m m' sig(m) sig(m') 3) m,m' CONTAIN (T): sig(m) sig(m') type(m)=type(m') 4) m CONTAIN (T): abstract mods(m) abstract mods(T)
34
34 Scopes and Environments Well-Formedness Constraints (2/3) 5) (m,m') REPLACE : static mods(m) static mods(m') 6) (m,m') REPLACE : type(m)=type(m') 7) (m,m') REPLACE : public mods(m') public mods(m)
35
35 Scopes and Environments Well-Formedness Constraints (3/3) 8) (m,m') REPLACE : e except(m): e' except(m'): e e' 9) (m,m') REPLACE : final mods(m') 10) f,f' DECLARE (T): f f' name(f) name(f') 11) S,S' SUPER (T): f CONTAIN (S), f' CONTAIN (S'): name(f) name(f') decl(f) decl(f') Special Joos restriction, Java is more complicated
36
36 Scopes and Environments Field and Method Environment The methods that are available in an object of type T are exactly those in CONTAIN (T) The fields that are available in an object of type T are exactly those in CONTAIN (T)
37
37 Scopes and Environments Block Environments Blocks and formals use static nested scope rules Except that overlapping scopes are not allowed: { int x; x = 42; { int y y = 12; } { boolean y; y = true; } { String x; x = "foo"; } identifier already declared
38
38 Scopes and Environments Arbitrary Local Declarations Can be understood through a transformation: int x; x = 42; int y = x+1; y = x+12; int z; z = x+y; { int x; x = 42; { int y = x+1; y = x+12; { int z; z = x+y; }
39
39 Scopes and Environments Static Scope Rules Java has this search path of environments: 1.the local block environments (including formals) 2.the field and method environments of the enclosing class 3.the type environment of the enclosing class This defines a function lookupName(...)
40
40 Scopes and Environments Resolving The Lvalue A Check that lookupName(A) returns one of: a local a static field a non-static field
41
41 Scopes and Environments Resolving The Method Invocation A(...) Check that the method environment of the enclosing class declares A as one of: a static method a non-static method
42
42 Scopes and Environments Ambiguous Names Names occurring as: lvalues: A.B.C.D method invocations: A.B.C.D(...) are syntactically ambiguous We must now resolve their meanings...
43
43 Scopes and Environments Disambiguating A 1.A 2....A n If lookupName(A 1 ) is a local, then A 2,...,A n are all non-static fields If lookupName(A 1 ) is a field, then A 2,...,A n are all non-static fields If lookupName(A 1 ) is an unqualified type, then A 2 is a static field and A 3,...,A n are all non-static fields If lookupName(A 1....A k ) is a qualified type and 1<k<n then A k+1 is a static field and A k+2,...,A n are all non-static fields
44
44 Scopes and Environments Disambiguating A 1.A 2....A n (...) (1/2) If lookupName(A 1 ) is a local, then A 2,...,A n-1 are all non-static fields and A n is a non-static method If lookupName(A 1 ) is a field, then A 2,...,A n-1 are all non-static fields and A n is a non-static method If lookupName(A 1 ) is an unqualified type and n=2, then A 2 is a static method If lookupName(A 1 ) is an unqualified type and n>2, then A 2 is a static field, A 3,...,A n-1 are non-static fields and A n is a non-static method
45
45 Scopes and Environments Disambiguating A 1.A 2....A n (...) (2/2) If lookupName(A 1...A k ) is a qualified type and 1<k=n-1, then A n is a static method If lookupName(A 1...A k ) is a qualified type and 1<k<n-1, then A k+1 is a static field, A k+1,...,A n-1 are non-static fields, and A n is a non-static method
46
46 Scopes and Environments Resolving Local and Static Field Uses Every use of a local variable or a static field is now linked to its declaration Other uses of fields and methods look like: exp.foo exp.foo(...) which can only be resolved using knowledge about the type of exp
47
47 Scopes and Environments Type Checking Type checking will for every expression determine a static type (class, interface, or primitive type) The run-time type of the result will be a subclass of the static type We will later see how to do this...
48
48 Scopes and Environments Resolving Field and Method Uses For a non-static field: get the declaration from the field environment of the static type of its object For a method: get the declaration from the field and method environment of the static type of its object, using the static types of the arguments to resolve overloading
49
49 Scopes and Environments Distinct Namespaces (1/2) Fields and methods reside in different namespaces public class Foo { public static int x; public static int x(int x) { return x+1; } public static void main(String[] args) { x = x(x); }
50
50 Scopes and Environments Distinct Namespaces (2/2) Locals and types reside in distinct namespaces (provided they syntactically distinguishable) public class Foo { public void m(int a) { String String = "bar"; String = String; String s = "baz"; String = s; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.