bedc9d74e5018bd446dc65dd6c36b38df9262d88
[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;
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                 public void SetAssigned (VariableInfo vi)
461                 {
462                         CurrentUsageVector.SetAssigned (vi);
463                 }
464
465                 public void SetFieldAssigned (VariableInfo vi, string name)
466                 {
467                         CurrentUsageVector.SetFieldAssigned (vi, name);
468                 }
469
470                 public override string ToString ()
471                 {
472                         StringBuilder sb = new StringBuilder ();
473                         sb.Append (GetType ());
474                         sb.Append (" (");
475
476                         sb.Append (id);
477                         sb.Append (",");
478                         sb.Append (Type);
479                         if (Block != null) {
480                                 sb.Append (" - ");
481                                 sb.Append (Block.ID);
482                                 sb.Append (" - ");
483                                 sb.Append (Block.StartLocation);
484                         }
485                         sb.Append (" - ");
486                         // sb.Append (Siblings.Length);
487                         // sb.Append (" - ");
488                         sb.Append (CurrentUsageVector);
489                         sb.Append (")");
490                         return sb.ToString ();
491                 }
492
493                 public string Name {
494                         get { return String.Format ("{0} ({1}:{2}:{3})", GetType (), id, Type, Location); }
495                 }
496         }
497
498         public class FlowBranchingBlock : FlowBranching
499         {
500                 UsageVector sibling_list = null;
501
502                 public FlowBranchingBlock (FlowBranching parent, BranchingType type,
503                                            SiblingType stype, Block block, Location loc)
504                         : base (parent, type, stype, block, loc)
505                 { }
506
507                 public override UsageVector CurrentUsageVector {
508                         get { return sibling_list; }
509                 }
510
511                 protected override void AddSibling (UsageVector sibling)
512                 {
513                         if (sibling_list != null && sibling_list.Type == SiblingType.Block)
514                                 throw new InternalErrorException ("Blocks don't have sibling flow paths");
515                         sibling.Next = sibling_list;
516                         sibling_list = sibling;
517                 }
518
519                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
520                 {
521                         LabeledStatement stmt = Block == null ? null : Block.LookupLabel (goto_stmt.Target);
522                         if (stmt == null)
523                                 return Parent.AddGotoOrigin (vector, goto_stmt);
524
525                         // forward jump
526                         goto_stmt.SetResolvedTarget (stmt);
527                         stmt.AddUsageVector (vector);
528                         return false;
529                 }
530                 
531                 public static void Error_UnknownLabel (Location loc, string label)
532                 {
533                         Report.Error(159, loc, "The label `{0}:' could not be found within the scope of the goto statement",
534                                 label);
535                 }
536
537                 protected override UsageVector Merge ()
538                 {
539                         Report.Debug (2, "  MERGING SIBLINGS", Name);
540                         UsageVector vector = UsageVector.MergeSiblings (sibling_list, Location);
541                         Report.Debug (2, "  MERGING SIBLINGS DONE", Name, vector);
542                         return vector;
543                 }
544         }
545
546         public class FlowBranchingBreakable : FlowBranchingBlock
547         {
548                 UsageVector break_origins;
549
550                 public FlowBranchingBreakable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
551                         : base (parent, type, stype, block, loc)
552                 { }
553
554                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
555                 {
556                         vector = vector.Clone ();
557                         vector.Next = break_origins;
558                         break_origins = vector;
559                         return false;
560                 }
561
562                 protected override UsageVector Merge ()
563                 {
564                         UsageVector vector = base.Merge ();
565                         vector.MergeOrigins (break_origins);
566                         return vector;
567                 }
568         }
569
570         public class FlowBranchingContinuable : FlowBranchingBlock
571         {
572                 UsageVector continue_origins;
573
574                 public FlowBranchingContinuable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
575                         : base (parent, type, stype, block, loc)
576                 { }
577
578                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
579                 {
580                         vector = vector.Clone ();
581                         vector.Next = continue_origins;
582                         continue_origins = vector;
583                         return false;
584                 }
585
586                 protected override UsageVector Merge ()
587                 {
588                         UsageVector vector = base.Merge ();
589                         vector.MergeOrigins (continue_origins);
590                         return vector;
591                 }
592         }
593
594         public class FlowBranchingLabeled : FlowBranchingBlock
595         {
596                 LabeledStatement stmt;
597                 UsageVector actual;
598
599                 public FlowBranchingLabeled (FlowBranching parent, LabeledStatement stmt)
600                         : base (parent, BranchingType.Labeled, SiblingType.Conditional, null, stmt.loc)
601                 {
602                         this.stmt = stmt;
603                         CurrentUsageVector.MergeOrigins (stmt.JumpOrigins);
604                         actual = CurrentUsageVector.Clone ();
605
606                         // stand-in for backward jumps
607                         CurrentUsageVector.ResetBarrier ();
608                 }
609
610                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
611                 {
612                         if (goto_stmt.Target != stmt.Name)
613                                 return Parent.AddGotoOrigin (vector, goto_stmt);
614
615                         // backward jump
616                         goto_stmt.SetResolvedTarget (stmt);
617                         actual.MergeOrigins (vector.Clone ());
618
619                         return false;
620                 }
621
622                 protected override UsageVector Merge ()
623                 {
624                         UsageVector vector = base.Merge ();
625
626                         if (actual.IsUnreachable)
627                                 Report.Warning (162, 2, stmt.loc, "Unreachable code detected");
628
629                         actual.MergeChild (vector, false);
630                         return actual;
631                 }
632         }
633
634         public class FlowBranchingIterator : FlowBranchingBlock
635         {
636                 Iterator iterator;
637                 public FlowBranchingIterator (FlowBranching parent, Iterator iterator)
638                         : base (parent, BranchingType.Iterator, SiblingType.Block, null, iterator.Location)
639                 {
640                         this.iterator = iterator;
641                 }
642
643                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
644                 {
645                         pc = iterator.AddResumePoint (stmt);
646                         return false;
647                 }
648         }
649
650         public class FlowBranchingToplevel : FlowBranchingBlock
651         {
652                 UsageVector return_origins;
653
654                 public FlowBranchingToplevel (FlowBranching parent, ToplevelBlock stmt)
655                         : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
656                 {
657                 }
658
659                 public override bool CheckRethrow (Location loc)
660                 {
661                         Report.Error (156, loc, "A throw statement with no arguments is not allowed outside of a catch clause");
662                         return false;
663                 }
664
665                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
666                 {
667                         throw new InternalErrorException ("A yield in a non-iterator block");
668                 }
669
670                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
671                 {
672                         Report.Error (139, loc, "No enclosing loop out of which to break or continue");
673                         return false;
674                 }
675
676                 public override bool AddContinueOrigin (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 AddReturnOrigin (UsageVector vector, ExitStatement stmt)
683                 {
684                         vector = vector.Clone ();
685                         vector.Location = stmt.loc;
686                         vector.Next = return_origins;
687                         return_origins = vector;
688                         return false;
689                 }
690
691                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
692                 {
693                         string name = goto_stmt.Target;
694                         LabeledStatement s = Block.LookupLabel (name);
695                         if (s != null)
696                                 throw new InternalErrorException ("Shouldn't get here");
697
698                         if (Parent == null) {
699                                 Error_UnknownLabel (goto_stmt.loc, name);
700                                 return false;
701                         }
702
703                         int errors = Report.Errors;
704                         Parent.AddGotoOrigin (vector, goto_stmt);
705                         if (errors == Report.Errors)
706                                 Report.Error (1632, goto_stmt.loc, "Control cannot leave the body of an anonymous method");
707                         return false;
708                 }
709
710                 protected override UsageVector Merge ()
711                 {
712                         for (UsageVector origin = return_origins; origin != null; origin = origin.Next)
713                                 Block.Toplevel.CheckOutParameters (origin, origin.Location);
714
715                         UsageVector vector = base.Merge ();
716                         Block.Toplevel.CheckOutParameters (vector, Block.loc);
717                         // Note: we _do_not_ merge in the return origins
718                         return vector;
719                 }
720
721                 public bool End ()
722                 {
723                         return Merge ().IsUnreachable;
724                 }
725         }
726
727         public class FlowBranchingTryCatch : FlowBranchingBlock
728         {
729                 TryCatch stmt;
730                 public FlowBranchingTryCatch (FlowBranching parent, TryCatch stmt)
731                         : base (parent, BranchingType.Block, SiblingType.Try, null, stmt.loc)
732                 {
733                         this.stmt = stmt;
734                 }
735
736                 public override bool CheckRethrow (Location loc)
737                 {
738                         return CurrentUsageVector.Next != null || Parent.CheckRethrow (loc);
739                 }
740
741                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
742                 {
743                         int errors = Report.Errors;
744                         Parent.AddResumePoint (stmt, loc, out pc);
745                         if (errors == Report.Errors) {
746                                 if (CurrentUsageVector.Next == null)
747                                         Report.Error (1626, loc, "Cannot yield a value in the body of a try block with a catch clause");
748                                 else
749                                         Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
750                         }
751                         return true;
752                 }
753
754                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
755                 {
756                         Parent.AddBreakOrigin (vector, loc);
757                         stmt.SomeCodeFollows ();
758                         return true;
759                 }
760
761                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
762                 {
763                         Parent.AddContinueOrigin (vector, loc);
764                         stmt.SomeCodeFollows ();
765                         return true;
766                 }
767
768                 public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
769                 {
770                         Parent.AddReturnOrigin (vector, exit_stmt);
771                         stmt.SomeCodeFollows ();
772                         return true;
773                 }
774
775                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
776                 {
777                         Parent.AddGotoOrigin (vector, goto_stmt);
778                         return true;
779                 }
780         }
781
782         public class FlowBranchingException : FlowBranching
783         {
784                 ExceptionStatement stmt;
785                 UsageVector current_vector;
786                 UsageVector try_vector;
787                 UsageVector finally_vector;
788
789                 abstract class SavedOrigin {
790                         public readonly SavedOrigin Next;
791                         public readonly UsageVector Vector;
792
793                         protected SavedOrigin (SavedOrigin next, UsageVector vector)
794                         {
795                                 Next = next;
796                                 Vector = vector.Clone ();
797                         }
798
799                         protected abstract void DoPropagateFinally (FlowBranching parent);
800                         public void PropagateFinally (UsageVector finally_vector, FlowBranching parent)
801                         {
802                                 if (finally_vector != null)
803                                         Vector.MergeChild (finally_vector, false);
804                                 DoPropagateFinally (parent);
805                         }
806                 }
807
808                 class BreakOrigin : SavedOrigin {
809                         Location Loc;
810                         public BreakOrigin (SavedOrigin next, UsageVector vector, Location loc)
811                                 : base (next, vector)
812                         {
813                                 Loc = loc;
814                         }
815
816                         protected override void DoPropagateFinally (FlowBranching parent)
817                         {
818                                 parent.AddBreakOrigin (Vector, Loc);
819                         }
820                 }
821
822                 class ContinueOrigin : SavedOrigin {
823                         Location Loc;
824                         public ContinueOrigin (SavedOrigin next, UsageVector vector, Location loc)
825                                 : base (next, vector)
826                         {
827                                 Loc = loc;
828                         }
829
830                         protected override void DoPropagateFinally (FlowBranching parent)
831                         {
832                                 parent.AddContinueOrigin (Vector, Loc);
833                         }
834                 }
835
836                 class ReturnOrigin : SavedOrigin {
837                         public ExitStatement Stmt;
838
839                         public ReturnOrigin (SavedOrigin next, UsageVector vector, ExitStatement stmt)
840                                 : base (next, vector)
841                         {
842                                 Stmt = stmt;
843                         }
844
845                         protected override void DoPropagateFinally (FlowBranching parent)
846                         {
847                                 parent.AddReturnOrigin (Vector, Stmt);
848                         }
849                 }
850
851                 class GotoOrigin : SavedOrigin {
852                         public Goto Stmt;
853
854                         public GotoOrigin (SavedOrigin next, UsageVector vector, Goto stmt)
855                                 : base (next, vector)
856                         {
857                                 Stmt = stmt;
858                         }
859
860                         protected override void DoPropagateFinally (FlowBranching parent)
861                         {
862                                 parent.AddGotoOrigin (Vector, Stmt);
863                         }
864                 }
865
866                 SavedOrigin saved_origins;
867
868                 public FlowBranchingException (FlowBranching parent,
869                                                ExceptionStatement stmt)
870                         : base (parent, BranchingType.Exception, SiblingType.Try,
871                                 null, stmt.loc)
872                 {
873                         this.stmt = stmt;
874                 }
875
876                 protected override void AddSibling (UsageVector sibling)
877                 {
878                         switch (sibling.Type) {
879                         case SiblingType.Try:
880                                 try_vector = sibling;
881                                 break;
882                         case SiblingType.Finally:
883                                 finally_vector = sibling;
884                                 break;
885                         default:
886                                 throw new InvalidOperationException ();
887                         }
888                         current_vector = sibling;
889                 }
890
891                 public override UsageVector CurrentUsageVector {
892                         get { return current_vector; }
893                 }
894
895                 public override bool CheckRethrow (Location loc)
896                 {
897                         if (!Parent.CheckRethrow (loc))
898                                 return false;
899                         if (finally_vector == null)
900                                 return true;
901                         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");
902                         return false;
903                 }
904
905                 public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
906                 {
907                         int errors = Report.Errors;
908                         Parent.AddResumePoint (this.stmt, loc, out pc);
909                         if (errors == Report.Errors) {
910                                 if (finally_vector == null)
911                                         this.stmt.AddResumePoint (stmt, pc);
912                                 else
913                                         Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
914                         }
915                         return true;
916                 }
917
918                 public override bool AddBreakOrigin (UsageVector vector, Location loc)
919                 {
920                         if (finally_vector != null) {
921                                 int errors = Report.Errors;
922                                 Parent.AddBreakOrigin (vector, loc);
923                                 if (errors == Report.Errors)
924                                         Report.Error (157, loc, "Control cannot leave the body of a finally clause");
925                         } else {
926                                 saved_origins = new BreakOrigin (saved_origins, vector, loc);
927                         }
928
929                         // either the loop test or a back jump will follow code
930                         stmt.SomeCodeFollows ();
931                         return true;
932                 }
933
934                 public override bool AddContinueOrigin (UsageVector vector, Location loc)
935                 {
936                         if (finally_vector != null) {
937                                 int errors = Report.Errors;
938                                 Parent.AddContinueOrigin (vector, loc);
939                                 if (errors == Report.Errors)
940                                         Report.Error (157, loc, "Control cannot leave the body of a finally clause");
941                         } else {
942                                 saved_origins = new ContinueOrigin (saved_origins, vector, loc);
943                         }
944
945                         // either the loop test or a back jump will follow code
946                         stmt.SomeCodeFollows ();
947                         return true;
948                 }
949
950                 public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
951                 {
952                         if (finally_vector != null) {
953                                 int errors = Report.Errors;
954                                 Parent.AddReturnOrigin (vector, exit_stmt);
955                                 if (errors == Report.Errors)
956                                         exit_stmt.Error_FinallyClause ();
957                         } else {
958                                 saved_origins = new ReturnOrigin (saved_origins, vector, exit_stmt);
959                         }
960
961                         // sets ec.NeedReturnLabel()
962                         stmt.SomeCodeFollows ();
963                         return true;
964                 }
965
966                 public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
967                 {
968                         LabeledStatement s = current_vector.Block == null ? null : current_vector.Block.LookupLabel (goto_stmt.Target);
969                         if (s != null)
970                                 throw new InternalErrorException ("Shouldn't get here");
971
972                         if (finally_vector != null) {
973                                 int errors = Report.Errors;
974                                 Parent.AddGotoOrigin (vector, goto_stmt);
975                                 if (errors == Report.Errors)
976                                         Report.Error (157, goto_stmt.loc, "Control cannot leave the body of a finally clause");
977                         } else {
978                                 saved_origins = new GotoOrigin (saved_origins, vector, goto_stmt);
979                         }
980                         return true;
981                 }
982
983                 protected override UsageVector Merge ()
984                 {
985                         UsageVector vector = try_vector.Clone ();
986
987                         if (finally_vector != null)
988                                 vector.MergeChild (finally_vector, false);
989
990                         for (SavedOrigin origin = saved_origins; origin != null; origin = origin.Next)
991                                 origin.PropagateFinally (finally_vector, Parent);
992
993                         return vector;
994                 }
995         }
996
997         // <summary>
998         //   This is used by the flow analysis code to keep track of the type of local variables
999         //   and variables.
1000         //
1001         //   The flow code uses a BitVector to keep track of whether a variable has been assigned
1002         //   or not.  This is easy for fundamental types (int, char etc.) or reference types since
1003         //   you can only assign the whole variable as such.
1004         //
1005         //   For structs, we also need to keep track of all its fields.  To do this, we allocate one
1006         //   bit for the struct itself (it's used if you assign/access the whole struct) followed by
1007         //   one bit for each of its fields.
1008         //
1009         //   This class computes this `layout' for each type.
1010         // </summary>
1011         public class TypeInfo
1012         {
1013                 public readonly Type Type;
1014
1015                 // <summary>
1016                 //   Total number of bits a variable of this type consumes in the flow vector.
1017                 // </summary>
1018                 public readonly int TotalLength;
1019
1020                 // <summary>
1021                 //   Number of bits the simple fields of a variable of this type consume
1022                 //   in the flow vector.
1023                 // </summary>
1024                 public readonly int Length;
1025
1026                 // <summary>
1027                 //   This is only used by sub-structs.
1028                 // </summary>
1029                 public readonly int Offset;
1030
1031                 // <summary>
1032                 //   If this is a struct.
1033                 // </summary>
1034                 public readonly bool IsStruct;       
1035
1036                 // <summary>
1037                 //   If this is a struct, all fields which are structs theirselves.
1038                 // </summary>
1039                 public TypeInfo[] SubStructInfo;
1040
1041                 protected readonly StructInfo struct_info;
1042                 private static Hashtable type_hash = new Hashtable ();
1043
1044                 public static TypeInfo GetTypeInfo (Type type)
1045                 {
1046                         TypeInfo info = (TypeInfo) type_hash [type];
1047                         if (info != null)
1048                                 return info;
1049
1050                         info = new TypeInfo (type);
1051                         type_hash.Add (type, info);
1052                         return info;
1053                 }
1054
1055                 public static TypeInfo GetTypeInfo (TypeContainer tc)
1056                 {
1057                         TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
1058                         if (info != null)
1059                                 return info;
1060
1061                         info = new TypeInfo (tc);
1062                         type_hash.Add (tc.TypeBuilder, info);
1063                         return info;
1064                 }
1065
1066                 private TypeInfo (Type type)
1067                 {
1068                         this.Type = type;
1069
1070                         struct_info = StructInfo.GetStructInfo (type);
1071                         if (struct_info != null) {
1072                                 Length = struct_info.Length;
1073                                 TotalLength = struct_info.TotalLength;
1074                                 SubStructInfo = struct_info.StructFields;
1075                                 IsStruct = true;
1076                         } else {
1077                                 Length = 0;
1078                                 TotalLength = 1;
1079                                 IsStruct = false;
1080                         }
1081                 }
1082
1083                 private TypeInfo (TypeContainer tc)
1084                 {
1085                         this.Type = tc.TypeBuilder;
1086
1087                         struct_info = StructInfo.GetStructInfo (tc);
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                 protected TypeInfo (StructInfo struct_info, int offset)
1101                 {
1102                         this.struct_info = struct_info;
1103                         this.Offset = offset;
1104                         this.Length = struct_info.Length;
1105                         this.TotalLength = struct_info.TotalLength;
1106                         this.SubStructInfo = struct_info.StructFields;
1107                         this.Type = struct_info.Type;
1108                         this.IsStruct = true;
1109                 }
1110
1111                 public int GetFieldIndex (string name)
1112                 {
1113                         if (struct_info == null)
1114                                 return 0;
1115
1116                         return struct_info [name];
1117                 }
1118
1119                 public TypeInfo GetSubStruct (string name)
1120                 {
1121                         if (struct_info == null)
1122                                 return null;
1123
1124                         return struct_info.GetStructField (name);
1125                 }
1126
1127                 // <summary>
1128                 //   A struct's constructor must always assign all fields.
1129                 //   This method checks whether it actually does so.
1130                 // </summary>
1131                 public bool IsFullyInitialized (FlowBranching branching, VariableInfo vi, Location loc)
1132                 {
1133                         if (struct_info == null)
1134                                 return true;
1135
1136                         bool ok = true;
1137                         for (int i = 0; i < struct_info.Count; i++) {
1138                                 FieldInfo field = struct_info.Fields [i];
1139
1140                                 if (!branching.IsFieldAssigned (vi, field.Name)) {
1141                                         FieldBase fb = TypeManager.GetField (field);
1142                                         if (fb != null && (fb.ModFlags & Modifiers.BACKING_FIELD) != 0) {
1143                                                 Report.Error (843, loc,
1144                                                         "An automatically implemented property `{0}' must be fully assigned before control leaves the constructor. Consider calling default contructor",
1145                                                         fb.GetSignatureForError ());
1146                                         } else {
1147                                                 Report.Error (171, loc,
1148                                                         "Field `{0}' must be fully assigned before control leaves the constructor",
1149                                                         TypeManager.GetFullNameSignature (field));
1150                                         }
1151                                         ok = false;
1152                                 }
1153                         }
1154
1155                         return ok;
1156                 }
1157
1158                 public override string ToString ()
1159                 {
1160                         return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
1161                                               Type, Offset, Length, TotalLength);
1162                 }
1163
1164                 protected class StructInfo {
1165                         public readonly Type Type;
1166                         public readonly FieldInfo[] Fields;
1167                         public readonly TypeInfo[] StructFields;
1168                         public readonly int Count;
1169                         public readonly int CountPublic;
1170                         public readonly int CountNonPublic;
1171                         public readonly int Length;
1172                         public readonly int TotalLength;
1173                         public readonly bool HasStructFields;
1174
1175                         private static Hashtable field_type_hash = new Hashtable ();
1176                         private Hashtable struct_field_hash;
1177                         private Hashtable field_hash;
1178
1179                         protected bool InTransit = false;
1180
1181                         // Private constructor.  To save memory usage, we only need to create one instance
1182                         // of this class per struct type.
1183                         private StructInfo (Type type)
1184                         {
1185                                 this.Type = type;
1186
1187                                 field_type_hash.Add (type, this);
1188
1189                                 if (type.Module == CodeGen.Module.Builder) {
1190                                         TypeContainer tc = TypeManager.LookupTypeContainer (TypeManager.DropGenericTypeArguments (type));
1191
1192                                         ArrayList public_fields = new ArrayList ();
1193                                         ArrayList non_public_fields = new ArrayList ();
1194
1195                                         //
1196                                         // TODO: tc != null is needed because FixedBuffers are not cached
1197                                         //
1198                                         if (tc != null) {                                       
1199                                         ArrayList fields = tc.Fields;
1200
1201                                         if (fields != null) {
1202                                                 foreach (FieldBase field in fields) {
1203                                                         if ((field.ModFlags & Modifiers.STATIC) != 0)
1204                                                                 continue;
1205                                                         if ((field.ModFlags & Modifiers.PUBLIC) != 0)
1206                                                                 public_fields.Add (field.FieldBuilder);
1207                                                         else
1208                                                                 non_public_fields.Add (field.FieldBuilder);
1209                                                 }
1210                                         }
1211
1212                                         if (tc.Events != null) {
1213                                                 foreach (Event e in tc.Events) {
1214                                                         if ((e.ModFlags & Modifiers.STATIC) != 0)
1215                                                                 continue;
1216
1217                                                         EventField ef = e as EventField;
1218                                                         if (ef == null)
1219                                                                 continue;
1220
1221                                                         if ((ef.ModFlags & Modifiers.PUBLIC) != 0)
1222                                                                 public_fields.Add (ef.FieldBuilder);
1223                                                         else
1224                                                                 non_public_fields.Add (ef.FieldBuilder);
1225                                                 }
1226                                         }
1227                                         }
1228
1229                                         CountPublic = public_fields.Count;
1230                                         CountNonPublic = non_public_fields.Count;
1231                                         Count = CountPublic + CountNonPublic;
1232
1233                                         Fields = new FieldInfo [Count];
1234                                         public_fields.CopyTo (Fields, 0);
1235                                         non_public_fields.CopyTo (Fields, CountPublic);
1236 #if GMCS_SOURCE
1237                                 } else if (type is GenericTypeParameterBuilder) {
1238                                         CountPublic = CountNonPublic = Count = 0;
1239
1240                                         Fields = new FieldInfo [0];
1241 #endif
1242                                 } else {
1243                                         FieldInfo[] public_fields = type.GetFields (
1244                                                 BindingFlags.Instance|BindingFlags.Public);
1245                                         FieldInfo[] non_public_fields = type.GetFields (
1246                                                 BindingFlags.Instance|BindingFlags.NonPublic);
1247
1248                                         CountPublic = public_fields.Length;
1249                                         CountNonPublic = non_public_fields.Length;
1250                                         Count = CountPublic + CountNonPublic;
1251
1252                                         Fields = new FieldInfo [Count];
1253                                         public_fields.CopyTo (Fields, 0);
1254                                         non_public_fields.CopyTo (Fields, CountPublic);
1255                                 }
1256
1257                                 struct_field_hash = new Hashtable ();
1258                                 field_hash = new Hashtable ();
1259
1260                                 Length = 0;
1261                                 StructFields = new TypeInfo [Count];
1262                                 StructInfo[] sinfo = new StructInfo [Count];
1263
1264                                 InTransit = true;
1265
1266                                 for (int i = 0; i < Count; i++) {
1267                                         FieldInfo field = (FieldInfo) Fields [i];
1268
1269                                         sinfo [i] = GetStructInfo (field.FieldType);
1270                                         if (sinfo [i] == null)
1271                                                 field_hash.Add (field.Name, ++Length);
1272                                         else if (sinfo [i].InTransit) {
1273                                                 Report.Error (523, String.Format (
1274                                                                       "Struct member `{0}.{1}' of type `{2}' causes " +
1275                                                                       "a cycle in the structure layout",
1276                                                                       type, field.Name, sinfo [i].Type));
1277                                                 sinfo [i] = null;
1278                                                 return;
1279                                         }
1280                                 }
1281
1282                                 InTransit = false;
1283
1284                                 TotalLength = Length + 1;
1285                                 for (int i = 0; i < Count; i++) {
1286                                         FieldInfo field = (FieldInfo) Fields [i];
1287
1288                                         if (sinfo [i] == null)
1289                                                 continue;
1290
1291                                         field_hash.Add (field.Name, TotalLength);
1292
1293                                         HasStructFields = true;
1294                                         StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
1295                                         struct_field_hash.Add (field.Name, StructFields [i]);
1296                                         TotalLength += sinfo [i].TotalLength;
1297                                 }
1298                         }
1299
1300                         public int this [string name] {
1301                                 get {
1302                                         if (field_hash.Contains (name))
1303                                                 return (int) field_hash [name];
1304                                         else
1305                                                 return 0;
1306                                 }
1307                         }
1308
1309                         public TypeInfo GetStructField (string name)
1310                         {
1311                                 return (TypeInfo) struct_field_hash [name];
1312                         }
1313
1314                         public static StructInfo GetStructInfo (Type type)
1315                         {
1316                                 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
1317                                     TypeManager.IsBuiltinType (type))
1318                                         return null;
1319
1320                                 if (TypeManager.IsGenericParameter (type))
1321                                         return null;
1322
1323                                 StructInfo info = (StructInfo) field_type_hash [type];
1324                                 if (info != null)
1325                                         return info;
1326
1327                                 return new StructInfo (type);
1328                         }
1329
1330                         public static StructInfo GetStructInfo (TypeContainer tc)
1331                         {
1332                                 StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
1333                                 if (info != null)
1334                                         return info;
1335
1336                                 return new StructInfo (tc.TypeBuilder);
1337                         }
1338                 }
1339         }
1340
1341         // <summary>
1342         //   This is used by the flow analysis code to store information about a single local variable
1343         //   or parameter.  Depending on the variable's type, we need to allocate one or more elements
1344         //   in the BitVector - if it's a fundamental or reference type, we just need to know whether
1345         //   it has been assigned or not, but for structs, we need this information for each of its fields.
1346         // </summary>
1347         public class VariableInfo {
1348                 public readonly string Name;
1349                 public readonly TypeInfo TypeInfo;
1350
1351                 // <summary>
1352                 //   The bit offset of this variable in the flow vector.
1353                 // </summary>
1354                 public readonly int Offset;
1355
1356                 // <summary>
1357                 //   The number of bits this variable needs in the flow vector.
1358                 //   The first bit always specifies whether the variable as such has been assigned while
1359                 //   the remaining bits contain this information for each of a struct's fields.
1360                 // </summary>
1361                 public readonly int Length;
1362
1363                 // <summary>
1364                 //   If this is a parameter of local variable.
1365                 // </summary>
1366                 public readonly bool IsParameter;
1367
1368                 public readonly LocalInfo LocalInfo;
1369
1370                 readonly VariableInfo Parent;
1371                 VariableInfo[] sub_info;
1372
1373                 bool is_ever_assigned;
1374                 public bool IsEverAssigned {
1375                         get { return is_ever_assigned; }
1376                 }
1377
1378                 protected VariableInfo (string name, Type type, int offset)
1379                 {
1380                         this.Name = name;
1381                         this.Offset = offset;
1382                         this.TypeInfo = TypeInfo.GetTypeInfo (type);
1383
1384                         Length = TypeInfo.TotalLength;
1385
1386                         Initialize ();
1387                 }
1388
1389                 protected VariableInfo (VariableInfo parent, TypeInfo type)
1390                 {
1391                         this.Name = parent.Name;
1392                         this.TypeInfo = type;
1393                         this.Offset = parent.Offset + type.Offset;
1394                         this.Parent = parent;
1395                         this.Length = type.TotalLength;
1396
1397                         this.IsParameter = parent.IsParameter;
1398                         this.LocalInfo = parent.LocalInfo;
1399
1400                         Initialize ();
1401                 }
1402
1403                 protected void Initialize ()
1404                 {
1405                         TypeInfo[] sub_fields = TypeInfo.SubStructInfo;
1406                         if (sub_fields != null) {
1407                                 sub_info = new VariableInfo [sub_fields.Length];
1408                                 for (int i = 0; i < sub_fields.Length; i++) {
1409                                         if (sub_fields [i] != null)
1410                                                 sub_info [i] = new VariableInfo (this, sub_fields [i]);
1411                                 }
1412                         } else
1413                                 sub_info = new VariableInfo [0];
1414                 }
1415
1416                 public VariableInfo (LocalInfo local_info, int offset)
1417                         : this (local_info.Name, local_info.VariableType, offset)
1418                 {
1419                         this.LocalInfo = local_info;
1420                         this.IsParameter = false;
1421                 }
1422
1423                 public VariableInfo (Parameters ip, int i, int offset)
1424                         : this (ip.FixedParameters [i].Name, ip.Types [i], offset)
1425                 {
1426                         this.IsParameter = true;
1427                 }
1428
1429                 public bool IsAssigned (EmitContext ec)
1430                 {
1431                         return !ec.DoFlowAnalysis ||
1432                                 ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
1433                                 ec.CurrentBranching.IsAssigned (this);
1434                 }
1435
1436                 public bool IsAssigned (EmitContext ec, Location loc)
1437                 {
1438                         if (IsAssigned (ec))
1439                                 return true;
1440
1441                         Report.Error (165, loc,
1442                                       "Use of unassigned local variable `" + Name + "'");
1443                         ec.CurrentBranching.SetAssigned (this);
1444                         return false;
1445                 }
1446
1447                 public bool IsAssigned (MyBitVector vector)
1448                 {
1449                         if (vector == null)
1450                                 return true;
1451
1452                         if (vector [Offset])
1453                                 return true;
1454
1455                         // FIXME: Fix SetFieldAssigned to set the whole range like SetAssigned below. Then, get rid of this stanza
1456                         for (VariableInfo parent = Parent; parent != null; parent = parent.Parent) {
1457                                 if (vector [parent.Offset]) {
1458                                         // 'parent' is assigned, but someone forgot to note that all its components are assigned too
1459                                         parent.SetAssigned (vector);
1460                                         return true;
1461                                 }
1462                         }
1463
1464                         // Return unless this is a struct.
1465                         if (!TypeInfo.IsStruct)
1466                                 return false;
1467
1468                         // Ok, so each field must be assigned.
1469                         for (int i = 0; i < TypeInfo.Length; i++) {
1470                                 if (!vector [Offset + i + 1])
1471                                         return false;
1472                         }
1473
1474                         // Ok, now check all fields which are structs.
1475                         for (int i = 0; i < sub_info.Length; i++) {
1476                                 VariableInfo sinfo = sub_info [i];
1477                                 if (sinfo == null)
1478                                         continue;
1479
1480                                 if (!sinfo.IsAssigned (vector))
1481                                         return false;
1482                         }
1483
1484                         vector [Offset] = true;
1485                         is_ever_assigned = true;
1486                         return true;
1487                 }
1488
1489                 public void SetAssigned (EmitContext ec)
1490                 {
1491                         if (ec.DoFlowAnalysis)
1492                                 ec.CurrentBranching.SetAssigned (this);
1493                 }
1494
1495                 public void SetAssigned (MyBitVector vector)
1496                 {
1497                         if (Length == 1)
1498                                 vector [Offset] = true;
1499                         else
1500                                 vector.SetRange (Offset, Length);
1501                         is_ever_assigned = true;
1502                 }
1503
1504                 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
1505                 {
1506                         if (!ec.DoFlowAnalysis ||
1507                                 ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
1508                                 ec.CurrentBranching.IsFieldAssigned (this, name))
1509                                 return true;
1510
1511                         Report.Error (170, loc,
1512                                       "Use of possibly unassigned field `" + name + "'");
1513                         ec.CurrentBranching.SetFieldAssigned (this, name);
1514                         return false;
1515                 }
1516
1517                 public bool IsFieldAssigned (MyBitVector vector, string field_name)
1518                 {
1519                         int field_idx = TypeInfo.GetFieldIndex (field_name);
1520
1521                         if (field_idx == 0)
1522                                 return true;
1523
1524                         return vector [Offset + field_idx];
1525                 }
1526
1527                 public void SetFieldAssigned (EmitContext ec, string name)
1528                 {
1529                         if (ec.DoFlowAnalysis)
1530                                 ec.CurrentBranching.SetFieldAssigned (this, name);
1531                 }
1532
1533                 public void SetFieldAssigned (MyBitVector vector, string field_name)
1534                 {
1535                         int field_idx = TypeInfo.GetFieldIndex (field_name);
1536
1537                         if (field_idx == 0)
1538                                 return;
1539
1540                         vector [Offset + field_idx] = true;
1541                         is_ever_assigned = true;
1542                 }
1543
1544                 public VariableInfo GetSubStruct (string name)
1545                 {
1546                         TypeInfo type = TypeInfo.GetSubStruct (name);
1547
1548                         if (type == null)
1549                                 return null;
1550
1551                         return new VariableInfo (this, type);
1552                 }
1553
1554                 public override string ToString ()
1555                 {
1556                         return String.Format ("VariableInfo ({0}:{1}:{2}:{3}:{4})",
1557                                               Name, TypeInfo, Offset, Length, IsParameter);
1558                 }
1559         }
1560
1561         // <summary>
1562         //   This is a special bit vector which can inherit from another bit vector doing a
1563         //   copy-on-write strategy.  The inherited vector may have a smaller size than the
1564         //   current one.
1565         // </summary>
1566         public class MyBitVector {
1567                 public readonly int Count;
1568                 public static readonly MyBitVector Empty = new MyBitVector ();
1569
1570                 // Invariant: vector != null => vector.Count == Count
1571                 // Invariant: vector == null || shared == null
1572                 //            i.e., at most one of 'vector' and 'shared' can be non-null.  They can both be null -- that means all-ones
1573                 // The object in 'shared' cannot be modified, while 'vector' can be freely modified
1574                 BitArray vector, shared;
1575
1576                 MyBitVector ()
1577                 {
1578                         shared = new BitArray (0, false);
1579                 }
1580
1581                 public MyBitVector (MyBitVector InheritsFrom, int Count)
1582                 {
1583                         if (InheritsFrom != null)
1584                                 shared = InheritsFrom.MakeShared (Count);
1585
1586                         this.Count = Count;
1587                 }
1588
1589                 BitArray MakeShared (int new_count)
1590                 {
1591                         // Post-condition: vector == null
1592
1593                         // ensure we don't leak out dirty bits from the BitVector we inherited from
1594                         if (new_count > Count &&
1595                             ((shared != null && shared.Count > Count) ||
1596                              (shared == null && vector == null)))
1597                                 initialize_vector ();
1598
1599                         if (vector != null) {
1600                                 shared = vector;
1601                                 vector = null;
1602                         }
1603
1604                         return shared;
1605                 }
1606
1607                 // <summary>
1608                 //   Get/set bit `index' in the bit vector.
1609                 // </summary>
1610                 public bool this [int index] {
1611                         get {
1612                                 if (index >= Count)
1613                                         // FIXME: Disabled due to missing anonymous method flow analysis
1614                                         // throw new ArgumentOutOfRangeException ();
1615                                         return true; 
1616
1617                                 if (vector != null)
1618                                         return vector [index];
1619                                 if (shared == null)
1620                                         return true;
1621                                 if (index < shared.Count)
1622                                         return shared [index];
1623                                 return false;
1624                         }
1625
1626                         set {
1627                                 // Only copy the vector if we're actually modifying it.
1628                                 if (this [index] != value) {
1629                                         if (vector == null)
1630                                                 initialize_vector ();
1631                                         vector [index] = value;
1632                                 }
1633                         }
1634                 }
1635
1636                 // <summary>
1637                 //   Performs an `or' operation on the bit vector.  The `new_vector' may have a
1638                 //   different size than the current one.
1639                 // </summary>
1640                 private MyBitVector Or (MyBitVector new_vector)
1641                 {
1642                         if (Count == 0 || new_vector.Count == 0)
1643                                 return this;
1644
1645                         BitArray o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
1646
1647                         if (o == null) {
1648                                 int n = new_vector.Count;
1649                                 if (n < Count) {
1650                                         for (int i = 0; i < n; ++i)
1651                                                 this [i] = true;
1652                                 } else {
1653                                         SetAll (true);
1654                                 }
1655                                 return this;
1656                         }
1657
1658                         if (Count == o.Count) {
1659                                 if (vector == null) {
1660                                         if (shared == null)
1661                                                 return this;
1662                                         initialize_vector ();
1663                                 }
1664                                 vector.Or (o);
1665                                 return this;
1666                         }
1667
1668                         int min = o.Count;
1669                         if (Count < min)
1670                                 min = Count;
1671
1672                         for (int i = 0; i < min; i++) {
1673                                 if (o [i])
1674                                         this [i] = true;
1675                         }
1676
1677                         return this;
1678                 }
1679
1680                 // <summary>
1681                 //   Performs an `and' operation on the bit vector.  The `new_vector' may have
1682                 //   a different size than the current one.
1683                 // </summary>
1684                 private MyBitVector And (MyBitVector new_vector)
1685                 {
1686                         if (Count == 0)
1687                                 return this;
1688
1689                         BitArray o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
1690
1691                         if (o == null) {
1692                                 for (int i = new_vector.Count; i < Count; ++i)
1693                                         this [i] = false;
1694                                 return this;
1695                         }
1696
1697                         if (o.Count == 0) {
1698                                 SetAll (false);
1699                                 return this;
1700                         }
1701
1702                         if (Count == o.Count) {
1703                                 if (vector == null) {
1704                                         if (shared == null) {
1705                                                 shared = new_vector.MakeShared (Count);
1706                                                 return this;
1707                                         }
1708                                         initialize_vector ();
1709                                 }
1710                                 vector.And (o);
1711                                 return this;
1712                         }
1713
1714                         int min = o.Count;
1715                         if (Count < min)
1716                                 min = Count;
1717
1718                         for (int i = 0; i < min; i++) {
1719                                 if (! o [i])
1720                                         this [i] = false;
1721                         }
1722
1723                         for (int i = min; i < Count; i++)
1724                                 this [i] = false;
1725
1726                         return this;
1727                 }
1728
1729                 public static MyBitVector operator & (MyBitVector a, MyBitVector b)
1730                 {
1731                         if (a == b)
1732                                 return a;
1733                         if (a == null)
1734                                 return b.Clone ();
1735                         if (b == null)
1736                                 return a.Clone ();
1737                         if (a.Count > b.Count)
1738                                 return a.Clone ().And (b);
1739                         else
1740                                 return b.Clone ().And (a);                                      
1741                 }
1742
1743                 public static MyBitVector operator | (MyBitVector a, MyBitVector b)
1744                 {
1745                         if (a == b)
1746                                 return a;
1747                         if (a == null)
1748                                 return new MyBitVector (null, b.Count);
1749                         if (b == null)
1750                                 return new MyBitVector (null, a.Count);
1751                         if (a.Count > b.Count)
1752                                 return a.Clone ().Or (b);
1753                         else
1754                                 return b.Clone ().Or (a);
1755                 }
1756
1757                 public MyBitVector Clone ()
1758                 {
1759                         return Count == 0 ? Empty : new MyBitVector (this, Count);
1760                 }
1761
1762                 public void SetRange (int offset, int length)
1763                 {
1764                         if (offset > Count || offset + length > Count)
1765                                 throw new ArgumentOutOfRangeException ();
1766
1767                         if (shared == null && vector == null)
1768                                 return;
1769
1770                         int i = 0;
1771                         if (shared != null) {
1772                                 if (offset + length <= shared.Count) {
1773                                         for (; i < length; ++i)
1774                                                 if (!shared [i+offset])
1775                                                     break;
1776                                         if (i == length)
1777                                                 return;
1778                                 }
1779                                 initialize_vector ();
1780                         }
1781                         for (; i < length; ++i)
1782                                 vector [i+offset] = true;
1783
1784                 }
1785
1786                 public void SetAll (bool value)
1787                 {
1788                         // Don't clobber Empty
1789                         if (Count == 0)
1790                                 return;
1791                         shared = value ? null : Empty.MakeShared (Count);
1792                         vector = null;
1793                 }
1794
1795                 void initialize_vector ()
1796                 {
1797                         // Post-condition: vector != null
1798                         if (shared == null) {
1799                                 vector = new BitArray (Count, true);
1800                                 return;
1801                         }
1802
1803                         vector = new BitArray (shared);
1804                         if (Count != vector.Count)
1805                                 vector.Length = Count;
1806                         shared = null;
1807                 }
1808
1809                 StringBuilder Dump (StringBuilder sb)
1810                 {
1811                         BitArray dump = vector == null ? shared : vector;
1812                         if (dump == null)
1813                                 return sb.Append ("/");
1814                         if (dump == shared)
1815                                 sb.Append ("=");
1816                         for (int i = 0; i < dump.Count; i++)
1817                                 sb.Append (dump [i] ? "1" : "0");
1818                         return sb;
1819                 }
1820
1821                 public override string ToString ()
1822                 {
1823                         return Dump (new StringBuilder ("{")).Append ("}").ToString ();
1824                 }
1825         }
1826 }