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