require "java-core.k" require "java-expressions.k" require "java-statements.k" require "java-arrays.k" require "class-dec/RuntimeException.k" require "class-dec/AssertionError.k" module JAVA-CLASSES imports JAVA-CORE imports JAVA-EXPRESSIONS imports JAVA-STATEMENTS imports JAVA-ARRAYS imports CLASS-DEC-RuntimeException imports CLASS-DEC-AssertionError syntax Type ::= "function" "from" List{K} "to" Type [latex({#1}\ \texttt{->}\ {#2})] | "class" Id syntax RawVal ::= "objectClosure" "(" Bag ")" | "methodClosure" "(" Id "," // Class - method class Int "," // OL - object location K "," // 'ListWrap(Params) - params K "," // S - method body Type // MethodType ")" //@ \subsection{Unused Labels} syntax KLabel ::= "'Static" | "'TypeImportOnDemandDec" | "'PackageName" /*@ \subsection{Object and method closures} The only change to untyped KOOL's values is that closures are now typed (their last argument holds their type). */ //@ \subsection{Type labels} //@Here we rewrite java type ast into simple types rule 'ClassOrInterfaceType('TypeName(K:K),,_) => 'TypeName(K) [structural] rule 'TypeName(X:Id) => class X [structural] rule class X:Id => rtString when Id2String(X) ==String "String" [structural] /*@ \subsection{Method declaration} Methods are now typed and we need to store their types in their closures, so that their type contract can be checked at invocation time. The rule below is conceptually similar to that of untyped KOOL; the only difference is the addition of the types. */ context 'MethodDec('MethodDecHead(_:K,, _:K,, HOLE,, _:List{K}),, _) rule [MethodDec]: 'MethodDec( 'MethodDecHead(_,, _,, ReturnType:Type,, Name:Id,, 'ListWrap(Params:List{K}),, _),, S:K ) => storeMethod(Name, 'ListWrap(Params), S, function from Params to ReturnType) [structural] syntax K ::= "storeMethod" "(" Id "," K "," K "," Type ")" context storeMethod(_, _, _, function from (_:List{K},, (HOLE => typeOf(HOLE)),, _:List{K}) to _) rule [storeMethod]: storeMethod(Name:Id, 'ListWrap(Params:List{K}), S:K, MethodType:Type) =>. ... Class:Id OL:Int Env:Map => Env[L/Name] ... . => L |-> methodClosure(Class, OL, 'ListWrap(Params), S, MethodType) :: MethodType ... L:Int => L +Int 1 rule [ConstrDecSuperCallDesugar]: 'ConstrDec( _:K,, 'ConstrBody( ( 'None(_) => 'Some('SuperConstrInv( 'None(.List{K}),, 'ListWrap(.List{K}) )) ),, _:K ) ) rule [ConstrDec]: 'ConstrDec('ConstrDecHead(_,, _,, Name:Id,, 'ListWrap(Params:List{K}),, _),, 'ConstrBody( 'Some('SuperConstrInv( _,, SuperParamsList:K )) ,,S:K)) => storeConstructor(Name, 'ListWrap(Params), SuperParamsList, S, function from Params to void) syntax K ::= "storeConstructor" "(" Id "," K "," K "," K "," Type ")" context storeConstructor(_, _, _, _, function from (_:List{K},, (HOLE => typeOf(HOLE)),, _:List{K}) to _) rule [storeConstructor]: storeConstructor(Name:Id, 'ListWrap(Params:List{K}), SuperParamsList:K, S:K, MethodType:Type) => storeMethod( Name, 'ListWrap(Params), 'ExprStm( 'Invoke( 'Method('MethodName( BaseClass )),, SuperParamsList ) ) ~> S, MethodType ) ... Class:Id Class BaseClass:Id when Id2String(BaseClass) =/=String "Object" rule [storeConstructorSubclassOfObject]: storeConstructor(Name:Id, 'ListWrap(Params:List{K}), _, S:K, MethodType:Type) => storeMethod( Name, 'ListWrap(Params), S, MethodType ) ... Class:Id Class BaseClass:Id when Id2String(BaseClass) ==String "Object" /*@ \subsection{Program initialization} When done with the first pass, call \texttt{main()}. */ rule [CompilationUnit]: 'CompilationUnit(_,,_,,K:K) => K ~> classDecRuntimeException ~> classDecAssertionError ~> execute syntax K ::= "execute" rule [execute]: execute => //k-ast for new ().main(new String[0]); 'ExprStm( 'Invoke( 'Method( 'NewInstance( 'None(.List{K}),, 'ClassOrInterfaceType( 'TypeName(MainClass),, 'None(.List{K}) ),, 'ListWrap(.List{K}),, 'None(.List{K}) ),, 'None(.List{K}),, String2Id("main") ),, 'ListWrap('NewArray( 'TypeName( String2Id("String") ),, 'ListWrap('Dim(0 :: int)),, 'ListWrap(.List{K}))) ) ) . MainClass:Id //@ \subsection{Auxiliary operations} //@ \texttt{bindto} also checks the well-formedness of the function parameters syntax K ::= "bindto" List{K} "," List{K} context bindto('Param(_:K,,HOLE,,_),, _:List{K}), _:List{K} rule [bindto]: (. => 'ExprStm('Assign('ExprName(X),,TV:TypedVal))) ~> bindto( ('Param(_,,T:Type,,X:Id) => .List{K}),, _:List{K} ), ( (TV => .List{K}),, _:List{K} ) ... Env:Map => Env[L/X] ... . => L |-> unruled(T) :: T ... L:Int => L +Int 1 rule [bindtoEmpty]: bindto .List{K},.List{K} => . ... [structural] //@ \texttt{typeOf and toString} rule typeOf('Param(_:K,,K:K,,_)) => K [anywhere] rule toString(class Class:Id) => Id2String(Class) :: rtString when Id2String(Class) =/=String "RuntimeException" andBool Id2String(Class) =/=String "AssertionError" [anywhere] rule toString(class Class:Id) => "java.lang." +String Id2String(Class) :: rtString when Id2String(Class) ==String "RuntimeException" orBool Id2String(Class) ==String "AssertionError" [anywhere] rule toString(objectClosure(Obj:Bag) :: T:Type) => 'Invoke( 'MethodName(objectClosure(Obj) :: T,, String2Id("toString")),, 'ListWrap(.List{K}) ) [anywhere] /*@ Recall that lists of the form "a,b,c" are a syntactic convenience, which eventually are translated into cons-lists "(a,(b,(c,.)))". Unfortunately, the current K tool is not able to cons-listify the RHSes of the rules below, so we have to do it manually. */ /*@ \subsection{Changes to SIMPLE Semantics} We extend/change the semantics of several SIMPLE constructs in order to take advantage of the richer KOOL semantic infrastructure and thus get more from the existing SIMPLE constructs. */ /*@ \subsubsection{Subtyping constraints} The subclass relation introduces a subtyping relation. */ rule [subtypeRed]: subtype class Class1:Id, class Class:Id => subtype class Class2, class Class ... Class1 Class2:Id when Class1 =/=K Class [structural] rule [subtypeFalse]: subtype class X:Id, class Class:Id => false :: bool when Id2String(X) ==String "Object" andBool X =/=K Class /*@ \subsubsection{Unsafe Casting} Performs unsafe casting. One should only use it in combination with the subtype relation above. */ rule unsafeCast(objectClosure(_:K Obj:Bag) :: _, class Class:Id) => objectClosure( Class Obj) :: class Class [anywhere] /*@ \subsection{Class declaration} Like in untyped KOOL. */ rule [ClassDecWithoutExtendsDesugar]: 'ClassDec('ClassDecHead(_:K,, _:Id,, _:K,, ( 'None(_) => 'Some('SuperDec('ClassType('TypeName( String2Id("Object") ),, 'None(.List{K})))) ),, _:K),, _:K ) [structural] rule [ClassDec]: 'ClassDec('ClassDecHead('ListWrap(ClassModifiers:List{K}),, Class:Id,, _:K,, 'Some('SuperDec('ClassType('TypeName( BaseClass:Id ),, _:K))),, _:K),, 'ClassBody( S:K ) ) => classDecImpl(ClassModifiers, Class, BaseClass, //public empty constructor //will be overwritten by any other constructor in S 'ConstrDec( 'ConstrDecHead( 'ListWrap('Public(.List{K})),, 'None(.List{K}),, Class,, 'ListWrap(.List{K}),, 'None(.List{K}) ),, 'ConstrBody('None(.List{K}),, 'ListWrap(.List{K})) ) ~> S ) [structural] syntax K ::= "classDecImpl" "(" List{K} "," Id "," Id "," K ")" rule [classDecImplModPublic]: classDecImpl( ('Public(_) => .List{K}),, _:List{K}, Class:Id, _, _ ) ... . => Class rule [classDecImplModDiscard]: classDecImpl( (K:K => .List{K}),, _:List{K}, _,_,_ ) when K =/=K 'Public(.List{K}) rule [classDecImpl]: classDecImpl(.List{K}, Class:Id, BaseClass:Id, S:K) => . ... ... ( . => Class BaseClass S ) ... /*@ \subsection{New} The semantics of \texttt{new} in dynamically typed KOOL is also similar to that in untyped KOOL, the main difference being the management of the return types. Indeed, when a new object is created we also have to stack the current type in the \textsf{return} cell in order to be recovered after the creation of the new object. Only the first rule below needs to be changed; the others are identical to those in untyped KOOL. */ context 'NewInstance(_:K,,_:K,, 'ListWrap(_,,HOLE,,_),, _) rule [NewInstance]: 'NewInstance( _:K,, 'ClassOrInterfaceType('TypeName( Class:Id ),, _:K),, 'ListWrap( Vals:List{K} ),, _:K ) ~> K:K => create(Class) ~> storeObj ~> 'ExprStm('Invoke( 'Method('MethodName( Class )),, 'ListWrap( Vals ) )) ~> 'Return('Some('This( .List{K} ))) Env:Map => . C:Bag Obj:Bag => String2Id("Object") ListItem((String2Id("object"), .Map)) L T:Type => class Class . => ListItem((return, .K, .K, K, Env, C T Obj )) ... L:Int => L +Int 1 when isKResult(Vals) syntax K ::= "create" "(" K ")" rule [create]: create(Class:Id) => create(BaseClass:Id) ~> setCrntClass(Class) ~> S ~> addEnvLayer ... Class BaseClass S:K [structural] rule [createObjectDiscard]: create( X:Id ) => . ... when Id2String(X) ==String "Object" [structural] syntax K ::= "setCrntClass" "(" Id ")" rule [setCrntClass]: setCrntClass(Class:Id) => . ... _ => Class [structural] syntax K ::= "addEnvLayer" rule [addEnvLayer]: addEnvLayer => . ... Env:Map => . Class:Id . => ListItem((Class, Env)) ... [structural] syntax K ::= "storeObj" rule [storeObj]: storeObj => . ... Obj:Bag Class:Id ( L:Int => .) ... . => L |-> objectClosure(Obj Class ) :: class Class ... /*@ \subsection{Instance Of} Like in untyped KOOL. */ syntax KLabel ::= "'InstanceOf" [seqstrict] rule [InstanceOfTrue]: 'InstanceOf( objectClosure((_ ListItem((Class:Id, _))...))::_,, class Class ) => true :: bool rule [InstanceOfRed]: 'InstanceOf( objectClosure( ( _ ListItem((Class1:Id, _)) => . ... ) )::_,, class Class2:Id ) when Class1 =/=K Class2 [structural] rule [InstanceOfFalse]: 'InstanceOf( objectClosure((_ .List ))::_,, class _:Id ) => false :: bool /*@ \subsection{Cast} Unlike in untyped KOOL, in typed KOOL we actually check that the object can indeed be cast to the claimed type. */ syntax KLabel ::= "'CastRef" [seqstrict] rule [CastRef]: 'CastRef(class Class:Id,, objectClosure(( _:K Obj:Bag))::T:Type ) => 'InstanceOf( objectClosure(Obj)::T,, class Class) ~> true? ~> objectClosure( Class Obj):: class Class ... [structural] //@ \subsection{Names} rule 'AmbName(Ks:List{K}) => 'ExprName(Ks) [structural, anywhere] rule 'Field(Ks:List{K}) => 'ExprName(Ks) [structural, anywhere] rule [ExprNameLocalVar]: 'ExprName(X:Id) => X ... Env:Map when X in keys(Env) [structural] rule [ExprNameMember]: 'ExprName(X:Id) => 'ExprName( 'This(.List{K}),, X ) ... Env:Map when notBool( X in keys(Env) orBool Id2String(X) ==String "System" ) [structural] context 'ExprName(HOLE,,_:K) /*@ \subsection{Self reference} Like in untyped KOOL. */ rule [This]: 'This(.List{K}) => objectClosure(Obj Class ) :: class Class ... Obj:Bag Class:Id /*@ \subsection{Object member access} */ //o.x rule [ExprNameQualified]: 'ExprName(objectClosure( Class:Id ... ListItem((Class, EnvC:BagItem)) EStack:List )::_ ,,X:Id) => lookupMember(ListItem((Class, EnvC)) EStack, X) ... [structural] rule [SuperField]: 'SuperField(X:Id) => lookupMember(EStack, X) ... Class:Id ... ListItem((Class, _)) EStack:List [structural] /*@\subsection{Method invocation} The method lookup is the same as in untyped KOOL. The method closure application and return are slightly different, since we need to check that the type of the returned value is an instance of the claimed return type of the method. The first group of rules below are identical to those in untyped KOOL. */ rule 'Method('MethodName(Ks:List{K})) => 'MethodName(Ks) rule 'Method(K:K,, _:K,, X:Id) => 'MethodName(K,, X) rule [MethodNameUnqualified]: 'MethodName(X:Id) => 'MethodName('This(.List{K}),, X) [structural] context 'MethodName(HOLE,,_:K) //objClos.X(_) => lookupMember(objClos,X)(_) rule [MethodNameQualified]: 'MethodName(objectClosure(_:Bag EStack:List )::_,, X:Id) => lookupMember( EStack , X) ... when Id2String(X) =/=String "getClass" [structural] // super.X(_) => lookupMember(... , )(_) rule [SuperMethod]: 'SuperMethod(_,, X:Id) => lookupMember( EStack , X) ... Class:Id ... ListItem((Class, _)) EStack:List [structural] /*@ Closure application needs to also set a new return type in the \textsf{return} cell, in order for the values returned by its body to be checked against the return type of the method. To do this correctly, we also need to stack the current status of the \textsf{return} cell and then pop it when the method returns. */ context 'Invoke(HOLE,, _) context 'Invoke(_:KResult ,,'ListWrap(_:List{K},,HOLE,,_:List{K})) rule [Invoke]: 'Invoke(methodClosure(Class:Id, OL:Int, 'ListWrap(Params:List{K}), S:K, function from _ to T:Type)::_,, 'ListWrap(ParamValues:List{K})) ~> K:K => bindto Params, ParamValues ~> S ~> 'Return('None(.List{K})) C:Bag . => ListItem((return,.K,.K,K, Env, C Obj2 T2 )) ... Obj2:Bag => Class Obj T2:Type => T Env:Map => . ... OL |-> objectClosure(_:IdObj:Bag) :: _ ... when isKResult(ParamValues) /*@ \subsection{Lookup member} Like in untyped KOOL. */ //typed version of lookup syntax K ::= "typedLookup" "(" Int ")" rule [typedLookup]: typedLookup(L:Int) => lookup(L)::T ... ... L |-> _::T:Type ... rule [lvalueTypedLookup]: lvalue( typedLookup(L:Int) => lookup(L)::T ) ... ... L |-> _::T:Type ... syntax K ::= "(" Id "," Bag ")" //lookup member result is always typed syntax K ::= "lookupMember" "(" BagItem "," Id ")" //todo this definition should fail the test sketched in log.txt rule lookupMember( ListItem((_:Id, ... X:Id |-> L:Int ...)) ... , X ) => typedLookup(L) [anywhere] rule lookupMember( ListItem((_:Id, Env:Map )) => . ... , X:Id ) when notBool(X in keys(Env)) [anywhere] //@ \texttt{lvalue} rule [lvalueExprNameMember]: lvalue( 'ExprName(X:Id) => 'ExprName('This(.List{K}),, X) ) ... Env:Map when notBool(X in keys(Env)) [structural] context lvalue('ExprName(HOLE,, _:Id) ) rule [lvalueExprNameQualified]: lvalue( 'ExprName( objectClosure( Class:Id ... ListItem((Class, EnvC:BagItem)) EStack:List ) :: class Class,, X:Id ) => lookupMember( ListItem((Class, EnvC)) EStack:List , X ) ) ... [structural] rule [lvalueSuperField]: lvalue('SuperField(X:Id) => lookupMember(EStack, X)) ... Class:Id ... ListItem((Class, _)) EStack:List [structural] endmodule