210 likes | 538 Views
The object-oriented extension Java o of Java c (Java and the Java Virtual Machine ch. 5). PSLab 문세원 . 1. Static semantics of Java o. Definition 5.1.1 : Types A, B, C are generated as follows Primitive types are types(see table 3.1) Classes and interfaces are types
E N D
The object-oriented extension Javaoof Javac(Java and the Java Virtual Machine ch. 5) PSLab 문세원
1. Static semantics of Javao • Definition 5.1.1 : Types A, B, C are generated as follows • Primitive types are types(see table 3.1) • Classes and interfaces are types • Null and void are types • If A is a type different from Null and void, then A[] is a type
1. Static semantics of Javao • Definition 5.1.2 : For reference types, the relation ⊆ is the least reflexive and transitive relation satisfying the following conditions • If A ⊂d B, then A ⊆ B. • If A is a reference type, then Null ⊆ A and A ⊆ Object • A[] ⊆ Cloneable and A[] ⊆ Serializable. • If A ⊆ B and A, B are reference types, then A[] ⊆ B[]
1. Static semantics of Javao • Example 5.1.1 : Although every reference type is a subtype of Object, this is not true for primitive types. • Java.lang.Integer ⊆ Object • int ⊆ Object (x) • Note, that although int is a subtype of long the type array int is not a subtype of array of long. • int ⊆ long, but int[] ⊆ long[] is not true.
1.2 Syntax of Javao • Syntax of Javao • Exp :=...| null | this | Exp.Field | super.Field | Expinstanceof Class | (Class) Exp • Asgn := ...| Exp.Field = Exp | super.Field= Exp • Invk : = ...| newClass(Exps) | Exp.Meth(Exps) | super.Meth(Exps)
1.3 Constructor declarations • Syntax <public|protected|private> A(B1 loc1, …,Bn locn) cbody cbody := block | {this(exps); bstm…} | {super(exps); bstm..}
1.3 Constructor declarations • Example Class A{ private int x; private int y = 17; static int z = 3; A(int x){ this.x = x; } } A(int x){ super(); y = 17; this.x = x; }
1.4 Field access expressions • Instance field access expressions are transformed at compile-time into the abstract form exp.C/field • Instance fields can be accessed • exp.field → exp.C/field • super.field → this.C/field • field → this.C/field
1.5 Overloaded methods • Instance method invocations expressions are transformed at compile-time into the abstract form exp.D/msig(exps) • Instance methods can be invoked • α(βexp.methν(exps)) → α(βexp.D/mν(exps)) • αsuper.methν(exps) → α(βthis.D/mν(exps)) • αmethν(exps) →α(βthis.D/mν(exps)) • first step : Compiler computes a set app(α) • next step : A most specific method is selected.
1.5 Overloaded methods • Instance method invocations have an additional callKind which is used for method lookup (assume D/m) • dataKind = Special | Virtual | Super • Special, overriding is not allowed and the instance method m in class D is called directly • Virtual, then the instance method m is looked up dynamically starting at the class of the target reference • Super, the instance method m in class D is called directly
1.6 Instance creation expressions • be treated like ordinary method invocations • new C(exps) → (new C).C/msig(exps) • Since constructors are not inherited, applicable constructors are always in the same class. • The callKind of a constructor invocation is Special
1.7 Type checking of Javao • Table 5.2 Type constraints for Javao
1.7 Type checking of Javao • Table 5.3 Type constraints after introduction of primitive type casts
1.8 Vocabulary of Javao • The following static functions look up compile-time info. in the environment. • instanceFields : class → Powerset(Class/Field) • defaultVal : Type → Val • type: Class/Field → Type • lookup: (Class, Class/Msig) → Class • type Val = ... | Ref | null • data Heap = Object(Class, Map( Class/Field, Val)) heap : Ref → Heap classOf : Ref → Class classOf(ref) = case heap(ref) of Object(c, fields) → c
1.8 Vocabulary of Javao package p; public class A{ String m(){ return “p”;} public String n(){ return this.m();} } package q; public class B extends p.A{ public String m(){ return “q”;} public static void main(String[] _){ B x = new B(); Sytem.out.println(x.n()); } } lookup(q.B, p.A/m) → lookup(p.A, p.A/m) → p.A If p.A/m() is declared public, lookup(q.B, p.A/m) → lookup(q.B, p.A/m) → q.B Class A{ private int x; public int y; public static int z; } Class B extends A{ private int x; } instanceFields(B) =[A/x, A/y, B/x]
2. Transition rules for Javao • Fig. 5.2 Execution of Javao expressions execJavaExpo = case context(pos) of this → yield(locals(“this”)) new c → if initialized(c) then create ref heap(ref) := Object(c,{(f,defaultVal(type(f))) | f ∈ instanceFields(c)}) yield(ref) else initialize(c)
2. Transition rules for Javao αexp.c/f → pos : = α ►ref.c/f → if ref not null then yieldUp(getField(ref,c/f)) αexp1.c/f = βexp2→ pos : = α ►ref.c/f = βexp → pos : = β αref.c/f = ►val → if ref not null then setField(ref, c/f, val) yieldUp(val) αexp instanceof c → pos : = α ►ref instanceof c → yieldUp(ref not null ∧ classOf(ref) ⊆ c)
2. Transition rules for Javao (c)αexp → pos : = α (c)►ref → if ref = null ∨ classOf(ref) ⊆ c then yieldUp(ref) αexp.c/mβ(exps) → pos : = α ►ref.c/mβ(exps) → pos : = β αref.c/m►(vals) → if ref not null then let c´ = case callKind(up(pos)) of Virual → lookup(classOf(ref), c/m) Super → lookup(super(classNm(meth)), c/m) Special → c invokdMethod(up(pos), c´/m, [ref]• vals)
2. Transition rules for Javao getField(ref, f) = case heap(ref) of Object(c, fields) → fields(f) setField(ref, f, val) = heap(ref) := Object(c, fields + {(f, val)}) where Object(c, fields) = heap(ref) exitMethod(result)= … elseif methNm(meth) = “<init>” ∧ result = Norm then restbody := oldPgm[locals(“this”) / oldPos] …