Objects

• What objects are: – Aggregate structures that combine data (fields) with computation (methods) – Fields have public/private qualifiers (can model ADTs) • Need special support in many compilation stages: Classes, Objects, Subtyping – Type checking – Static analysis and optimizations – Implementation, run-time support

• Features: – inheritance, subclassing, polymorphism, subtyping, overriding, overloading, , abstract classes, interfaces, etc. 1 CS 412/413 Spring 2007 Introduction to Compilers 2

Inheritance Single vs. • Single inheritance: inherit from at most one other object (Java) • Inheritance = mechanism that exposes common features of • Multiple inheritance: may inherit from multiple objects (C++) different objects classclass AA{ { classclass BB{ { • Class B extends class A = “B has the features of A, plus some intint a; a; intint b; b; additional ones”, i.e., B inherits the features of A intint geta(){…}; geta(){…}; intint getb(){…}; getb(){…}; –B is subclass of A; and A is superclass of B }} }}

classclass Point Point{ { floatfloat x, x, y; y; floatfloat getx(){ getx(){ … … }; }; classclass CC :: A,A, BB{ { floatfloat gety(){ gety(){ … … }; }; intint c; c; }} intint getc(){…}; getc(){…}; } classclass ColoredPoint ColoredPointextends extends Point Point { { } intint color; color; intint getcolor(){ getcolor(){ … … }; }; }} CS 412/413 Spring 2007 Introduction to Compilers 3 CS 412/413 Spring 2007 Introduction to Compilers 4

1 Inheritance and Scopes Example class A { Global symtab • How do objects access fields and methods of: int x; A obj =A= – Their own? int f(int z) { B obj =B= int v; … C obj =C= – Their superclasses? } – Other unrelated objects? } =A= symtab =C= symtab • Each class declaration introduces a scope class B extends A { xvarint ovar=A= – Contains declared fields and methods bool y; ffunint → int zvarint int t; – Scopes of methods are sub-scopes } =B= symtab f symtab y var bool z par int • Inheritance implies a hierarchy of class scopes class C { tvarint vvarint A o; – If B extends A, then scope of A is a parent scope for B int z; … }

CS 412/413 Spring 2007 Introduction to Compilers 5 CS 412/413 Spring 2007 Introduction to Compilers 6

Example Class Scopes class A { Global symtab int x; A obj =A= int f(int z) { B obj =B= • Resolve an identifier occurrence in a method: int v; … C obj =C= – Look for symbols starting with the symbol table of the } } =A= symtab =C= symtab current block in that method class B extends A { xvarint ovar=A= bool y; ffunint → int zvarint • Resolve qualified accesses: int t; – Accesses o.f, where o is an object of class A } =B= symtab f symtab y var bool z par int – Walk the symbol table hierarchy starting with the symbol class C { tvarint vvarint table of class A and look for identifier f A o; int z; … – Special keyword this refers to the current object, start with } the symbol table of the enclosing class

CS 412/413 Spring 2007 Introduction to Compilers 7 CS 412/413 Spring 2007 Introduction to Compilers 8

2 Class Scopes Inheritance and Typing

• Multiple inheritance: • Classes have types – A class scope has multiple parent scopes – Type is Cartesian product of field and method types – Which should we search first? – Type name is the class name – Problem: may find symbol in both parent scopes! • What is the relation between types of parent and inherited objects? class A • Overriding fields: • Subtyping: if class B extends A then – Fields defined in a class and in a subclass –Type B is a subtype of A B extends A – Inner declaration shadows outer declaration –Type A is a supertype B – Symbol present in multiple scopes • Notation: B <: A

CS 412/413 Spring 2007 Introduction to Compilers 9 CS 412/413 Spring 2007 Introduction to Compilers 10

Subtype ≈ Subset Subtype Properties

“A value of type S may be used wherever • If type S is a subtype of type T (S <: T), then: a value of type T is expected” a value of type S may be used wherever a value of type T is expected (e.g., assignment to a variable, passed as argument, returned from method) ColoredPoint <: Point

S <: T → values(S) ⊆ values(T) Point x; subtype supertype ColoredPoint y; values of values of x = y; type S type T • Polymorphism: a value is usable as several types • Subtype polymorphism: code using T’s can also use S’s; S objects can be used as S’s or T’s. CS 412/413 Spring 2007 Introduction to Compilers 11 CS 412/413 Spring 2007 Introduction to Compilers 12

3 How To Test the SubType Assignment Statements (Revisited) class A { Relation int x; Global symtab int f(int z) { A obj =A= A, id:T |– E : T B obj =B= (original) int v; … A, id:T |– id = E : T } C obj =C= } =A= symtab =C= symtab class B extends A { xvarint avar=A= bool y; ffunint → int bvar=B= int t; } =B= symtab f symtab A, id:T |– E : S where S<:T (with subtyping) class C { y var bool zargint A, id:T |– id = E : T A a; tvarint vvarint B b; … … a = b; } CS 412/413 Spring 2007 Introduction to Compilers 13 CS 412/413 Spring 2007 Introduction to Compilers 14

Implications of Subtyping Example class A { Global symtab • Type of object may be different from the declared type of reference int x; A obj =A int f(int z) { B obj =B= – Can be the declared class or any subclass int v; … C obj =C= – Precise types of objects known only at run-time } } = • Problem: overriden fields / methods =A= symtab C symtab class B extends A { – Declared in multiple classes in hierarchy. Don’t know statically x var int a var =A= bool y; f fun int → int which declaration to use at compile time int g(int z) { b var =B= int w; … – Java solution: =B= symtab f symtab } • statically resolve fields using declared type of reference; no field } y var bool z arg int overriding g fun int → int v var int • dynamically resolve methods using the object’s type (dynamic class C { dispatch); in support of static type checking, a method m overrides A a = new B(); … B b = new B(); g symtab m’ only if the signatures are “nearly” identical --- the same number …a.x… z arg int and types of parameters, and the return type of m a subtype of the … b.y ... w var int return type of m’ } CS 412/413 Spring 2007 Introduction to Compilers 15 CS 412/413 Spring 2007 Introduction to Compilers 16

4 Example Example class A { Global symtab class A { Global symtab int x; A obj =A int x; A obj =A int f(int z) { int f(int z) { B obj =B= B obj =B= int v; … int v; … = = } C obj =C } C obj =C = = } =A= symtab =C= symtab } =A= symtab =C= symtab class B extends A { x var int a var =A= class B extends A { x var int a var =A= bool x; bool x; f fun int → int f fun int → int int f(int z) { b var =B= int f(int z) { b var =B= int w; … int w; … =B= symtab f symtab =B= symtab f symtab } } } x var bool z arg int } x var bool z arg int v var int v var int class C { f fun int → int class C { f fun int → int A a = new B(); … A a = new B(); … B b = new B(); f symtab B b = new B(); f symtab …a.x … z arg int …a.f(1) … z arg int …b.x ... w var int …b.f(1) ... w var int } } CS 412/413 Spring 2007 Introduction to Compilers 17 CS 412/413 Spring 2007 Introduction to Compilers 18

Objects and Typing Interfaces

• Objects have types • Interfaces are pure types; they don’t give any – … but also have implementation code for methods implementation specification implementation

• ADT perspective: List { classclass MyList MyList implements implements List List{ { interface List { – Specification = typing private int len; intint length(); length(); private int len; List append(int ); privateprivate Cell Cell head, head, tail; tail; List append(int d); – Implementation = method code, private fields intint first(); first(); public int length() {…}; ListList rest(); rest(); – Objects mix specification with implementation public int length() {…}; } publicpublic List List append(int append(int d) d) {…}; {…}; } publicpublic int int first() first() {…} {…} ; ; publicpublic List List rest() rest() {…}; {…}; • Can we separate types from implementation? }}

CS 412/413 Spring 2007 Introduction to Compilers 19 CS 412/413 Spring 2007 Introduction to Compilers 20

5 Multiple Implementations Implementations of Multiple Interfaces • Interfaces allow multiple implementations

interface A { interface List { class SimpleList implements List { int foo(); int length(); private int data; } List append(int); private SimpleList next; interface B { int first(); public int length() List rest(); } { return 1+next.length() } … int bar(); } class AB implements A, B { class LenList implements List { int foo(){ … } private int len; int bar(){ … } private Cell head, tail; } private LenList() {…} public List append(int d) {…} public int length() { return len; } … CS 412/413 Spring 2007 Introduction to Compilers 21 CS 412/413 Spring 2007 Introduction to Compilers 22

Subtyping vs. Subclassing Abstract Classes

• Can use inheritance for interfaces • Classes define types and some values – Build a hierarchy of interfaces (methods) B <: A interface A {…} • Interfaces are pure object types interface B extends A {…}

• Objects can implement interfaces C <: A • Abstract classes are halfway: – define some methods class C implements A {…} – leave others unimplemented – no objects (instances) of abstract class • Subtyping: interface inheritance • Subclassing: object (class) inheritance – Subclassing implies subtyping CS 412/413 Spring 2007 Introduction to Compilers 23 CS 412/413 Spring 2007 Introduction to Compilers 24

6 Subtypes in Java Subtype Hierarchy

class C class C interface I1 1 • Introduction of subtype relation creates a implements I { extends C extends I2 { … } 2 …} hierarchy of types: subtype hierarchy I1 I I C 2 2 type or C1 I2 I3 I C C subtype 1 1 hierarchy C2 C3 C4 class/inheritance I <: I C <: I C <: C hierarchy 1 2 1 2 C5

CS 412/413 Spring 2007 Introduction to Compilers 25 CS 412/413 Spring 2007 Introduction to Compilers 26

7 8 9 o1 o2

10 11 Type unification

• Unified type is least common ancestor in type hierarchy • Least common ancestor is also known as least upper bound (lub) • What if lub does not exist? I1

C1 I2 I3 What is lub(C2,C3)? C2 C3 C4

C5

CS 412/413 Spring 2007 Introduction to Compilers 48

12 Conclusions

• Adding objects and subtyping can complicate type checking • No consensus yet on subtyping – Many design choices – What is best in practice?

13