2 // flowanalyis.cs: The control flow analysis code
5 // Martin Baulig (martin@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Diagnostics;
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.
23 public abstract class FlowBranching
26 // The type of a FlowBranching.
28 public enum BranchingType {
29 // Normal (conditional or toplevel) block.
49 // The type of one sibling of a branching.
51 public enum SiblingType {
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
65 public enum FlowReturns {
68 // It can never return.
71 // This means that the block contains a conditional return statement
75 // The code always returns, ie. there's an unconditional return / break
80 public sealed class Reachability
82 FlowReturns returns, breaks, throws, barrier, reachable;
84 public FlowReturns Returns {
85 get { return returns; }
87 public FlowReturns Breaks {
88 get { return breaks; }
90 public FlowReturns Throws {
91 get { return throws; }
93 public FlowReturns Barrier {
94 get { return barrier; }
96 public FlowReturns Reachable {
97 get { return reachable; }
100 public Reachability (FlowReturns returns, FlowReturns breaks,
101 FlowReturns throws, FlowReturns barrier)
103 this.returns = returns;
104 this.breaks = breaks;
105 this.throws = throws;
106 this.barrier = barrier;
111 public Reachability Clone ()
113 Reachability cloned = new Reachability (returns, breaks, throws, barrier);
114 cloned.reachable = reachable;
118 public static void And (ref Reachability a, Reachability b, bool do_break)
126 // `break' does not "break" in a Switch or a LoopBlock
128 bool a_breaks = do_break && a.AlwaysBreaks;
129 bool b_breaks = do_break && b.AlwaysBreaks;
131 bool a_has_barrier, b_has_barrier;
134 // This is the normal case: the code following a barrier
135 // cannot be reached.
137 a_has_barrier = a.AlwaysHasBarrier;
138 b_has_barrier = b.AlwaysHasBarrier;
141 // Special case for Switch and LoopBlocks: we can reach the
142 // code after the barrier via the `break'.
144 a_has_barrier = !a.AlwaysBreaks && a.AlwaysHasBarrier;
145 b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
148 bool a_unreachable = a_breaks || a.AlwaysThrows || a_has_barrier;
149 bool b_unreachable = b_breaks || b.AlwaysThrows || b_has_barrier;
152 // Do all code paths always return ?
154 if (a.AlwaysReturns) {
155 if (b.AlwaysReturns || b_unreachable)
156 a.returns = FlowReturns.Always;
158 a.returns = FlowReturns.Sometimes;
159 } else if (b.AlwaysReturns) {
160 if (a.AlwaysReturns || a_unreachable)
161 a.returns = FlowReturns.Always;
163 a.returns = FlowReturns.Sometimes;
164 } else if (!a.MayReturn) {
166 a.returns = FlowReturns.Sometimes;
168 a.returns = FlowReturns.Never;
169 } else if (!b.MayReturn) {
171 a.returns = FlowReturns.Sometimes;
173 a.returns = FlowReturns.Never;
176 a.breaks = AndFlowReturns (a.breaks, b.breaks);
177 a.throws = AndFlowReturns (a.throws, b.throws);
178 a.barrier = AndFlowReturns (a.barrier, b.barrier);
180 a.reachable = AndFlowReturns (a.reachable, b.reachable);
183 public static Reachability Never ()
185 return new Reachability (
186 FlowReturns.Never, FlowReturns.Never,
187 FlowReturns.Never, FlowReturns.Never);
192 if ((returns == FlowReturns.Always) || (breaks == FlowReturns.Always) ||
193 (throws == FlowReturns.Always) || (barrier == FlowReturns.Always))
194 reachable = FlowReturns.Never;
195 else if ((returns == FlowReturns.Never) && (breaks == FlowReturns.Never) &&
196 (throws == FlowReturns.Never) && (barrier == FlowReturns.Never))
197 reachable = FlowReturns.Always;
199 reachable = FlowReturns.Sometimes;
202 public bool AlwaysBreaks {
203 get { return breaks == FlowReturns.Always; }
206 public bool MayBreak {
207 get { return breaks != FlowReturns.Never; }
210 public bool AlwaysReturns {
211 get { return returns == FlowReturns.Always; }
214 public bool MayReturn {
215 get { return returns != FlowReturns.Never; }
218 public bool AlwaysThrows {
219 get { return throws == FlowReturns.Always; }
222 public bool MayThrow {
223 get { return throws != FlowReturns.Never; }
226 public bool AlwaysHasBarrier {
227 get { return barrier == FlowReturns.Always; }
230 public bool MayHaveBarrier {
231 get { return barrier != FlowReturns.Never; }
234 public bool IsUnreachable {
235 get { return reachable == FlowReturns.Never; }
238 public void SetReturns ()
240 returns = FlowReturns.Always;
244 public void SetReturnsSometimes ()
246 returns = FlowReturns.Sometimes;
250 public void SetBreaks ()
252 breaks = FlowReturns.Always;
256 public void ResetBreaks ()
258 breaks = FlowReturns.Never;
262 public void SetThrows ()
264 throws = FlowReturns.Always;
268 public void SetBarrier ()
270 barrier = FlowReturns.Always;
274 static string ShortName (FlowReturns returns)
277 case FlowReturns.Never:
279 case FlowReturns.Sometimes:
286 public override string ToString ()
288 return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
289 ShortName (returns), ShortName (breaks),
290 ShortName (throws), ShortName (barrier),
291 ShortName (reachable));
295 public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
298 case BranchingType.Exception:
299 return new FlowBranchingException (parent, type, block, loc);
301 case BranchingType.Switch:
302 return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
304 case BranchingType.SwitchSection:
305 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
307 case BranchingType.Block:
308 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
311 return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
316 // The type of this flow branching.
318 public readonly BranchingType Type;
321 // The block this branching is contained in. This may be null if it's not
322 // a top-level block and it doesn't declare any local variables.
324 public readonly Block Block;
327 // The parent of this branching or null if this is the top-block.
329 public readonly FlowBranching Parent;
332 // Start-Location of this flow branching.
334 public readonly Location Location;
337 // If this is an infinite loop.
339 public bool Infinite;
344 VariableMap param_map, local_map;
346 static int next_id = 0;
350 // Performs an `And' operation on the FlowReturns status
351 // (for instance, a block only returns Always if all its siblings
354 public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
356 if (a == FlowReturns.Undefined)
360 case FlowReturns.Never:
361 if (b == FlowReturns.Never)
362 return FlowReturns.Never;
364 return FlowReturns.Sometimes;
366 case FlowReturns.Sometimes:
367 return FlowReturns.Sometimes;
369 case FlowReturns.Always:
370 if (b == FlowReturns.Always)
371 return FlowReturns.Always;
373 return FlowReturns.Sometimes;
376 throw new ArgumentException ();
381 // The vector contains a BitArray with information about which local variables
382 // and parameters are already initialized at the current code position.
384 public class UsageVector {
386 // The type of this branching.
388 public readonly SiblingType Type;
391 // Start location of this branching.
393 public readonly Location Location;
396 // If this is true, then the usage vector has been modified and must be
397 // merged when we're done with this branching.
402 // The number of parameters in this block.
404 public readonly int CountParameters;
407 // The number of locals in this block.
409 public readonly int CountLocals;
412 // If not null, then we inherit our state from this vector and do a
413 // copy-on-write. If null, then we're the first sibling in a top-level
414 // block and inherit from the empty vector.
416 public readonly UsageVector InheritsFrom;
419 // This is used to construct a list of UsageVector's.
421 public UsageVector Next;
426 MyBitVector locals, parameters;
427 Reachability reachability;
430 static int next_id = 0;
434 // Normally, you should not use any of these constructors.
436 public UsageVector (SiblingType type, UsageVector parent, Location loc, int num_params, int num_locals)
440 this.InheritsFrom = parent;
441 this.CountParameters = num_params;
442 this.CountLocals = num_locals;
444 if (parent != null) {
445 locals = new MyBitVector (parent.locals, CountLocals);
447 parameters = new MyBitVector (parent.parameters, num_params);
449 reachability = parent.Reachability.Clone ();
451 locals = new MyBitVector (null, CountLocals);
453 parameters = new MyBitVector (null, num_params);
455 reachability = Reachability.Never ();
461 public UsageVector (SiblingType type, UsageVector parent, Location loc)
462 : this (type, parent, loc, parent.CountParameters, parent.CountLocals)
465 public UsageVector (MyBitVector parameters, MyBitVector locals,
466 Reachability reachability, Location loc)
468 this.Type = SiblingType.Block;
471 this.reachability = reachability;
472 this.parameters = parameters;
473 this.locals = locals;
479 // This does a deep copy of the usage vector.
481 public UsageVector Clone ()
483 UsageVector retval = new UsageVector (Type, null, Location, CountParameters, CountLocals);
485 retval.locals = locals.Clone ();
486 if (parameters != null)
487 retval.parameters = parameters.Clone ();
488 retval.reachability = reachability.Clone ();
493 public bool IsAssigned (VariableInfo var)
495 if (!var.IsParameter && Reachability.AlwaysBreaks)
498 return var.IsAssigned (var.IsParameter ? parameters : locals);
501 public void SetAssigned (VariableInfo var)
503 if (!var.IsParameter && Reachability.AlwaysBreaks)
507 var.SetAssigned (var.IsParameter ? parameters : locals);
510 public bool IsFieldAssigned (VariableInfo var, string name)
512 if (!var.IsParameter && Reachability.AlwaysBreaks)
515 return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
518 public void SetFieldAssigned (VariableInfo var, string name)
520 if (!var.IsParameter && Reachability.AlwaysBreaks)
524 var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
527 public Reachability Reachability {
533 public void Return ()
535 if (!reachability.IsUnreachable) {
537 reachability.SetReturns ();
543 if (!reachability.IsUnreachable) {
545 reachability.SetBreaks ();
551 if (!reachability.IsUnreachable) {
553 reachability.SetThrows ();
559 if (!reachability.IsUnreachable) {
561 reachability.SetBarrier ();
566 // Merges a child branching.
568 public UsageVector MergeChild (FlowBranching branching)
570 UsageVector result = branching.Merge ();
572 Report.Debug (2, " MERGING CHILD", this, IsDirty,
573 result.ParameterVector, result.LocalVector,
574 result.Reachability, Type);
576 reachability = result.Reachability;
578 if (branching.Type == BranchingType.LoopBlock) {
579 bool may_leave_loop = reachability.MayBreak;
580 reachability.ResetBreaks ();
582 if (branching.Infinite && !may_leave_loop) {
583 if (reachability.Returns == FlowReturns.Sometimes) {
584 // If we're an infinite loop and do not break,
585 // the code after the loop can never be reached.
586 // However, if we may return from the loop,
587 // then we do always return (or stay in the
589 reachability.SetReturns ();
592 reachability.SetBarrier ();
594 if (reachability.Returns == FlowReturns.Always) {
595 // We're either finite or we may leave the loop.
596 reachability.SetReturnsSometimes ();
599 } else if (branching.Type == BranchingType.Switch)
600 reachability.ResetBreaks ();
603 // We've now either reached the point after the branching or we will
604 // never get there since we always return or always throw an exception.
606 // If we can reach the point after the branching, mark all locals and
607 // parameters as initialized which have been initialized in all branches
608 // we need to look at (see above).
611 if ((Type == SiblingType.SwitchSection) && !reachability.IsUnreachable) {
612 Report.Error (163, Location,
613 "Control cannot fall through from one " +
614 "case label to another");
618 if (result.LocalVector != null)
619 locals.Or (result.LocalVector);
621 if (result.ParameterVector != null)
622 parameters.Or (result.ParameterVector);
624 Report.Debug (2, " MERGING CHILD DONE", this);
631 protected void MergeFinally (FlowBranching branching, UsageVector f_origins,
632 MyBitVector f_params)
634 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
635 MyBitVector temp_params = f_params.Clone ();
636 temp_params.Or (vector.Parameters);
638 branching.CheckOutParameters (temp_params, branching.Location);
642 public void MergeFinally (FlowBranching branching, UsageVector f_vector,
643 UsageVector f_origins)
645 if (parameters != null) {
646 if (f_vector != null) {
647 MergeFinally (branching, f_origins, f_vector.Parameters);
648 MyBitVector.Or (ref parameters, f_vector.ParameterVector);
650 MergeFinally (branching, f_origins, parameters);
653 if (f_vector != null)
654 MyBitVector.Or (ref locals, f_vector.LocalVector);
658 // Tells control flow analysis that the current code position may be reached with
659 // a forward jump from any of the origins listed in `origin_vectors' which is a
660 // list of UsageVectors.
662 // This is used when resolving forward gotos - in the following example, the
663 // variable `a' is uninitialized in line 8 becase this line may be reached via
664 // the goto in line 4:
674 // 8 Console.WriteLine (a);
677 public void MergeJumpOrigins (ICollection origin_vectors)
679 Report.Debug (1, " MERGING JUMP ORIGINS", this);
681 reachability = Reachability.Never ();
683 if (origin_vectors == null)
688 foreach (UsageVector vector in origin_vectors) {
689 Report.Debug (1, " MERGING JUMP ORIGIN", vector);
692 locals.Or (vector.locals);
693 if (parameters != null)
694 parameters.Or (vector.parameters);
697 locals.And (vector.locals);
698 if (parameters != null)
699 parameters.And (vector.parameters);
702 Reachability.And (ref reachability, vector.Reachability, true);
705 Report.Debug (1, " MERGING JUMP ORIGINS DONE", this);
709 // This is used at the beginning of a finally block if there were
710 // any return statements in the try block or one of the catch blocks.
712 public void MergeFinallyOrigins (ICollection finally_vectors)
714 Report.Debug (1, " MERGING FINALLY ORIGIN", this);
717 RealBreaks = FlowReturns.Never;
720 foreach (UsageVector vector in finally_vectors) {
721 Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
723 if (parameters != null)
724 parameters.And (vector.parameters);
727 RealBreaks = AndFlowReturns (Breaks, vector.Breaks);
733 Report.Debug (1, " MERGING FINALLY ORIGIN DONE", this);
736 public void CheckOutParameters (FlowBranching branching)
738 if (parameters != null)
739 branching.CheckOutParameters (parameters, branching.Location);
743 // Performs an `or' operation on the locals and the parameters.
745 public void Or (UsageVector new_vector)
748 locals.Or (new_vector.locals);
749 if (parameters != null)
750 parameters.Or (new_vector.parameters);
754 // Performs an `and' operation on the locals.
756 public void AndLocals (UsageVector new_vector)
759 locals.And (new_vector.locals);
762 public bool HasParameters {
764 return parameters != null;
768 public bool HasLocals {
770 return locals != null;
775 // Returns a deep copy of the parameters.
777 public MyBitVector Parameters {
779 if (parameters != null)
780 return parameters.Clone ();
787 // Returns a deep copy of the locals.
789 public MyBitVector Locals {
791 return locals.Clone ();
795 public MyBitVector ParameterVector {
801 public MyBitVector LocalVector {
811 public override string ToString ()
813 StringBuilder sb = new StringBuilder ();
815 sb.Append ("Vector (");
820 sb.Append (reachability);
821 if (parameters != null) {
823 sb.Append (parameters);
829 return sb.ToString ();
834 // Creates a new flow branching which is contained in `parent'.
835 // You should only pass non-null for the `block' argument if this block
836 // introduces any new variables - in this case, we need to create a new
837 // usage vector with a different size than our parent's one.
839 protected FlowBranching (FlowBranching parent, BranchingType type, SiblingType stype,
840 Block block, Location loc)
850 param_map = Block.ParameterMap;
851 local_map = Block.LocalMap;
853 UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
854 vector = new UsageVector (stype, parent_vector, loc, param_map.Length, local_map.Length);
856 param_map = Parent.param_map;
857 local_map = Parent.local_map;
858 vector = new UsageVector (stype, Parent.CurrentUsageVector, loc);
864 public abstract UsageVector CurrentUsageVector {
869 // Creates a sibling of the current usage vector.
871 public virtual void CreateSibling (SiblingType type)
873 AddSibling (new UsageVector (type, Parent.CurrentUsageVector, Location));
875 Report.Debug (1, " CREATED SIBLING", CurrentUsageVector);
878 protected abstract void AddSibling (UsageVector uv);
880 public abstract void Label (ArrayList origin_vectors);
883 // Check whether all `out' parameters have been assigned.
885 public void CheckOutParameters (MyBitVector parameters, Location loc)
887 for (int i = 0; i < param_map.Count; i++) {
888 VariableInfo var = param_map [i];
893 if (var.IsAssigned (parameters))
896 Report.Error (177, loc, "The out parameter `" +
897 param_map.VariableNames [i] + "' must be " +
898 "assigned before control leave the current method.");
902 protected UsageVector Merge (UsageVector sibling_list)
904 if (sibling_list.Next == null)
907 MyBitVector locals = null;
908 MyBitVector parameters = null;
910 Reachability reachability = null;
912 Report.Debug (2, " MERGING SIBLINGS", this, Name);
914 for (UsageVector child = sibling_list; child != null; child = child.Next) {
915 bool do_break = (Type != BranchingType.Switch) &&
916 (Type != BranchingType.LoopBlock);
918 Report.Debug (2, " MERGING SIBLING ", child,
919 child.Locals, child.Parameters,
920 reachability, child.Reachability, do_break);
922 Reachability.And (ref reachability, child.Reachability, do_break);
924 // A local variable is initialized after a flow branching if it
925 // has been initialized in all its branches which do neither
926 // always return or always throw an exception.
928 // If a branch may return, but does not always return, then we
929 // can treat it like a never-returning branch here: control will
930 // only reach the code position after the branching if we did not
933 // It's important to distinguish between always and sometimes
934 // returning branches here:
937 // 2 if (something) {
941 // 6 Console.WriteLine (a);
943 // The if block in lines 3-4 always returns, so we must not look
944 // at the initialization of `a' in line 4 - thus it'll still be
945 // uninitialized in line 6.
947 // On the other hand, the following is allowed:
954 // 6 Console.WriteLine (a);
956 // Here, `a' is initialized in line 3 and we must not look at
957 // line 5 since it always returns.
959 bool do_break_2 = (child.Type != SiblingType.Block) &&
960 (child.Type != SiblingType.SwitchSection);
961 bool unreachable = (do_break_2 && child.Reachability.AlwaysBreaks) ||
962 child.Reachability.AlwaysThrows ||
963 child.Reachability.AlwaysReturns ||
964 child.Reachability.AlwaysHasBarrier;
966 Report.Debug (2, " MERGING SIBLING #1", reachability,
967 Type, child.Type, child.Reachability.IsUnreachable,
968 do_break_2, unreachable);
971 MyBitVector.And (ref locals, child.LocalVector);
973 // An `out' parameter must be assigned in all branches which do
974 // not always throw an exception.
975 if ((child.Type != SiblingType.Catch) &&
976 (child.ParameterVector != null) && !child.Reachability.AlwaysThrows)
977 MyBitVector.And (ref parameters, child.ParameterVector);
980 if (reachability == null)
981 reachability = Reachability.Never ();
983 Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
984 reachability, Infinite);
986 return new UsageVector (parameters, locals, reachability, Location);
989 protected abstract UsageVector Merge ();
992 // Merge a child branching.
994 public Reachability MergeChild (FlowBranching child)
996 UsageVector result = CurrentUsageVector.MergeChild (child);
998 return result.Reachability;
1002 // Does the toplevel merging.
1004 public Reachability MergeTopBlock ()
1006 if ((Type != BranchingType.Block) || (Block == null))
1007 throw new NotSupportedException ();
1009 UsageVector vector = new UsageVector (
1010 SiblingType.Conditional, null, Location, param_map.Length, local_map.Length);
1012 UsageVector result = vector.MergeChild (this);
1014 Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
1016 if (vector.Reachability.Throws != FlowReturns.Always)
1017 CheckOutParameters (vector.Parameters, Location);
1019 return result.Reachability;
1022 public virtual bool InTryBlock ()
1025 return Parent.InTryBlock ();
1030 public virtual void AddFinallyVector (UsageVector vector)
1033 Parent.AddFinallyVector (vector);
1035 throw new NotSupportedException ();
1038 public bool IsAssigned (VariableInfo vi)
1040 return CurrentUsageVector.IsAssigned (vi);
1043 public bool IsFieldAssigned (VariableInfo vi, string field_name)
1045 if (CurrentUsageVector.IsAssigned (vi))
1048 return CurrentUsageVector.IsFieldAssigned (vi, field_name);
1051 public void SetAssigned (VariableInfo vi)
1053 CurrentUsageVector.SetAssigned (vi);
1056 public void SetFieldAssigned (VariableInfo vi, string name)
1058 CurrentUsageVector.SetFieldAssigned (vi, name);
1061 public override string ToString ()
1063 StringBuilder sb = new StringBuilder ();
1064 sb.Append (GetType ());
1070 if (Block != null) {
1072 sb.Append (Block.ID);
1074 sb.Append (Block.StartLocation);
1077 // sb.Append (Siblings.Length);
1078 // sb.Append (" - ");
1079 sb.Append (CurrentUsageVector);
1081 return sb.ToString ();
1084 public string Name {
1086 return String.Format ("{0} ({1}:{2}:{3})",
1087 GetType (), id, Type, Location);
1092 public class FlowBranchingBlock : FlowBranching
1094 UsageVector sibling_list = null;
1096 public FlowBranchingBlock (FlowBranching parent, BranchingType type, SiblingType stype,
1097 Block block, Location loc)
1098 : base (parent, type, stype, block, loc)
1101 public override UsageVector CurrentUsageVector {
1102 get { return sibling_list; }
1105 protected override void AddSibling (UsageVector sibling)
1107 sibling.Next = sibling_list;
1108 sibling_list = sibling;
1111 public override void Label (ArrayList origin_vectors)
1113 if (!CurrentUsageVector.Reachability.IsUnreachable) {
1114 if (origin_vectors == null)
1115 origin_vectors = new ArrayList (1);
1116 origin_vectors.Add (CurrentUsageVector.Clone ());
1119 CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1122 protected override UsageVector Merge ()
1124 return Merge (sibling_list);
1128 public class FlowBranchingException : FlowBranching
1130 UsageVector current_vector;
1131 UsageVector try_vector;
1132 UsageVector catch_vectors;
1133 UsageVector finally_vector;
1134 UsageVector finally_origins;
1136 public FlowBranchingException (FlowBranching parent, BranchingType type, Block block, Location loc)
1137 : base (parent, type, SiblingType.Try, block, loc)
1140 protected override void AddSibling (UsageVector sibling)
1142 if (sibling.Type == SiblingType.Try) {
1143 try_vector = sibling;
1144 sibling.Next = catch_vectors;
1145 catch_vectors = sibling;
1146 } else if (sibling.Type == SiblingType.Catch) {
1147 sibling.Next = catch_vectors;
1148 catch_vectors = sibling;
1149 } else if (sibling.Type == SiblingType.Finally) {
1150 // sibling.MergeFinallyOrigins (finally_vectors);
1151 finally_vector = sibling;
1153 throw new InvalidOperationException ();
1155 current_vector = sibling;
1158 public override UsageVector CurrentUsageVector {
1159 get { return current_vector; }
1162 public override bool InTryBlock ()
1167 public override void AddFinallyVector (UsageVector vector)
1169 vector = vector.Clone ();
1170 vector.Next = finally_origins;
1171 finally_origins = vector;
1174 public override void Label (ArrayList origin_vectors)
1176 CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1179 protected override UsageVector Merge ()
1181 UsageVector vector = Merge (catch_vectors);
1183 vector.MergeFinally (this, finally_vector, finally_origins);
1190 // This is used by the flow analysis code to keep track of the type of local variables
1193 // The flow code uses a BitVector to keep track of whether a variable has been assigned
1194 // or not. This is easy for fundamental types (int, char etc.) or reference types since
1195 // you can only assign the whole variable as such.
1197 // For structs, we also need to keep track of all its fields. To do this, we allocate one
1198 // bit for the struct itself (it's used if you assign/access the whole struct) followed by
1199 // one bit for each of its fields.
1201 // This class computes this `layout' for each type.
1203 public class TypeInfo
1205 public readonly Type Type;
1208 // Total number of bits a variable of this type consumes in the flow vector.
1210 public readonly int TotalLength;
1213 // Number of bits the simple fields of a variable of this type consume
1214 // in the flow vector.
1216 public readonly int Length;
1219 // This is only used by sub-structs.
1221 public readonly int Offset;
1224 // If this is a struct.
1226 public readonly bool IsStruct;
1229 // If this is a struct, all fields which are structs theirselves.
1231 public TypeInfo[] SubStructInfo;
1233 protected readonly StructInfo struct_info;
1234 private static Hashtable type_hash = new Hashtable ();
1236 public static TypeInfo GetTypeInfo (Type type)
1238 TypeInfo info = (TypeInfo) type_hash [type];
1242 info = new TypeInfo (type);
1243 type_hash.Add (type, info);
1247 public static TypeInfo GetTypeInfo (TypeContainer tc)
1249 TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
1253 info = new TypeInfo (tc);
1254 type_hash.Add (tc.TypeBuilder, info);
1258 private TypeInfo (Type type)
1262 struct_info = StructInfo.GetStructInfo (type);
1263 if (struct_info != null) {
1264 Length = struct_info.Length;
1265 TotalLength = struct_info.TotalLength;
1266 SubStructInfo = struct_info.StructFields;
1275 private TypeInfo (TypeContainer tc)
1277 this.Type = tc.TypeBuilder;
1279 struct_info = StructInfo.GetStructInfo (tc);
1280 if (struct_info != null) {
1281 Length = struct_info.Length;
1282 TotalLength = struct_info.TotalLength;
1283 SubStructInfo = struct_info.StructFields;
1292 protected TypeInfo (StructInfo struct_info, int offset)
1294 this.struct_info = struct_info;
1295 this.Offset = offset;
1296 this.Length = struct_info.Length;
1297 this.TotalLength = struct_info.TotalLength;
1298 this.SubStructInfo = struct_info.StructFields;
1299 this.Type = struct_info.Type;
1300 this.IsStruct = true;
1303 public int GetFieldIndex (string name)
1305 if (struct_info == null)
1308 return struct_info [name];
1311 public TypeInfo GetSubStruct (string name)
1313 if (struct_info == null)
1316 return struct_info.GetStructField (name);
1320 // A struct's constructor must always assign all fields.
1321 // This method checks whether it actually does so.
1323 public bool IsFullyInitialized (FlowBranching branching, VariableInfo vi, Location loc)
1325 if (struct_info == null)
1329 for (int i = 0; i < struct_info.Count; i++) {
1330 FieldInfo field = struct_info.Fields [i];
1332 if (!branching.IsFieldAssigned (vi, field.Name)) {
1333 Report.Error (171, loc,
1334 "Field `" + TypeManager.CSharpName (Type) +
1335 "." + field.Name + "' must be fully initialized " +
1336 "before control leaves the constructor");
1344 public override string ToString ()
1346 return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
1347 Type, Offset, Length, TotalLength);
1350 protected class StructInfo {
1351 public readonly Type Type;
1352 public readonly FieldInfo[] Fields;
1353 public readonly TypeInfo[] StructFields;
1354 public readonly int Count;
1355 public readonly int CountPublic;
1356 public readonly int CountNonPublic;
1357 public readonly int Length;
1358 public readonly int TotalLength;
1359 public readonly bool HasStructFields;
1361 private static Hashtable field_type_hash = new Hashtable ();
1362 private Hashtable struct_field_hash;
1363 private Hashtable field_hash;
1365 protected bool InTransit = false;
1367 // Private constructor. To save memory usage, we only need to create one instance
1368 // of this class per struct type.
1369 private StructInfo (Type type)
1373 field_type_hash.Add (type, this);
1375 if (type is TypeBuilder) {
1376 TypeContainer tc = TypeManager.LookupTypeContainer (type);
1378 ArrayList fields = tc.Fields;
1380 ArrayList public_fields = new ArrayList ();
1381 ArrayList non_public_fields = new ArrayList ();
1383 if (fields != null) {
1384 foreach (Field field in fields) {
1385 if ((field.ModFlags & Modifiers.STATIC) != 0)
1387 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
1388 public_fields.Add (field.FieldBuilder);
1390 non_public_fields.Add (field.FieldBuilder);
1394 CountPublic = public_fields.Count;
1395 CountNonPublic = non_public_fields.Count;
1396 Count = CountPublic + CountNonPublic;
1398 Fields = new FieldInfo [Count];
1399 public_fields.CopyTo (Fields, 0);
1400 non_public_fields.CopyTo (Fields, CountPublic);
1402 FieldInfo[] public_fields = type.GetFields (
1403 BindingFlags.Instance|BindingFlags.Public);
1404 FieldInfo[] non_public_fields = type.GetFields (
1405 BindingFlags.Instance|BindingFlags.NonPublic);
1407 CountPublic = public_fields.Length;
1408 CountNonPublic = non_public_fields.Length;
1409 Count = CountPublic + CountNonPublic;
1411 Fields = new FieldInfo [Count];
1412 public_fields.CopyTo (Fields, 0);
1413 non_public_fields.CopyTo (Fields, CountPublic);
1416 struct_field_hash = new Hashtable ();
1417 field_hash = new Hashtable ();
1420 StructFields = new TypeInfo [Count];
1421 StructInfo[] sinfo = new StructInfo [Count];
1425 for (int i = 0; i < Count; i++) {
1426 FieldInfo field = (FieldInfo) Fields [i];
1428 sinfo [i] = GetStructInfo (field.FieldType);
1429 if (sinfo [i] == null)
1430 field_hash.Add (field.Name, ++Length);
1431 else if (sinfo [i].InTransit) {
1432 Report.Error (523, String.Format (
1433 "Struct member '{0}.{1}' of type '{2}' causes " +
1434 "a cycle in the structure layout",
1435 type, field.Name, sinfo [i].Type));
1443 TotalLength = Length + 1;
1444 for (int i = 0; i < Count; i++) {
1445 FieldInfo field = (FieldInfo) Fields [i];
1447 if (sinfo [i] == null)
1450 field_hash.Add (field.Name, TotalLength);
1452 HasStructFields = true;
1453 StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
1454 struct_field_hash.Add (field.Name, StructFields [i]);
1455 TotalLength += sinfo [i].TotalLength;
1459 public int this [string name] {
1461 if (field_hash.Contains (name))
1462 return (int) field_hash [name];
1468 public TypeInfo GetStructField (string name)
1470 return (TypeInfo) struct_field_hash [name];
1473 public static StructInfo GetStructInfo (Type type)
1475 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
1476 TypeManager.IsBuiltinType (type))
1479 StructInfo info = (StructInfo) field_type_hash [type];
1483 return new StructInfo (type);
1486 public static StructInfo GetStructInfo (TypeContainer tc)
1488 StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
1492 return new StructInfo (tc.TypeBuilder);
1498 // This is used by the flow analysis code to store information about a single local variable
1499 // or parameter. Depending on the variable's type, we need to allocate one or more elements
1500 // in the BitVector - if it's a fundamental or reference type, we just need to know whether
1501 // it has been assigned or not, but for structs, we need this information for each of its fields.
1503 public class VariableInfo {
1504 public readonly string Name;
1505 public readonly TypeInfo TypeInfo;
1508 // The bit offset of this variable in the flow vector.
1510 public readonly int Offset;
1513 // The number of bits this variable needs in the flow vector.
1514 // The first bit always specifies whether the variable as such has been assigned while
1515 // the remaining bits contain this information for each of a struct's fields.
1517 public readonly int Length;
1520 // If this is a parameter of local variable.
1522 public readonly bool IsParameter;
1524 public readonly LocalInfo LocalInfo;
1525 public readonly int ParameterIndex;
1527 readonly VariableInfo Parent;
1528 VariableInfo[] sub_info;
1530 protected VariableInfo (string name, Type type, int offset)
1533 this.Offset = offset;
1534 this.TypeInfo = TypeInfo.GetTypeInfo (type);
1536 Length = TypeInfo.TotalLength;
1541 protected VariableInfo (VariableInfo parent, TypeInfo type)
1543 this.Name = parent.Name;
1544 this.TypeInfo = type;
1545 this.Offset = parent.Offset + type.Offset;
1546 this.Parent = parent;
1547 this.Length = type.TotalLength;
1549 this.IsParameter = parent.IsParameter;
1550 this.LocalInfo = parent.LocalInfo;
1551 this.ParameterIndex = parent.ParameterIndex;
1556 protected void Initialize ()
1558 TypeInfo[] sub_fields = TypeInfo.SubStructInfo;
1559 if (sub_fields != null) {
1560 sub_info = new VariableInfo [sub_fields.Length];
1561 for (int i = 0; i < sub_fields.Length; i++) {
1562 if (sub_fields [i] != null)
1563 sub_info [i] = new VariableInfo (this, sub_fields [i]);
1566 sub_info = new VariableInfo [0];
1569 public VariableInfo (LocalInfo local_info, int offset)
1570 : this (local_info.Name, local_info.VariableType, offset)
1572 this.LocalInfo = local_info;
1573 this.IsParameter = false;
1576 public VariableInfo (string name, Type type, int param_idx, int offset)
1577 : this (name, type, offset)
1579 this.ParameterIndex = param_idx;
1580 this.IsParameter = true;
1583 public bool IsAssigned (EmitContext ec)
1585 return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (this);
1588 public bool IsAssigned (EmitContext ec, Location loc)
1590 if (IsAssigned (ec))
1593 Report.Error (165, loc,
1594 "Use of unassigned local variable `" + Name + "'");
1595 ec.CurrentBranching.SetAssigned (this);
1599 public bool IsAssigned (MyBitVector vector)
1601 if (vector [Offset])
1604 for (VariableInfo parent = Parent; parent != null; parent = parent.Parent)
1605 if (vector [parent.Offset])
1608 // Return unless this is a struct.
1609 if (!TypeInfo.IsStruct)
1612 // Ok, so each field must be assigned.
1613 for (int i = 0; i < TypeInfo.Length; i++) {
1614 if (!vector [Offset + i + 1])
1618 // Ok, now check all fields which are structs.
1619 for (int i = 0; i < sub_info.Length; i++) {
1620 VariableInfo sinfo = sub_info [i];
1624 if (!sinfo.IsAssigned (vector))
1628 vector [Offset] = true;
1632 public void SetAssigned (EmitContext ec)
1634 if (ec.DoFlowAnalysis)
1635 ec.CurrentBranching.SetAssigned (this);
1638 public void SetAssigned (MyBitVector vector)
1640 vector [Offset] = true;
1643 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
1645 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsFieldAssigned (this, name))
1648 Report.Error (170, loc,
1649 "Use of possibly unassigned field `" + name + "'");
1650 ec.CurrentBranching.SetFieldAssigned (this, name);
1654 public bool IsFieldAssigned (MyBitVector vector, string field_name)
1656 int field_idx = TypeInfo.GetFieldIndex (field_name);
1661 return vector [Offset + field_idx];
1664 public void SetFieldAssigned (EmitContext ec, string name)
1666 if (ec.DoFlowAnalysis)
1667 ec.CurrentBranching.SetFieldAssigned (this, name);
1670 public void SetFieldAssigned (MyBitVector vector, string field_name)
1672 int field_idx = TypeInfo.GetFieldIndex (field_name);
1677 vector [Offset + field_idx] = true;
1680 public VariableInfo GetSubStruct (string name)
1682 TypeInfo type = TypeInfo.GetSubStruct (name);
1687 return new VariableInfo (this, type);
1690 public override string ToString ()
1692 return String.Format ("VariableInfo ({0}:{1}:{2}:{3}:{4})",
1693 Name, TypeInfo, Offset, Length, IsParameter);
1698 // This is used by the flow code to hold the `layout' of the flow vector for
1699 // all locals and all parameters (ie. we create one instance of this class for the
1700 // locals and another one for the params).
1702 public class VariableMap {
1704 // The number of variables in the map.
1706 public readonly int Count;
1709 // Total length of the flow vector for this map.
1711 public readonly int Length;
1714 // Type and name of all the variables.
1715 // Note that this is null for variables for which we do not need to compute
1718 public readonly Type[] VariableTypes;
1719 public readonly string[] VariableNames;
1723 public VariableMap (InternalParameters ip)
1725 Count = ip != null ? ip.Count : 0;
1726 map = new VariableInfo [Count];
1727 VariableNames = new string [Count];
1728 VariableTypes = new Type [Count];
1731 for (int i = 0; i < Count; i++) {
1732 Parameter.Modifier mod = ip.ParameterModifier (i);
1734 if ((mod & Parameter.Modifier.OUT) == 0)
1737 VariableNames [i] = ip.ParameterName (i);
1738 VariableTypes [i] = TypeManager.GetElementType (ip.ParameterType (i));
1740 map [i] = new VariableInfo (VariableNames [i], VariableTypes [i], i, Length);
1741 Length += map [i].Length;
1745 public VariableMap (LocalInfo[] locals)
1746 : this (null, locals)
1749 public VariableMap (VariableMap parent, LocalInfo[] locals)
1751 int offset = 0, start = 0;
1752 if (parent != null) {
1753 offset = parent.Length;
1754 start = parent.Count;
1757 Count = locals.Length + start;
1758 map = new VariableInfo [Count];
1759 VariableNames = new string [Count];
1760 VariableTypes = new Type [Count];
1763 if (parent != null) {
1764 parent.map.CopyTo (map, 0);
1765 parent.VariableNames.CopyTo (VariableNames, 0);
1766 parent.VariableTypes.CopyTo (VariableTypes, 0);
1769 for (int i = start; i < Count; i++) {
1770 LocalInfo li = locals [i-start];
1772 if (li.VariableType == null)
1775 VariableNames [i] = li.Name;
1776 VariableTypes [i] = li.VariableType;
1778 map [i] = li.VariableInfo = new VariableInfo (li, Length);
1779 Length += map [i].Length;
1784 // Returns the VariableInfo for variable @index or null if we don't need to
1785 // compute assignment info for this variable.
1787 public VariableInfo this [int index] {
1793 public override string ToString ()
1795 return String.Format ("VariableMap ({0}:{1})", Count, Length);
1800 // This is a special bit vector which can inherit from another bit vector doing a
1801 // copy-on-write strategy. The inherited vector may have a smaller size than the
1804 public class MyBitVector {
1805 public readonly int Count;
1806 public readonly MyBitVector InheritsFrom;
1811 public MyBitVector (int Count)
1812 : this (null, Count)
1815 public MyBitVector (MyBitVector InheritsFrom, int Count)
1817 this.InheritsFrom = InheritsFrom;
1822 // Checks whether this bit vector has been modified. After setting this to true,
1823 // we won't use the inherited vector anymore, but our own copy of it.
1825 public bool IsDirty {
1832 initialize_vector ();
1837 // Get/set bit `index' in the bit vector.
1839 public bool this [int index]
1843 throw new ArgumentOutOfRangeException ();
1845 // We're doing a "copy-on-write" strategy here; as long
1846 // as nobody writes to the array, we can use our parent's
1847 // copy instead of duplicating the vector.
1850 return vector [index];
1851 else if (InheritsFrom != null) {
1852 BitArray inherited = InheritsFrom.Vector;
1854 if (index < inherited.Count)
1855 return inherited [index];
1864 throw new ArgumentOutOfRangeException ();
1866 // Only copy the vector if we're actually modifying it.
1868 if (this [index] != value) {
1869 initialize_vector ();
1871 vector [index] = value;
1877 // If you explicitly convert the MyBitVector to a BitArray, you will get a deep
1878 // copy of the bit vector.
1880 public static explicit operator BitArray (MyBitVector vector)
1882 vector.initialize_vector ();
1883 return vector.Vector;
1887 // Performs an `or' operation on the bit vector. The `new_vector' may have a
1888 // different size than the current one.
1890 public void Or (MyBitVector new_vector)
1892 BitArray new_array = new_vector.Vector;
1894 initialize_vector ();
1897 if (vector.Count < new_array.Count)
1898 upper = vector.Count;
1900 upper = new_array.Count;
1902 for (int i = 0; i < upper; i++)
1903 vector [i] = vector [i] | new_array [i];
1907 // Perfonrms an `and' operation on the bit vector. The `new_vector' may have
1908 // a different size than the current one.
1910 public void And (MyBitVector new_vector)
1912 BitArray new_array = new_vector.Vector;
1914 initialize_vector ();
1917 if (vector.Count < new_array.Count)
1918 lower = upper = vector.Count;
1920 lower = new_array.Count;
1921 upper = vector.Count;
1924 for (int i = 0; i < lower; i++)
1925 vector [i] = vector [i] & new_array [i];
1927 for (int i = lower; i < upper; i++)
1931 public static void And (ref MyBitVector target, MyBitVector vector)
1934 target.And (vector);
1936 target = vector.Clone ();
1939 public static void Or (ref MyBitVector target, MyBitVector vector)
1944 target = vector.Clone ();
1948 // This does a deep copy of the bit vector.
1950 public MyBitVector Clone ()
1952 MyBitVector retval = new MyBitVector (Count);
1954 retval.Vector = Vector;
1963 else if (!is_dirty && (InheritsFrom != null))
1964 return InheritsFrom.Vector;
1966 initialize_vector ();
1972 initialize_vector ();
1974 for (int i = 0; i < System.Math.Min (vector.Count, value.Count); i++)
1975 vector [i] = value [i];
1979 void initialize_vector ()
1984 vector = new BitArray (Count, false);
1985 if (InheritsFrom != null)
1986 Vector = InheritsFrom.Vector;
1991 public override string ToString ()
1993 StringBuilder sb = new StringBuilder ("{");
1995 BitArray vector = Vector;
1998 for (int i = 0; i < vector.Count; i++) {
1999 sb.Append (vector [i] ? "1" : "0");
2003 return sb.ToString ();