types and inheritances 66.648 compiler design lecture (03/04//98) computer science rensselaer...

22
Types and Types and Inheritances Inheritances 66.648 Compiler Design Lecture 66.648 Compiler Design Lecture (03/04//98) (03/04//98) Computer Science Computer Science Rensselaer Polytechnic Rensselaer Polytechnic

Upload: anne-bryant

Post on 27-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Types and InheritancesTypes and Inheritances

66.648 Compiler Design Lecture (03/04//98)66.648 Compiler Design Lecture (03/04//98)

Computer ScienceComputer Science

Rensselaer PolytechnicRensselaer Polytechnic

Lecture OutlineLecture Outline

• UnificationUnification

• ExamplesExamples

• InheritancesInheritances

• AdministrationAdministration

Unification AlgorithmUnification Algorithm

Boolean unify(node m, node n)Boolean unify(node m, node n)

{ s= find(m); t = find(n); /* find the root node */{ s= find(m); t = find(n); /* find the root node */

if (s==t) return true; else if (s and t represent the if (s==t) return true; else if (s and t represent the same basic type) return true;same basic type) return true;

else if s is an op-node with children s1 and s2 else if s is an op-node with children s1 and s2 and t is an op_node with children t1 and t2 and t is an op_node with children t1 and t2

{ union(s,t); return unify(s1,t1) and unify(s2,32) }{ union(s,t); return unify(s1,t1) and unify(s2,32) }

else if s or t represents a variable { union(s,t);else if s or t represents a variable { union(s,t);

return true;} else return false; }return true;} else return false; }

ExampleExample(a1->a2) x list(a3) -> list(a2)(a1->a2) x list(a3) -> list(a2)

(a3->a4)x list(a2) -> a5(a3->a4)x list(a2) -> a5

fun length(lptr) = if null(lptr) then 0 elsefun length(lptr) = if null(lptr) then 0 else

length(tl(lptr))+1 end;length(tl(lptr))+1 end;

list(a) -> integer list(a) -> integer

Fun temp(a,b) = a(b);Fun temp(a,b) = a(b);

Fun temp1 a b = a(b); Fun temp1 a b = a(b);

Fun temp2 a b = temp1 b a; Fun temp2 a b = temp1 b a;

Fun x1(a,b) = a;Fun x1(a,b) = a;

Fun X2(a,b) =b;Fun X2(a,b) =b;

Fun X3(a,b) =X1(a,X2(a,b));Fun X3(a,b) =X1(a,X2(a,b));

Fun map(f,lptr) = if null(lptr) then nil elseFun map(f,lptr) = if null(lptr) then nil else

cons( f(hd(l)) , map(f, tl(l)) );cons( f(hd(l)) , map(f, tl(l)) );

ExamplesExamples

Inheritance extends abstract data types by Inheritance extends abstract data types by allowing for super type / subtype relationships. allowing for super type / subtype relationships. Inheritance supports incremental code reuse by Inheritance supports incremental code reuse by allowing one to define a suntype with allowing one to define a suntype with incremental changes to the procedural and data incremental changes to the procedural and data abstractions in the base type.abstractions in the base type.

Example:Example:

public class animal {public class animal {

}}

InheritanceInheritance

Inheritance-ContdInheritance-Contd

Public class brontasaurus extends dinosaur {Public class brontasaurus extends dinosaur {

int neck_length;int neck_length;

private int compute_neck_length { ..}private int compute_neck_length { ..}

public void print_neck_length() {..}public void print_neck_length() {..}

}}

The class brontasaurus is a subtype of class The class brontasaurus is a subtype of class dinosaur. An instance of brontasaurus contains dinosaur. An instance of brontasaurus contains all the fields of dinosaur. All public and private all the fields of dinosaur. All public and private functions are available to brontasarus.functions are available to brontasarus.

Inheritance - ContdInheritance - Contd

Single Inheritance: When the type system restricts Single Inheritance: When the type system restricts each type to have at most one super type.each type to have at most one super type.

Multiple Inheritance: When the type system Multiple Inheritance: When the type system permits a type to have more than one super permits a type to have more than one super type.type.

Inheritance Hierarchy: The type/supertype Inheritance Hierarchy: The type/supertype relationship defined by extends construct is relationship defined by extends construct is called an inheritance hierarchy. For a type called an inheritance hierarchy. For a type system with single inheritance, the inheritance system with single inheritance, the inheritance hierarchy can be modeled as a forest trees. hierarchy can be modeled as a forest trees.

Access RulesAccess Rules

1. Scope visibility: variables and fields of type 1. Scope visibility: variables and fields of type class T1 can be declared anywhere in a program class T1 can be declared anywhere in a program that a declaration is permitted and T1’s that a declaration is permitted and T1’s definition is visible.definition is visible.

Note: If the language permits two classes T1 and Note: If the language permits two classes T1 and T2 to contain functions with the same name T2 to contain functions with the same name when class T1 is an ancestor of T2, then name when class T1 is an ancestor of T2, then name resolution follows standard scope rules by resolution follows standard scope rules by treating T2 is a scope contained with T1.treating T2 is a scope contained with T1.

2. Data access: public data-fields of class T1 are 2. Data access: public data-fields of class T1 are accessible. accessible.

Access - ContdAccess - Contd

3. Access to private procedures: private 3. Access to private procedures: private procedures of an class T1 can only be invoked procedures of an class T1 can only be invoked by functions in class T1 or in T2 that is a by functions in class T1 or in T2 that is a descendant of T1 in the inheritance hierarchy.descendant of T1 in the inheritance hierarchy.

4. Access to public functions: public functions of 4. Access to public functions: public functions of T1 can be invoked by any function that can T1 can be invoked by any function that can declared as an instance of T1.declared as an instance of T1.

5. Automatic Type Conversion:an expression of 5. Automatic Type Conversion:an expression of type class T2 is coerced into an ancestor of type class T2 is coerced into an ancestor of class T1 when required but not vice-versa.class T1 when required but not vice-versa.

AggregationAggregation

A class T2 is an aggregation of class T1 if T2 A class T2 is an aggregation of class T1 if T2 contains one or more fields of type T1. Unlike in contains one or more fields of type T1. Unlike in inheritance class T2 cannot access private inheritance class T2 cannot access private functions in T1.functions in T1.

Public class Jurassic_Park {Public class Jurassic_Park {

dinosaur animals[]; dinosaur animals[];

}}

We cannot invoke private functions from Jurassic We cannot invoke private functions from Jurassic Park.Park.

Multiple InheritanceMultiple Inheritance

Some languages permit multiple inheritance I.e., Some languages permit multiple inheritance I.e., allow a class to be an extension of mutiple allow a class to be an extension of mutiple classes. This leads to more complicated classes. This leads to more complicated semantics for sub typing.semantics for sub typing.

Public class flying extends dinosaur, fly {Public class flying extends dinosaur, fly {

}}

Multiple InheritanceMultiple Inheritance

The inheritance hierarchy becomes a directed The inheritance hierarchy becomes a directed acyclic graph in the case of multiple acyclic graph in the case of multiple inheritance. If a function is defined both inheritance. If a function is defined both classes, then the leftmost parent leading to the classes, then the leftmost parent leading to the definition of the super type is taken.definition of the super type is taken.

Java ProgramJava Program

Public class test1 {Public class test1 {

public static void main(String argsv[]) {public static void main(String argsv[]) {

int I;int I;

I = 234;I = 234;

I = 5000;I = 5000;

}}

}}

Byte CodeByte Code

Class File FormatClass File Format

Page 84 of the Virtual Machine BookPage 84 of the Virtual Machine Book

ClassFile {ClassFile {

u4 magic;u4 magic;

u2 minorversion;u2 minorversion;

u2 majorversion;u2 majorversion;

u2 constant_pool_count;u2 constant_pool_count;

cp_info constant_pool[constant_pool_count-1];cp_info constant_pool[constant_pool_count-1];

u2 access_flags;u2 access_flags;

Class File Format- ContdClass File Format- Contd

U2 this_class;U2 this_class;

u2 super_class;u2 super_class;

u2 interfaces_count;u2 interfaces_count;

u2 interfaces[interfaces_count];u2 interfaces[interfaces_count];

u2 fields_count;u2 fields_count;

field_info fields_count[fields_count];field_info fields_count[fields_count];

u2 methods_count;u2 methods_count;

methods_info methods[methods_count];methods_info methods[methods_count];

Class File Format- ContdClass File Format- Contd

U2 attributes_count;U2 attributes_count;

attribute_info attributes[attributes_count];attribute_info attributes[attributes_count];

}}

Example:Example:

Type SignaturesType Signatures

B byteB byte

C Char Field DescriptorC Char Field Descriptor

D DoubleD Double

F FloatF Float

I IntI Int

J LongJ Long

L<classname> an instance of a classL<classname> an instance of a class

S ShortS Short

Type Signatures-ContdType Signatures-Contd

Z BooleanZ Boolean

[ one array of one dimension[ one array of one dimension

Example: double d[][];Example: double d[][];

[[D[[D

Method Descriptor:Method Descriptor:

(Parameter Descriptor *) Return Descriptor(Parameter Descriptor *) Return Descriptor

Eg: Object Mymethod(int I, doble d) Eg: Object Mymethod(int I, doble d) (ID)Ljava/lang/Object(ID)Ljava/lang/Object

Constant PoolConstant Pool

Cp_info {Cp_info {

u1 tag;u1 tag;

u1 info[];}u1 info[];}

Tags are 1 unicode, 3 integer, 4 float, 5 long, 6 Tags are 1 unicode, 3 integer, 4 float, 5 long, 6 double, 7 class, 8 String, 9 Fieldref, 10 double, 7 class, 8 String, 9 Fieldref, 10 Methodref, 11 InterfaceMethodref and 12 Methodref, 11 InterfaceMethodref and 12 NameandType.NameandType.

Comments and FeedbackComments and Feedback

Project 2 is out. Please start working. PLEASE do Project 2 is out. Please start working. PLEASE do not wait for the due date to come.not wait for the due date to come.

We have finished chapter 6 and looked at the We have finished chapter 6 and looked at the relavant portion of Java.. Please keep studying relavant portion of Java.. Please keep studying this material. It may look difficult.this material. It may look difficult.