-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjava-classes.k
More file actions
647 lines (545 loc) · 19.2 KB
/
Copy pathjava-classes.k
File metadata and controls
647 lines (545 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
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]:
<k> storeMethod(Name:Id, 'ListWrap(Params:List{K}), S:K, MethodType:Type) =>. ...</k>
<crntClass> Class:Id </crntClass>
<location> OL:Int </location>
<env> Env:Map => Env[L/Name] </env>
<store>
...
. => L |-> methodClosure(Class, OL, 'ListWrap(Params), S, MethodType) :: MethodType
...
</store>
<nextLoc> L:Int => L +Int 1 </nextLoc>
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]:
<k>
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
)
...
</k>
<crntClass> Class:Id </crntClass>
<className> Class </className>
<extends> BaseClass:Id </extends>
when
Id2String(BaseClass) =/=String "Object"
rule [storeConstructorSubclassOfObject]:
<k>
storeConstructor(Name:Id, 'ListWrap(Params:List{K}), _, S:K, MethodType:Type)
=> storeMethod(
Name,
'ListWrap(Params),
S,
MethodType
)
...
</k>
<crntClass> Class:Id </crntClass>
<className> Class </className>
<extends> BaseClass:Id </extends>
when
Id2String(BaseClass) ==String "Object"
/*@ \subsection{Program initialization}
When done with the first pass, call \texttt{main()}. */
rule [CompilationUnit]:
<k>
'CompilationUnit(_,,_,,K:K)
=> K ~> classDecRuntimeException ~> classDecAssertionError ~> execute
</k>
syntax K ::= "execute"
rule [execute]:
<k> execute =>
//k-ast for new <MainClass>().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})))
)
)
</k>
<env> . </env>
<mainClass> MainClass:Id </mainClass>
//@ \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]:
<k>
(. => 'ExprStm('Assign('ExprName(X),,TV:TypedVal)))
~> bindto( ('Param(_,,T:Type,,X:Id) => .List{K}),, _:List{K} ),
( (TV => .List{K}),, _:List{K} )
...
</k>
<env> Env:Map => Env[L/X] </env>
<store>... . => L |-> unruled(T) :: T ...</store>
<nextLoc> L:Int => L +Int 1 </nextLoc>
rule [bindtoEmpty]:
<k> bindto .List{K},.List{K} => . ...</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]:
<k>
subtype class Class1:Id, class Class:Id
=> subtype class Class2, class Class
...
</k>
<className> Class1 </className>
<extends> Class2:Id </extends>
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(<crntClass>_:K </crntClass> Obj:Bag) :: _, class Class:Id)
=> objectClosure(<crntClass> Class </crntClass> 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]:
<k> classDecImpl( ('Public(_) => .List{K}),, _:List{K}, Class:Id, _, _ ) ...</k>
<mainClass> . => Class </mainClass>
rule [classDecImplModDiscard]:
classDecImpl( (K:K => .List{K}),, _:List{K}, _,_,_ )
when
K =/=K 'Public(.List{K})
rule [classDecImpl]:
<k> classDecImpl(.List{K}, Class:Id, BaseClass:Id, S:K) => . ...</k>
<classes>
...
( . => <class>
<className> Class </className>
<extends> BaseClass </extends>
<declarations> S </declarations>
</class> )
...
</classes>
/*@ \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]:
<k>
'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} )))
</k>
<env> Env:Map => . </env>
<control>
C:Bag
<crntObj>
Obj:Bag
=> <crntClass> String2Id("Object") </crntClass>
<envStack> ListItem((String2Id("object"), <env>.Map</env>)) </envStack>
<location> L </location>
</crntObj>
<return> T:Type => class Class </return>
<stack>
. => ListItem((return, .K, .K, K, Env,
C <return> T </return> <crntObj> Obj </crntObj>))
...
</stack>
</control>
<nextLoc> L:Int => L +Int 1 </nextLoc>
when isKResult(Vals)
syntax K ::= "create" "(" K ")"
rule [create]:
<k>
create(Class:Id)
=> create(BaseClass:Id) ~> setCrntClass(Class) ~> S ~> addEnvLayer
...
</k>
<className> Class </className>
<extends> BaseClass </extends>
<declarations> S:K </declarations>
[structural]
rule [createObjectDiscard]:
<k> create( X:Id ) => . ...</k>
when
Id2String(X) ==String "Object"
[structural]
syntax K ::= "setCrntClass" "(" Id ")"
rule [setCrntClass]:
<k> setCrntClass(Class:Id) => . ...</k>
<crntClass> _ => Class </crntClass>
[structural]
syntax K ::= "addEnvLayer"
rule [addEnvLayer]:
<k> addEnvLayer => . ...</k>
<env> Env:Map => . </env>
<crntClass> Class:Id </crntClass>
<envStack> . => ListItem((Class, <env>Env</env>)) ...</envStack>
[structural]
syntax K ::= "storeObj"
rule [storeObj]:
<k> storeObj => . ...</k>
<crntObj>
Obj:Bag
<crntClass> Class:Id </crntClass>
(<location> L:Int </location> => .)
</crntObj>
<store>
...
. => L |-> objectClosure(Obj <crntClass> Class </crntClass>) :: class Class
...
</store>
/*@ \subsection{Instance Of}
Like in untyped KOOL. */
syntax KLabel ::= "'InstanceOf" [seqstrict]
rule [InstanceOfTrue]:
'InstanceOf(
objectClosure((_ <envStack>ListItem((Class:Id, _))...</envStack>))::_,,
class Class
) => true :: bool
rule [InstanceOfRed]:
'InstanceOf(
objectClosure(
( _ <envStack> ListItem((Class1:Id, _)) => . ...</envStack> )
)::_,,
class Class2:Id
)
when Class1 =/=K Class2 [structural]
rule [InstanceOfFalse]:
'InstanceOf(
objectClosure((_ <envStack> .List </envStack>))::_,,
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]:
<k>
'CastRef(class Class:Id,, objectClosure((<crntClass> _:K </crntClass> Obj:Bag))::T:Type )
=> 'InstanceOf( objectClosure(Obj)::T,, class Class) ~> true?
~> objectClosure(<crntClass> Class </crntClass> Obj):: class Class
...
</k>
[structural]
//@ \subsection{Names}
rule 'AmbName(Ks:List{K}) => 'ExprName(Ks) [structural, anywhere]
rule 'Field(Ks:List{K}) => 'ExprName(Ks) [structural, anywhere]
rule [ExprNameLocalVar]:
<k> 'ExprName(X:Id) => X ...</k>
<env> Env:Map </env>
when X in keys(Env)
[structural]
rule [ExprNameMember]:
<k>
'ExprName(X:Id) => 'ExprName( 'This(.List{K}),, X )
...
</k>
<env> Env:Map </env>
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]:
<k>
'This(.List{K})
=> objectClosure(Obj <crntClass> Class </crntClass>) :: class Class
...
</k>
<crntObj> Obj:Bag <crntClass> Class:Id </crntClass> </crntObj>
/*@ \subsection{Object member access} */
//o.x
rule [ExprNameQualified]:
<k>
'ExprName(objectClosure(<crntClass> Class:Id </crntClass>
<envStack>... ListItem((Class, EnvC:BagItem)) EStack:List </envStack>)::_ ,,X:Id)
=> lookupMember(<envStack>ListItem((Class, EnvC)) EStack</envStack>, X)
...
</k>
[structural]
rule [SuperField]:
<k>'SuperField(X:Id) => lookupMember(<envStack>EStack</envStack>, X) ...</k>
<crntClass> Class:Id </crntClass>
<envStack>... ListItem((Class, _)) EStack:List </envStack>
[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]:
<k>
'MethodName(objectClosure(_:Bag <envStack> EStack:List </envStack>)::_,, X:Id)
=> lookupMember(<envStack> EStack </envStack>, X)
...
</k>
when
Id2String(X) =/=String "getClass"
[structural]
// super.X(_) => lookupMember(... , )(_)
rule [SuperMethod]:
<k>
'SuperMethod(_,, X:Id) => lookupMember(<envStack> EStack </envStack>, X)
...
</k>
<crntClass> Class:Id </crntClass>
<envStack>... ListItem((Class, _)) EStack:List </envStack>
[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]:
<k>
'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}))
</k>
<control> C:Bag
<stack>
. => ListItem((return,.K,.K,K, Env,
C <crntObj> Obj2 </crntObj>
<return> T2 </return>))
...
</stack>
<crntObj> Obj2:Bag => <crntClass>Class</crntClass> Obj </crntObj>
<return> T2:Type => T </return>
</control>
<env> Env:Map => . </env>
<store>
...
OL |-> objectClosure(<crntClass>_:Id</crntClass>Obj:Bag) :: _
...
</store>
when isKResult(ParamValues)
/*@ \subsection{Lookup member}
Like in untyped KOOL. */
//typed version of lookup
syntax K ::= "typedLookup" "(" Int ")"
rule [typedLookup]:
<k> typedLookup(L:Int) => lookup(L)::T ...</k>
<store>... L |-> _::T:Type ...</store>
rule [lvalueTypedLookup]:
<k> lvalue( typedLookup(L:Int) => lookup(L)::T ) ...</k>
<store>... L |-> _::T:Type ...</store>
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(
<envStack>
ListItem((_:Id, <env>... X:Id |-> L:Int ...</env>))
...
</envStack>,
X
) => typedLookup(L) [anywhere]
rule lookupMember(
<envStack>
ListItem((_:Id, <env> Env:Map </env>)) => .
...
</envStack>,
X:Id
)
when notBool(X in keys(Env)) [anywhere]
//@ \texttt{lvalue}
rule [lvalueExprNameMember]:
<k> lvalue( 'ExprName(X:Id) => 'ExprName('This(.List{K}),, X) ) ...</k>
<env> Env:Map </env>
when notBool(X in keys(Env)) [structural]
context lvalue('ExprName(HOLE,, _:Id) )
rule [lvalueExprNameQualified]:
<k>
lvalue(
'ExprName(
objectClosure(
<crntClass> Class:Id </crntClass>
<envStack>... ListItem((Class, EnvC:BagItem)) EStack:List </envStack>
) :: class Class,,
X:Id
)
=> lookupMember(
<envStack> ListItem((Class, EnvC)) EStack:List </envStack>,
X
)
)
...
</k>
[structural]
rule [lvalueSuperField]:
<k> lvalue('SuperField(X:Id) => lookupMember(<envStack>EStack</envStack>, X)) ...</k>
<crntClass> Class:Id </crntClass>
<envStack>... ListItem((Class, _)) EStack:List </envStack>
[structural]
endmodule