Compilation 2007 Scopes and Environments Michael I. Schwartzbach BRICS, University of Aarhus.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Programming Languages and Paradigms
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Access to Names Namespaces, Scopes, Access privileges.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Programming Languages and Paradigms Imperative Programming.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
Types in programming languages1 What are types, and why do we need them?
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Classes, Interfaces and Packages
CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
4. The procedural extension Java c of Java I pslab 김윤경.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Modern Programming Tools And Techniques-I
Name Spaces: ALL versus OOL
Inheritance-Basics.
Inheritance and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Programming
Overloading and Constructors
Interface.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Chapter 5 Classes.
Presentation transcript:

Compilation 2007 Scopes and Environments Michael I. Schwartzbach BRICS, University of Aarhus

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 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 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 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 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 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 Scopes and Environments Static Nested Scopes A B G C H D I E F J ABCDEFIJABCDEFIJ

9 Scopes and Environments Most Closely Nested A1A1 B G C H D I A2A2 F A3A3 A3BCDFIA3BCDFI

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Scopes and Environments Populating Interfaces  Interfaces without superinterfaces implicitly include abstract versions of all public methods in the class Object

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 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 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 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 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 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 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 Scopes and Environments Resolving The Lvalue A  Check that lookupName(A) returns one of: a local a static field a non-static field

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 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 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 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 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 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 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 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 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 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; }