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