93c0c3ebb1b4bcf707e071afd7650c84f86516a0
[mono.git] / mcs / mcs / flowanalysis.cs
1 //
2 // flowanalyis.cs: The control flow analysis code
3 //
4 // Author:
5 //   Martin Baulig (martin@ximian.com)
6 //   Raja R Harinath (rharinath@novell.com)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 //
11
12 using System;
13 using System.Text;
14 using System.Collections.Generic;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.Diagnostics;
18
19 namespace Mono.CSharp
20 {
21         // <summary>
22         //   A new instance of this class is created every time a new block is resolved
23         //   and if there's branching in the block's control flow.
24         // </summary>
25         public abstract class FlowBranching
26         {
27                 // <summary>
28                 //   The type of a FlowBranching.
29                 // </summary>
30                 public enum BranchingType : byte {
31                         // Normal (conditional or toplevel) block.
32                         Block,
33
34                         // Conditional.
35                         Conditional,
36
37                         // A loop block.
38                         Loop,
39
40                         // The statement embedded inside a loop
41                         Embedded,
42
43                         // part of a block headed by a jump target
44                         Labeled,
45
46                         // TryCatch block.
47                         TryCatch,
48
49                         // TryFinally, Using, Lock, CollectionForeach
50                         Exception,
51
52                         // Switch block.
53                         Switch,
54
55                         // The toplevel block of a function
56                         Toplevel,
57
58                         // An iterator block
59                         Iterator
60                 }
61
62                 // <summary>
63                 //   The type of one sibling of a branching.
64                 // </summary>
65                 public enum SiblingType : byte {
66                         Block,
67                         Conditional,
68                         SwitchSection,
69                         Try,
70                         Catch,
71                         Finally
72                 }
73
74                 public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
75                 {
76                         switch (type) {
77                         case BranchingType.Exception:
78                         case BranchingType.Labeled:
79                         case BranchingType.Toplevel:
80                         case BranchingType.TryCatch:
81                                 throw new InvalidOperationException ();
82
83                         case BranchingType.Switch:
84                                 return new FlowBranchingBreakable (parent, type, SiblingType.SwitchSection, block, loc);
85
86                         case BranchingType.Block:
87                                 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
88
89                         case BranchingType.Loop:
90                                 return new FlowBranchingBreakable (parent, type, SiblingType.Conditional, block, loc);
91
92                         case BranchingType.Embedded:
93                                 return new FlowBranchingContinuable (parent, type, SiblingType.Conditional, block, loc);
94
95                         default:
96                                 return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
97                         }
98                 }
99
100                 // <summary>
101                 //   The type of this flow branching.
102                 // </summary>
103                 public readonly BranchingType Type;
104
105                 // <summary>
106                 //   The block this branching is contained in.  This may be null if it's not
107                 //   a top-level block and it doesn't declare any local variables.
108                 // </summary>
109                 public readonly Block Block;
110
111                 // <summary>
112                 //   The parent of this branching or null if this is the top-block.
113                 // </summary>
114                 public readonly FlowBranching Parent;
115
116                 // <summary>
117                 //   Start-Location of this flow branching.
118                 // </summary>
119                 public readonly Location Location;
120
121                 static int next_id = 0;
122                 int id;
123
124                 // <summary>
125                 //   The vector contains a BitArray with information about which local variables
126                 //   and parameters are already initialized at the current code position.
127                 // </summary>
128                 public class UsageVector {
129                         // <summary>
130                         //   The type of this branching.
131                         // </summary>
132                         public readonly SiblingType Type;
133
134                         // <summary>
135                         //   Start location of this branching.
136                         // </summary>
137                         public Location Location;
138
139                         // <summary>
140                         //   This is only valid for SwitchSection, Try, Catch and Finally.
141                         // </summary>
142                         public readonly Block Block;
143
144                         // <summary>
145                         //   The number of locals in this block.
146                         // </summary>
147                         public readonly int CountLocals;
148
149                         // <summary>
150                         //   If not null, then we inherit our state from this vector and do a
151                         //   copy-on-write.  If null, then we're the first sibling in a top-level
152                         //   block and inherit from the empty vector.
153                         // </summary>
154                         public readonly UsageVector InheritsFrom;
155
156                         // <summary>
157                         //   This is used to construct a list of UsageVector's.
158                         // </summary>
159                         public UsageVector Next;
160
161                         //
162                         // Private.
163                         //
164                         MyBitVector locals;
165                         bool is_unreachable;
166
167                         static int next_id = 0;
168                         int id;
169
170                         //
171                         // Normally, you should not use any of these constructors.
172                         //
173                         public UsageVector (SiblingType type, UsageVector parent, Block block, Location loc, int num_locals)
174                         {
175                                 this.Type = type;
176                                 this.Block = block;
177                                 this.Location = loc;
178                                 this.InheritsFrom = parent;
179                                 this.CountLocals = num_locals;
180
181                                 locals = num_locals == 0 
182                                         ? MyBitVector.Empty
183                                         : new MyBitVector (parent == null ? MyBitVector.Empty : parent.locals, num_locals);
184
185                                 if (parent != null)
186                                         is_unreachable = parent.is_unreachable;
187
188                                 id = ++next_id;
189
190                         }
191
192                         public UsageVector (SiblingType type, UsageVector parent, Block block, Location loc)
193                                 : this (type, parent, block, loc, parent.CountLocals)
194                         { }
195
196                         private UsageVector (MyBitVector locals, bool is_unreachable, Block block, Location loc)
197                         {
198                                 this.Type = SiblingType.Block;
199                                 this.Location = loc;
200                                 this.Block = block;
201
202                                 this.is_unreachable = is_unreachable;
203
204                                 this.locals = locals;
205
206                                 id = ++next_id;
207
208                         }
209
210                         // <summary>
211                         //   This does a deep copy of the usage vector.
212                         // </summary>
213                         public UsageVector Clone ()
214                         {
215                                 UsageVector retval = new UsageVector (Type, null, Block, Location, CountLocals);
216
217                                 retval.locals = locals.Clone ();
218                                 retval.is_unreachable = is_unreachable;
219
220                                 return retval;
221                         }
222
223                         public bool IsAssigned (VariableInfo var, bool ignoreReachability)
224                         {
225                                 if (!ignoreReachability && !var.IsParameter && IsUnreachable)
226                                         return true;
227
228                                 return var.IsAssigned (locals);
229                         }
230
231                         public void SetAssigned (VariableInfo var)
232                         {
233                                 if (!var.IsParameter && IsUnreachable)
234                                         return;
235
236                                 var.SetAssigned (locals);
237                         }
238
239                         public bool IsFieldAssigned (VariableInfo var, string name)
240                         {
241                                 if (!var.IsParameter && IsUnreachable)
242                                         return true;
243
244                                 return var.IsFieldAssigned (locals, name);
245                         }
246
247                         public void SetFieldAssigned (VariableInfo var, string name)
248                         {
249                                 if (!var.IsParameter && IsUnreachable)
250                                         return;
251
252                                 var.SetFieldAssigned (locals, name);
253                         }
254
255                         public bool IsUnreachable {
256                                 get { return is_unreachable; }
257                         }
258
259                         public void ResetBarrier ()
260                         {
261                                 is_unreachable = false;
262                         }
263
264                         public void Goto ()
265                         {
266                                 is_unreachable = true;
267                         }
268
269                         public static UsageVector MergeSiblings (UsageVector sibling_list, Location loc)
270                         {
271                                 if (sibling_list.Next == null)
272                                         return sibling_list;
273
274                                 MyBitVector locals = null;
275                                 bool is_unreachable = sibling_list.is_unreachable;
276
277                                 if (!sibling_list.IsUnreachable)
278                                         locals &= sibling_list.locals;
279
280                                 for (UsageVector child = sibling_list.Next; child != null; child = child.Next) {
281                                         is_unreachable &= child.is_unreachable;
282
283                                         if (!child.IsUnreachable)
284                                                 locals &= child.locals;
285                                 }
286
287                                 return new UsageVector (locals, is_unreachable, null, loc);
288                         }
289
290                         // <summary>
291                         //   Merges a child branching.
292                         // </summary>
293                         public UsageVector MergeChild (UsageVector child, bool overwrite)
294                         {
295                                 Report.Debug (2, "    MERGING CHILD EFFECTS", this, child, Type);
296
297                                 bool new_isunr = child.is_unreachable;
298
299                                 //
300                                 // We've now either reached the point after the branching or we will
301                                 // never get there since we always return or always throw an exception.
302                                 //
303                                 // If we can reach the point after the branching, mark all locals and
304                                 // parameters as initialized which have been initialized in all branches
305                                 // we need to look at (see above).
306                                 //
307
308                                 if ((Type == SiblingType.SwitchSection) && !new_isunr) {
309                                         Report.Error (163, Location,
310                                                       "Control cannot fall through from one " +
311                                                       "case label to another");
312                                         return child;
313                                 }
314
315                                 locals |= child.locals;
316
317                                 // throw away un-necessary information about variables in child blocks
318                                 if (locals.Count != CountLocals)
319                                         locals = new MyBitVector (locals, CountLocals);
320
321                                 if (overwrite)
322                                         is_unreachable = new_isunr;
323                                 else
324                                         is_unreachable |= new_isunr;
325
326                                 return child;
327                         }
328
329                         public void MergeOrigins (UsageVector o_vectors)
330                         {
331                                 Report.Debug (1, "  MERGING BREAK ORIGINS", this);
332
333                                 if (o_vectors == null)
334                                         return;
335
336                                 if (IsUnreachable && locals != null)
337                                         locals.SetAll (true);
338
339                                 for (UsageVector vector = o_vectors; vector != null; vector = vector.Next) {
340                                         Report.Debug (1, "    MERGING BREAK ORIGIN", vector);
341                                         if (vector.IsUnreachable)
342                                                 continue;
343                                         locals &= vector.locals;
344                                         is_unreachable &= vector.is_unreachable;
345                                 }
346
347                                 Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
348                         }
349
350                         //
351                         // Debugging stuff.
352                         //
353
354                         public override string ToString ()
355                         {
356                                 return String.Format ("Vector ({0},{1},{2}-{3})", Type, id, is_unreachable, locals);
357                         }
358                 }
359
360                 // <summary>
361                 //   Creates a new flow branching which is contained in `parent'.
362                 //   You should only pass non-null for the `block' argument if this block
363                 //   introduces any new variables - in this case, we need to create a new
364                 //   usage vector with a different size than our parent's one.
365                 // </summary>
366                 protected FlowBranching (FlowBranching parent, BranchingType type, SiblingType stype,
367                                          Block block, Location loc)
368                 {
369                         Parent = parent;
370                         Block = block;
371                         Location = loc;
372                         Type = type;
373                         id = ++next_id;
374
375                         UsageVector vector;
376                         if (Block != null) {
377                                 UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
378                                 vector = new UsageVector (stype, parent_vector, Block, loc, Block.AssignableSlots);
379                         } else {
380                                 vector = new UsageVector (stype, Parent.CurrentUsageVector, null, loc);
381                         }
382
383                         AddSibling (vector);
384                 }
385
386                 public abstract UsageVector CurrentUsageVector {
387                         get;
388                 }                               
389
390                 // <summary>
391                 //   Creates a sibling of the current usage vector.
392                 // </summary>
393                 public virtual void CreateSibling (Block block, SiblingType type)
394                 {
395                         UsageVector vector = new UsageVector (
396                                 type, Parent.CurrentUsageVector, block, Location);
397                         AddSibling (vector);
398
399                         Report.Debug (1, "  CREATED SIBLING", CurrentUsageVector);
400                 }
401
402                 public void CreateSibling ()
403                 {
404                         CreateSibling (null, SiblingType.Conditional);
405                 }
406
407                 protected abstract void AddSibling (UsageVector uv);
408
409                 protected abstract UsageVector Merge ();
410
411                 public UsageVector MergeChild (FlowBranching child)
412                 {
413                         return CurrentUsageVector.MergeChild (child.Merge (), true);
414                 }
415
416                 public virtual bool CheckRethrow (Location loc)
417                 {
418                         return Parent.CheckRethrow (loc);
419                 }
420
421                 public virtual bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
422                 {
423                         return Parent.AddResumePoint (stmt, loc, out pc);
424                 }
425
426                 // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
427                 public virtual bool AddBreakOrigin (UsageVector vector, Location loc)
428                 {
429                         return Parent.AddBreakOrigin (vector, loc);
430                 }
431
432                 // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
433                 public virtual bool AddContinueOrigin (UsageVector vector, Location loc)
434                 {
435                         return Parent.AddContinueOrigin (vector, loc);
436                 }
437
438                 // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
439                 public virtual bool AddReturnOrigin (UsageVector vector, ExitStatement stmt)
440                 {
441                         return Parent.AddReturnOrigin (vector, stmt);
442                 }
443
444                 // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
445                 public virtual bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
446                 {
447                         return Parent.AddGotoOrigin (vector, goto_stmt);
448                 }
449
450                 public bool IsAssigned (VariableInfo vi)
451                 {
452                         return CurrentUsageVector.IsAssigned (vi, false);
453                 }
454
455                 public bool IsFieldAssigned (VariableInfo vi, string field_name)
456                 {
457                         return CurrentUsageVector.IsAssigned (vi, false) || CurrentUsageVector.IsFieldAssigned (vi, field_name);
458                 }
459
460                 protected static Report Report {
461                         get { return RootContext.ToplevelTypes.Compiler.Report; }
462                 }
463
464                 public void SetAssigned (VariableInfo vi)
465                 {
466                         CurrentUsageVector.SetAssigned (vi);
467                 }
468
469                 public void SetFieldAssigned (VariableInfo vi, string name)
470                 {
471                         CurrentUsageVector.SetFieldAssigned (vi, name);
472                 }
473
474 #if DEBUG
475                 public override string ToString ()
476                 {
477                         StringBuilder sb = new StringBuilder ();
478                         sb.Append (GetType ());
479                         sb.Append (" (");
480
481                         sb.Append (id);
482                         sb.Append (",");
483                         sb.Append (Type);
484                         if (Block != null) {
485                                 sb.Append (" - ");
486                                 sb.Append (Block.ID);
487                                 sb.Append (" - ");
488                                 sb.Append (Block.StartLocation);
489                         }
490                         sb.Append (" - ");
491                         // sb.Append (Siblings.Length);
492                         // sb.Append (" - ");
493                         sb.Append (CurrentUsageVector);
494                         sb.Append (")");
495                         return sb.ToString ();
496                 }
497 #endif
498
499                 public string Name {
500                         get { return String.Format ("{0} ({1}:{2}:{3})", GetType (), id, Type, Location); }
501                 }
502         }
503
504         public class FlowBranchingBlock : FlowBranching
505         {
506                 UsageVector sibling_list = null;
507
508                 public FlowBranchingBlock (FlowBranching parent, BranchingType type,
509                                            SiblingType stype, Block block, Location loc)
510                         : base (parent, type, stype, block, loc)
511                 { }
512
513                 public override UsageVector CurrentUsageVector {
514                         get { return sibling_list; }
515                 }
516
517                 protected override void AddSibling (UsageVector sibling)
518                 {
519                         if (sibling_list != null && sibling_list.Type == SiblingType.Block)
520                                 throw new InternalErrorException ("Blocks don't have sibling flow paths");
521                         sibling.Next = sibling_list;
522                         sibling_list = sibling;
523                 }
524
525                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
526                 {
527                         LabeledStatement stmt = Block == null ? null : Block.LookupLabel (goto_stmt.Target);
528                         if (stmt == null)
529                                 return Parent.AddGotoOrigin (vector, goto_stmt);
530
531                         // forward jump
532                         goto_stmt.SetResolvedTarget (stmt);
533                         stmt.AddUsageVector (vector);
534                         return false;
535                 }
536                 
537                 public static void Error_UnknownLabel (Location loc, string label, Report Report)
538                 {
539                         Report.Error(159, loc, "The label `{0}:' could not be found within the scope of the goto statement",
540                                 label);
541                 }
542
543                 protected override UsageVector Merge ()
544                 {
545                         Report.Debug (2, "  MERGING SIBLINGS", Name);
546                         UsageVector vector = UsageVector.MergeSiblings (sibling_list, Location);
547                         Report.Debug (2, "  MERGING SIBLINGS DONE", Name, vector);
548                         return vector;
549                 }
550         }
551
552         public class FlowBranchingBreakable : FlowBranchingBlock
553         {
554                 UsageVector break_origins;
555
556                 public FlowBranchingBreakable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
557                         : base (parent, type, stype, block, loc)
558                 { }
559
560                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
561                 {
562                         vector = vector.Clone ();
563                         vector.Next = break_origins;
564                         break_origins = vector;
565                         return false;
566                 }
567
568                 protected override UsageVector Merge ()
569                 {
570                         UsageVector vector = base.Merge ();
571                         vector.MergeOrigins (break_origins);
572                         return vector;
573                 }
574         }
575
576         public class FlowBranchingContinuable : FlowBranchingBlock
577         {
578                 UsageVector continue_origins;
579
580                 public FlowBranchingContinuable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
581                         : base (parent, type, stype, block, loc)
582                 { }
583
584                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
585                 {
586                         vector = vector.Clone ();
587                         vector.Next = continue_origins;
588                         continue_origins = vector;
589                         return false;
590                 }
591
592                 protected override UsageVector Merge ()
593                 {
594                         UsageVector vector = base.Merge ();
595                         vector.MergeOrigins (continue_origins);
596                         return vector;
597                 }
598         }
599
600         public class FlowBranchingLabeled : FlowBranchingBlock
601         {
602                 LabeledStatement stmt;
603                 UsageVector actual;
604
605                 public FlowBranchingLabeled (FlowBranching parent, LabeledStatement stmt)
606                         : base (parent, BranchingType.Labeled, SiblingType.Conditional, null, stmt.loc)
607                 {
608                         this.stmt = stmt;
609                         CurrentUsageVector.MergeOrigins (stmt.JumpOrigins);
610                         actual = CurrentUsageVector.Clone ();
611
612                         // stand-in for backward jumps
613                         CurrentUsageVector.ResetBarrier ();
614                 }
615
616                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
617                 {
618                         if (goto_stmt.Target != stmt.Name)
619                                 return Parent.AddGotoOrigin (vector, goto_stmt);
620
621                         // backward jump
622                         goto_stmt.SetResolvedTarget (stmt);
623                         actual.MergeOrigins (vector.Clone ());
624
625                         return false;
626                 }
627
628                 protected override UsageVector Merge ()
629                 {
630                         UsageVector vector = base.Merge ();
631
632                         if (actual.IsUnreachable)
633                                 Report.Warning (162, 2, stmt.loc, "Unreachable code detected");
634
635                         actual.MergeChild (vector, false);
636                         return actual;
637                 }
638         }
639
640         public class FlowBranchingIterator : FlowBranchingBlock
641         {
642                 Iterator iterator;
643                 public FlowBranchingIterator (FlowBranching parent, Iterator iterator)
644                         : base (parent, BranchingType.Iterator, SiblingType.Block, iterator.Block, iterator.Location)
645                 {
646                         this.iterator = iterator;
647                 }
648
649                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
650                 {
651                         pc = iterator.AddResumePoint (stmt);
652                         return false;
653                 }
654         }
655
656         public class FlowBranchingToplevel : FlowBranchingBlock
657         {
658                 UsageVector return_origins;
659
660                 public FlowBranchingToplevel (FlowBranching parent, ParametersBlock stmt)
661                         : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
662                 {
663                 }
664
665                 public override bool CheckRethrow (Location loc)
666                 {
667                         Report.Error (156, loc, "A throw statement with no arguments is not allowed outside of a catch clause");
668                         return false;
669                 }
670
671                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
672                 {
673                         throw new InternalErrorException ("A yield in a non-iterator block");
674                 }
675
676                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
677                 {
678                         Report.Error (139, loc, "No enclosing loop out of which to break or continue");
679                         return false;
680                 }
681
682                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
683                 {
684                         Report.Error (139, loc, "No enclosing loop out of which to break or continue");
685                         return false;
686                 }
687
688                 public override bool AddReturnOrigin (UsageVector vector, ExitStatement stmt)
689                 {
690                         vector = vector.Clone ();
691                         vector.Location = stmt.loc;
692                         vector.Next = return_origins;
693                         return_origins = vector;
694                         return false;
695                 }
696
697                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
698                 {
699                         string name = goto_stmt.Target;
700                         LabeledStatement s = Block.LookupLabel (name);
701                         if (s != null)
702                                 throw new InternalErrorException ("Shouldn't get here");
703
704                         if (Parent == null) {
705                                 Error_UnknownLabel (goto_stmt.loc, name, Report);
706                                 return false;
707                         }
708
709                         int errors = Report.Errors;
710                         Parent.AddGotoOrigin (vector, goto_stmt);
711                         if (errors == Report.Errors)
712                                 Report.Error (1632, goto_stmt.loc, "Control cannot leave the body of an anonymous method");
713                         return false;
714                 }
715
716                 protected override UsageVector Merge ()
717                 {
718                         for (UsageVector origin = return_origins; origin != null; origin = origin.Next)
719                                 Block.ParametersBlock.CheckOutParameters (origin, origin.Location);
720
721                         UsageVector vector = base.Merge ();
722                         Block.ParametersBlock.CheckOutParameters (vector, Block.loc);
723                         // Note: we _do_not_ merge in the return origins
724                         return vector;
725                 }
726
727                 public bool End ()
728                 {
729                         return Merge ().IsUnreachable;
730                 }
731         }
732
733         public class FlowBranchingTryCatch : FlowBranchingBlock
734         {
735                 TryCatch stmt;
736                 public FlowBranchingTryCatch (FlowBranching parent, TryCatch stmt)
737                         : base (parent, BranchingType.Block, SiblingType.Try, null, stmt.loc)
738                 {
739                         this.stmt = stmt;
740                 }
741
742                 public override bool CheckRethrow (Location loc)
743                 {
744                         return CurrentUsageVector.Next != null || Parent.CheckRethrow (loc);
745                 }
746
747                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
748                 {
749                         int errors = Report.Errors;
750                         Parent.AddResumePoint (stmt, loc, out pc);
751                         if (errors == Report.Errors) {
752                                 if (CurrentUsageVector.Next == null)
753                                         Report.Error (1626, loc, "Cannot yield a value in the body of a try block with a catch clause");
754                                 else
755                                         Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
756                         }
757                         return true;
758                 }
759
760                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
761                 {
762                         Parent.AddBreakOrigin (vector, loc);
763                         stmt.SomeCodeFollows ();
764                         return true;
765                 }
766
767                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
768                 {
769                         Parent.AddContinueOrigin (vector, loc);
770                         stmt.SomeCodeFollows ();
771                         return true;
772                 }
773
774                 public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
775                 {
776                         Parent.AddReturnOrigin (vector, exit_stmt);
777                         stmt.SomeCodeFollows ();
778                         return true;
779                 }
780
781                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
782                 {
783                         Parent.AddGotoOrigin (vector, goto_stmt);
784                         return true;
785                 }
786         }
787
788         public class FlowBranchingException : FlowBranching
789         {
790                 ExceptionStatement stmt;
791                 UsageVector current_vector;
792                 UsageVector try_vector;
793                 UsageVector finally_vector;
794
795                 abstract class SavedOrigin {
796                         public readonly SavedOrigin Next;
797                         public readonly UsageVector Vector;
798
799                         protected SavedOrigin (SavedOrigin next, UsageVector vector)
800                         {
801                                 Next = next;
802                                 Vector = vector.Clone ();
803                         }
804
805                         protected abstract void DoPropagateFinally (FlowBranching parent);
806                         public void PropagateFinally (UsageVector finally_vector, FlowBranching parent)
807                         {
808                                 if (finally_vector != null)
809                                         Vector.MergeChild (finally_vector, false);
810                                 DoPropagateFinally (parent);
811                         }
812                 }
813
814                 class BreakOrigin : SavedOrigin {
815                         Location Loc;
816                         public BreakOrigin (SavedOrigin next, UsageVector vector, Location loc)
817                                 : base (next, vector)
818                         {
819                                 Loc = loc;
820                         }
821
822                         protected override void DoPropagateFinally (FlowBranching parent)
823                         {
824                                 parent.AddBreakOrigin (Vector, Loc);
825                         }
826                 }
827
828                 class ContinueOrigin : SavedOrigin {
829                         Location Loc;
830                         public ContinueOrigin (SavedOrigin next, UsageVector vector, Location loc)
831                                 : base (next, vector)
832                         {
833                                 Loc = loc;
834                         }
835
836                         protected override void DoPropagateFinally (FlowBranching parent)
837                         {
838                                 parent.AddContinueOrigin (Vector, Loc);
839                         }
840                 }
841
842                 class ReturnOrigin : SavedOrigin {
843                         public ExitStatement Stmt;
844
845                         public ReturnOrigin (SavedOrigin next, UsageVector vector, ExitStatement stmt)
846                                 : base (next, vector)
847                         {
848                                 Stmt = stmt;
849                         }
850
851                         protected override void DoPropagateFinally (FlowBranching parent)
852                         {
853                                 parent.AddReturnOrigin (Vector, Stmt);
854                         }
855                 }
856
857                 class GotoOrigin : SavedOrigin {
858                         public Goto Stmt;
859
860                         public GotoOrigin (SavedOrigin next, UsageVector vector, Goto stmt)
861                                 : base (next, vector)
862                         {
863                                 Stmt = stmt;
864                         }
865
866                         protected override void DoPropagateFinally (FlowBranching parent)
867                         {
868                                 parent.AddGotoOrigin (Vector, Stmt);
869                         }
870                 }
871
872                 SavedOrigin saved_origins;
873
874                 public FlowBranchingException (FlowBranching parent,
875                                                ExceptionStatement stmt)
876                         : base (parent, BranchingType.Exception, SiblingType.Try,
877                                 null, stmt.loc)
878                 {
879                         this.stmt = stmt;
880                 }
881
882                 protected override void AddSibling (UsageVector sibling)
883                 {
884                         switch (sibling.Type) {
885                         case SiblingType.Try:
886                                 try_vector = sibling;
887                                 break;
888                         case SiblingType.Finally:
889                                 finally_vector = sibling;
890                                 break;
891                         default:
892                                 throw new InvalidOperationException ();
893                         }
894                         current_vector = sibling;
895                 }
896
897                 public override UsageVector CurrentUsageVector {
898                         get { return current_vector; }
899                 }
900
901                 public override bool CheckRethrow (Location loc)
902                 {
903                         if (!Parent.CheckRethrow (loc))
904                                 return false;
905                         if (finally_vector == null)
906                                 return true;
907                         Report.Error (724, loc, "A throw statement with no arguments is not allowed inside of a finally clause nested inside of the innermost catch clause");
908                         return false;
909                 }
910
911                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
912                 {
913                         int errors = Report.Errors;
914                         Parent.AddResumePoint (this.stmt, loc, out pc);
915                         if (errors == Report.Errors) {
916                                 if (finally_vector == null)
917                                         this.stmt.AddResumePoint (stmt, pc);
918                                 else
919                                         Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
920                         }
921                         return true;
922                 }
923
924                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
925                 {
926                         if (finally_vector != null) {
927                                 int errors = Report.Errors;
928                                 Parent.AddBreakOrigin (vector, loc);
929                                 if (errors == Report.Errors)
930                                         Report.Error (157, loc, "Control cannot leave the body of a finally clause");
931                         } else {
932                                 saved_origins = new BreakOrigin (saved_origins, vector, loc);
933                         }
934
935                         // either the loop test or a back jump will follow code
936                         stmt.SomeCodeFollows ();
937                         return true;
938                 }
939
940                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
941                 {
942                         if (finally_vector != null) {
943                                 int errors = Report.Errors;
944                                 Parent.AddContinueOrigin (vector, loc);
945                                 if (errors == Report.Errors)
946                                         Report.Error (157, loc, "Control cannot leave the body of a finally clause");
947                         } else {
948                                 saved_origins = new ContinueOrigin (saved_origins, vector, loc);
949                         }
950
951                         // either the loop test or a back jump will follow code
952                         stmt.SomeCodeFollows ();
953                         return true;
954                 }
955
956                 public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
957                 {
958                         if (finally_vector != null) {
959                                 int errors = Report.Errors;
960                                 Parent.AddReturnOrigin (vector, exit_stmt);
961                                 if (errors == Report.Errors)
962                                         exit_stmt.Error_FinallyClause (Report);
963                         } else {
964                                 saved_origins = new ReturnOrigin (saved_origins, vector, exit_stmt);
965                         }
966
967                         // sets ec.NeedReturnLabel()
968                         stmt.SomeCodeFollows ();
969                         return true;
970                 }
971
972                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
973                 {
974                         LabeledStatement s = current_vector.Block == null ? null : current_vector.Block.LookupLabel (goto_stmt.Target);
975                         if (s != null)
976                                 throw new InternalErrorException ("Shouldn't get here");
977
978                         if (finally_vector != null) {
979                                 int errors = Report.Errors;
980                                 Parent.AddGotoOrigin (vector, goto_stmt);
981                                 if (errors == Report.Errors)
982                                         Report.Error (157, goto_stmt.loc, "Control cannot leave the body of a finally clause");
983                         } else {
984                                 saved_origins = new GotoOrigin (saved_origins, vector, goto_stmt);
985                         }
986                         return true;
987                 }
988
989                 protected override UsageVector Merge ()
990                 {
991                         UsageVector vector = try_vector.Clone ();
992
993                         if (finally_vector != null)
994                                 vector.MergeChild (finally_vector, false);
995
996                         for (SavedOrigin origin = saved_origins; origin != null; origin = origin.Next)
997                                 origin.PropagateFinally (finally_vector, Parent);
998
999                         return vector;
1000                 }
1001         }
1002
1003         // <summary>
1004         //   This is used by the flow analysis code to keep track of the type of local variables
1005         //   and variables.
1006         //
1007         //   The flow code uses a BitVector to keep track of whether a variable has been assigned
1008         //   or not.  This is easy for fundamental types (int, char etc.) or reference types since
1009         //   you can only assign the whole variable as such.
1010         //
1011         //   For structs, we also need to keep track of all its fields.  To do this, we allocate one
1012         //   bit for the struct itself (it's used if you assign/access the whole struct) followed by
1013         //   one bit for each of its fields.
1014         //
1015         //   This class computes this `layout' for each type.
1016         // </summary>
1017         public class TypeInfo
1018         {
1019                 public readonly TypeSpec Type;
1020
1021                 // <summary>
1022                 //   Total number of bits a variable of this type consumes in the flow vector.
1023                 // </summary>
1024                 public readonly int TotalLength;
1025
1026                 // <summary>
1027                 //   Number of bits the simple fields of a variable of this type consume
1028                 //   in the flow vector.
1029                 // </summary>
1030                 public readonly int Length;
1031
1032                 // <summary>
1033                 //   This is only used by sub-structs.
1034                 // </summary>
1035                 public readonly int Offset;
1036
1037                 // <summary>
1038                 //   If this is a struct.
1039                 // </summary>
1040                 public readonly bool IsStruct;       
1041
1042                 // <summary>
1043                 //   If this is a struct, all fields which are structs theirselves.
1044                 // </summary>
1045                 public TypeInfo[] SubStructInfo;
1046
1047                 readonly StructInfo struct_info;
1048                 private static Dictionary<TypeSpec, TypeInfo> type_hash;
1049                 
1050                 static TypeInfo ()
1051                 {
1052                         Reset ();
1053                 }
1054                 
1055                 public static void Reset ()
1056                 {
1057                         type_hash = new Dictionary<TypeSpec, TypeInfo> ();
1058                         StructInfo.field_type_hash = new Dictionary<TypeSpec, StructInfo> ();
1059                 }
1060
1061                 public static TypeInfo GetTypeInfo (TypeSpec type)
1062                 {
1063                         TypeInfo info;
1064                         if (type_hash.TryGetValue (type, out info))
1065                                 return info;
1066
1067                         info = new TypeInfo (type);
1068                         type_hash.Add (type, info);
1069                         return info;
1070                 }
1071
1072                 public static TypeInfo GetTypeInfo (TypeContainer tc)
1073                 {
1074                         TypeInfo info;
1075                         if (type_hash.TryGetValue (tc.Definition, out info))
1076                                 return info;
1077
1078                         info = new TypeInfo (tc);
1079                         type_hash.Add (tc.Definition, info);
1080                         return info;
1081                 }
1082
1083                 private TypeInfo (TypeSpec type)
1084                 {
1085                         this.Type = type;
1086
1087                         struct_info = StructInfo.GetStructInfo (type);
1088                         if (struct_info != null) {
1089                                 Length = struct_info.Length;
1090                                 TotalLength = struct_info.TotalLength;
1091                                 SubStructInfo = struct_info.StructFields;
1092                                 IsStruct = true;
1093                         } else {
1094                                 Length = 0;
1095                                 TotalLength = 1;
1096                                 IsStruct = false;
1097                         }
1098                 }
1099
1100                 private TypeInfo (TypeContainer tc)
1101                 {
1102                         this.Type = tc.Definition;
1103
1104                         struct_info = StructInfo.GetStructInfo (tc);
1105                         if (struct_info != null) {
1106                                 Length = struct_info.Length;
1107                                 TotalLength = struct_info.TotalLength;
1108                                 SubStructInfo = struct_info.StructFields;
1109                                 IsStruct = true;
1110                         } else {
1111                                 Length = 0;
1112                                 TotalLength = 1;
1113                                 IsStruct = false;
1114                         }
1115                 }
1116
1117                 TypeInfo (StructInfo struct_info, int offset)
1118                 {
1119                         this.struct_info = struct_info;
1120                         this.Offset = offset;
1121                         this.Length = struct_info.Length;
1122                         this.TotalLength = struct_info.TotalLength;
1123                         this.SubStructInfo = struct_info.StructFields;
1124                         this.Type = struct_info.Type;
1125                         this.IsStruct = true;
1126                 }
1127
1128                 public int GetFieldIndex (string name)
1129                 {
1130                         if (struct_info == null)
1131                                 return 0;
1132
1133                         return struct_info [name];
1134                 }
1135
1136                 public TypeInfo GetSubStruct (string name)
1137                 {
1138                         if (struct_info == null)
1139                                 return null;
1140
1141                         return struct_info.GetStructField (name);
1142                 }
1143
1144                 // <summary>
1145                 //   A struct's constructor must always assign all fields.
1146                 //   This method checks whether it actually does so.
1147                 // </summary>
1148                 public bool IsFullyInitialized (BlockContext ec, VariableInfo vi, Location loc)
1149                 {
1150                         if (struct_info == null)
1151                                 return true;
1152
1153                         bool ok = true;
1154                         FlowBranching branching = ec.CurrentBranching;
1155                         for (int i = 0; i < struct_info.Count; i++) {
1156                                 var field = struct_info.Fields [i];
1157
1158                                 if (!branching.IsFieldAssigned (vi, field.Name)) {
1159                                         if (field.MemberDefinition is Property.BackingField) {
1160                                                 ec.Report.Error (843, loc,
1161                                                         "An automatically implemented property `{0}' must be fully assigned before control leaves the constructor. Consider calling default contructor",
1162                                                         field.GetSignatureForError ());
1163                                         } else {
1164                                                 ec.Report.Error (171, loc,
1165                                                         "Field `{0}' must be fully assigned before control leaves the constructor",
1166                                                         field.GetSignatureForError ());
1167                                         }
1168                                         ok = false;
1169                                 }
1170                         }
1171
1172                         return ok;
1173                 }
1174
1175                 public override string ToString ()
1176                 {
1177                         return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
1178                                               Type, Offset, Length, TotalLength);
1179                 }
1180
1181                 class StructInfo {
1182                         public readonly TypeSpec Type;
1183                         public readonly FieldSpec[] Fields;
1184                         public readonly TypeInfo[] StructFields;
1185                         public readonly int Count;
1186                         public readonly int CountPublic;
1187                         public readonly int CountNonPublic;
1188                         public readonly int Length;
1189                         public readonly int TotalLength;
1190                         public readonly bool HasStructFields;
1191
1192                         public static Dictionary<TypeSpec, StructInfo> field_type_hash;
1193                         private Dictionary<string, TypeInfo> struct_field_hash;
1194                         private Dictionary<string, int> field_hash;
1195
1196                         protected bool InTransit = false;
1197
1198                         // Private constructor.  To save memory usage, we only need to create one instance
1199                         // of this class per struct type.
1200                         private StructInfo (TypeSpec type)
1201                         {
1202                                 this.Type = type;
1203
1204                                 field_type_hash.Add (type, this);
1205
1206                                 TypeContainer tc = type.MemberDefinition as TypeContainer;
1207
1208                                 var public_fields = new List<FieldSpec> ();
1209                                 var non_public_fields = new List<FieldSpec> ();
1210
1211                                 if (tc != null) {
1212                                         var fields = tc.Fields;
1213
1214                                         if (fields != null) {
1215                                                 foreach (FieldBase field in fields) {
1216                                                         if ((field.ModFlags & Modifiers.STATIC) != 0)
1217                                                                 continue;
1218                                                         if ((field.ModFlags & Modifiers.PUBLIC) != 0)
1219                                                                 public_fields.Add (field.Spec);
1220                                                         else
1221                                                                 non_public_fields.Add (field.Spec);
1222                                                 }
1223                                         }
1224                                 }
1225
1226                                 CountPublic = public_fields.Count;
1227                                 CountNonPublic = non_public_fields.Count;
1228                                 Count = CountPublic + CountNonPublic;
1229
1230                                 Fields = new FieldSpec[Count];
1231                                 public_fields.CopyTo (Fields, 0);
1232                                 non_public_fields.CopyTo (Fields, CountPublic);
1233
1234                                 struct_field_hash = new Dictionary<string, TypeInfo> ();
1235                                 field_hash = new Dictionary<string, int> ();
1236
1237                                 Length = 0;
1238                                 StructFields = new TypeInfo [Count];
1239                                 StructInfo[] sinfo = new StructInfo [Count];
1240
1241                                 InTransit = true;
1242
1243                                 for (int i = 0; i < Count; i++) {
1244                                         var field = Fields [i];
1245
1246                                         sinfo [i] = GetStructInfo (field.MemberType);
1247                                         if (sinfo [i] == null)
1248                                                 field_hash.Add (field.Name, ++Length);
1249                                         else if (sinfo [i].InTransit) {
1250                                                 sinfo [i] = null;
1251                                                 return;
1252                                         }
1253                                 }
1254
1255                                 InTransit = false;
1256
1257                                 TotalLength = Length + 1;
1258                                 for (int i = 0; i < Count; i++) {
1259                                         var field = Fields [i];
1260
1261                                         if (sinfo [i] == null)
1262                                                 continue;
1263
1264                                         field_hash.Add (field.Name, TotalLength);
1265
1266                                         HasStructFields = true;
1267                                         StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
1268                                         struct_field_hash.Add (field.Name, StructFields [i]);
1269                                         TotalLength += sinfo [i].TotalLength;
1270                                 }
1271                         }
1272
1273                         public int this [string name] {
1274                                 get {
1275                                         int val;
1276                                         if (!field_hash.TryGetValue (name, out val))
1277                                                 return 0;
1278
1279                                         return val;
1280                                 }
1281                         }
1282
1283                         public TypeInfo GetStructField (string name)
1284                         {
1285                                 TypeInfo ti;
1286                                 if (struct_field_hash.TryGetValue (name, out ti))
1287                                         return ti;
1288
1289                                 return null;
1290                         }
1291
1292                         public static StructInfo GetStructInfo (TypeSpec type)
1293                         {
1294                                 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
1295                                     TypeManager.IsBuiltinType (type))
1296                                         return null;
1297
1298                                 if (TypeManager.IsGenericParameter (type))
1299                                         return null;
1300
1301                                 StructInfo info;
1302                                 if (field_type_hash.TryGetValue (type, out info))
1303                                         return info;
1304
1305                                 return new StructInfo (type);
1306                         }
1307
1308                         public static StructInfo GetStructInfo (TypeContainer tc)
1309                         {
1310                                 StructInfo info;
1311                                 if (field_type_hash.TryGetValue (tc.Definition, out info))
1312                                         return info;
1313
1314                                 return new StructInfo (tc.Definition);
1315                         }
1316                 }
1317         }
1318
1319         // <summary>
1320         //   This is used by the flow analysis code to store information about a single local variable
1321         //   or parameter.  Depending on the variable's type, we need to allocate one or more elements
1322         //   in the BitVector - if it's a fundamental or reference type, we just need to know whether
1323         //   it has been assigned or not, but for structs, we need this information for each of its fields.
1324         // </summary>
1325         public class VariableInfo {
1326                 public readonly string Name;
1327                 public readonly TypeInfo TypeInfo;
1328
1329                 // <summary>
1330                 //   The bit offset of this variable in the flow vector.
1331                 // </summary>
1332                 public readonly int Offset;
1333
1334                 // <summary>
1335                 //   The number of bits this variable needs in the flow vector.
1336                 //   The first bit always specifies whether the variable as such has been assigned while
1337                 //   the remaining bits contain this information for each of a struct's fields.
1338                 // </summary>
1339                 public readonly int Length;
1340
1341                 // <summary>
1342                 //   If this is a parameter of local variable.
1343                 // </summary>
1344                 public readonly bool IsParameter;
1345
1346                 public readonly LocalVariable LocalInfo;
1347
1348                 readonly VariableInfo Parent;
1349                 VariableInfo[] sub_info;
1350
1351                 bool is_ever_assigned;
1352                 public bool IsEverAssigned {
1353                         get { return is_ever_assigned; }
1354                 }
1355
1356                 protected VariableInfo (string name, TypeSpec type, int offset)
1357                 {
1358                         this.Name = name;
1359                         this.Offset = offset;
1360                         this.TypeInfo = TypeInfo.GetTypeInfo (type);
1361
1362                         Length = TypeInfo.TotalLength;
1363
1364                         Initialize ();
1365                 }
1366
1367                 protected VariableInfo (VariableInfo parent, TypeInfo type)
1368                 {
1369                         this.Name = parent.Name;
1370                         this.TypeInfo = type;
1371                         this.Offset = parent.Offset + type.Offset;
1372                         this.Parent = parent;
1373                         this.Length = type.TotalLength;
1374
1375                         this.IsParameter = parent.IsParameter;
1376                         this.LocalInfo = parent.LocalInfo;
1377
1378                         Initialize ();
1379                 }
1380
1381                 protected void Initialize ()
1382                 {
1383                         TypeInfo[] sub_fields = TypeInfo.SubStructInfo;
1384                         if (sub_fields != null) {
1385                                 sub_info = new VariableInfo [sub_fields.Length];
1386                                 for (int i = 0; i < sub_fields.Length; i++) {
1387                                         if (sub_fields [i] != null)
1388                                                 sub_info [i] = new VariableInfo (this, sub_fields [i]);
1389                                 }
1390                         } else
1391                                 sub_info = new VariableInfo [0];
1392                 }
1393
1394                 public VariableInfo (LocalVariable local_info, int offset)
1395                         : this (local_info.Name, local_info.Type, offset)
1396                 {
1397                         this.LocalInfo = local_info;
1398                         this.IsParameter = false;
1399                 }
1400
1401                 public VariableInfo (ParametersCompiled ip, int i, int offset)
1402                         : this (ip.FixedParameters [i].Name, ip.Types [i], offset)
1403                 {
1404                         this.IsParameter = true;
1405                 }
1406
1407                 public bool IsAssigned (ResolveContext ec)
1408                 {
1409                         return !ec.DoFlowAnalysis ||
1410                                 ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
1411                                 ec.CurrentBranching.IsAssigned (this);
1412                 }
1413
1414                 public bool IsAssigned (ResolveContext ec, Location loc)
1415                 {
1416                         if (IsAssigned (ec))
1417                                 return true;
1418
1419                         ec.Report.Error (165, loc,
1420                                       "Use of unassigned local variable `" + Name + "'");
1421                         ec.CurrentBranching.SetAssigned (this);
1422                         return false;
1423                 }
1424
1425                 public bool IsAssigned (MyBitVector vector)
1426                 {
1427                         if (vector == null)
1428                                 return true;
1429
1430                         if (vector [Offset])
1431                                 return true;
1432
1433                         // FIXME: Fix SetFieldAssigned to set the whole range like SetAssigned below. Then, get rid of this stanza
1434                         for (VariableInfo parent = Parent; parent != null; parent = parent.Parent) {
1435                                 if (vector [parent.Offset]) {
1436                                         // 'parent' is assigned, but someone forgot to note that all its components are assigned too
1437                                         parent.SetAssigned (vector);
1438                                         return true;
1439                                 }
1440                         }
1441
1442                         // Return unless this is a struct.
1443                         if (!TypeInfo.IsStruct)
1444                                 return false;
1445
1446                         // Ok, so each field must be assigned.
1447                         for (int i = 0; i < TypeInfo.Length; i++) {
1448                                 if (!vector [Offset + i + 1])
1449                                         return false;
1450                         }
1451
1452                         // Ok, now check all fields which are structs.
1453                         for (int i = 0; i < sub_info.Length; i++) {
1454                                 VariableInfo sinfo = sub_info [i];
1455                                 if (sinfo == null)
1456                                         continue;
1457
1458                                 if (!sinfo.IsAssigned (vector))
1459                                         return false;
1460                         }
1461
1462                         vector [Offset] = true;
1463                         is_ever_assigned = true;
1464                         return true;
1465                 }
1466
1467                 public void SetAssigned (ResolveContext ec)
1468                 {
1469                         if (ec.DoFlowAnalysis)
1470                                 ec.CurrentBranching.SetAssigned (this);
1471                 }
1472
1473                 public void SetAssigned (MyBitVector vector)
1474                 {
1475                         if (Length == 1)
1476                                 vector [Offset] = true;
1477                         else
1478                                 vector.SetRange (Offset, Length);
1479                         is_ever_assigned = true;
1480                 }
1481
1482                 public bool IsFieldAssigned (ResolveContext ec, string name, Location loc)
1483                 {
1484                         if (!ec.DoFlowAnalysis ||
1485                                 ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
1486                                 ec.CurrentBranching.IsFieldAssigned (this, name))
1487                                 return true;
1488
1489                         ec.Report.Error (170, loc,
1490                                       "Use of possibly unassigned field `" + name + "'");
1491                         ec.CurrentBranching.SetFieldAssigned (this, name);
1492                         return false;
1493                 }
1494
1495                 public bool IsFieldAssigned (MyBitVector vector, string field_name)
1496                 {
1497                         int field_idx = TypeInfo.GetFieldIndex (field_name);
1498
1499                         if (field_idx == 0)
1500                                 return true;
1501
1502                         return vector [Offset + field_idx];
1503                 }
1504
1505                 public void SetFieldAssigned (ResolveContext ec, string name)
1506                 {
1507                         if (ec.DoFlowAnalysis)
1508                                 ec.CurrentBranching.SetFieldAssigned (this, name);
1509                 }
1510
1511                 public void SetFieldAssigned (MyBitVector vector, string field_name)
1512                 {
1513                         int field_idx = TypeInfo.GetFieldIndex (field_name);
1514
1515                         if (field_idx == 0)
1516                                 return;
1517
1518                         vector [Offset + field_idx] = true;
1519                         is_ever_assigned = true;
1520                 }
1521
1522                 public VariableInfo GetSubStruct (string name)
1523                 {
1524                         TypeInfo type = TypeInfo.GetSubStruct (name);
1525
1526                         if (type == null)
1527                                 return null;
1528
1529                         return new VariableInfo (this, type);
1530                 }
1531
1532                 public override string ToString ()
1533                 {
1534                         return String.Format ("VariableInfo ({0}:{1}:{2}:{3}:{4})",
1535                                               Name, TypeInfo, Offset, Length, IsParameter);
1536                 }
1537         }
1538
1539         // <summary>
1540         //   This is a special bit vector which can inherit from another bit vector doing a
1541         //   copy-on-write strategy.  The inherited vector may have a smaller size than the
1542         //   current one.
1543         // </summary>
1544         public class MyBitVector {
1545                 public readonly int Count;
1546                 public static readonly MyBitVector Empty = new MyBitVector ();
1547
1548                 // Invariant: vector != null => vector.Count == Count
1549                 // Invariant: vector == null || shared == null
1550                 //            i.e., at most one of 'vector' and 'shared' can be non-null.  They can both be null -- that means all-ones
1551                 // The object in 'shared' cannot be modified, while 'vector' can be freely modified
1552                 System.Collections.BitArray vector, shared;
1553
1554                 MyBitVector ()
1555                 {
1556                         shared = new System.Collections.BitArray (0, false);
1557                 }
1558
1559                 public MyBitVector (MyBitVector InheritsFrom, int Count)
1560                 {
1561                         if (InheritsFrom != null)
1562                                 shared = InheritsFrom.MakeShared (Count);
1563
1564                         this.Count = Count;
1565                 }
1566
1567                 System.Collections.BitArray MakeShared (int new_count)
1568                 {
1569                         // Post-condition: vector == null
1570
1571                         // ensure we don't leak out dirty bits from the BitVector we inherited from
1572                         if (new_count > Count &&
1573                             ((shared != null && shared.Count > Count) ||
1574                              (shared == null && vector == null)))
1575                                 initialize_vector ();
1576
1577                         if (vector != null) {
1578                                 shared = vector;
1579                                 vector = null;
1580                         }
1581
1582                         return shared;
1583                 }
1584
1585                 // <summary>
1586                 //   Get/set bit `index' in the bit vector.
1587                 // </summary>
1588                 public bool this [int index] {
1589                         get {
1590                                 if (index >= Count)
1591                                         // FIXME: Disabled due to missing anonymous method flow analysis
1592                                         // throw new ArgumentOutOfRangeException ();
1593                                         return true; 
1594
1595                                 if (vector != null)
1596                                         return vector [index];
1597                                 if (shared == null)
1598                                         return true;
1599                                 if (index < shared.Count)
1600                                         return shared [index];
1601                                 return false;
1602                         }
1603
1604                         set {
1605                                 // Only copy the vector if we're actually modifying it.
1606                                 if (this [index] != value) {
1607                                         if (vector == null)
1608                                                 initialize_vector ();
1609                                         vector [index] = value;
1610                                 }
1611                         }
1612                 }
1613
1614                 // <summary>
1615                 //   Performs an `or' operation on the bit vector.  The `new_vector' may have a
1616                 //   different size than the current one.
1617                 // </summary>
1618                 private MyBitVector Or (MyBitVector new_vector)
1619                 {
1620                         if (Count == 0 || new_vector.Count == 0)
1621                                 return this;
1622
1623                         var o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
1624
1625                         if (o == null) {
1626                                 int n = new_vector.Count;
1627                                 if (n < Count) {
1628                                         for (int i = 0; i < n; ++i)
1629                                                 this [i] = true;
1630                                 } else {
1631                                         SetAll (true);
1632                                 }
1633                                 return this;
1634                         }
1635
1636                         if (Count == o.Count) {
1637                                 if (vector == null) {
1638                                         if (shared == null)
1639                                                 return this;
1640                                         initialize_vector ();
1641                                 }
1642                                 vector.Or (o);
1643                                 return this;
1644                         }
1645
1646                         int min = o.Count;
1647                         if (Count < min)
1648                                 min = Count;
1649
1650                         for (int i = 0; i < min; i++) {
1651                                 if (o [i])
1652                                         this [i] = true;
1653                         }
1654
1655                         return this;
1656                 }
1657
1658                 // <summary>
1659                 //   Performs an `and' operation on the bit vector.  The `new_vector' may have
1660                 //   a different size than the current one.
1661                 // </summary>
1662                 private MyBitVector And (MyBitVector new_vector)
1663                 {
1664                         if (Count == 0)
1665                                 return this;
1666
1667                         var o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
1668
1669                         if (o == null) {
1670                                 for (int i = new_vector.Count; i < Count; ++i)
1671                                         this [i] = false;
1672                                 return this;
1673                         }
1674
1675                         if (o.Count == 0) {
1676                                 SetAll (false);
1677                                 return this;
1678                         }
1679
1680                         if (Count == o.Count) {
1681                                 if (vector == null) {
1682                                         if (shared == null) {
1683                                                 shared = new_vector.MakeShared (Count);
1684                                                 return this;
1685                                         }
1686                                         initialize_vector ();
1687                                 }
1688                                 vector.And (o);
1689                                 return this;
1690                         }
1691
1692                         int min = o.Count;
1693                         if (Count < min)
1694                                 min = Count;
1695
1696                         for (int i = 0; i < min; i++) {
1697                                 if (! o [i])
1698                                         this [i] = false;
1699                         }
1700
1701                         for (int i = min; i < Count; i++)
1702                                 this [i] = false;
1703
1704                         return this;
1705                 }
1706
1707                 public static MyBitVector operator & (MyBitVector a, MyBitVector b)
1708                 {
1709                         if (a == b)
1710                                 return a;
1711                         if (a == null)
1712                                 return b.Clone ();
1713                         if (b == null)
1714                                 return a.Clone ();
1715                         if (a.Count > b.Count)
1716                                 return a.Clone ().And (b);
1717                         else
1718                                 return b.Clone ().And (a);                                      
1719                 }
1720
1721                 public static MyBitVector operator | (MyBitVector a, MyBitVector b)
1722                 {
1723                         if (a == b)
1724                                 return a;
1725                         if (a == null)
1726                                 return new MyBitVector (null, b.Count);
1727                         if (b == null)
1728                                 return new MyBitVector (null, a.Count);
1729                         if (a.Count > b.Count)
1730                                 return a.Clone ().Or (b);
1731                         else
1732                                 return b.Clone ().Or (a);
1733                 }
1734
1735                 public MyBitVector Clone ()
1736                 {
1737                         return Count == 0 ? Empty : new MyBitVector (this, Count);
1738                 }
1739
1740                 public void SetRange (int offset, int length)
1741                 {
1742                         if (offset > Count || offset + length > Count)
1743                                 throw new ArgumentOutOfRangeException ("flow-analysis");
1744
1745                         if (shared == null && vector == null)
1746                                 return;
1747
1748                         int i = 0;
1749                         if (shared != null) {
1750                                 if (offset + length <= shared.Count) {
1751                                         for (; i < length; ++i)
1752                                                 if (!shared [i+offset])
1753                                                     break;
1754                                         if (i == length)
1755                                                 return;
1756                                 }
1757                                 initialize_vector ();
1758                         }
1759                         for (; i < length; ++i)
1760                                 vector [i+offset] = true;
1761
1762                 }
1763
1764                 public void SetAll (bool value)
1765                 {
1766                         // Don't clobber Empty
1767                         if (Count == 0)
1768                                 return;
1769                         shared = value ? null : Empty.MakeShared (Count);
1770                         vector = null;
1771                 }
1772
1773                 void initialize_vector ()
1774                 {
1775                         // Post-condition: vector != null
1776                         if (shared == null) {
1777                                 vector = new System.Collections.BitArray (Count, true);
1778                                 return;
1779                         }
1780
1781                         vector = new System.Collections.BitArray (shared);
1782                         if (Count != vector.Count)
1783                                 vector.Length = Count;
1784                         shared = null;
1785                 }
1786
1787                 StringBuilder Dump (StringBuilder sb)
1788                 {
1789                         var dump = vector == null ? shared : vector;
1790                         if (dump == null)
1791                                 return sb.Append ("/");
1792                         if (dump == shared)
1793                                 sb.Append ("=");
1794                         for (int i = 0; i < dump.Count; i++)
1795                                 sb.Append (dump [i] ? "1" : "0");
1796                         return sb;
1797                 }
1798
1799                 public override string ToString ()
1800                 {
1801                         return Dump (new StringBuilder ("{")).Append ("}").ToString ();
1802                 }
1803         }
1804 }