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