update for methods and renaming of assignments for clarity bug fix
[IRC.git] / Robust / src / Tests / OwnershipAnalysisTest / test01 / test01.java
1 /*
2 public class Parameter {
3   flag w;
4   int a, b;
5   Parameter f, g;
6   Penguin p;
7   Foo h;
8   
9   public Parameter() { a = 0; b = 0; f = null; g = null; }
10   
11   public void bar() { foo(); }
12   public void foo() { bar(); }
13 }
14
15 public class Penguin {
16   int x, y;
17   Foo h;    
18   
19   public Penguin() { x = 0; y = 0; }
20   
21   public void bar() { x = 1; }
22 }
23
24 public class Voo {
25   flag f; int x; Baw b; Baw bb;
26   
27   public Voo() {}
28   
29   static Voo getAVoo( Voo bogus ) {
30     while( false ) {
31       if( false ) {
32         return new Voo();
33       } else {
34         return new Voo();
35       }
36     }
37     return new Voo();
38   }
39 }
40
41 public class Baw {
42   int y;
43   Foo f;
44   
45   public Baw() {}
46   
47   public void doTheBaw( Voo v ) { v = new Voo(); }
48 }
49 */
50
51
52 task ObjectChainByMethodCalls( Foo a{ f } ) {
53
54   Foo f = a.getAFoo();
55   Foo g = a.newFooChain( f );
56   // g -> f
57
58   taskexit( a{ !f } );
59 }
60
61
62 public class Foo {
63   flag f;
64   
65   public Foo() {}
66   
67   public Foo x;
68   public Foo y;
69   public Foo z;
70
71
72   public Foo getAFoo() {
73     return new Foo();
74   }
75
76   public Foo newFooChain( Foo a ) {
77     Foo b = new Foo();
78     b.x = a;
79     return b;
80   }
81
82   
83   /*
84   public void ruinSomeFoos( Foo a, Foo b ) {
85     a.x = b.x;
86   }
87   
88   static public void aStaticMethod( Foo p0, Foo p1 ) {
89     Foo f0 = new Foo();
90     Foo f1 = new Foo();
91     Foo f2 = new Foo();
92     
93     f0.x = f1;
94     p0.x = f0;
95     p1.x = f1;
96     p1.x = f2;
97   }
98   
99   static public void m1_( Foo p0 ) {
100     Foo g0 = new Foo();
101     Foo g1 = new Foo();
102     
103     g0.x = p0;
104     p0.x = g1;
105   }
106   
107   static public void m2_( Foo p0 ) {
108     Foo g0 = new Foo();
109     
110     g0.x = p0;
111     g0.y = p0;
112   }
113   
114   static public void m3_( Foo p0 ) {
115     Foo g0 = new Foo();
116     
117     p0.x = g0;
118     p0.y = g0;
119   }
120
121   static public void m4_( Foo p0 ) {
122     p0.x = p0;
123   }
124
125   static public void m5_( Foo p0 ) {
126     Foo g0 = new Foo();
127     p0.x = g0;
128     g0.x = p0;
129   }
130
131   static public void m6_( Foo p0, Foo p1 ) {
132     Foo g0 = new Foo();
133     Foo g1 = new Foo();
134     Foo g2 = new Foo();
135     Foo g3 = new Foo();
136
137     g0.x = p0;
138     p0.x = g1;
139
140     g2.x = p1;
141     p1.x = g3;
142
143     p0.y = p1;
144   }
145
146   static public void m7_( Foo p0, Foo p1 ) {
147     Foo g0 = new Foo();
148     Foo g1 = new Foo();
149     Foo g2 = new Foo();
150     Foo g3 = new Foo();
151
152     g0.x = p0;
153     p0.x = g1;
154
155     g2.x = p1;
156     p1.x = g3;
157
158     p0.y = p1;
159     p0.z = p1;
160   }
161
162   static public void m8_( Foo p0, Foo p1 ) {
163     Foo g0 = new Foo();
164     Foo g1 = new Foo();
165     Foo g2 = new Foo();
166     Foo g3 = new Foo();
167
168     g0.x = p0;
169     p0.x = g1;
170
171     g2.x = p1;
172     p1.x = g3;
173
174     p0.y = p1;
175     p1.y = p0;
176   }
177   */
178 }
179
180
181 // this empty task should still create a non-empty
182 // ownership graph showing parameter allocation
183 // look for the parameter s as a label referencing
184 // a heap region that is multi-object, flagged, not summary
185 task Startup( StartupObject s{ initialstate } ) {
186     taskexit( s{ !initialstate } );
187 }
188
189 /*
190 task NewObjectA( Foo a{ f }, Foo b{ f } ) {
191
192     Foo c = new Foo();
193     Foo d = new Foo();
194
195     c.x = d;
196     a.x = c;
197
198     taskexit( a{ !f }, b{ !f } );
199 }
200
201
202 task NewObjectB( Foo a{ f }, Foo b{ f } ) {
203
204     Foo c = new Foo();
205     Foo d = new Foo();
206
207     a.x = c;
208     c.x = d;
209
210     taskexit( a{ !f }, b{ !f } );
211 }
212
213
214 task NewObjectC( Foo a{ f }, Foo b{ f } ) {
215
216     Foo z = new Foo();
217     a.x = z;
218     a.y = z;
219
220     Foo c;
221
222     while( false ) {
223         c     = new Foo();
224         Foo f = new Foo();
225         c.x   = f;
226         c.y   = f;
227     }
228
229     taskexit( a{ !f }, b{ !f } );
230 }
231
232
233 task forMethod( Foo p0{ f } ) {
234
235     Foo a0;
236     Foo a1;
237
238     while( false ) {    
239         a1 = a0;            
240         if( false ) {
241             a0 = new Foo();
242         }
243     }
244
245     Foo z = new Foo();
246     a1.x = z;
247     z.x  = a1;
248
249
250     taskexit( p0{ !f } );
251 }
252
253
254 // this task allocates a new object, so there should
255 // be a heap region for the parameter, and several
256 // heap regions for the allocation site, but the label
257 // merely points to the newest region
258 task NewObjectInMethod( Voo v{ f } ) {
259     Voo w = new Voo();
260     Baw b = new Baw();
261     b.doTheBaw( w );
262
263     taskexit( v{ !f } );
264 }
265
266
267 task ClobberInitParamReflex( Voo v{ f }, Voo w{ f } ) {
268     v.b = v.bb;
269
270     taskexit( v{ !f }, w{ !f } );
271 }
272
273
274 task BackToItself( Parameter p0{ w } ) {
275
276     Penguin p = new Penguin();
277     p0.p = p;
278     p.h = p0.h;
279
280     while( false ) {
281         Parameter p1   = new Parameter();
282                   p1.h = new Foo();
283         Penguin   q    = new Penguin();
284         p1.p = q;
285         q.h  = p1.h;
286     }
287
288     taskexit( p0{ !w } );
289 }
290
291
292 task SummaryNodeTokens( Foo p0{ f } ) {
293
294     while( false ) {
295         Foo a = new Foo();
296         a.x   = new Foo();
297         a.x.x = new Foo();
298     }
299     
300     Foo b;
301     while( false ) {
302         Foo c = new Foo();
303         c.x = b;
304         b = c;
305     }
306
307     taskexit( p0{ !f } );
308 }
309
310
311 task strongUpdates( Foo p0{ f } ) {
312
313     Foo b = new Foo();
314
315     Foo a = new Foo();
316     if( false ) {
317         a.x = new Foo();
318         a.y = new Foo();
319     } else if( false ) {
320         a.x = new Foo();
321         a.y = new Foo();
322     }
323
324     // this should effect a strong update
325     a.x = b;
326
327
328     if( false ) {
329         p0.x = new Foo();
330         p0.y = new Foo();
331     } else if( false ) {
332         p0.x = new Foo();
333         p0.y = new Foo();
334     }
335
336     // p0 points to a multiple-object heap region
337     // so this should not make a strong update
338     p0.x = b;
339     
340     taskexit( p0{ !f } );
341 }
342
343
344 task methodTest( Foo p0{ f } ) {
345
346     Foo up0 = new Foo();
347     Foo up1 = new Foo();
348     Foo up2 = new Foo();
349
350     Foo a0;
351     Foo a1;
352
353     if( false ) {
354         a0    = new Foo();
355         up0.x = a0;     
356         a0.x  = new Foo();
357         //Foo temp = new Foo();
358     }
359
360     if( false ) {
361         a0    = new Foo();
362         a0.x  = new Foo();
363         a1    = a0;
364         up1.x = a0;
365     }
366
367     if( false ) {
368         a1    = new Foo();
369         up2.x = a1;
370     }
371
372     // Foo.test( a0, a1 );
373
374     taskexit( p0{ !f } );
375 }
376
377
378 task getNewFromMethod( Foo p0{ f } ) { 
379   Voo g = Voo.getAVoo( null );
380
381   taskexit( p0{ !f } );
382 }
383
384
385 task methodTest01_( Foo p0{ f }, Foo p1{ f } ) {
386
387   Foo a0before = new Foo();
388   if( false ) {
389     a0before.x = new Foo();
390   } else {
391     a0before.x = new Foo();
392   }
393
394   Foo a0after = new Foo();
395   if( false ) {
396     a0after.x = new Foo();
397   } else {
398     a0after.x = new Foo();
399   }
400
401   Foo.m1_( a0after );
402
403
404   taskexit( p0{ !f }, p1{ !f } );
405 }
406
407
408 task methodTest02_( Foo p0{ f }, Foo p1{ f } ) {
409
410   Foo a0before = new Foo();
411   if( false ) {
412     a0before.x = new Foo();
413   } else {
414     a0before.x = new Foo();
415   }
416
417   Foo a0after = new Foo();
418   if( false ) {
419     a0after.x = new Foo();
420   } else {
421     a0after.x = new Foo();
422   }
423
424   Foo.m2_( a0after );
425
426
427   taskexit( p0{ !f }, p1{ !f } );
428 }
429
430
431 task methodTest03_( Foo p0{ f }, Foo p1{ f } ) {
432
433   Foo a0before = new Foo();
434   if( false ) {
435     a0before.x = new Foo();
436   } else {
437     a0before.x = new Foo();
438   }
439
440   Foo a0after = new Foo();
441   if( false ) {
442     a0after.x = new Foo();
443   } else {
444     a0after.x = new Foo();
445   }
446
447   Foo.m3_( a0after );
448
449
450   taskexit( p0{ !f }, p1{ !f } );
451 }
452
453
454 task methodTest04_( Foo p0{ f }, Foo p1{ f } ) {
455
456   Foo a0before = new Foo();
457   if( false ) {
458     a0before.x = new Foo();
459   } else {
460     a0before.x = new Foo();
461   }
462
463   Foo a0after = new Foo();
464   if( false ) {
465     a0after.x = new Foo();
466   } else {
467     a0after.x = new Foo();
468   }
469
470   Foo.m4_( a0after );
471
472
473   taskexit( p0{ !f }, p1{ !f } );
474 }
475
476
477 task methodTest05_( Foo p0{ f }, Foo p1{ f } ) {
478
479   Foo a0before = new Foo();
480   if( false ) {
481     a0before.x = new Foo();
482   } else {
483     a0before.x = new Foo();
484   }
485
486   Foo a0after = new Foo();
487   if( false ) {
488     a0after.x = new Foo();
489   } else {
490     a0after.x = new Foo();
491   }
492
493   Foo.m5_( a0after );
494
495
496   taskexit( p0{ !f }, p1{ !f } );
497 }
498
499
500 task methodTest06_( Foo p0{ f }, Foo p1{ f } ) {
501
502   Foo a0before = new Foo();
503   if( false ) {
504     a0before.x = new Foo();
505   } else {
506     a0before.x = new Foo();
507   }
508
509   Foo a0after = new Foo();
510   if( false ) {
511     a0after.x = new Foo();
512   } else {
513     a0after.x = new Foo();
514   }
515
516   Foo a1before = new Foo();
517   if( false ) {
518     a1before.x = new Foo();
519   } else {
520     a1before.x = new Foo();
521   }
522
523   Foo a1after = new Foo();
524   if( false ) {
525     a1after.x = new Foo();
526   } else {
527     a1after.x = new Foo();
528   }
529
530   Foo.m6_( a0after, a1after );
531
532
533   taskexit( p0{ !f }, p1{ !f } );
534 }
535
536
537 task methodTest07_( Foo p0{ f }, Foo p1{ f } ) {
538
539   Foo a0before = new Foo();
540   if( false ) {
541     a0before.x = new Foo();
542   } else {
543     a0before.x = new Foo();
544   }
545
546   Foo a0after = new Foo();
547   if( false ) {
548     a0after.x = new Foo();
549   } else {
550     a0after.x = new Foo();
551   }
552
553   Foo a1before = new Foo();
554   if( false ) {
555     a1before.x = new Foo();
556   } else {
557     a1before.x = new Foo();
558   }
559
560   Foo a1after = new Foo();
561   if( false ) {
562     a1after.x = new Foo();
563   } else {
564     a1after.x = new Foo();
565   }
566
567   Foo.m7_( a0after, a1after );
568
569
570   taskexit( p0{ !f }, p1{ !f } );
571 }
572
573
574 task methodTest08_( Foo p0{ f }, Foo p1{ f } ) {
575
576   Foo a0before = new Foo();
577   if( false ) {
578     a0before.x = new Foo();
579   } else {
580     a0before.x = new Foo();
581   }
582
583   Foo a0after = new Foo();
584   if( false ) {
585     a0after.x = new Foo();
586   } else {
587     a0after.x = new Foo();
588   }
589
590   Foo a1before = new Foo();
591   if( false ) {
592     a1before.x = new Foo();
593   } else {
594     a1before.x = new Foo();
595   }
596
597   Foo a1after = new Foo();
598   if( false ) {
599     a1after.x = new Foo();
600   } else {
601     a1after.x = new Foo();
602   }
603
604   Foo.m8_( a0after, a1after );
605
606
607   taskexit( p0{ !f }, p1{ !f } );
608 }
609 */