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