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 : byte {
29 // Normal (conditional or toplevel) block.
49 // The type of one sibling of a branching.
51 public enum SiblingType : byte {
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 : byte {
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;
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 Reachability (FlowReturns returns, FlowReturns breaks,
97 FlowReturns throws, FlowReturns barrier)
99 this.returns = returns;
100 this.breaks = breaks;
101 this.throws = throws;
102 this.barrier = barrier;
105 public Reachability Clone ()
107 return new Reachability (returns, breaks, throws, barrier);
111 // Performs an `And' operation on the FlowReturns status
112 // (for instance, a block only returns Always if all its siblings
115 public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
117 if (a == FlowReturns.Undefined)
121 case FlowReturns.Never:
122 if (b == FlowReturns.Never)
123 return FlowReturns.Never;
125 return FlowReturns.Sometimes;
127 case FlowReturns.Sometimes:
128 return FlowReturns.Sometimes;
130 case FlowReturns.Always:
131 if (b == FlowReturns.Always)
132 return FlowReturns.Always;
134 return FlowReturns.Sometimes;
137 throw new ArgumentException ();
141 public static FlowReturns OrFlowReturns (FlowReturns a, FlowReturns b)
143 if (a == FlowReturns.Undefined)
147 case FlowReturns.Never:
150 case FlowReturns.Sometimes:
151 if (b == FlowReturns.Always)
152 return FlowReturns.Always;
154 return FlowReturns.Sometimes;
156 case FlowReturns.Always:
157 return FlowReturns.Always;
160 throw new ArgumentException ();
164 public static void And (ref Reachability a, Reachability b, bool do_break)
172 // `break' does not "break" in a Switch or a LoopBlock
174 bool a_breaks = do_break && a.AlwaysBreaks;
175 bool b_breaks = do_break && b.AlwaysBreaks;
177 bool a_has_barrier, b_has_barrier;
180 // This is the normal case: the code following a barrier
181 // cannot be reached.
183 a_has_barrier = a.AlwaysHasBarrier;
184 b_has_barrier = b.AlwaysHasBarrier;
187 // Special case for Switch and LoopBlocks: we can reach the
188 // code after the barrier via the `break'.
190 a_has_barrier = !a.AlwaysBreaks && a.AlwaysHasBarrier;
191 b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
194 bool a_unreachable = a_breaks || a.AlwaysThrows || a_has_barrier;
195 bool b_unreachable = b_breaks || b.AlwaysThrows || b_has_barrier;
198 // Do all code paths always return ?
200 if (a.AlwaysReturns) {
201 if (b.AlwaysReturns || b_unreachable)
202 a.returns = FlowReturns.Always;
204 a.returns = FlowReturns.Sometimes;
205 } else if (b.AlwaysReturns) {
206 if (a.AlwaysReturns || a_unreachable)
207 a.returns = FlowReturns.Always;
209 a.returns = FlowReturns.Sometimes;
210 } else if (!a.MayReturn) {
212 a.returns = FlowReturns.Sometimes;
214 a.returns = FlowReturns.Never;
215 } else if (!b.MayReturn) {
217 a.returns = FlowReturns.Sometimes;
219 a.returns = FlowReturns.Never;
222 a.breaks = AndFlowReturns (a.breaks, b.breaks);
223 a.throws = AndFlowReturns (a.throws, b.throws);
224 a.barrier = AndFlowReturns (a.barrier, b.barrier);
226 if (a_unreachable && b_unreachable)
227 a.barrier = FlowReturns.Always;
228 else if (a_unreachable || b_unreachable)
229 a.barrier = FlowReturns.Sometimes;
231 a.barrier = FlowReturns.Never;
234 public void Or (Reachability b)
236 returns = OrFlowReturns (returns, b.returns);
237 breaks = OrFlowReturns (breaks, b.breaks);
238 throws = OrFlowReturns (throws, b.throws);
239 barrier = OrFlowReturns (barrier, b.barrier);
242 public static Reachability Never ()
244 return new Reachability (
245 FlowReturns.Never, FlowReturns.Never,
246 FlowReturns.Never, FlowReturns.Never);
249 public FlowReturns Reachable {
251 if ((returns == FlowReturns.Always) ||
252 (breaks == FlowReturns.Always) ||
253 (throws == FlowReturns.Always) ||
254 (barrier == FlowReturns.Always))
255 return FlowReturns.Never;
256 else if ((returns == FlowReturns.Never) &&
257 (breaks == FlowReturns.Never) &&
258 (throws == FlowReturns.Never) &&
259 (barrier == FlowReturns.Never))
260 return FlowReturns.Always;
262 return FlowReturns.Sometimes;
266 public bool AlwaysBreaks {
267 get { return breaks == FlowReturns.Always; }
270 public bool MayBreak {
271 get { return breaks != FlowReturns.Never; }
274 public bool AlwaysReturns {
275 get { return returns == FlowReturns.Always; }
278 public bool MayReturn {
279 get { return returns != FlowReturns.Never; }
282 public bool AlwaysThrows {
283 get { return throws == FlowReturns.Always; }
286 public bool MayThrow {
287 get { return throws != FlowReturns.Never; }
290 public bool AlwaysHasBarrier {
291 get { return barrier == FlowReturns.Always; }
294 public bool MayHaveBarrier {
295 get { return barrier != FlowReturns.Never; }
298 public bool IsUnreachable {
299 get { return Reachable == FlowReturns.Never; }
302 public void SetReturns ()
304 returns = FlowReturns.Always;
307 public void SetReturnsSometimes ()
309 returns = FlowReturns.Sometimes;
312 public void SetBreaks ()
314 breaks = FlowReturns.Always;
317 public void ResetBreaks ()
319 breaks = FlowReturns.Never;
322 public void SetThrows ()
324 throws = FlowReturns.Always;
327 public void SetThrowsSometimes ()
329 throws = FlowReturns.Sometimes;
332 public void SetBarrier ()
334 barrier = FlowReturns.Always;
337 static string ShortName (FlowReturns returns)
340 case FlowReturns.Never:
342 case FlowReturns.Sometimes:
349 public override string ToString ()
351 return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
352 ShortName (returns), ShortName (breaks),
353 ShortName (throws), ShortName (barrier),
354 ShortName (Reachable));
358 public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
361 case BranchingType.Exception:
362 return new FlowBranchingException (parent, block, loc);
364 case BranchingType.Switch:
365 return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
367 case BranchingType.SwitchSection:
368 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
370 case BranchingType.Block:
371 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
373 case BranchingType.Loop:
374 return new FlowBranchingLoop (parent, block, loc);
377 return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
382 // The type of this flow branching.
384 public readonly BranchingType Type;
387 // The block this branching is contained in. This may be null if it's not
388 // a top-level block and it doesn't declare any local variables.
390 public readonly Block Block;
393 // The parent of this branching or null if this is the top-block.
395 public readonly FlowBranching Parent;
398 // Start-Location of this flow branching.
400 public readonly Location Location;
403 // If this is an infinite loop.
405 public bool Infinite;
410 VariableMap param_map, local_map;
412 static int next_id = 0;
416 // The vector contains a BitArray with information about which local variables
417 // and parameters are already initialized at the current code position.
419 public class UsageVector {
421 // The type of this branching.
423 public readonly SiblingType Type;
426 // Start location of this branching.
428 public readonly Location Location;
431 // This is only valid for SwitchSection, Try, Catch and Finally.
433 public readonly Block Block;
436 // If this is true, then the usage vector has been modified and must be
437 // merged when we're done with this branching.
442 // The number of parameters in this block.
444 public readonly int CountParameters;
447 // The number of locals in this block.
449 public readonly int CountLocals;
452 // If not null, then we inherit our state from this vector and do a
453 // copy-on-write. If null, then we're the first sibling in a top-level
454 // block and inherit from the empty vector.
456 public readonly UsageVector InheritsFrom;
459 // This is used to construct a list of UsageVector's.
461 public UsageVector Next;
466 MyBitVector locals, parameters;
467 Reachability reachability;
469 static int next_id = 0;
473 // Normally, you should not use any of these constructors.
475 public UsageVector (SiblingType type, UsageVector parent,
476 Block block, Location loc,
477 int num_params, int num_locals)
482 this.InheritsFrom = parent;
483 this.CountParameters = num_params;
484 this.CountLocals = num_locals;
486 if (parent != null) {
488 locals = new MyBitVector (parent.locals, CountLocals);
491 parameters = new MyBitVector (parent.parameters, num_params);
493 reachability = parent.Reachability.Clone ();
496 locals = new MyBitVector (null, CountLocals);
499 parameters = new MyBitVector (null, num_params);
501 reachability = Reachability.Never ();
507 public UsageVector (SiblingType type, UsageVector parent,
508 Block block, Location loc)
509 : this (type, parent, block, loc,
510 parent.CountParameters, parent.CountLocals)
513 public UsageVector (MyBitVector parameters, MyBitVector locals,
514 Reachability reachability, Block block,
517 this.Type = SiblingType.Block;
521 this.reachability = reachability;
522 this.parameters = parameters;
523 this.locals = locals;
529 // This does a deep copy of the usage vector.
531 public UsageVector Clone ()
533 UsageVector retval = new UsageVector (
534 Type, null, Block, Location,
535 CountParameters, CountLocals);
537 if (retval.locals != null)
538 retval.locals = locals.Clone ();
540 if (parameters != null)
541 retval.parameters = parameters.Clone ();
543 retval.reachability = reachability.Clone ();
548 public bool IsAssigned (VariableInfo var)
550 if (!var.IsParameter && Reachability.IsUnreachable)
553 return var.IsAssigned (var.IsParameter ? parameters : locals);
556 public void SetAssigned (VariableInfo var)
558 if (!var.IsParameter && Reachability.IsUnreachable)
562 var.SetAssigned (var.IsParameter ? parameters : locals);
565 public bool IsFieldAssigned (VariableInfo var, string name)
567 if (!var.IsParameter && Reachability.IsUnreachable)
570 return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
573 public void SetFieldAssigned (VariableInfo var, string name)
575 if (!var.IsParameter && Reachability.IsUnreachable)
579 var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
582 public Reachability Reachability {
588 public void Return ()
590 if (!reachability.IsUnreachable) {
592 reachability.SetReturns ();
598 if (!reachability.IsUnreachable) {
600 reachability.SetBreaks ();
606 if (!reachability.IsUnreachable) {
608 reachability.SetThrows ();
614 if (!reachability.IsUnreachable) {
616 reachability.SetBarrier ();
621 // Merges a child branching.
623 public UsageVector MergeChild (FlowBranching branching)
625 UsageVector result = branching.Merge ();
627 Report.Debug (2, " MERGING CHILD", this, IsDirty,
628 result.ParameterVector, result.LocalVector,
629 result.Reachability, reachability, Type);
631 Reachability new_r = result.Reachability;
633 if (branching.Type == BranchingType.Loop) {
634 bool may_leave_loop = new_r.MayBreak;
635 new_r.ResetBreaks ();
637 if (branching.Infinite && !may_leave_loop) {
638 if (new_r.Returns == FlowReturns.Sometimes) {
639 // If we're an infinite loop and do not break,
640 // the code after the loop can never be reached.
641 // However, if we may return from the loop,
642 // then we do always return (or stay in the
649 if (new_r.Returns == FlowReturns.Always) {
650 // We're either finite or we may leave the loop.
651 new_r.SetReturnsSometimes ();
653 if (new_r.Throws == FlowReturns.Always) {
654 // We're either finite or we may leave the loop.
655 new_r.SetThrowsSometimes ();
658 } else if (branching.Type == BranchingType.Switch)
659 new_r.ResetBreaks ();
662 // We've now either reached the point after the branching or we will
663 // never get there since we always return or always throw an exception.
665 // If we can reach the point after the branching, mark all locals and
666 // parameters as initialized which have been initialized in all branches
667 // we need to look at (see above).
670 if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
671 Report.Error (163, Location,
672 "Control cannot fall through from one " +
673 "case label to another");
677 if (locals != null && result.LocalVector != null)
678 locals.Or (result.LocalVector);
680 if (result.ParameterVector != null)
681 parameters.Or (result.ParameterVector);
683 reachability.Or (new_r);
685 Report.Debug (2, " MERGING CHILD DONE", this, result,
686 new_r, reachability);
693 protected void MergeFinally (FlowBranching branching, UsageVector f_origins,
694 MyBitVector f_params)
696 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
697 MyBitVector temp_params = f_params.Clone ();
698 temp_params.Or (vector.Parameters);
702 public void MergeFinally (FlowBranching branching, UsageVector f_vector,
703 UsageVector f_origins)
705 if (parameters != null) {
706 if (f_vector != null) {
707 MergeFinally (branching, f_origins, f_vector.Parameters);
708 MyBitVector.Or (ref parameters, f_vector.ParameterVector);
710 MergeFinally (branching, f_origins, parameters);
713 if (f_vector != null && f_vector.LocalVector != null)
714 MyBitVector.Or (ref locals, f_vector.LocalVector);
718 // Tells control flow analysis that the current code position may be reached with
719 // a forward jump from any of the origins listed in `origin_vectors' which is a
720 // list of UsageVectors.
722 // This is used when resolving forward gotos - in the following example, the
723 // variable `a' is uninitialized in line 8 becase this line may be reached via
724 // the goto in line 4:
734 // 8 Console.WriteLine (a);
737 public void MergeJumpOrigins (UsageVector o_vectors)
739 Report.Debug (1, " MERGING JUMP ORIGINS", this);
741 reachability = Reachability.Never ();
743 if (o_vectors == null)
748 for (UsageVector vector = o_vectors; vector != null;
749 vector = vector.Next) {
750 Report.Debug (1, " MERGING JUMP ORIGIN", vector);
753 if (locals != null && vector.Locals != null)
754 locals.Or (vector.locals);
756 if (parameters != null)
757 parameters.Or (vector.parameters);
760 if (locals != null && vector.Locals != null)
761 locals.And (vector.locals);
762 if (parameters != null)
763 parameters.And (vector.parameters);
766 Reachability.And (ref reachability, vector.Reachability, true);
769 Report.Debug (1, " MERGING JUMP ORIGINS DONE", this);
773 // This is used at the beginning of a finally block if there were
774 // any return statements in the try block or one of the catch blocks.
776 public void MergeFinallyOrigins (UsageVector f_origins)
778 Report.Debug (1, " MERGING FINALLY ORIGIN", this);
780 reachability = Reachability.Never ();
782 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
783 Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
785 if (parameters != null)
786 parameters.And (vector.parameters);
788 Reachability.And (ref reachability, vector.Reachability, true);
791 Report.Debug (1, " MERGING FINALLY ORIGIN DONE", this);
794 public void MergeBreakOrigins (UsageVector o_vectors)
796 Report.Debug (1, " MERGING BREAK ORIGINS", this);
798 if (o_vectors == null)
803 for (UsageVector vector = o_vectors; vector != null;
804 vector = vector.Next) {
805 Report.Debug (1, " MERGING BREAK ORIGIN", vector);
808 if (locals != null && vector.Locals != null)
809 locals.Or (vector.locals);
811 if (parameters != null)
812 parameters.Or (vector.parameters);
815 if (locals != null && vector.Locals != null)
816 locals.And (vector.locals);
817 if (parameters != null)
818 parameters.And (vector.parameters);
822 Report.Debug (1, " MERGING BREAK ORIGINS DONE", this);
825 public void CheckOutParameters (FlowBranching branching)
827 if (parameters != null)
828 branching.CheckOutParameters (parameters, branching.Location);
832 // Performs an `or' operation on the locals and the parameters.
834 public void Or (UsageVector new_vector)
837 locals.Or (new_vector.locals);
838 if (parameters != null)
839 parameters.Or (new_vector.parameters);
843 // Performs an `and' operation on the locals.
845 public void AndLocals (UsageVector new_vector)
848 locals.And (new_vector.locals);
851 public bool HasParameters {
853 return parameters != null;
857 public bool HasLocals {
859 return locals != null;
864 // Returns a deep copy of the parameters.
866 public MyBitVector Parameters {
868 if (parameters != null)
869 return parameters.Clone ();
876 // Returns a deep copy of the locals.
878 public MyBitVector Locals {
881 return locals.Clone ();
887 public MyBitVector ParameterVector {
893 public MyBitVector LocalVector {
903 public override string ToString ()
905 StringBuilder sb = new StringBuilder ();
907 sb.Append ("Vector (");
914 sb.Append (reachability);
915 if (parameters != null) {
917 sb.Append (parameters);
923 return sb.ToString ();
928 // Creates a new flow branching which is contained in `parent'.
929 // You should only pass non-null for the `block' argument if this block
930 // introduces any new variables - in this case, we need to create a new
931 // usage vector with a different size than our parent's one.
933 protected FlowBranching (FlowBranching parent, BranchingType type, SiblingType stype,
934 Block block, Location loc)
944 param_map = Block.ParameterMap;
945 local_map = Block.LocalMap;
947 UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
948 vector = new UsageVector (
949 stype, parent_vector, Block, loc,
950 param_map.Length, local_map.Length);
952 param_map = Parent.param_map;
953 local_map = Parent.local_map;
954 vector = new UsageVector (
955 stype, Parent.CurrentUsageVector, null, loc);
961 public abstract UsageVector CurrentUsageVector {
966 // Creates a sibling of the current usage vector.
968 public virtual void CreateSibling (Block block, SiblingType type)
970 UsageVector vector = new UsageVector (
971 type, Parent.CurrentUsageVector, block, Location);
974 Report.Debug (1, " CREATED SIBLING", CurrentUsageVector);
977 public void CreateSibling ()
979 CreateSibling (null, SiblingType.Conditional);
982 protected abstract void AddSibling (UsageVector uv);
984 public virtual LabeledStatement LookupLabel (string name, Location loc)
987 return Parent.LookupLabel (name, loc);
991 "No such label `" + name + "' in this scope");
995 public abstract void Label (UsageVector origin_vectors);
998 // Check whether all `out' parameters have been assigned.
1000 public void CheckOutParameters (MyBitVector parameters, Location loc)
1002 for (int i = 0; i < param_map.Count; i++) {
1003 VariableInfo var = param_map [i];
1008 if (var.IsAssigned (parameters))
1011 Report.Error (177, loc, "The out parameter `" +
1012 var.Name + "' must be " +
1013 "assigned before control leaves the current method.");
1017 protected UsageVector Merge (UsageVector sibling_list)
1019 if (sibling_list.Next == null)
1020 return sibling_list;
1022 MyBitVector locals = null;
1023 MyBitVector parameters = null;
1025 Reachability reachability = null;
1027 Report.Debug (2, " MERGING SIBLINGS", this, Name);
1029 for (UsageVector child = sibling_list; child != null; child = child.Next) {
1030 bool do_break = (Type != BranchingType.Switch) &&
1031 (Type != BranchingType.Loop);
1033 Report.Debug (2, " MERGING SIBLING ", child,
1034 child.ParameterVector, child.LocalVector,
1035 reachability, child.Reachability, do_break);
1037 Reachability.And (ref reachability, child.Reachability, do_break);
1039 // A local variable is initialized after a flow branching if it
1040 // has been initialized in all its branches which do neither
1041 // always return or always throw an exception.
1043 // If a branch may return, but does not always return, then we
1044 // can treat it like a never-returning branch here: control will
1045 // only reach the code position after the branching if we did not
1048 // It's important to distinguish between always and sometimes
1049 // returning branches here:
1052 // 2 if (something) {
1056 // 6 Console.WriteLine (a);
1058 // The if block in lines 3-4 always returns, so we must not look
1059 // at the initialization of `a' in line 4 - thus it'll still be
1060 // uninitialized in line 6.
1062 // On the other hand, the following is allowed:
1069 // 6 Console.WriteLine (a);
1071 // Here, `a' is initialized in line 3 and we must not look at
1072 // line 5 since it always returns.
1074 bool do_break_2 = (child.Type != SiblingType.Block) &&
1075 (child.Type != SiblingType.SwitchSection);
1076 bool always_throws = (child.Type != SiblingType.Try) &&
1077 child.Reachability.AlwaysThrows;
1078 bool unreachable = always_throws ||
1079 (do_break_2 && child.Reachability.AlwaysBreaks) ||
1080 child.Reachability.AlwaysReturns ||
1081 child.Reachability.AlwaysHasBarrier;
1083 Report.Debug (2, " MERGING SIBLING #1", reachability,
1084 Type, child.Type, child.Reachability.IsUnreachable,
1085 do_break_2, always_throws, unreachable);
1087 if (!unreachable && (child.LocalVector != null))
1088 MyBitVector.And (ref locals, child.LocalVector);
1090 // An `out' parameter must be assigned in all branches which do
1091 // not always throw an exception.
1092 if ((child.ParameterVector != null) && !child.Reachability.AlwaysThrows)
1093 MyBitVector.And (ref parameters, child.ParameterVector);
1095 Report.Debug (2, " MERGING SIBLING #2", parameters, locals);
1098 if (reachability == null)
1099 reachability = Reachability.Never ();
1101 Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
1102 reachability, Infinite);
1104 return new UsageVector (
1105 parameters, locals, reachability, null, Location);
1108 protected abstract UsageVector Merge ();
1111 // Merge a child branching.
1113 public UsageVector MergeChild (FlowBranching child)
1115 return CurrentUsageVector.MergeChild (child);
1119 // Does the toplevel merging.
1121 public Reachability MergeTopBlock ()
1123 if ((Type != BranchingType.Block) || (Block == null))
1124 throw new NotSupportedException ();
1126 UsageVector vector = new UsageVector (
1127 SiblingType.Conditional, null, Block, Location,
1128 param_map.Length, local_map.Length);
1130 UsageVector result = vector.MergeChild (this);
1132 Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
1134 if ((vector.Reachability.Throws != FlowReturns.Always) &&
1135 (vector.Reachability.Barrier != FlowReturns.Always))
1136 CheckOutParameters (vector.Parameters, Location);
1138 return result.Reachability;
1142 // Checks whether we're in a `try' block.
1144 public virtual bool InTryOrCatch (bool is_return)
1146 if ((Block != null) && Block.IsDestructor)
1148 else if (!is_return &&
1149 ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
1151 else if (Parent != null)
1152 return Parent.InTryOrCatch (is_return);
1158 // Checks whether we're in a `catch' block.
1160 public virtual bool InCatch ()
1163 return Parent.InCatch ();
1169 // Checks whether we're in a `finally' block.
1171 public virtual bool InFinally (bool is_return)
1174 ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
1176 else if (Parent != null)
1177 return Parent.InFinally (is_return);
1182 public virtual bool InLoop ()
1184 if (Type == BranchingType.Loop)
1186 else if (Parent != null)
1187 return Parent.InLoop ();
1192 public virtual bool InSwitch ()
1194 if (Type == BranchingType.Switch)
1196 else if (Parent != null)
1197 return Parent.InSwitch ();
1202 public virtual bool BreakCrossesTryCatchBoundary ()
1204 if ((Type == BranchingType.Loop) || (Type == BranchingType.Switch))
1206 else if (Parent != null)
1207 return Parent.BreakCrossesTryCatchBoundary ();
1212 public virtual void AddFinallyVector (UsageVector vector)
1215 Parent.AddFinallyVector (vector);
1216 else if ((Block == null) || !Block.IsDestructor)
1217 throw new NotSupportedException ();
1220 public virtual void AddBreakVector (UsageVector vector)
1223 Parent.AddBreakVector (vector);
1224 else if ((Block == null) || !Block.IsDestructor)
1225 throw new NotSupportedException ();
1228 public bool IsAssigned (VariableInfo vi)
1230 return CurrentUsageVector.IsAssigned (vi);
1233 public bool IsFieldAssigned (VariableInfo vi, string field_name)
1235 if (CurrentUsageVector.IsAssigned (vi))
1238 return CurrentUsageVector.IsFieldAssigned (vi, field_name);
1241 public void SetAssigned (VariableInfo vi)
1243 CurrentUsageVector.SetAssigned (vi);
1246 public void SetFieldAssigned (VariableInfo vi, string name)
1248 CurrentUsageVector.SetFieldAssigned (vi, name);
1251 public override string ToString ()
1253 StringBuilder sb = new StringBuilder ();
1254 sb.Append (GetType ());
1260 if (Block != null) {
1262 sb.Append (Block.ID);
1264 sb.Append (Block.StartLocation);
1267 // sb.Append (Siblings.Length);
1268 // sb.Append (" - ");
1269 sb.Append (CurrentUsageVector);
1271 return sb.ToString ();
1274 public string Name {
1276 return String.Format ("{0} ({1}:{2}:{3})",
1277 GetType (), id, Type, Location);
1282 public class FlowBranchingBlock : FlowBranching
1284 UsageVector sibling_list = null;
1286 public FlowBranchingBlock (FlowBranching parent, BranchingType type,
1287 SiblingType stype, Block block, Location loc)
1288 : base (parent, type, stype, block, loc)
1291 public override UsageVector CurrentUsageVector {
1292 get { return sibling_list; }
1295 protected override void AddSibling (UsageVector sibling)
1297 sibling.Next = sibling_list;
1298 sibling_list = sibling;
1301 public override LabeledStatement LookupLabel (string name, Location loc)
1304 return base.LookupLabel (name, loc);
1306 LabeledStatement s = Block.LookupLabel (name);
1310 return base.LookupLabel (name, loc);
1313 public override void Label (UsageVector origin_vectors)
1315 if (!CurrentUsageVector.Reachability.IsUnreachable) {
1316 UsageVector vector = CurrentUsageVector.Clone ();
1317 vector.Next = origin_vectors;
1318 origin_vectors = vector;
1321 CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1324 protected override UsageVector Merge ()
1326 return Merge (sibling_list);
1330 public class FlowBranchingLoop : FlowBranchingBlock
1332 UsageVector break_origins;
1334 public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
1335 : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
1338 public override void AddBreakVector (UsageVector vector)
1340 vector = vector.Clone ();
1341 vector.Next = break_origins;
1342 break_origins = vector;
1345 protected override UsageVector Merge ()
1347 UsageVector vector = base.Merge ();
1349 vector.MergeBreakOrigins (break_origins);
1355 public class FlowBranchingException : FlowBranching
1357 UsageVector current_vector;
1358 UsageVector catch_vectors;
1359 UsageVector finally_vector;
1360 UsageVector finally_origins;
1363 public FlowBranchingException (FlowBranching parent, Block block, Location loc)
1364 : base (parent, BranchingType.Exception, SiblingType.Try, block, loc)
1367 protected override void AddSibling (UsageVector sibling)
1369 if (sibling.Type == SiblingType.Try) {
1370 sibling.Next = catch_vectors;
1371 catch_vectors = sibling;
1373 } else if (sibling.Type == SiblingType.Catch) {
1374 sibling.Next = catch_vectors;
1375 catch_vectors = sibling;
1377 } else if (sibling.Type == SiblingType.Finally) {
1378 sibling.MergeFinallyOrigins (finally_origins);
1379 finally_vector = sibling;
1382 throw new InvalidOperationException ();
1384 current_vector = sibling;
1387 public override UsageVector CurrentUsageVector {
1388 get { return current_vector; }
1391 public override bool InTryOrCatch (bool is_return)
1393 return finally_vector == null;
1396 public override bool InCatch ()
1398 return !in_try && (finally_vector == null);
1401 public override bool InFinally (bool is_return)
1403 return finally_vector != null;
1406 public override bool BreakCrossesTryCatchBoundary ()
1411 public override void AddFinallyVector (UsageVector vector)
1413 vector = vector.Clone ();
1414 vector.Next = finally_origins;
1415 finally_origins = vector;
1418 public override LabeledStatement LookupLabel (string name, Location loc)
1420 if (current_vector.Block == null)
1421 return base.LookupLabel (name, loc);
1423 LabeledStatement s = current_vector.Block.LookupLabel (name);
1427 if (finally_vector != null) {
1429 157, loc, "Control can not leave the body " +
1430 "of the finally block");
1434 return base.LookupLabel (name, loc);
1437 public override void Label (UsageVector origin_vectors)
1439 CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1442 protected override UsageVector Merge ()
1444 UsageVector vector = Merge (catch_vectors);
1446 vector.MergeFinally (this, finally_vector, finally_origins);
1453 // This is used by the flow analysis code to keep track of the type of local variables
1456 // The flow code uses a BitVector to keep track of whether a variable has been assigned
1457 // or not. This is easy for fundamental types (int, char etc.) or reference types since
1458 // you can only assign the whole variable as such.
1460 // For structs, we also need to keep track of all its fields. To do this, we allocate one
1461 // bit for the struct itself (it's used if you assign/access the whole struct) followed by
1462 // one bit for each of its fields.
1464 // This class computes this `layout' for each type.
1466 public class TypeInfo
1468 public readonly Type Type;
1471 // Total number of bits a variable of this type consumes in the flow vector.
1473 public readonly int TotalLength;
1476 // Number of bits the simple fields of a variable of this type consume
1477 // in the flow vector.
1479 public readonly int Length;
1482 // This is only used by sub-structs.
1484 public readonly int Offset;
1487 // If this is a struct.
1489 public readonly bool IsStruct;
1492 // If this is a struct, all fields which are structs theirselves.
1494 public TypeInfo[] SubStructInfo;
1496 protected readonly StructInfo struct_info;
1497 private static Hashtable type_hash = new Hashtable ();
1499 public static TypeInfo GetTypeInfo (Type type)
1501 TypeInfo info = (TypeInfo) type_hash [type];
1505 info = new TypeInfo (type);
1506 type_hash.Add (type, info);
1510 public static TypeInfo GetTypeInfo (TypeContainer tc)
1512 TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
1516 info = new TypeInfo (tc);
1517 type_hash.Add (tc.TypeBuilder, info);
1521 private TypeInfo (Type type)
1525 struct_info = StructInfo.GetStructInfo (type);
1526 if (struct_info != null) {
1527 Length = struct_info.Length;
1528 TotalLength = struct_info.TotalLength;
1529 SubStructInfo = struct_info.StructFields;
1538 private TypeInfo (TypeContainer tc)
1540 this.Type = tc.TypeBuilder;
1542 struct_info = StructInfo.GetStructInfo (tc);
1543 if (struct_info != null) {
1544 Length = struct_info.Length;
1545 TotalLength = struct_info.TotalLength;
1546 SubStructInfo = struct_info.StructFields;
1555 protected TypeInfo (StructInfo struct_info, int offset)
1557 this.struct_info = struct_info;
1558 this.Offset = offset;
1559 this.Length = struct_info.Length;
1560 this.TotalLength = struct_info.TotalLength;
1561 this.SubStructInfo = struct_info.StructFields;
1562 this.Type = struct_info.Type;
1563 this.IsStruct = true;
1566 public int GetFieldIndex (string name)
1568 if (struct_info == null)
1571 return struct_info [name];
1574 public TypeInfo GetSubStruct (string name)
1576 if (struct_info == null)
1579 return struct_info.GetStructField (name);
1583 // A struct's constructor must always assign all fields.
1584 // This method checks whether it actually does so.
1586 public bool IsFullyInitialized (FlowBranching branching, VariableInfo vi, Location loc)
1588 if (struct_info == null)
1592 for (int i = 0; i < struct_info.Count; i++) {
1593 FieldInfo field = struct_info.Fields [i];
1595 if (!branching.IsFieldAssigned (vi, field.Name)) {
1596 Report.Error (171, loc,
1597 "Field `" + TypeManager.CSharpName (Type) +
1598 "." + field.Name + "' must be fully initialized " +
1599 "before control leaves the constructor");
1607 public override string ToString ()
1609 return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
1610 Type, Offset, Length, TotalLength);
1613 protected class StructInfo {
1614 public readonly Type Type;
1615 public readonly FieldInfo[] Fields;
1616 public readonly TypeInfo[] StructFields;
1617 public readonly int Count;
1618 public readonly int CountPublic;
1619 public readonly int CountNonPublic;
1620 public readonly int Length;
1621 public readonly int TotalLength;
1622 public readonly bool HasStructFields;
1624 private static Hashtable field_type_hash = new Hashtable ();
1625 private Hashtable struct_field_hash;
1626 private Hashtable field_hash;
1628 protected bool InTransit = false;
1630 // Private constructor. To save memory usage, we only need to create one instance
1631 // of this class per struct type.
1632 private StructInfo (Type type)
1636 field_type_hash.Add (type, this);
1638 if (type is TypeBuilder) {
1639 TypeContainer tc = TypeManager.LookupTypeContainer (type);
1641 ArrayList fields = tc.Fields;
1643 ArrayList public_fields = new ArrayList ();
1644 ArrayList non_public_fields = new ArrayList ();
1646 if (fields != null) {
1647 foreach (Field field in fields) {
1648 if ((field.ModFlags & Modifiers.STATIC) != 0)
1650 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
1651 public_fields.Add (field.FieldBuilder);
1653 non_public_fields.Add (field.FieldBuilder);
1657 CountPublic = public_fields.Count;
1658 CountNonPublic = non_public_fields.Count;
1659 Count = CountPublic + CountNonPublic;
1661 Fields = new FieldInfo [Count];
1662 public_fields.CopyTo (Fields, 0);
1663 non_public_fields.CopyTo (Fields, CountPublic);
1664 } else if (type is GenericTypeParameterBuilder) {
1665 CountPublic = CountNonPublic = Count = 0;
1667 Fields = new FieldInfo [0];
1669 FieldInfo[] public_fields = type.GetFields (
1670 BindingFlags.Instance|BindingFlags.Public);
1671 FieldInfo[] non_public_fields = type.GetFields (
1672 BindingFlags.Instance|BindingFlags.NonPublic);
1674 CountPublic = public_fields.Length;
1675 CountNonPublic = non_public_fields.Length;
1676 Count = CountPublic + CountNonPublic;
1678 Fields = new FieldInfo [Count];
1679 public_fields.CopyTo (Fields, 0);
1680 non_public_fields.CopyTo (Fields, CountPublic);
1683 struct_field_hash = new Hashtable ();
1684 field_hash = new Hashtable ();
1687 StructFields = new TypeInfo [Count];
1688 StructInfo[] sinfo = new StructInfo [Count];
1692 for (int i = 0; i < Count; i++) {
1693 FieldInfo field = (FieldInfo) Fields [i];
1695 sinfo [i] = GetStructInfo (field.FieldType);
1696 if (sinfo [i] == null)
1697 field_hash.Add (field.Name, ++Length);
1698 else if (sinfo [i].InTransit) {
1699 Report.Error (523, String.Format (
1700 "Struct member '{0}.{1}' of type '{2}' causes " +
1701 "a cycle in the structure layout",
1702 type, field.Name, sinfo [i].Type));
1710 TotalLength = Length + 1;
1711 for (int i = 0; i < Count; i++) {
1712 FieldInfo field = (FieldInfo) Fields [i];
1714 if (sinfo [i] == null)
1717 field_hash.Add (field.Name, TotalLength);
1719 HasStructFields = true;
1720 StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
1721 struct_field_hash.Add (field.Name, StructFields [i]);
1722 TotalLength += sinfo [i].TotalLength;
1726 public int this [string name] {
1728 if (field_hash.Contains (name))
1729 return (int) field_hash [name];
1735 public TypeInfo GetStructField (string name)
1737 return (TypeInfo) struct_field_hash [name];
1740 public static StructInfo GetStructInfo (Type type)
1742 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
1743 TypeManager.IsBuiltinType (type))
1746 StructInfo info = (StructInfo) field_type_hash [type];
1750 return new StructInfo (type);
1753 public static StructInfo GetStructInfo (TypeContainer tc)
1755 StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
1759 return new StructInfo (tc.TypeBuilder);
1765 // This is used by the flow analysis code to store information about a single local variable
1766 // or parameter. Depending on the variable's type, we need to allocate one or more elements
1767 // in the BitVector - if it's a fundamental or reference type, we just need to know whether
1768 // it has been assigned or not, but for structs, we need this information for each of its fields.
1770 public class VariableInfo {
1771 public readonly string Name;
1772 public readonly TypeInfo TypeInfo;
1775 // The bit offset of this variable in the flow vector.
1777 public readonly int Offset;
1780 // The number of bits this variable needs in the flow vector.
1781 // The first bit always specifies whether the variable as such has been assigned while
1782 // the remaining bits contain this information for each of a struct's fields.
1784 public readonly int Length;
1787 // If this is a parameter of local variable.
1789 public readonly bool IsParameter;
1791 public readonly LocalInfo LocalInfo;
1792 public readonly int ParameterIndex;
1794 readonly VariableInfo Parent;
1795 VariableInfo[] sub_info;
1797 protected VariableInfo (string name, Type type, int offset)
1800 this.Offset = offset;
1801 this.TypeInfo = TypeInfo.GetTypeInfo (type);
1803 Length = TypeInfo.TotalLength;
1808 protected VariableInfo (VariableInfo parent, TypeInfo type)
1810 this.Name = parent.Name;
1811 this.TypeInfo = type;
1812 this.Offset = parent.Offset + type.Offset;
1813 this.Parent = parent;
1814 this.Length = type.TotalLength;
1816 this.IsParameter = parent.IsParameter;
1817 this.LocalInfo = parent.LocalInfo;
1818 this.ParameterIndex = parent.ParameterIndex;
1823 protected void Initialize ()
1825 TypeInfo[] sub_fields = TypeInfo.SubStructInfo;
1826 if (sub_fields != null) {
1827 sub_info = new VariableInfo [sub_fields.Length];
1828 for (int i = 0; i < sub_fields.Length; i++) {
1829 if (sub_fields [i] != null)
1830 sub_info [i] = new VariableInfo (this, sub_fields [i]);
1833 sub_info = new VariableInfo [0];
1836 public VariableInfo (LocalInfo local_info, int offset)
1837 : this (local_info.Name, local_info.VariableType, offset)
1839 this.LocalInfo = local_info;
1840 this.IsParameter = false;
1843 public VariableInfo (string name, Type type, int param_idx, int offset)
1844 : this (name, type, offset)
1846 this.ParameterIndex = param_idx;
1847 this.IsParameter = true;
1850 public bool IsAssigned (EmitContext ec)
1852 return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (this);
1855 public bool IsAssigned (EmitContext ec, Location loc)
1857 if (IsAssigned (ec))
1860 Report.Error (165, loc,
1861 "Use of unassigned local variable `" + Name + "'");
1862 ec.CurrentBranching.SetAssigned (this);
1866 public bool IsAssigned (MyBitVector vector)
1868 if (vector [Offset])
1871 for (VariableInfo parent = Parent; parent != null; parent = parent.Parent)
1872 if (vector [parent.Offset])
1875 // Return unless this is a struct.
1876 if (!TypeInfo.IsStruct)
1879 // Ok, so each field must be assigned.
1880 for (int i = 0; i < TypeInfo.Length; i++) {
1881 if (!vector [Offset + i + 1])
1885 // Ok, now check all fields which are structs.
1886 for (int i = 0; i < sub_info.Length; i++) {
1887 VariableInfo sinfo = sub_info [i];
1891 if (!sinfo.IsAssigned (vector))
1895 vector [Offset] = true;
1899 public void SetAssigned (EmitContext ec)
1901 if (ec.DoFlowAnalysis)
1902 ec.CurrentBranching.SetAssigned (this);
1905 public void SetAssigned (MyBitVector vector)
1907 vector [Offset] = true;
1910 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
1912 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsFieldAssigned (this, name))
1915 Report.Error (170, loc,
1916 "Use of possibly unassigned field `" + name + "'");
1917 ec.CurrentBranching.SetFieldAssigned (this, name);
1921 public bool IsFieldAssigned (MyBitVector vector, string field_name)
1923 int field_idx = TypeInfo.GetFieldIndex (field_name);
1928 return vector [Offset + field_idx];
1931 public void SetFieldAssigned (EmitContext ec, string name)
1933 if (ec.DoFlowAnalysis)
1934 ec.CurrentBranching.SetFieldAssigned (this, name);
1937 public void SetFieldAssigned (MyBitVector vector, string field_name)
1939 int field_idx = TypeInfo.GetFieldIndex (field_name);
1944 vector [Offset + field_idx] = true;
1947 public VariableInfo GetSubStruct (string name)
1949 TypeInfo type = TypeInfo.GetSubStruct (name);
1954 return new VariableInfo (this, type);
1957 public override string ToString ()
1959 return String.Format ("VariableInfo ({0}:{1}:{2}:{3}:{4})",
1960 Name, TypeInfo, Offset, Length, IsParameter);
1965 // This is used by the flow code to hold the `layout' of the flow vector for
1966 // all locals and all parameters (ie. we create one instance of this class for the
1967 // locals and another one for the params).
1969 public class VariableMap {
1971 // The number of variables in the map.
1973 public readonly int Count;
1976 // Total length of the flow vector for this map.
1978 public readonly int Length;
1982 public VariableMap (InternalParameters ip)
1984 Count = ip != null ? ip.Count : 0;
1986 // Dont bother allocating anything!
1992 for (int i = 0; i < Count; i++) {
1993 Parameter.Modifier mod = ip.ParameterModifier (i);
1995 if ((mod & Parameter.Modifier.OUT) == 0)
1998 // Dont allocate till we find an out var.
2000 map = new VariableInfo [Count];
2002 map [i] = new VariableInfo (ip.ParameterName (i),
2003 TypeManager.GetElementType (ip.ParameterType (i)), i, Length);
2005 Length += map [i].Length;
2009 public VariableMap (LocalInfo[] locals)
2010 : this (null, locals)
2013 public VariableMap (VariableMap parent, LocalInfo[] locals)
2015 int offset = 0, start = 0;
2016 if (parent != null && parent.map != null) {
2017 offset = parent.Length;
2018 start = parent.Count;
2021 Count = locals.Length + start;
2026 map = new VariableInfo [Count];
2029 if (parent != null && parent.map != null) {
2030 parent.map.CopyTo (map, 0);
2033 for (int i = start; i < Count; i++) {
2034 LocalInfo li = locals [i-start];
2036 if (li.VariableType == null)
2039 map [i] = li.VariableInfo = new VariableInfo (li, Length);
2040 Length += map [i].Length;
2045 // Returns the VariableInfo for variable @index or null if we don't need to
2046 // compute assignment info for this variable.
2048 public VariableInfo this [int index] {
2057 public override string ToString ()
2059 return String.Format ("VariableMap ({0}:{1})", Count, Length);
2064 // This is a special bit vector which can inherit from another bit vector doing a
2065 // copy-on-write strategy. The inherited vector may have a smaller size than the
2068 public class MyBitVector {
2069 public readonly int Count;
2070 public readonly MyBitVector InheritsFrom;
2075 public MyBitVector (int Count)
2076 : this (null, Count)
2079 public MyBitVector (MyBitVector InheritsFrom, int Count)
2081 this.InheritsFrom = InheritsFrom;
2086 // Checks whether this bit vector has been modified. After setting this to true,
2087 // we won't use the inherited vector anymore, but our own copy of it.
2089 public bool IsDirty {
2096 initialize_vector ();
2101 // Get/set bit `index' in the bit vector.
2103 public bool this [int index]
2107 throw new ArgumentOutOfRangeException ();
2109 // We're doing a "copy-on-write" strategy here; as long
2110 // as nobody writes to the array, we can use our parent's
2111 // copy instead of duplicating the vector.
2114 return vector [index];
2115 else if (InheritsFrom != null) {
2116 BitArray inherited = InheritsFrom.Vector;
2118 if (index < inherited.Count)
2119 return inherited [index];
2128 throw new ArgumentOutOfRangeException ();
2130 // Only copy the vector if we're actually modifying it.
2132 if (this [index] != value) {
2133 initialize_vector ();
2135 vector [index] = value;
2141 // If you explicitly convert the MyBitVector to a BitArray, you will get a deep
2142 // copy of the bit vector.
2144 public static explicit operator BitArray (MyBitVector vector)
2146 vector.initialize_vector ();
2147 return vector.Vector;
2151 // Performs an `or' operation on the bit vector. The `new_vector' may have a
2152 // different size than the current one.
2154 public void Or (MyBitVector new_vector)
2156 BitArray new_array = new_vector.Vector;
2158 initialize_vector ();
2161 if (vector.Count < new_array.Count)
2162 upper = vector.Count;
2164 upper = new_array.Count;
2166 for (int i = 0; i < upper; i++)
2167 vector [i] = vector [i] | new_array [i];
2171 // Perfonrms an `and' operation on the bit vector. The `new_vector' may have
2172 // a different size than the current one.
2174 public void And (MyBitVector new_vector)
2176 BitArray new_array = new_vector.Vector;
2178 initialize_vector ();
2181 if (vector.Count < new_array.Count)
2182 lower = upper = vector.Count;
2184 lower = new_array.Count;
2185 upper = vector.Count;
2188 for (int i = 0; i < lower; i++)
2189 vector [i] = vector [i] & new_array [i];
2191 for (int i = lower; i < upper; i++)
2195 public static void And (ref MyBitVector target, MyBitVector vector)
2198 target.And (vector);
2200 target = vector.Clone ();
2203 public static void Or (ref MyBitVector target, MyBitVector vector)
2208 target = vector.Clone ();
2212 // This does a deep copy of the bit vector.
2214 public MyBitVector Clone ()
2216 MyBitVector retval = new MyBitVector (Count);
2218 retval.Vector = Vector;
2227 else if (!is_dirty && (InheritsFrom != null))
2228 return InheritsFrom.Vector;
2230 initialize_vector ();
2236 initialize_vector ();
2238 for (int i = 0; i < System.Math.Min (vector.Count, value.Count); i++)
2239 vector [i] = value [i];
2243 void initialize_vector ()
2248 vector = new BitArray (Count, false);
2249 if (InheritsFrom != null)
2250 Vector = InheritsFrom.Vector;
2255 public override string ToString ()
2257 StringBuilder sb = new StringBuilder ("{");
2259 BitArray vector = Vector;
2262 for (int i = 0; i < vector.Count; i++) {
2263 sb.Append (vector [i] ? "1" : "0");
2267 return sb.ToString ();