test case for the ownership checking
[IRC.git] / Robust / src / Tests / ssJava / ownership / test.java
1 @LATTICE("L<M,M<H,H<C,C*,M*")
2 @METHODDEFAULT("L<M,M<H,H<IN,THISLOC=M,RETURNLOC=M")
3 public class test {
4
5   @LOC("H")
6   Foo foo;
7   @LOC("H")
8   Foo f1;
9
10   public static void main(@LOC("H") String args[]) {
11     @LOC("H") test t = new test();
12
13     SSJAVA: while (true) {
14       t.doDelegationTest();
15       t.doDelegationTest2();
16     }
17
18   }
19
20   public void doDelegationTest() {
21
22     @LOC("H") Foo ownedFoo = new Foo();
23     delegate(ownedFoo);
24     // ERROR::
25     // System.out.println(ownedFoo.v);
26
27     changeLocationTest(ownedFoo);
28   }
29
30   public void delegate(@DELEGATE @LOC("IN") Foo foo) {
31     @LOC("M") Foo lowerFoo = foo; // allowed to lower
32
33     // create a temp var to walk the subtree
34     @LOC("L") Bar tempVar = lowerFoo.bar1;
35     lowerFoo.bar1 = null;
36
37   }
38
39   public void changeLocationTest(@DELEGATE @LOC("IN") Foo foo) {
40
41     // local variable refers to the sub object the foo
42     @LOC("M") Bar tempVar = foo.bar1;
43
44     // ERROR: not allowed to lower if there is a local var alias to the subtree
45     // of the foo
46     // @LOC("M") Foo lowerFoo = foo;
47
48   }
49
50   public void doDelegationTest2() {
51
52     @LOC("H") Foo ownedFoo = new Foo();
53     notDelegate(ownedFoo);
54
55   }
56
57   public Foo notDelegate(@LOC("IN") Foo notownedFoo) {
58
59     // ERROR: not allowed to lower
60     // @LOC("M") Foo lowerFoo = notownedFoo;
61
62     // ERROR: now allowed to return not-owned-ref
63     return notownedFoo;
64
65   }
66
67 }
68
69 @LATTICE("L<M,M<H,H<C,C*,M*")
70 @METHODDEFAULT("T")
71 class Foo {
72   @LOC("H")
73   Bar bar1;
74   @LOC("H")
75   Bar bar2;
76   @LOC("H")
77   int v;
78 }
79
80 @LATTICE("L<M,M<H,H<C,C*,M*")
81 @METHODDEFAULT("T")
82 class Bar {
83   @LOC("M")
84   int v1;
85   @LOC("M")
86   int v2;
87 }