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