* mcs/flowanalysis.cs (UsageVector.MergeChild): Move special-case
[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 //
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 //
9
10 using System;
11 using System.Text;
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Diagnostics;
16
17 namespace Mono.CSharp
18 {
19         // <summary>
20         //   A new instance of this class is created every time a new block is resolved
21         //   and if there's branching in the block's control flow.
22         // </summary>
23         public abstract class FlowBranching
24         {
25                 // <summary>
26                 //   The type of a FlowBranching.
27                 // </summary>
28                 public enum BranchingType : byte {
29                         // Normal (conditional or toplevel) block.
30                         Block,
31
32                         // Conditional.
33                         Conditional,
34
35                         // A loop block.
36                         Loop,
37
38                         // Try/Catch block.
39                         Exception,
40
41                         // Switch block.
42                         Switch,
43
44                         // Switch section.
45                         SwitchSection
46                 }
47
48                 // <summary>
49                 //   The type of one sibling of a branching.
50                 // </summary>
51                 public enum SiblingType : byte {
52                         Block,
53                         Conditional,
54                         SwitchSection,
55                         Try,
56                         Catch,
57                         Finally
58                 }
59
60                 // <summary>
61                 //   This is used in the control flow analysis code to specify whether the
62                 //   current code block may return to its enclosing block before reaching
63                 //   its end.
64                 // </summary>
65                 public enum FlowReturns : byte {
66                         Undefined = 0,
67
68                         // It can never return.
69                         Never,
70
71                         // This means that the block contains a conditional return statement
72                         // somewhere.
73                         Sometimes,
74
75                         // The code always returns, ie. there's an unconditional return / break
76                         // statement in it.
77                         Always
78                 }
79
80                 public sealed class Reachability
81                 {
82                         FlowReturns returns, breaks, throws, barrier;
83
84                         public FlowReturns Returns {
85                                 get { return returns; }
86                         }
87                         public FlowReturns Breaks {
88                                 get { return breaks; }
89                         }
90                         public FlowReturns Throws {
91                                 get { return throws; }
92                         }
93                         public FlowReturns Barrier {
94                                 get { return barrier; }
95                         }
96                         public Reachability (FlowReturns returns, FlowReturns breaks,
97                                              FlowReturns throws, FlowReturns barrier)
98                         {
99                                 this.returns = returns;
100                                 this.breaks = breaks;
101                                 this.throws = throws;
102                                 this.barrier = barrier;
103                         }
104
105                         public Reachability Clone ()
106                         {
107                                 return new Reachability (returns, breaks, throws, barrier);
108                         }
109
110                         // <summary>
111                         //   Performs an `And' operation on the FlowReturns status
112                         //   (for instance, a block only returns Always if all its siblings
113                         //   always return).
114                         // </summary>
115                         public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
116                         {
117                                 switch (a) {
118                                 case FlowReturns.Undefined:
119                                         return b;
120
121                                 case FlowReturns.Never:
122                                         if (b == FlowReturns.Never)
123                                                 return FlowReturns.Never;
124                                         else
125                                                 return FlowReturns.Sometimes;
126
127                                 case FlowReturns.Sometimes:
128                                         return FlowReturns.Sometimes;
129
130                                 case FlowReturns.Always:
131                                         if (b == FlowReturns.Always)
132                                                 return FlowReturns.Always;
133                                         else
134                                                 return FlowReturns.Sometimes;
135                                 }
136
137                                 throw new ArgumentException ("shouldn't get here");
138                         }
139
140                         public static FlowReturns OrFlowReturns (FlowReturns a, FlowReturns b)
141                         {
142                                 switch (a) {
143                                 case FlowReturns.Undefined:
144                                         return b;
145
146                                 case FlowReturns.Never:
147                                         return b;
148
149                                 case FlowReturns.Sometimes:
150                                         if (b == FlowReturns.Always)
151                                                 return FlowReturns.Always;
152                                         else
153                                                 return FlowReturns.Sometimes;
154
155                                 case FlowReturns.Always:
156                                         return FlowReturns.Always;
157                                 }
158
159                                 throw new ArgumentException ("shouldn't get here");
160                         }
161
162                         public static void And (ref Reachability a, Reachability b, bool do_break)
163                         {
164                                 if (a == null) {
165                                         a = b.Clone ();
166                                         return;
167                                 }
168
169                                 a.And (b, do_break);
170                         }
171
172                         public void And (Reachability b, bool do_break)
173                         {
174                                 //
175                                 // `break' does not "break" in a Switch or a LoopBlock
176                                 //
177                                 bool a_breaks = do_break && AlwaysBreaks;
178                                 bool b_breaks = do_break && b.AlwaysBreaks;
179
180                                 bool a_has_barrier, b_has_barrier;
181                                 if (do_break) {
182                                         //
183                                         // This is the normal case: the code following a barrier
184                                         // cannot be reached.
185                                         //
186                                         a_has_barrier = AlwaysHasBarrier;
187                                         b_has_barrier = b.AlwaysHasBarrier;
188                                 } else {
189                                         //
190                                         // Special case for Switch and LoopBlocks: we can reach the
191                                         // code after the barrier via the `break'.
192                                         //
193                                         a_has_barrier = !AlwaysBreaks && AlwaysHasBarrier;
194                                         b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
195                                 }
196
197                                 bool a_unreachable = a_breaks || AlwaysThrows || a_has_barrier;
198                                 bool b_unreachable = b_breaks || b.AlwaysThrows || b_has_barrier;
199
200                                 //
201                                 // Do all code paths always return ?
202                                 //
203                                 if (AlwaysReturns) {
204                                         if (b.AlwaysReturns || b_unreachable)
205                                                 returns = FlowReturns.Always;
206                                         else
207                                                 returns = FlowReturns.Sometimes;
208                                 } else if (b.AlwaysReturns) {
209                                         if (AlwaysReturns || a_unreachable)
210                                                 returns = FlowReturns.Always;
211                                         else
212                                                 returns = FlowReturns.Sometimes;
213                                 } else if (!MayReturn) {
214                                         if (b.MayReturn)
215                                                 returns = FlowReturns.Sometimes;
216                                         else
217                                                 returns = FlowReturns.Never;
218                                 } else if (!b.MayReturn) {
219                                         if (MayReturn)
220                                                 returns = FlowReturns.Sometimes;
221                                         else
222                                                 returns = FlowReturns.Never;
223                                 }
224
225                                 breaks = AndFlowReturns (breaks, b.breaks);
226                                 throws = AndFlowReturns (throws, b.throws);
227                                 barrier = AndFlowReturns (barrier, b.barrier);
228
229                                 if (a_unreachable && b_unreachable)
230                                         barrier = FlowReturns.Always;
231                                 else if (a_unreachable || b_unreachable)
232                                         barrier = FlowReturns.Sometimes;
233                                 else
234                                         barrier = FlowReturns.Never;
235                         }
236
237                         public void Or (Reachability b)
238                         {
239                                 returns = OrFlowReturns (returns, b.returns);
240                                 breaks = OrFlowReturns (breaks, b.breaks);
241                                 throws = OrFlowReturns (throws, b.throws);
242                                 barrier = OrFlowReturns (barrier, b.barrier);
243                         }
244
245                         public static Reachability Never ()
246                         {
247                                 return new Reachability (
248                                         FlowReturns.Never, FlowReturns.Never,
249                                         FlowReturns.Never, FlowReturns.Never);
250                         }
251
252                         public FlowReturns Reachable {
253                                 get {
254                                         if ((returns == FlowReturns.Always) ||
255                                             (breaks == FlowReturns.Always) ||
256                                             (throws == FlowReturns.Always) ||
257                                             (barrier == FlowReturns.Always))
258                                                 return FlowReturns.Never;
259                                         else if ((returns == FlowReturns.Never) &&
260                                                  (breaks == FlowReturns.Never) &&
261                                                  (throws == FlowReturns.Never) &&
262                                                  (barrier == FlowReturns.Never))
263                                                 return FlowReturns.Always;
264                                         else
265                                                 return FlowReturns.Sometimes;
266                                 }
267                         }
268
269                         public bool AlwaysBreaks {
270                                 get { return breaks == FlowReturns.Always; }
271                         }
272
273                         public bool MayBreak {
274                                 get { return breaks != FlowReturns.Never; }
275                         }
276
277                         public bool AlwaysReturns {
278                                 get { return returns == FlowReturns.Always; }
279                         }
280
281                         public bool MayReturn {
282                                 get { return returns != FlowReturns.Never; }
283                         }
284
285                         public bool AlwaysThrows {
286                                 get { return throws == FlowReturns.Always; }
287                         }
288
289                         public bool MayThrow {
290                                 get { return throws != FlowReturns.Never; }
291                         }
292
293                         public bool AlwaysHasBarrier {
294                                 get { return barrier == FlowReturns.Always; }
295                         }
296
297                         public bool MayHaveBarrier {
298                                 get { return barrier != FlowReturns.Never; }
299                         }
300
301                         public bool IsUnreachable {
302                                 get { return Reachable == FlowReturns.Never; }
303                         }
304
305                         public void SetReturns ()
306                         {
307                                 returns = FlowReturns.Always;
308                         }
309
310                         public void SetReturnsSometimes ()
311                         {
312                                 returns = FlowReturns.Sometimes;
313                         }
314
315                         public void SetBreaks ()
316                         {
317                                 breaks = FlowReturns.Always;
318                         }
319
320                         public void ResetBreaks ()
321                         {
322                                 breaks = FlowReturns.Never;
323                         }
324
325                         public void SetThrows ()
326                         {
327                                 throws = FlowReturns.Always;
328                         }
329
330                         public void SetThrowsSometimes ()
331                         {
332                                 throws = FlowReturns.Sometimes;
333                         }
334
335                         public void SetBarrier ()
336                         {
337                                 barrier = FlowReturns.Always;
338                         }
339
340                         public void ResetBarrier ()
341                         {
342                                 barrier = FlowReturns.Never;
343                         }
344
345                         static string ShortName (FlowReturns returns)
346                         {
347                                 switch (returns) {
348                                 case FlowReturns.Never:
349                                         return "N";
350                                 case FlowReturns.Sometimes:
351                                         return "S";
352                                 default:
353                                         return "A";
354                                 }
355                         }
356
357                         public override string ToString ()
358                         {
359                                 return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
360                                                       ShortName (returns), ShortName (breaks),
361                                                       ShortName (throws), ShortName (barrier),
362                                                       ShortName (Reachable));
363                         }
364                 }
365
366                 public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
367                 {
368                         switch (type) {
369                         case BranchingType.Exception:
370                                 throw new InvalidOperationException ();
371
372                         case BranchingType.Switch:
373                                 return new FlowBranchingSwitch (parent, block, loc);
374
375                         case BranchingType.SwitchSection:
376                                 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
377
378                         case BranchingType.Block:
379                                 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
380
381                         case BranchingType.Loop:
382                                 return new FlowBranchingLoop (parent, block, loc);
383
384                         default:
385                                 return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
386                         }
387                 }
388
389                 // <summary>
390                 //   The type of this flow branching.
391                 // </summary>
392                 public readonly BranchingType Type;
393
394                 // <summary>
395                 //   The block this branching is contained in.  This may be null if it's not
396                 //   a top-level block and it doesn't declare any local variables.
397                 // </summary>
398                 public readonly Block Block;
399
400                 // <summary>
401                 //   The parent of this branching or null if this is the top-block.
402                 // </summary>
403                 public readonly FlowBranching Parent;
404
405                 // <summary>
406                 //   Start-Location of this flow branching.
407                 // </summary>
408                 public readonly Location Location;
409
410                 // <summary>
411                 //   If this is an infinite loop.
412                 // </summary>
413                 public bool Infinite;
414
415                 //
416                 // Private
417                 //
418                 VariableMap param_map, local_map;
419
420                 static int next_id = 0;
421                 int id;
422
423                 // <summary>
424                 //   The vector contains a BitArray with information about which local variables
425                 //   and parameters are already initialized at the current code position.
426                 // </summary>
427                 public class UsageVector {
428                         // <summary>
429                         //   The type of this branching.
430                         // </summary>
431                         public readonly SiblingType Type;
432
433                         // <summary>
434                         //   Start location of this branching.
435                         // </summary>
436                         public readonly Location Location;
437
438                         // <summary>
439                         //   This is only valid for SwitchSection, Try, Catch and Finally.
440                         // </summary>
441                         public readonly Block Block;
442
443                         // <summary>
444                         //   If this is true, then the usage vector has been modified and must be
445                         //   merged when we're done with this branching.
446                         // </summary>
447                         public bool IsDirty;
448
449                         // <summary>
450                         //   The number of parameters in this block.
451                         // </summary>
452                         public readonly int CountParameters;
453
454                         // <summary>
455                         //   The number of locals in this block.
456                         // </summary>
457                         public readonly int CountLocals;
458
459                         // <summary>
460                         //   If not null, then we inherit our state from this vector and do a
461                         //   copy-on-write.  If null, then we're the first sibling in a top-level
462                         //   block and inherit from the empty vector.
463                         // </summary>
464                         public readonly UsageVector InheritsFrom;
465
466                         // <summary>
467                         //   This is used to construct a list of UsageVector's.
468                         // </summary>
469                         public UsageVector Next;
470
471                         //
472                         // Private.
473                         //
474                         MyBitVector locals, parameters;
475                         Reachability reachability;
476
477                         static int next_id = 0;
478                         int id;
479
480                         //
481                         // Normally, you should not use any of these constructors.
482                         //
483                         public UsageVector (SiblingType type, UsageVector parent,
484                                             Block block, Location loc,
485                                             int num_params, int num_locals)
486                         {
487                                 this.Type = type;
488                                 this.Block = block;
489                                 this.Location = loc;
490                                 this.InheritsFrom = parent;
491                                 this.CountParameters = num_params;
492                                 this.CountLocals = num_locals;
493
494                                 if (parent != null) {
495                                         if (num_locals > 0)
496                                                 locals = new MyBitVector (parent.locals, CountLocals);
497                                         
498                                         if (num_params > 0)
499                                                 parameters = new MyBitVector (parent.parameters, num_params);
500
501                                         reachability = parent.Reachability.Clone ();
502                                 } else {
503                                         if (num_locals > 0)
504                                                 locals = new MyBitVector (null, CountLocals);
505                                         
506                                         if (num_params > 0)
507                                                 parameters = new MyBitVector (null, num_params);
508
509                                         reachability = Reachability.Never ();
510                                 }
511
512                                 id = ++next_id;
513                         }
514
515                         public UsageVector (SiblingType type, UsageVector parent,
516                                             Block block, Location loc)
517                                 : this (type, parent, block, loc,
518                                         parent.CountParameters, parent.CountLocals)
519                         { }
520
521                         public UsageVector (MyBitVector parameters, MyBitVector locals,
522                                             Reachability reachability, Block block,
523                                             Location loc)
524                         {
525                                 this.Type = SiblingType.Block;
526                                 this.Location = loc;
527                                 this.Block = block;
528
529                                 this.reachability = reachability;
530                                 this.parameters = parameters;
531                                 this.locals = locals;
532
533                                 id = ++next_id;
534                         }
535
536                         // <summary>
537                         //   This does a deep copy of the usage vector.
538                         // </summary>
539                         public UsageVector Clone ()
540                         {
541                                 UsageVector retval = new UsageVector (
542                                         Type, null, Block, Location,
543                                         CountParameters, CountLocals);
544
545                                 if (retval.locals != null)
546                                         retval.locals = locals.Clone ();
547                                 
548                                 if (parameters != null)
549                                         retval.parameters = parameters.Clone ();
550                                 
551                                 retval.reachability = reachability.Clone ();
552
553                                 return retval;
554                         }
555
556                         public bool IsAssigned (VariableInfo var)
557                         {
558                                 if (!var.IsParameter && Reachability.IsUnreachable)
559                                         return true;
560
561                                 return var.IsAssigned (var.IsParameter ? parameters : locals);
562                         }
563
564                         public void SetAssigned (VariableInfo var)
565                         {
566                                 if (!var.IsParameter && Reachability.IsUnreachable)
567                                         return;
568
569                                 IsDirty = true;
570                                 var.SetAssigned (var.IsParameter ? parameters : locals);
571                         }
572
573                         public bool IsFieldAssigned (VariableInfo var, string name)
574                         {
575                                 if (!var.IsParameter && Reachability.IsUnreachable)
576                                         return true;
577
578                                 return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
579                         }
580
581                         public void SetFieldAssigned (VariableInfo var, string name)
582                         {
583                                 if (!var.IsParameter && Reachability.IsUnreachable)
584                                         return;
585
586                                 IsDirty = true;
587                                 var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
588                         }
589
590                         public Reachability Reachability {
591                                 get { return reachability; }
592                         }
593
594                         public void Return ()
595                         {
596                                 if (!reachability.IsUnreachable) {
597                                         IsDirty = true;
598                                         reachability.SetReturns ();
599                                 }
600                         }
601
602                         public void Break ()
603                         {
604                                 if (!reachability.IsUnreachable) {
605                                         IsDirty = true;
606                                         reachability.SetBreaks ();
607                                 }
608                         }
609
610                         public void Throw ()
611                         {
612                                 if (!reachability.IsUnreachable) {
613                                         IsDirty = true;
614                                         reachability.SetThrows ();
615                                 }
616                         }
617
618                         public void Goto ()
619                         {
620                                 if (!reachability.IsUnreachable) {
621                                         IsDirty = true;
622                                         reachability.SetBarrier ();
623                                 }
624                         }
625
626                         // <summary>
627                         //   Merges a child branching.
628                         // </summary>
629                         public UsageVector MergeChild (FlowBranching branching)
630                         {
631                                 UsageVector result = branching.Merge ();
632
633                                 Report.Debug (2, "  MERGING CHILD", this, branching, IsDirty,
634                                               result.ParameterVector, result.LocalVector,
635                                               result.Reachability, reachability, Type);
636
637                                 Reachability new_r = result.Reachability;
638
639                                 //
640                                 // We've now either reached the point after the branching or we will
641                                 // never get there since we always return or always throw an exception.
642                                 //
643                                 // If we can reach the point after the branching, mark all locals and
644                                 // parameters as initialized which have been initialized in all branches
645                                 // we need to look at (see above).
646                                 //
647
648                                 if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
649                                         Report.Error (163, Location,
650                                                       "Control cannot fall through from one " +
651                                                       "case label to another");
652                                         return result;
653                                 }
654
655                                 if (locals != null && result.LocalVector != null)
656                                         locals.Or (result.LocalVector);
657
658                                 if (result.ParameterVector != null)
659                                         parameters.Or (result.ParameterVector);
660
661                                 if ((branching.Type == BranchingType.Block) && branching.Block.Implicit)
662                                         reachability = new_r.Clone ();
663                                 else
664                                         reachability.Or (new_r);
665
666                                 Report.Debug (2, "  MERGING CHILD DONE", this, result,
667                                               new_r, reachability);
668
669                                 IsDirty = true;
670
671                                 return result;
672                         }
673
674                         protected static void MergeFinally (UsageVector f_origins,
675                                                      MyBitVector f_params)
676                         {
677                                 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
678                                         MyBitVector temp_params = f_params.Clone ();
679                                         temp_params.Or (vector.Parameters);
680                                 }
681                         }
682
683                         public void MergeFinally (UsageVector f_vector,
684                                                   UsageVector f_origins)
685                         {
686                                 if (parameters != null) {
687                                         if (f_vector != null) {
688                                                 MergeFinally (f_origins, f_vector.Parameters);
689                                                 MyBitVector.Or (ref parameters, f_vector.ParameterVector);
690                                         } else
691                                                 MergeFinally (f_origins, parameters);
692                                 }
693
694                                 if (f_vector != null && f_vector.LocalVector != null)
695                                         MyBitVector.Or (ref locals, f_vector.LocalVector);
696                         }
697
698                         // <summary>
699                         //   Tells control flow analysis that the current code position may be reached with
700                         //   a forward jump from any of the origins listed in `origin_vectors' which is a
701                         //   list of UsageVectors.
702                         //
703                         //   This is used when resolving forward gotos - in the following example, the
704                         //   variable `a' is uninitialized in line 8 becase this line may be reached via
705                         //   the goto in line 4:
706                         //
707                         //      1     int a;
708                         //
709                         //      3     if (something)
710                         //      4        goto World;
711                         //
712                         //      6     a = 5;
713                         //
714                         //      7  World:
715                         //      8     Console.WriteLine (a);
716                         //
717                         // </summary>
718                         public void MergeJumpOrigins (UsageVector o_vectors)
719                         {
720                                 Report.Debug (1, "  MERGING JUMP ORIGINS", this);
721
722                                 reachability = Reachability.Never ();
723
724                                 if (o_vectors == null) {
725                                         reachability.SetBarrier ();
726                                         return;
727                                 }
728
729                                 bool first = true;
730
731                                 for (UsageVector vector = o_vectors; vector != null;
732                                      vector = vector.Next) {
733                                         Report.Debug (1, "  MERGING JUMP ORIGIN", vector,
734                                                       first, locals, vector.Locals);
735
736                                         if (first) {
737                                                 if (locals != null && vector.Locals != null)
738                                                         locals.Or (vector.locals);
739                                                 
740                                                 if (parameters != null)
741                                                         parameters.Or (vector.parameters);
742                                                 first = false;
743                                         } else {
744                                                 if (locals != null)
745                                                         locals.And (vector.locals);
746                                                 if (parameters != null)
747                                                         parameters.And (vector.parameters);
748                                         }
749
750                                         Reachability.And (ref reachability, vector.Reachability, true);
751
752                                         Report.Debug (1, "  MERGING JUMP ORIGIN #1", vector);
753                                 }
754
755                                 Report.Debug (1, "  MERGING JUMP ORIGINS DONE", this);
756                         }
757
758                         // <summary>
759                         //   This is used at the beginning of a finally block if there were
760                         //   any return statements in the try block or one of the catch blocks.
761                         // </summary>
762                         public void MergeFinallyOrigins (UsageVector f_origins)
763                         {
764                                 Report.Debug (1, "  MERGING FINALLY ORIGIN", this);
765
766                                 reachability = Reachability.Never ();
767
768                                 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
769                                         Report.Debug (1, "    MERGING FINALLY ORIGIN", vector);
770
771                                         if (parameters != null)
772                                                 parameters.And (vector.parameters);
773
774                                         Reachability.And (ref reachability, vector.Reachability, true);
775                                 }
776
777                                 Report.Debug (1, "  MERGING FINALLY ORIGIN DONE", this);
778                         }
779
780                         public void MergeBreakOrigins (FlowBranching branching, UsageVector o_vectors)
781                         {
782                                 Report.Debug (1, "  MERGING BREAK ORIGINS", this);
783
784                                 if (o_vectors == null)
785                                         return;
786
787                                 bool first = branching.Infinite;
788
789                                 for (UsageVector vector = o_vectors; vector != null;
790                                      vector = vector.Next) {
791                                         Report.Debug (1, "    MERGING BREAK ORIGIN", vector, first);
792
793                                         if (first) {
794                                                 if (locals != null && vector.Locals != null)
795                                                         locals.Or (vector.locals);
796                                                 
797                                                 if (parameters != null)
798                                                         parameters.Or (vector.parameters);
799                                                 first = false;
800                                         } else {
801                                                 if (locals != null && vector.Locals != null)
802                                                         locals.And (vector.locals);
803                                                 if (parameters != null)
804                                                         parameters.And (vector.parameters);
805                                         }
806
807                                         reachability.And (vector.Reachability, false);
808                                 }
809
810                                 Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
811                         }
812
813                         public void CheckOutParameters (FlowBranching branching)
814                         {
815                                 if (parameters != null)
816                                         branching.CheckOutParameters (parameters, branching.Location);
817                         }
818
819                         // <summary>
820                         //   Performs an `or' operation on the locals and the parameters.
821                         // </summary>
822                         public void Or (UsageVector new_vector)
823                         {
824                                 IsDirty = true;
825                                 locals.Or (new_vector.locals);
826                                 if (parameters != null)
827                                         parameters.Or (new_vector.parameters);
828                         }
829
830                         // <summary>
831                         //   Performs an `and' operation on the locals.
832                         // </summary>
833                         public void AndLocals (UsageVector new_vector)
834                         {
835                                 IsDirty = true;
836                                 locals.And (new_vector.locals);
837                         }
838
839                         public bool HasParameters {
840                                 get { return parameters != null; }
841                         }
842
843                         public bool HasLocals {
844                                 get { return locals != null; }
845                         }
846
847                         // <summary>
848                         //   Returns a deep copy of the parameters.
849                         // </summary>
850                         public MyBitVector Parameters {
851                                 get { return parameters == null ? null : parameters.Clone (); }
852                         }
853
854                         // <summary>
855                         //   Returns a deep copy of the locals.
856                         // </summary>
857                         public MyBitVector Locals {
858                                 get { return locals == null ? null : locals.Clone (); }
859                         }
860
861                         public MyBitVector ParameterVector {
862                                 get { return parameters; }
863                         }
864
865                         public MyBitVector LocalVector {
866                                 get { return locals; }
867                         }
868
869                         //
870                         // Debugging stuff.
871                         //
872
873                         public override string ToString ()
874                         {
875                                 StringBuilder sb = new StringBuilder ();
876
877                                 sb.Append ("Vector (");
878                                 sb.Append (Type);
879                                 sb.Append (",");
880                                 sb.Append (id);
881                                 sb.Append (",");
882                                 sb.Append (IsDirty);
883                                 sb.Append (",");
884                                 sb.Append (reachability);
885                                 if (parameters != null) {
886                                         sb.Append (" - ");
887                                         sb.Append (parameters);
888                                 }
889                                 sb.Append (" - ");
890                                 sb.Append (locals);
891                                 sb.Append (")");
892
893                                 return sb.ToString ();
894                         }
895                 }
896
897                 // <summary>
898                 //   Creates a new flow branching which is contained in `parent'.
899                 //   You should only pass non-null for the `block' argument if this block
900                 //   introduces any new variables - in this case, we need to create a new
901                 //   usage vector with a different size than our parent's one.
902                 // </summary>
903                 protected FlowBranching (FlowBranching parent, BranchingType type, SiblingType stype,
904                                          Block block, Location loc)
905                 {
906                         Parent = parent;
907                         Block = block;
908                         Location = loc;
909                         Type = type;
910                         id = ++next_id;
911
912                         UsageVector vector;
913                         if (Block != null) {
914                                 param_map = Block.ParameterMap;
915                                 local_map = Block.LocalMap;
916
917                                 UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
918                                 vector = new UsageVector (
919                                         stype, parent_vector, Block, loc,
920                                         param_map.Length, local_map.Length);
921                         } else {
922                                 param_map = Parent.param_map;
923                                 local_map = Parent.local_map;
924                                 vector = new UsageVector (
925                                         stype, Parent.CurrentUsageVector, null, loc);
926                         }
927
928                         AddSibling (vector);
929                 }
930
931                 public abstract UsageVector CurrentUsageVector {
932                         get;
933                 }                               
934
935                 // <summary>
936                 //   Creates a sibling of the current usage vector.
937                 // </summary>
938                 public virtual void CreateSibling (Block block, SiblingType type)
939                 {
940                         UsageVector vector = new UsageVector (
941                                 type, Parent.CurrentUsageVector, block, Location);
942                         AddSibling (vector);
943
944                         Report.Debug (1, "  CREATED SIBLING", CurrentUsageVector);
945                 }
946
947                 public void CreateSibling ()
948                 {
949                         CreateSibling (null, SiblingType.Conditional);
950                 }
951
952                 protected abstract void AddSibling (UsageVector uv);
953
954                 public virtual LabeledStatement LookupLabel (string name, Location loc)
955                 {
956                         if (Parent != null)
957                                 return Parent.LookupLabel (name, loc);
958
959                         Report.Error (
960                                 159, loc,
961                                 "No such label `" + name + "' in this scope");
962                         return null;
963                 }
964
965                 public abstract void Label (UsageVector origin_vectors);
966
967                 // <summary>
968                 //   Check whether all `out' parameters have been assigned.
969                 // </summary>
970                 public void CheckOutParameters (MyBitVector parameters, Location loc)
971                 {
972                         for (int i = 0; i < param_map.Count; i++) {
973                                 VariableInfo var = param_map [i];
974
975                                 if (var == null)
976                                         continue;
977
978                                 if (var.IsAssigned (parameters))
979                                         continue;
980
981                                 Report.Error (177, loc, "The out parameter `{0}' must be assigned to before control leaves the current method",
982                                         var.Name);
983                         }
984                 }
985
986                 protected UsageVector Merge (UsageVector sibling_list)
987                 {
988                         if (sibling_list.Next == null)
989                                 return sibling_list;
990
991                         MyBitVector locals = null;
992                         MyBitVector parameters = null;
993
994                         Reachability reachability = null;
995
996                         Report.Debug (2, "  MERGING SIBLINGS", this, Name);
997
998                         for (UsageVector child = sibling_list; child != null; child = child.Next) {
999                                 bool do_break = (Type != BranchingType.Switch) &&
1000                                         (Type != BranchingType.Loop);
1001
1002                                 Report.Debug (2, "    MERGING SIBLING   ", child,
1003                                               child.ParameterVector, child.LocalVector,
1004                                               reachability, child.Reachability, do_break);
1005
1006                                 Reachability.And (ref reachability, child.Reachability, do_break);
1007
1008                                 // A local variable is initialized after a flow branching if it
1009                                 // has been initialized in all its branches which do neither
1010                                 // always return or always throw an exception.
1011                                 //
1012                                 // If a branch may return, but does not always return, then we
1013                                 // can treat it like a never-returning branch here: control will
1014                                 // only reach the code position after the branching if we did not
1015                                 // return here.
1016                                 //
1017                                 // It's important to distinguish between always and sometimes
1018                                 // returning branches here:
1019                                 //
1020                                 //    1   int a;
1021                                 //    2   if (something) {
1022                                 //    3      return;
1023                                 //    4      a = 5;
1024                                 //    5   }
1025                                 //    6   Console.WriteLine (a);
1026                                 //
1027                                 // The if block in lines 3-4 always returns, so we must not look
1028                                 // at the initialization of `a' in line 4 - thus it'll still be
1029                                 // uninitialized in line 6.
1030                                 //
1031                                 // On the other hand, the following is allowed:
1032                                 //
1033                                 //    1   int a;
1034                                 //    2   if (something)
1035                                 //    3      a = 5;
1036                                 //    4   else
1037                                 //    5      return;
1038                                 //    6   Console.WriteLine (a);
1039                                 //
1040                                 // Here, `a' is initialized in line 3 and we must not look at
1041                                 // line 5 since it always returns.
1042                                 // 
1043                                 bool do_break_2 = (child.Type != SiblingType.Block) &&
1044                                         (child.Type != SiblingType.SwitchSection);
1045                                 bool always_throws = (child.Type != SiblingType.Try) &&
1046                                         child.Reachability.AlwaysThrows;
1047                                 bool unreachable = always_throws ||
1048                                         (do_break_2 && child.Reachability.AlwaysBreaks) ||
1049                                         child.Reachability.AlwaysReturns ||
1050                                         child.Reachability.AlwaysHasBarrier;
1051
1052                                 Report.Debug (2, "    MERGING SIBLING #1", reachability,
1053                                               Type, child.Type, child.Reachability.IsUnreachable,
1054                                               do_break_2, always_throws, unreachable);
1055
1056                                 if (!unreachable && (child.LocalVector != null))
1057                                         MyBitVector.And (ref locals, child.LocalVector);
1058
1059                                 // An `out' parameter must be assigned in all branches which do
1060                                 // not always throw an exception.
1061                                 if ((child.ParameterVector != null) && !child.Reachability.AlwaysThrows)
1062                                         MyBitVector.And (ref parameters, child.ParameterVector);
1063
1064                                 Report.Debug (2, "    MERGING SIBLING #2", parameters, locals);
1065                         }
1066
1067                         if (reachability == null)
1068                                 reachability = Reachability.Never ();
1069
1070                         Report.Debug (2, "  MERGING SIBLINGS DONE", parameters, locals,
1071                                       reachability, Infinite);
1072
1073                         return new UsageVector (
1074                                 parameters, locals, reachability, null, Location);
1075                 }
1076
1077                 protected abstract UsageVector Merge ();
1078
1079                 // <summary>
1080                 //   Merge a child branching.
1081                 // </summary>
1082                 public UsageVector MergeChild (FlowBranching child)
1083                 {
1084                         return CurrentUsageVector.MergeChild (child);
1085                 }
1086
1087                 // <summary>
1088                 //   Does the toplevel merging.
1089                 // </summary>
1090                 public Reachability MergeTopBlock ()
1091                 {
1092                         if ((Type != BranchingType.Block) || (Block == null))
1093                                 throw new NotSupportedException ();
1094
1095                         UsageVector vector = new UsageVector (
1096                                 SiblingType.Block, null, Block, Location,
1097                                 param_map.Length, local_map.Length);
1098
1099                         UsageVector result = vector.MergeChild (this);
1100
1101                         Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
1102
1103                         if ((vector.Reachability.Throws != FlowReturns.Always) &&
1104                             (vector.Reachability.Barrier != FlowReturns.Always))
1105                                 CheckOutParameters (vector.Parameters, Location);
1106
1107                         return result.Reachability;
1108                 }
1109
1110                 //
1111                 // Checks whether we're in a `try' block.
1112                 //
1113                 public virtual bool InTryOrCatch (bool is_return)
1114                 {
1115                         if (Block != null && Block.IsDestructor)
1116                                 return true;
1117                         if (!is_return && (Type == BranchingType.Loop || Type == BranchingType.Switch))
1118                                 return false;
1119                         return Parent != null && Parent.InTryOrCatch (is_return);
1120                 }
1121
1122                 public virtual bool InTryWithCatch ()
1123                 {
1124                         return Parent != null && Parent.InTryWithCatch ();
1125                 }
1126
1127                 public virtual bool InLoop ()
1128                 {
1129                         return Parent != null && Parent.InLoop ();
1130                 }
1131
1132                 public virtual bool InSwitch ()
1133                 {
1134                         return Parent != null && Parent.InSwitch ();
1135                 }
1136
1137                 public virtual bool BreakCrossesTryCatchBoundary ()
1138                 {
1139                         return Parent != null && Parent.BreakCrossesTryCatchBoundary ();
1140                 }
1141
1142                 public virtual void AddFinallyVector (UsageVector vector)
1143                 {
1144                         if (Parent != null)
1145                                 Parent.AddFinallyVector (vector);
1146                         else if ((Block == null) || !Block.IsDestructor)
1147                                 throw new NotSupportedException ();
1148                 }
1149
1150                 public virtual void AddBreakVector (UsageVector vector)
1151                 {
1152                         if (Parent != null)
1153                                 Parent.AddBreakVector (vector);
1154                         else if ((Block == null) || !Block.IsDestructor)
1155                                 throw new NotSupportedException ();
1156                 }
1157
1158                 public virtual void StealFinallyClauses (ref ArrayList list)
1159                 {
1160                         if (Parent != null)
1161                                 Parent.StealFinallyClauses (ref list);
1162                 }
1163
1164                 public bool IsAssigned (VariableInfo vi)
1165                 {
1166                         return CurrentUsageVector.IsAssigned (vi);
1167                 }
1168
1169                 public bool IsFieldAssigned (VariableInfo vi, string field_name)
1170                 {
1171                         return CurrentUsageVector.IsAssigned (vi) || CurrentUsageVector.IsFieldAssigned (vi, field_name);
1172                 }
1173
1174                 public void SetAssigned (VariableInfo vi)
1175                 {
1176                         CurrentUsageVector.SetAssigned (vi);
1177                 }
1178
1179                 public void SetFieldAssigned (VariableInfo vi, string name)
1180                 {
1181                         CurrentUsageVector.SetFieldAssigned (vi, name);
1182                 }
1183
1184                 public override string ToString ()
1185                 {
1186                         StringBuilder sb = new StringBuilder ();
1187                         sb.Append (GetType ());
1188                         sb.Append (" (");
1189
1190                         sb.Append (id);
1191                         sb.Append (",");
1192                         sb.Append (Type);
1193                         if (Block != null) {
1194                                 sb.Append (" - ");
1195                                 sb.Append (Block.ID);
1196                                 sb.Append (" - ");
1197                                 sb.Append (Block.StartLocation);
1198                         }
1199                         sb.Append (" - ");
1200                         // sb.Append (Siblings.Length);
1201                         // sb.Append (" - ");
1202                         sb.Append (CurrentUsageVector);
1203                         sb.Append (")");
1204                         return sb.ToString ();
1205                 }
1206
1207                 public string Name {
1208                         get { return String.Format ("{0} ({1}:{2}:{3})", GetType (), id, Type, Location); }
1209                 }
1210         }
1211
1212         public class FlowBranchingBlock : FlowBranching
1213         {
1214                 UsageVector sibling_list = null;
1215
1216                 public FlowBranchingBlock (FlowBranching parent, BranchingType type,
1217                                            SiblingType stype, Block block, Location loc)
1218                         : base (parent, type, stype, block, loc)
1219                 { }
1220
1221                 public override UsageVector CurrentUsageVector {
1222                         get { return sibling_list; }
1223                 }
1224
1225                 protected override void AddSibling (UsageVector sibling)
1226                 {
1227                         sibling.Next = sibling_list;
1228                         sibling_list = sibling;
1229                 }
1230
1231                 public override LabeledStatement LookupLabel (string name, Location loc)
1232                 {
1233                         if (Block == null)
1234                                 return base.LookupLabel (name, loc);
1235
1236                         LabeledStatement s = Block.LookupLabel (name);
1237                         if (s != null)
1238                                 return s;
1239
1240                         return base.LookupLabel (name, loc);
1241                 }
1242
1243                 public override void Label (UsageVector origin_vectors)
1244                 {
1245                         if (!CurrentUsageVector.Reachability.IsUnreachable) {
1246                                 UsageVector vector = CurrentUsageVector.Clone ();
1247                                 vector.Next = origin_vectors;
1248                                 origin_vectors = vector;
1249                         }
1250
1251                         CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1252                 }
1253
1254                 protected override UsageVector Merge ()
1255                 {
1256                         return Merge (sibling_list);
1257                 }
1258         }
1259
1260         public class FlowBranchingLoop : FlowBranchingBlock
1261         {
1262                 UsageVector break_origins;
1263
1264                 public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
1265                         : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
1266                 { }
1267
1268                 public override void AddBreakVector (UsageVector vector)
1269                 {
1270                         vector = vector.Clone ();
1271                         vector.Next = break_origins;
1272                         break_origins = vector;
1273                 }
1274
1275                 public override bool InLoop ()
1276                 {
1277                         return true;
1278                 }
1279
1280                 public override bool BreakCrossesTryCatchBoundary ()
1281                 {
1282                         return false;
1283                 }
1284
1285                 protected override UsageVector Merge ()
1286                 {
1287                         UsageVector vector = base.Merge ();
1288
1289                         vector.MergeBreakOrigins (this, break_origins);
1290
1291                         Reachability r = vector.Reachability;
1292
1293                         if (r.MayBreak) {
1294                                 r.ResetBarrier ();
1295                         } else if (Infinite) {
1296                                 r.SetBarrier ();
1297                                 if (r.MayReturn) {
1298                                         // If we're an infinite loop and do not break,
1299                                         // the code after the loop can never be reached.
1300                                         // However, if we may return from the loop,
1301                                         // then we do always return (or stay in the
1302                                         // loop forever).
1303                                         r.SetReturns ();
1304                                 }
1305                         }
1306
1307                         // swallow up the 'break'
1308                         r.ResetBreaks ();
1309
1310                         return vector;
1311                 }
1312         }
1313
1314         public class FlowBranchingSwitch : FlowBranchingBlock
1315         {
1316                 UsageVector break_origins;
1317
1318                 public FlowBranchingSwitch (FlowBranching parent, Block block, Location loc)
1319                         : base (parent, BranchingType.Switch, SiblingType.SwitchSection, block, loc)
1320                 { }
1321
1322                 public override void AddBreakVector (UsageVector vector)
1323                 {
1324                         vector = vector.Clone ();
1325                         vector.Next = break_origins;
1326                         break_origins = vector;
1327                 }
1328
1329                 public override bool InSwitch ()
1330                 {
1331                         return true;
1332                 }
1333
1334                 public override bool BreakCrossesTryCatchBoundary ()
1335                 {
1336                         return false;
1337                 }
1338
1339                 protected override UsageVector Merge ()
1340                 {
1341                         UsageVector vector = base.Merge ();
1342
1343                         vector.MergeBreakOrigins (this, break_origins);
1344
1345                         Reachability r = vector.Reachability;
1346
1347                         if (r.MayBreak || r.MayReturn)
1348                                 r.ResetBarrier ();
1349
1350                         r.ResetBreaks ();
1351
1352                         return vector;
1353                 }
1354         }
1355
1356         public class FlowBranchingException : FlowBranching
1357         {
1358                 ExceptionStatement stmt;
1359                 UsageVector current_vector;
1360                 UsageVector catch_vectors;
1361                 UsageVector finally_vector;
1362                 UsageVector finally_origins;
1363                 bool emit_finally;
1364
1365                 public FlowBranchingException (FlowBranching parent,
1366                                                ExceptionStatement stmt)
1367                         : base (parent, BranchingType.Exception, SiblingType.Try,
1368                                 null, stmt.loc)
1369                 {
1370                         this.stmt = stmt;
1371                         this.emit_finally = true;
1372                 }
1373
1374                 protected override void AddSibling (UsageVector sibling)
1375                 {
1376                         if (sibling.Type == SiblingType.Try) {
1377                                 sibling.Next = catch_vectors;
1378                                 catch_vectors = sibling;
1379                         } else if (sibling.Type == SiblingType.Catch) {
1380                                 sibling.Next = catch_vectors;
1381                                 catch_vectors = sibling;
1382                         } else if (sibling.Type == SiblingType.Finally) {
1383                                 sibling.MergeFinallyOrigins (finally_origins);
1384                                 finally_vector = sibling;
1385                         } else
1386                                 throw new InvalidOperationException ();
1387
1388                         current_vector = sibling;
1389                 }
1390
1391                 public override UsageVector CurrentUsageVector {
1392                         get { return current_vector; }
1393                 }
1394
1395                 public override bool InTryOrCatch (bool is_return)
1396                 {
1397                         return finally_vector == null;
1398                 }
1399
1400                 public override bool InTryWithCatch ()
1401                 {
1402                         if (finally_vector == null) {
1403                                 Try t = stmt as Try;
1404                                 if (t != null && t.HasCatch)
1405                                         return true;
1406                         }
1407
1408                         return base.InTryWithCatch ();
1409                 }
1410
1411                 public override bool BreakCrossesTryCatchBoundary ()
1412                 {
1413                         return true;
1414                 }
1415
1416                 public override void AddFinallyVector (UsageVector vector)
1417                 {
1418                         vector = vector.Clone ();
1419                         vector.Next = finally_origins;
1420                         finally_origins = vector;
1421                 }
1422
1423                 public override void StealFinallyClauses (ref ArrayList list)
1424                 {
1425                         if (list == null)
1426                                 list = new ArrayList ();
1427                         list.Add (stmt);
1428                         emit_finally = false;
1429                         base.StealFinallyClauses (ref list);
1430                 }
1431
1432                 public bool EmitFinally {
1433                         get { return emit_finally; }
1434                 }
1435
1436                 public override LabeledStatement LookupLabel (string name, Location loc)
1437                 {
1438                         if (current_vector.Block == null)
1439                                 return base.LookupLabel (name, loc);
1440
1441                         LabeledStatement s = current_vector.Block.LookupLabel (name);
1442                         if (s != null)
1443                                 return s;
1444
1445                         if (finally_vector != null) {
1446                                 Report.Error (157, loc,
1447                                         "Control cannot leave the body of a finally clause");
1448                                 return null;
1449                         }
1450
1451                         return base.LookupLabel (name, loc);
1452                 }
1453
1454                 public override void Label (UsageVector origin_vectors)
1455                 {
1456                         CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1457                 }
1458
1459                 protected override UsageVector Merge ()
1460                 {
1461                         UsageVector vector = Merge (catch_vectors);
1462
1463                         vector.MergeFinally (finally_vector, finally_origins);
1464
1465                         return vector;
1466                 }
1467         }
1468
1469         // <summary>
1470         //   This is used by the flow analysis code to keep track of the type of local variables
1471         //   and variables.
1472         //
1473         //   The flow code uses a BitVector to keep track of whether a variable has been assigned
1474         //   or not.  This is easy for fundamental types (int, char etc.) or reference types since
1475         //   you can only assign the whole variable as such.
1476         //
1477         //   For structs, we also need to keep track of all its fields.  To do this, we allocate one
1478         //   bit for the struct itself (it's used if you assign/access the whole struct) followed by
1479         //   one bit for each of its fields.
1480         //
1481         //   This class computes this `layout' for each type.
1482         // </summary>
1483         public class TypeInfo
1484         {
1485                 public readonly Type Type;
1486
1487                 // <summary>
1488                 //   Total number of bits a variable of this type consumes in the flow vector.
1489                 // </summary>
1490                 public readonly int TotalLength;
1491
1492                 // <summary>
1493                 //   Number of bits the simple fields of a variable of this type consume
1494                 //   in the flow vector.
1495                 // </summary>
1496                 public readonly int Length;
1497
1498                 // <summary>
1499                 //   This is only used by sub-structs.
1500                 // </summary>
1501                 public readonly int Offset;
1502
1503                 // <summary>
1504                 //   If this is a struct.
1505                 // </summary>
1506                 public readonly bool IsStruct;       
1507
1508                 // <summary>
1509                 //   If this is a struct, all fields which are structs theirselves.
1510                 // </summary>
1511                 public TypeInfo[] SubStructInfo;
1512
1513                 protected readonly StructInfo struct_info;
1514                 private static Hashtable type_hash = new Hashtable ();
1515
1516                 public static TypeInfo GetTypeInfo (Type type)
1517                 {
1518                         TypeInfo info = (TypeInfo) type_hash [type];
1519                         if (info != null)
1520                                 return info;
1521
1522                         info = new TypeInfo (type);
1523                         type_hash.Add (type, info);
1524                         return info;
1525                 }
1526
1527                 public static TypeInfo GetTypeInfo (TypeContainer tc)
1528                 {
1529                         TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
1530                         if (info != null)
1531                                 return info;
1532
1533                         info = new TypeInfo (tc);
1534                         type_hash.Add (tc.TypeBuilder, info);
1535                         return info;
1536                 }
1537
1538                 private TypeInfo (Type type)
1539                 {
1540                         this.Type = type;
1541
1542                         struct_info = StructInfo.GetStructInfo (type);
1543                         if (struct_info != null) {
1544                                 Length = struct_info.Length;
1545                                 TotalLength = struct_info.TotalLength;
1546                                 SubStructInfo = struct_info.StructFields;
1547                                 IsStruct = true;
1548                         } else {
1549                                 Length = 0;
1550                                 TotalLength = 1;
1551                                 IsStruct = false;
1552                         }
1553                 }
1554
1555                 private TypeInfo (TypeContainer tc)
1556                 {
1557                         this.Type = tc.TypeBuilder;
1558
1559                         struct_info = StructInfo.GetStructInfo (tc);
1560                         if (struct_info != null) {
1561                                 Length = struct_info.Length;
1562                                 TotalLength = struct_info.TotalLength;
1563                                 SubStructInfo = struct_info.StructFields;
1564                                 IsStruct = true;
1565                         } else {
1566                                 Length = 0;
1567                                 TotalLength = 1;
1568                                 IsStruct = false;
1569                         }
1570                 }
1571
1572                 protected TypeInfo (StructInfo struct_info, int offset)
1573                 {
1574                         this.struct_info = struct_info;
1575                         this.Offset = offset;
1576                         this.Length = struct_info.Length;
1577                         this.TotalLength = struct_info.TotalLength;
1578                         this.SubStructInfo = struct_info.StructFields;
1579                         this.Type = struct_info.Type;
1580                         this.IsStruct = true;
1581                 }
1582
1583                 public int GetFieldIndex (string name)
1584                 {
1585                         if (struct_info == null)
1586                                 return 0;
1587
1588                         return struct_info [name];
1589                 }
1590
1591                 public TypeInfo GetSubStruct (string name)
1592                 {
1593                         if (struct_info == null)
1594                                 return null;
1595
1596                         return struct_info.GetStructField (name);
1597                 }
1598
1599                 // <summary>
1600                 //   A struct's constructor must always assign all fields.
1601                 //   This method checks whether it actually does so.
1602                 // </summary>
1603                 public bool IsFullyInitialized (FlowBranching branching, VariableInfo vi, Location loc)
1604                 {
1605                         if (struct_info == null)
1606                                 return true;
1607
1608                         bool ok = true;
1609                         for (int i = 0; i < struct_info.Count; i++) {
1610                                 FieldInfo field = struct_info.Fields [i];
1611
1612                                 if (!branching.IsFieldAssigned (vi, field.Name)) {
1613                                         Report.Error (171, loc,
1614                                                 "Field `{0}' must be fully assigned before control leaves the constructor",
1615                                                 TypeManager.GetFullNameSignature (field));
1616                                         ok = false;
1617                                 }
1618                         }
1619
1620                         return ok;
1621                 }
1622
1623                 public override string ToString ()
1624                 {
1625                         return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
1626                                               Type, Offset, Length, TotalLength);
1627                 }
1628
1629                 protected class StructInfo {
1630                         public readonly Type Type;
1631                         public readonly FieldInfo[] Fields;
1632                         public readonly TypeInfo[] StructFields;
1633                         public readonly int Count;
1634                         public readonly int CountPublic;
1635                         public readonly int CountNonPublic;
1636                         public readonly int Length;
1637                         public readonly int TotalLength;
1638                         public readonly bool HasStructFields;
1639
1640                         private static Hashtable field_type_hash = new Hashtable ();
1641                         private Hashtable struct_field_hash;
1642                         private Hashtable field_hash;
1643
1644                         protected bool InTransit = false;
1645
1646                         // Private constructor.  To save memory usage, we only need to create one instance
1647                         // of this class per struct type.
1648                         private StructInfo (Type type)
1649                         {
1650                                 this.Type = type;
1651
1652                                 field_type_hash.Add (type, this);
1653
1654                                 if (type is TypeBuilder) {
1655                                         TypeContainer tc = TypeManager.LookupTypeContainer (type);
1656
1657                                         ArrayList fields = null;
1658                                         if (tc != null)
1659                                                 fields = tc.Fields;
1660
1661                                         ArrayList public_fields = new ArrayList ();
1662                                         ArrayList non_public_fields = new ArrayList ();
1663
1664                                         if (fields != null) {
1665                                                 foreach (FieldMember field in fields) {
1666                                                         if ((field.ModFlags & Modifiers.STATIC) != 0)
1667                                                                 continue;
1668                                                         if ((field.ModFlags & Modifiers.PUBLIC) != 0)
1669                                                                 public_fields.Add (field.FieldBuilder);
1670                                                         else
1671                                                                 non_public_fields.Add (field.FieldBuilder);
1672                                                 }
1673                                         }
1674
1675                                         CountPublic = public_fields.Count;
1676                                         CountNonPublic = non_public_fields.Count;
1677                                         Count = CountPublic + CountNonPublic;
1678
1679                                         Fields = new FieldInfo [Count];
1680                                         public_fields.CopyTo (Fields, 0);
1681                                         non_public_fields.CopyTo (Fields, CountPublic);
1682                                 } else {
1683                                         FieldInfo[] public_fields = type.GetFields (
1684                                                 BindingFlags.Instance|BindingFlags.Public);
1685                                         FieldInfo[] non_public_fields = type.GetFields (
1686                                                 BindingFlags.Instance|BindingFlags.NonPublic);
1687
1688                                         CountPublic = public_fields.Length;
1689                                         CountNonPublic = non_public_fields.Length;
1690                                         Count = CountPublic + CountNonPublic;
1691
1692                                         Fields = new FieldInfo [Count];
1693                                         public_fields.CopyTo (Fields, 0);
1694                                         non_public_fields.CopyTo (Fields, CountPublic);
1695                                 }
1696
1697                                 struct_field_hash = new Hashtable ();
1698                                 field_hash = new Hashtable ();
1699
1700                                 Length = 0;
1701                                 StructFields = new TypeInfo [Count];
1702                                 StructInfo[] sinfo = new StructInfo [Count];
1703
1704                                 InTransit = true;
1705
1706                                 for (int i = 0; i < Count; i++) {
1707                                         FieldInfo field = (FieldInfo) Fields [i];
1708
1709                                         sinfo [i] = GetStructInfo (field.FieldType);
1710                                         if (sinfo [i] == null)
1711                                                 field_hash.Add (field.Name, ++Length);
1712                                         else if (sinfo [i].InTransit) {
1713                                                 Report.Error (523, String.Format (
1714                                                                       "Struct member `{0}.{1}' of type `{2}' causes " +
1715                                                                       "a cycle in the structure layout",
1716                                                                       type, field.Name, sinfo [i].Type));
1717                                                 sinfo [i] = null;
1718                                                 return;
1719                                         }
1720                                 }
1721
1722                                 InTransit = false;
1723
1724                                 TotalLength = Length + 1;
1725                                 for (int i = 0; i < Count; i++) {
1726                                         FieldInfo field = (FieldInfo) Fields [i];
1727
1728                                         if (sinfo [i] == null)
1729                                                 continue;
1730
1731                                         field_hash.Add (field.Name, TotalLength);
1732
1733                                         HasStructFields = true;
1734                                         StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
1735                                         struct_field_hash.Add (field.Name, StructFields [i]);
1736                                         TotalLength += sinfo [i].TotalLength;
1737                                 }
1738                         }
1739
1740                         public int this [string name] {
1741                                 get {
1742                                         if (field_hash.Contains (name))
1743                                                 return (int) field_hash [name];
1744                                         else
1745                                                 return 0;
1746                                 }
1747                         }
1748
1749                         public TypeInfo GetStructField (string name)
1750                         {
1751                                 return (TypeInfo) struct_field_hash [name];
1752                         }
1753
1754                         public static StructInfo GetStructInfo (Type type)
1755                         {
1756                                 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
1757                                     TypeManager.IsBuiltinType (type))
1758                                         return null;
1759
1760                                 StructInfo info = (StructInfo) field_type_hash [type];
1761                                 if (info != null)
1762                                         return info;
1763
1764                                 return new StructInfo (type);
1765                         }
1766
1767                         public static StructInfo GetStructInfo (TypeContainer tc)
1768                         {
1769                                 StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
1770                                 if (info != null)
1771                                         return info;
1772
1773                                 return new StructInfo (tc.TypeBuilder);
1774                         }
1775                 }
1776         }
1777
1778         // <summary>
1779         //   This is used by the flow analysis code to store information about a single local variable
1780         //   or parameter.  Depending on the variable's type, we need to allocate one or more elements
1781         //   in the BitVector - if it's a fundamental or reference type, we just need to know whether
1782         //   it has been assigned or not, but for structs, we need this information for each of its fields.
1783         // </summary>
1784         public class VariableInfo {
1785                 public readonly string Name;
1786                 public readonly TypeInfo TypeInfo;
1787
1788                 // <summary>
1789                 //   The bit offset of this variable in the flow vector.
1790                 // </summary>
1791                 public readonly int Offset;
1792
1793                 // <summary>
1794                 //   The number of bits this variable needs in the flow vector.
1795                 //   The first bit always specifies whether the variable as such has been assigned while
1796                 //   the remaining bits contain this information for each of a struct's fields.
1797                 // </summary>
1798                 public readonly int Length;
1799
1800                 // <summary>
1801                 //   If this is a parameter of local variable.
1802                 // </summary>
1803                 public readonly bool IsParameter;
1804
1805                 public readonly LocalInfo LocalInfo;
1806                 public readonly int ParameterIndex;
1807
1808                 readonly VariableInfo Parent;
1809                 VariableInfo[] sub_info;
1810
1811                 protected VariableInfo (string name, Type type, int offset)
1812                 {
1813                         this.Name = name;
1814                         this.Offset = offset;
1815                         this.TypeInfo = TypeInfo.GetTypeInfo (type);
1816
1817                         Length = TypeInfo.TotalLength;
1818
1819                         Initialize ();
1820                 }
1821
1822                 protected VariableInfo (VariableInfo parent, TypeInfo type)
1823                 {
1824                         this.Name = parent.Name;
1825                         this.TypeInfo = type;
1826                         this.Offset = parent.Offset + type.Offset;
1827                         this.Parent = parent;
1828                         this.Length = type.TotalLength;
1829
1830                         this.IsParameter = parent.IsParameter;
1831                         this.LocalInfo = parent.LocalInfo;
1832                         this.ParameterIndex = parent.ParameterIndex;
1833
1834                         Initialize ();
1835                 }
1836
1837                 protected void Initialize ()
1838                 {
1839                         TypeInfo[] sub_fields = TypeInfo.SubStructInfo;
1840                         if (sub_fields != null) {
1841                                 sub_info = new VariableInfo [sub_fields.Length];
1842                                 for (int i = 0; i < sub_fields.Length; i++) {
1843                                         if (sub_fields [i] != null)
1844                                                 sub_info [i] = new VariableInfo (this, sub_fields [i]);
1845                                 }
1846                         } else
1847                                 sub_info = new VariableInfo [0];
1848                 }
1849
1850                 public VariableInfo (LocalInfo local_info, int offset)
1851                         : this (local_info.Name, local_info.VariableType, offset)
1852                 {
1853                         this.LocalInfo = local_info;
1854                         this.IsParameter = false;
1855                 }
1856
1857                 public VariableInfo (string name, Type type, int param_idx, int offset)
1858                         : this (name, type, offset)
1859                 {
1860                         this.ParameterIndex = param_idx;
1861                         this.IsParameter = true;
1862                 }
1863
1864                 public bool IsAssigned (EmitContext ec)
1865                 {
1866                         return !ec.DoFlowAnalysis ||
1867                                 ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
1868                                 ec.CurrentBranching.IsAssigned (this);
1869                 }
1870
1871                 public bool IsAssigned (EmitContext ec, Location loc)
1872                 {
1873                         if (IsAssigned (ec))
1874                                 return true;
1875
1876                         Report.Error (165, loc,
1877                                       "Use of unassigned local variable `" + Name + "'");
1878                         ec.CurrentBranching.SetAssigned (this);
1879                         return false;
1880                 }
1881
1882                 public bool IsAssigned (MyBitVector vector)
1883                 {
1884                         if (vector [Offset])
1885                                 return true;
1886
1887                         for (VariableInfo parent = Parent; parent != null; parent = parent.Parent)
1888                                 if (vector [parent.Offset])
1889                                         return true;
1890
1891                         // Return unless this is a struct.
1892                         if (!TypeInfo.IsStruct)
1893                                 return false;
1894
1895                         // Ok, so each field must be assigned.
1896                         for (int i = 0; i < TypeInfo.Length; i++) {
1897                                 if (!vector [Offset + i + 1])
1898                                         return false;
1899                         }
1900
1901                         // Ok, now check all fields which are structs.
1902                         for (int i = 0; i < sub_info.Length; i++) {
1903                                 VariableInfo sinfo = sub_info [i];
1904                                 if (sinfo == null)
1905                                         continue;
1906
1907                                 if (!sinfo.IsAssigned (vector))
1908                                         return false;
1909                         }
1910
1911                         vector [Offset] = true;
1912                         return true;
1913                 }
1914
1915                 public void SetAssigned (EmitContext ec)
1916                 {
1917                         if (ec.DoFlowAnalysis)
1918                                 ec.CurrentBranching.SetAssigned (this);
1919                 }
1920
1921                 public void SetAssigned (MyBitVector vector)
1922                 {
1923                         vector [Offset] = true;
1924                 }
1925
1926                 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
1927                 {
1928                         if (!ec.DoFlowAnalysis ||
1929                                 ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
1930                                 ec.CurrentBranching.IsFieldAssigned (this, name))
1931                                 return true;
1932
1933                         Report.Error (170, loc,
1934                                       "Use of possibly unassigned field `" + name + "'");
1935                         ec.CurrentBranching.SetFieldAssigned (this, name);
1936                         return false;
1937                 }
1938
1939                 public bool IsFieldAssigned (MyBitVector vector, string field_name)
1940                 {
1941                         int field_idx = TypeInfo.GetFieldIndex (field_name);
1942
1943                         if (field_idx == 0)
1944                                 return true;
1945
1946                         return vector [Offset + field_idx];
1947                 }
1948
1949                 public void SetFieldAssigned (EmitContext ec, string name)
1950                 {
1951                         if (ec.DoFlowAnalysis)
1952                                 ec.CurrentBranching.SetFieldAssigned (this, name);
1953                 }
1954
1955                 public void SetFieldAssigned (MyBitVector vector, string field_name)
1956                 {
1957                         int field_idx = TypeInfo.GetFieldIndex (field_name);
1958
1959                         if (field_idx == 0)
1960                                 return;
1961
1962                         vector [Offset + field_idx] = true;
1963                 }
1964
1965                 public VariableInfo GetSubStruct (string name)
1966                 {
1967                         TypeInfo type = TypeInfo.GetSubStruct (name);
1968
1969                         if (type == null)
1970                                 return null;
1971
1972                         return new VariableInfo (this, type);
1973                 }
1974
1975                 public override string ToString ()
1976                 {
1977                         return String.Format ("VariableInfo ({0}:{1}:{2}:{3}:{4})",
1978                                               Name, TypeInfo, Offset, Length, IsParameter);
1979                 }
1980         }
1981
1982         // <summary>
1983         //   This is used by the flow code to hold the `layout' of the flow vector for
1984         //   all locals and all parameters (ie. we create one instance of this class for the
1985         //   locals and another one for the params).
1986         // </summary>
1987         public class VariableMap {
1988                 // <summary>
1989                 //   The number of variables in the map.
1990                 // </summary>
1991                 public readonly int Count;
1992
1993                 // <summary>
1994                 //   Total length of the flow vector for this map.
1995                 // <summary>
1996                 public readonly int Length;
1997
1998                 VariableInfo[] map;
1999
2000                 public VariableMap (Parameters ip)
2001                 {
2002                         Count = ip != null ? ip.Count : 0;
2003                         
2004                         // Dont bother allocating anything!
2005                         if (Count == 0)
2006                                 return;
2007                         
2008                         Length = 0;
2009
2010                         for (int i = 0; i < Count; i++) {
2011                                 Parameter.Modifier mod = ip.ParameterModifier (i);
2012
2013                                 if ((mod & Parameter.Modifier.OUT) != Parameter.Modifier.OUT)
2014                                         continue;
2015
2016                                 // Dont allocate till we find an out var.
2017                                 if (map == null)
2018                                         map = new VariableInfo [Count];
2019
2020                                 map [i] = new VariableInfo (ip.ParameterName (i),
2021                                         TypeManager.GetElementType (ip.ParameterType (i)), i, Length);
2022
2023                                 Length += map [i].Length;
2024                         }
2025                 }
2026
2027                 public VariableMap (LocalInfo[] locals)
2028                         : this (null, locals)
2029                 { }
2030
2031                 public VariableMap (VariableMap parent, LocalInfo[] locals)
2032                 {
2033                         int offset = 0, start = 0;
2034                         if (parent != null && parent.map != null) {
2035                                 offset = parent.Length;
2036                                 start = parent.Count;
2037                         }
2038
2039                         Count = locals.Length + start;
2040                         
2041                         if (Count == 0)
2042                                 return;
2043                         
2044                         map = new VariableInfo [Count];
2045                         Length = offset;
2046
2047                         if (parent != null && parent.map != null) {
2048                                 parent.map.CopyTo (map, 0);
2049                         }
2050
2051                         for (int i = start; i < Count; i++) {
2052                                 LocalInfo li = locals [i-start];
2053
2054                                 if (li.VariableType == null)
2055                                         continue;
2056
2057                                 map [i] = li.VariableInfo = new VariableInfo (li, Length);
2058                                 Length += map [i].Length;
2059                         }
2060                 }
2061
2062                 // <summary>
2063                 //   Returns the VariableInfo for variable @index or null if we don't need to
2064                 //   compute assignment info for this variable.
2065                 // </summary>
2066                 public VariableInfo this [int index] {
2067                         get {
2068                                 if (map == null)
2069                                         return null;
2070                                 
2071                                 return map [index];
2072                         }
2073                 }
2074
2075                 public override string ToString ()
2076                 {
2077                         return String.Format ("VariableMap ({0}:{1})", Count, Length);
2078                 }
2079         }
2080
2081         // <summary>
2082         //   This is a special bit vector which can inherit from another bit vector doing a
2083         //   copy-on-write strategy.  The inherited vector may have a smaller size than the
2084         //   current one.
2085         // </summary>
2086         public class MyBitVector {
2087                 public readonly int Count;
2088                 public readonly MyBitVector InheritsFrom;
2089
2090                 bool is_dirty;
2091                 BitArray vector;
2092
2093                 public MyBitVector (int Count)
2094                         : this (null, Count)
2095                 { }
2096
2097                 public MyBitVector (MyBitVector InheritsFrom, int Count)
2098                 {
2099                         this.InheritsFrom = InheritsFrom;
2100                         this.Count = Count;
2101                 }
2102
2103                 // <summary>
2104                 //   Checks whether this bit vector has been modified.  After setting this to true,
2105                 //   we won't use the inherited vector anymore, but our own copy of it.
2106                 // </summary>
2107                 public bool IsDirty {
2108                         get { return is_dirty; }
2109
2110                         set {
2111                                 if (!is_dirty)
2112                                         initialize_vector ();
2113                         }
2114                 }
2115
2116                 // <summary>
2117                 //   Get/set bit `index' in the bit vector.
2118                 // </summary>
2119                 public bool this [int index]
2120                 {
2121                         get {
2122                                 if (index > Count)
2123                                         throw new ArgumentOutOfRangeException ();
2124
2125                                 // We're doing a "copy-on-write" strategy here; as long
2126                                 // as nobody writes to the array, we can use our parent's
2127                                 // copy instead of duplicating the vector.
2128
2129                                 if (vector != null)
2130                                         return vector [index];
2131                                 else if (InheritsFrom != null) {
2132                                         BitArray inherited = InheritsFrom.Vector;
2133
2134                                         if (index < inherited.Count)
2135                                                 return inherited [index];
2136                                         else
2137                                                 return false;
2138                                 } else
2139                                         return false;
2140                         }
2141
2142                         set {
2143                                 if (index > Count)
2144                                         throw new ArgumentOutOfRangeException ();
2145
2146                                 // Only copy the vector if we're actually modifying it.
2147
2148                                 if (this [index] != value) {
2149                                         initialize_vector ();
2150
2151                                         vector [index] = value;
2152                                 }
2153                         }
2154                 }
2155
2156                 // <summary>
2157                 //   If you explicitly convert the MyBitVector to a BitArray, you will get a deep
2158                 //   copy of the bit vector.
2159                 // </summary>
2160                 public static explicit operator BitArray (MyBitVector vector)
2161                 {
2162                         vector.initialize_vector ();
2163                         return vector.Vector;
2164                 }
2165
2166                 // <summary>
2167                 //   Performs an `or' operation on the bit vector.  The `new_vector' may have a
2168                 //   different size than the current one.
2169                 // </summary>
2170                 public void Or (MyBitVector new_vector)
2171                 {
2172                         // Treat null 'new_vector' as all false, just like the And() below
2173                         if (new_vector == null)
2174                                 return;
2175                         BitArray new_array = new_vector.Vector;
2176
2177                         initialize_vector ();
2178
2179                         int upper;
2180                         if (vector.Count < new_array.Count)
2181                                 upper = vector.Count;
2182                         else
2183                                 upper = new_array.Count;
2184
2185                         for (int i = 0; i < upper; i++)
2186                                 vector [i] = vector [i] | new_array [i];
2187                 }
2188
2189                 // <summary>
2190                 //   Perfonrms an `and' operation on the bit vector.  The `new_vector' may have
2191                 //   a different size than the current one.
2192                 // </summary>
2193                 public void And (MyBitVector new_vector)
2194                 {
2195                         BitArray new_array;
2196
2197                         if (new_vector != null)
2198                                 new_array = new_vector.Vector;
2199                         else
2200                                 new_array = new BitArray (Count, false);
2201
2202                         initialize_vector ();
2203
2204                         int lower, upper;
2205                         if (vector.Count < new_array.Count)
2206                                 lower = upper = vector.Count;
2207                         else {
2208                                 lower = new_array.Count;
2209                                 upper = vector.Count;
2210                         }
2211
2212                         for (int i = 0; i < lower; i++)
2213                                 vector [i] = vector [i] & new_array [i];
2214
2215                         for (int i = lower; i < upper; i++)
2216                                 vector [i] = false;
2217                 }
2218
2219                 public static void And (ref MyBitVector target, MyBitVector vector)
2220                 {
2221                         if (target != null)
2222                                 target.And (vector);
2223                         else
2224                                 target = vector.Clone ();
2225                 }
2226
2227                 public static void Or (ref MyBitVector target, MyBitVector vector)
2228                 {
2229                         if (target != null)
2230                                 target.Or (vector);
2231                         else
2232                                 target = vector.Clone ();
2233                 }
2234
2235                 // <summary>
2236                 //   This does a deep copy of the bit vector.
2237                 // </summary>
2238                 public MyBitVector Clone ()
2239                 {
2240                         MyBitVector retval = new MyBitVector (Count);
2241
2242                         retval.Vector = Vector;
2243
2244                         return retval;
2245                 }
2246
2247                 BitArray Vector {
2248                         get {
2249                                 if (vector != null)
2250                                         return vector;
2251                                 else if (!is_dirty && (InheritsFrom != null))
2252                                         return InheritsFrom.Vector;
2253
2254                                 initialize_vector ();
2255
2256                                 return vector;
2257                         }
2258
2259                         set {
2260                                 initialize_vector ();
2261
2262                                 for (int i = 0; i < System.Math.Min (vector.Count, value.Count); i++)
2263                                         vector [i] = value [i];
2264                         }
2265                 }
2266
2267                 void initialize_vector ()
2268                 {
2269                         if (vector != null)
2270                                 return;
2271                         
2272                         vector = new BitArray (Count, false);
2273                         if (InheritsFrom != null)
2274                                 Vector = InheritsFrom.Vector;
2275
2276                         is_dirty = true;
2277                 }
2278
2279                 public override string ToString ()
2280                 {
2281                         StringBuilder sb = new StringBuilder ("{");
2282
2283                         BitArray vector = Vector;
2284                         if (!IsDirty)
2285                                 sb.Append ("=");
2286                         for (int i = 0; i < vector.Count; i++) {
2287                                 sb.Append (vector [i] ? "1" : "0");
2288                         }
2289                         
2290                         sb.Append ("}");
2291                         return sb.ToString ();
2292                 }
2293         }
2294 }