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