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 public void ResetBarrier ()
339 barrier = FlowReturns.Never;
342 static string ShortName (FlowReturns returns)
345 case FlowReturns.Never:
347 case FlowReturns.Sometimes:
354 public override string ToString ()
356 return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
357 ShortName (returns), ShortName (breaks),
358 ShortName (throws), ShortName (barrier),
359 ShortName (Reachable));
363 public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
366 case BranchingType.Exception:
367 throw new InvalidOperationException ();
369 case BranchingType.Switch:
370 return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
372 case BranchingType.SwitchSection:
373 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
375 case BranchingType.Block:
376 return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
378 case BranchingType.Loop:
379 return new FlowBranchingLoop (parent, block, loc);
382 return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
387 // The type of this flow branching.
389 public readonly BranchingType Type;
392 // The block this branching is contained in. This may be null if it's not
393 // a top-level block and it doesn't declare any local variables.
395 public readonly Block Block;
398 // The parent of this branching or null if this is the top-block.
400 public readonly FlowBranching Parent;
403 // Start-Location of this flow branching.
405 public readonly Location Location;
408 // If this is an infinite loop.
410 public bool Infinite;
415 VariableMap param_map, local_map;
417 static int next_id = 0;
421 // The vector contains a BitArray with information about which local variables
422 // and parameters are already initialized at the current code position.
424 public class UsageVector {
426 // The type of this branching.
428 public readonly SiblingType Type;
431 // Start location of this branching.
433 public readonly Location Location;
436 // This is only valid for SwitchSection, Try, Catch and Finally.
438 public readonly Block Block;
441 // If this is true, then the usage vector has been modified and must be
442 // merged when we're done with this branching.
447 // The number of parameters in this block.
449 public readonly int CountParameters;
452 // The number of locals in this block.
454 public readonly int CountLocals;
457 // If not null, then we inherit our state from this vector and do a
458 // copy-on-write. If null, then we're the first sibling in a top-level
459 // block and inherit from the empty vector.
461 public readonly UsageVector InheritsFrom;
464 // This is used to construct a list of UsageVector's.
466 public UsageVector Next;
471 MyBitVector locals, parameters;
472 Reachability reachability;
474 static int next_id = 0;
478 // Normally, you should not use any of these constructors.
480 public UsageVector (SiblingType type, UsageVector parent,
481 Block block, Location loc,
482 int num_params, int num_locals)
487 this.InheritsFrom = parent;
488 this.CountParameters = num_params;
489 this.CountLocals = num_locals;
491 if (parent != null) {
493 locals = new MyBitVector (parent.locals, CountLocals);
496 parameters = new MyBitVector (parent.parameters, num_params);
498 reachability = parent.Reachability.Clone ();
501 locals = new MyBitVector (null, CountLocals);
504 parameters = new MyBitVector (null, num_params);
506 reachability = Reachability.Never ();
512 public UsageVector (SiblingType type, UsageVector parent,
513 Block block, Location loc)
514 : this (type, parent, block, loc,
515 parent.CountParameters, parent.CountLocals)
518 public UsageVector (MyBitVector parameters, MyBitVector locals,
519 Reachability reachability, Block block,
522 this.Type = SiblingType.Block;
526 this.reachability = reachability;
527 this.parameters = parameters;
528 this.locals = locals;
534 // This does a deep copy of the usage vector.
536 public UsageVector Clone ()
538 UsageVector retval = new UsageVector (
539 Type, null, Block, Location,
540 CountParameters, CountLocals);
542 if (retval.locals != null)
543 retval.locals = locals.Clone ();
545 if (parameters != null)
546 retval.parameters = parameters.Clone ();
548 retval.reachability = reachability.Clone ();
553 public bool IsAssigned (VariableInfo var)
555 if (!var.IsParameter && Reachability.IsUnreachable)
558 return var.IsAssigned (var.IsParameter ? parameters : locals);
561 public void SetAssigned (VariableInfo var)
563 if (!var.IsParameter && Reachability.IsUnreachable)
567 var.SetAssigned (var.IsParameter ? parameters : locals);
570 public bool IsFieldAssigned (VariableInfo var, string name)
572 if (!var.IsParameter && Reachability.IsUnreachable)
575 return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
578 public void SetFieldAssigned (VariableInfo var, string name)
580 if (!var.IsParameter && Reachability.IsUnreachable)
584 var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
587 public Reachability Reachability {
593 public void Return ()
595 if (!reachability.IsUnreachable) {
597 reachability.SetReturns ();
603 if (!reachability.IsUnreachable) {
605 reachability.SetBreaks ();
611 if (!reachability.IsUnreachable) {
613 reachability.SetThrows ();
619 if (!reachability.IsUnreachable) {
621 reachability.SetBarrier ();
626 // Merges a child branching.
628 public UsageVector MergeChild (FlowBranching branching)
630 UsageVector result = branching.Merge ();
632 Report.Debug (2, " MERGING CHILD", this, branching, IsDirty,
633 result.ParameterVector, result.LocalVector,
634 result.Reachability, reachability, Type);
636 Reachability new_r = result.Reachability;
638 if (branching.Type == BranchingType.Loop) {
639 bool may_leave_loop = new_r.MayBreak;
640 new_r.ResetBreaks ();
642 if (branching.Infinite && !may_leave_loop) {
643 if (new_r.Returns == FlowReturns.Sometimes) {
644 // If we're an infinite loop and do not break,
645 // the code after the loop can never be reached.
646 // However, if we may return from the loop,
647 // then we do always return (or stay in the
654 if (new_r.Returns == FlowReturns.Always) {
655 // We're either finite or we may leave the loop.
656 new_r.SetReturnsSometimes ();
658 if (new_r.Throws == FlowReturns.Always) {
659 // We're either finite or we may leave the loop.
660 new_r.SetThrowsSometimes ();
663 } else if (branching.Type == BranchingType.Switch)
664 new_r.ResetBreaks ();
667 // We've now either reached the point after the branching or we will
668 // never get there since we always return or always throw an exception.
670 // If we can reach the point after the branching, mark all locals and
671 // parameters as initialized which have been initialized in all branches
672 // we need to look at (see above).
675 if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
676 Report.Error (163, Location,
677 "Control cannot fall through from one " +
678 "case label to another");
682 if (locals != null && result.LocalVector != null)
683 locals.Or (result.LocalVector);
685 if (result.ParameterVector != null)
686 parameters.Or (result.ParameterVector);
688 reachability.Or (new_r);
690 Report.Debug (2, " MERGING CHILD DONE", this, result,
691 new_r, reachability);
698 protected void MergeFinally (FlowBranching branching, UsageVector f_origins,
699 MyBitVector f_params)
701 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
702 MyBitVector temp_params = f_params.Clone ();
703 temp_params.Or (vector.Parameters);
707 public void MergeFinally (FlowBranching branching, UsageVector f_vector,
708 UsageVector f_origins)
710 if (parameters != null) {
711 if (f_vector != null) {
712 MergeFinally (branching, f_origins, f_vector.Parameters);
713 MyBitVector.Or (ref parameters, f_vector.ParameterVector);
715 MergeFinally (branching, f_origins, parameters);
718 if (f_vector != null && f_vector.LocalVector != null)
719 MyBitVector.Or (ref locals, f_vector.LocalVector);
723 // Tells control flow analysis that the current code position may be reached with
724 // a forward jump from any of the origins listed in `origin_vectors' which is a
725 // list of UsageVectors.
727 // This is used when resolving forward gotos - in the following example, the
728 // variable `a' is uninitialized in line 8 becase this line may be reached via
729 // the goto in line 4:
739 // 8 Console.WriteLine (a);
742 public void MergeJumpOrigins (UsageVector o_vectors)
744 Report.Debug (1, " MERGING JUMP ORIGINS", this);
746 reachability = Reachability.Never ();
748 if (o_vectors == null)
753 for (UsageVector vector = o_vectors; vector != null;
754 vector = vector.Next) {
755 Report.Debug (1, " MERGING JUMP ORIGIN", vector);
758 if (locals != null && vector.Locals != null)
759 locals.Or (vector.locals);
761 if (parameters != null)
762 parameters.Or (vector.parameters);
765 if (locals != null && vector.Locals != null)
766 locals.And (vector.locals);
767 if (parameters != null)
768 parameters.And (vector.parameters);
771 Reachability.And (ref reachability, vector.Reachability, true);
774 Report.Debug (1, " MERGING JUMP ORIGINS DONE", this);
778 // This is used at the beginning of a finally block if there were
779 // any return statements in the try block or one of the catch blocks.
781 public void MergeFinallyOrigins (UsageVector f_origins)
783 Report.Debug (1, " MERGING FINALLY ORIGIN", this);
785 reachability = Reachability.Never ();
787 for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
788 Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
790 if (parameters != null)
791 parameters.And (vector.parameters);
793 Reachability.And (ref reachability, vector.Reachability, true);
796 Report.Debug (1, " MERGING FINALLY ORIGIN DONE", this);
799 public void MergeBreakOrigins (UsageVector o_vectors)
801 Report.Debug (1, " MERGING BREAK ORIGINS", this);
803 if (o_vectors == null)
808 for (UsageVector vector = o_vectors; vector != null;
809 vector = vector.Next) {
810 Report.Debug (1, " MERGING BREAK ORIGIN", vector);
813 if (locals != null && vector.Locals != null)
814 locals.Or (vector.locals);
816 if (parameters != null)
817 parameters.Or (vector.parameters);
820 if (locals != null && vector.Locals != null)
821 locals.And (vector.locals);
822 if (parameters != null)
823 parameters.And (vector.parameters);
827 Report.Debug (1, " MERGING BREAK ORIGINS DONE", this);
830 public void CheckOutParameters (FlowBranching branching)
832 if (parameters != null)
833 branching.CheckOutParameters (parameters, branching.Location);
837 // Performs an `or' operation on the locals and the parameters.
839 public void Or (UsageVector new_vector)
842 locals.Or (new_vector.locals);
843 if (parameters != null)
844 parameters.Or (new_vector.parameters);
848 // Performs an `and' operation on the locals.
850 public void AndLocals (UsageVector new_vector)
853 locals.And (new_vector.locals);
856 public bool HasParameters {
858 return parameters != null;
862 public bool HasLocals {
864 return locals != null;
869 // Returns a deep copy of the parameters.
871 public MyBitVector Parameters {
873 if (parameters != null)
874 return parameters.Clone ();
881 // Returns a deep copy of the locals.
883 public MyBitVector Locals {
886 return locals.Clone ();
892 public MyBitVector ParameterVector {
898 public MyBitVector LocalVector {
908 public override string ToString ()
910 StringBuilder sb = new StringBuilder ();
912 sb.Append ("Vector (");
919 sb.Append (reachability);
920 if (parameters != null) {
922 sb.Append (parameters);
928 return sb.ToString ();
933 // Creates a new flow branching which is contained in `parent'.
934 // You should only pass non-null for the `block' argument if this block
935 // introduces any new variables - in this case, we need to create a new
936 // usage vector with a different size than our parent's one.
938 protected FlowBranching (FlowBranching parent, BranchingType type, SiblingType stype,
939 Block block, Location loc)
949 param_map = Block.ParameterMap;
950 local_map = Block.LocalMap;
952 UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
953 vector = new UsageVector (
954 stype, parent_vector, Block, loc,
955 param_map.Length, local_map.Length);
957 param_map = Parent.param_map;
958 local_map = Parent.local_map;
959 vector = new UsageVector (
960 stype, Parent.CurrentUsageVector, null, loc);
966 public abstract UsageVector CurrentUsageVector {
971 // Creates a sibling of the current usage vector.
973 public virtual void CreateSibling (Block block, SiblingType type)
975 UsageVector vector = new UsageVector (
976 type, Parent.CurrentUsageVector, block, Location);
979 Report.Debug (1, " CREATED SIBLING", CurrentUsageVector);
982 public void CreateSibling ()
984 CreateSibling (null, SiblingType.Conditional);
987 protected abstract void AddSibling (UsageVector uv);
989 public virtual LabeledStatement LookupLabel (string name, Location loc)
992 return Parent.LookupLabel (name, loc);
996 "No such label `" + name + "' in this scope");
1000 public abstract void Label (UsageVector origin_vectors);
1003 // Check whether all `out' parameters have been assigned.
1005 public void CheckOutParameters (MyBitVector parameters, Location loc)
1007 for (int i = 0; i < param_map.Count; i++) {
1008 VariableInfo var = param_map [i];
1013 if (var.IsAssigned (parameters))
1016 Report.Error (177, loc, "The out parameter `" +
1017 var.Name + "' must be " +
1018 "assigned before control leaves the current method.");
1022 protected UsageVector Merge (UsageVector sibling_list)
1024 if (sibling_list.Next == null)
1025 return sibling_list;
1027 MyBitVector locals = null;
1028 MyBitVector parameters = null;
1030 Reachability reachability = null;
1032 Report.Debug (2, " MERGING SIBLINGS", this, Name);
1034 for (UsageVector child = sibling_list; child != null; child = child.Next) {
1035 bool do_break = (Type != BranchingType.Switch) &&
1036 (Type != BranchingType.Loop);
1038 Report.Debug (2, " MERGING SIBLING ", child,
1039 child.ParameterVector, child.LocalVector,
1040 reachability, child.Reachability, do_break);
1042 Reachability.And (ref reachability, child.Reachability, do_break);
1044 // A local variable is initialized after a flow branching if it
1045 // has been initialized in all its branches which do neither
1046 // always return or always throw an exception.
1048 // If a branch may return, but does not always return, then we
1049 // can treat it like a never-returning branch here: control will
1050 // only reach the code position after the branching if we did not
1053 // It's important to distinguish between always and sometimes
1054 // returning branches here:
1057 // 2 if (something) {
1061 // 6 Console.WriteLine (a);
1063 // The if block in lines 3-4 always returns, so we must not look
1064 // at the initialization of `a' in line 4 - thus it'll still be
1065 // uninitialized in line 6.
1067 // On the other hand, the following is allowed:
1074 // 6 Console.WriteLine (a);
1076 // Here, `a' is initialized in line 3 and we must not look at
1077 // line 5 since it always returns.
1079 bool do_break_2 = (child.Type != SiblingType.Block) &&
1080 (child.Type != SiblingType.SwitchSection);
1081 bool always_throws = (child.Type != SiblingType.Try) &&
1082 child.Reachability.AlwaysThrows;
1083 bool unreachable = always_throws ||
1084 (do_break_2 && child.Reachability.AlwaysBreaks) ||
1085 child.Reachability.AlwaysReturns ||
1086 child.Reachability.AlwaysHasBarrier;
1088 Report.Debug (2, " MERGING SIBLING #1", reachability,
1089 Type, child.Type, child.Reachability.IsUnreachable,
1090 do_break_2, always_throws, unreachable);
1092 if (!unreachable && (child.LocalVector != null))
1093 MyBitVector.And (ref locals, child.LocalVector);
1095 // An `out' parameter must be assigned in all branches which do
1096 // not always throw an exception.
1097 if ((child.ParameterVector != null) && !child.Reachability.AlwaysThrows)
1098 MyBitVector.And (ref parameters, child.ParameterVector);
1100 Report.Debug (2, " MERGING SIBLING #2", parameters, locals);
1103 if (reachability == null)
1104 reachability = Reachability.Never ();
1106 Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
1107 reachability, Infinite);
1109 return new UsageVector (
1110 parameters, locals, reachability, null, Location);
1113 protected abstract UsageVector Merge ();
1116 // Merge a child branching.
1118 public UsageVector MergeChild (FlowBranching child)
1120 return CurrentUsageVector.MergeChild (child);
1124 // Does the toplevel merging.
1126 public Reachability MergeTopBlock ()
1128 if ((Type != BranchingType.Block) || (Block == null))
1129 throw new NotSupportedException ();
1131 UsageVector vector = new UsageVector (
1132 SiblingType.Conditional, null, Block, Location,
1133 param_map.Length, local_map.Length);
1135 UsageVector result = vector.MergeChild (this);
1137 Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
1139 if ((vector.Reachability.Throws != FlowReturns.Always) &&
1140 (vector.Reachability.Barrier != FlowReturns.Always))
1141 CheckOutParameters (vector.Parameters, Location);
1143 return result.Reachability;
1147 // Checks whether we're in a `try' block.
1149 public virtual bool InTryOrCatch (bool is_return)
1151 if ((Block != null) && Block.IsDestructor)
1153 else if (!is_return &&
1154 ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
1156 else if (Parent != null)
1157 return Parent.InTryOrCatch (is_return);
1163 // Checks whether we're in a `catch' block.
1165 public virtual bool InCatch ()
1168 return Parent.InCatch ();
1174 // Checks whether we're in a `finally' block.
1176 public virtual bool InFinally (bool is_return)
1179 ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
1181 else if (Parent != null)
1182 return Parent.InFinally (is_return);
1187 public virtual bool InLoop ()
1189 if (Type == BranchingType.Loop)
1191 else if (Parent != null)
1192 return Parent.InLoop ();
1197 public virtual bool InSwitch ()
1199 if (Type == BranchingType.Switch)
1201 else if (Parent != null)
1202 return Parent.InSwitch ();
1207 public virtual bool BreakCrossesTryCatchBoundary ()
1209 if ((Type == BranchingType.Loop) || (Type == BranchingType.Switch))
1211 else if (Parent != null)
1212 return Parent.BreakCrossesTryCatchBoundary ();
1217 public virtual void AddFinallyVector (UsageVector vector)
1220 Parent.AddFinallyVector (vector);
1221 else if ((Block == null) || !Block.IsDestructor)
1222 throw new NotSupportedException ();
1225 public virtual void AddBreakVector (UsageVector vector)
1228 Parent.AddBreakVector (vector);
1229 else if ((Block == null) || !Block.IsDestructor)
1230 throw new NotSupportedException ();
1233 public virtual void StealFinallyClauses (ref ArrayList list)
1236 Parent.StealFinallyClauses (ref list);
1239 public bool IsAssigned (VariableInfo vi)
1241 return CurrentUsageVector.IsAssigned (vi);
1244 public bool IsFieldAssigned (VariableInfo vi, string field_name)
1246 if (CurrentUsageVector.IsAssigned (vi))
1249 return CurrentUsageVector.IsFieldAssigned (vi, field_name);
1252 public void SetAssigned (VariableInfo vi)
1254 CurrentUsageVector.SetAssigned (vi);
1257 public void SetFieldAssigned (VariableInfo vi, string name)
1259 CurrentUsageVector.SetFieldAssigned (vi, name);
1262 public override string ToString ()
1264 StringBuilder sb = new StringBuilder ();
1265 sb.Append (GetType ());
1271 if (Block != null) {
1273 sb.Append (Block.ID);
1275 sb.Append (Block.StartLocation);
1278 // sb.Append (Siblings.Length);
1279 // sb.Append (" - ");
1280 sb.Append (CurrentUsageVector);
1282 return sb.ToString ();
1285 public string Name {
1287 return String.Format ("{0} ({1}:{2}:{3})",
1288 GetType (), id, Type, Location);
1293 public class FlowBranchingBlock : FlowBranching
1295 UsageVector sibling_list = null;
1297 public FlowBranchingBlock (FlowBranching parent, BranchingType type,
1298 SiblingType stype, Block block, Location loc)
1299 : base (parent, type, stype, block, loc)
1302 public override UsageVector CurrentUsageVector {
1303 get { return sibling_list; }
1306 protected override void AddSibling (UsageVector sibling)
1308 sibling.Next = sibling_list;
1309 sibling_list = sibling;
1312 public override LabeledStatement LookupLabel (string name, Location loc)
1315 return base.LookupLabel (name, loc);
1317 LabeledStatement s = Block.LookupLabel (name);
1321 return base.LookupLabel (name, loc);
1324 public override void Label (UsageVector origin_vectors)
1326 if (!CurrentUsageVector.Reachability.IsUnreachable) {
1327 UsageVector vector = CurrentUsageVector.Clone ();
1328 vector.Next = origin_vectors;
1329 origin_vectors = vector;
1332 CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1335 protected override UsageVector Merge ()
1337 return Merge (sibling_list);
1341 public class FlowBranchingLoop : FlowBranchingBlock
1343 UsageVector break_origins;
1345 public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
1346 : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
1349 public override void AddBreakVector (UsageVector vector)
1351 vector = vector.Clone ();
1352 vector.Next = break_origins;
1353 break_origins = vector;
1356 protected override UsageVector Merge ()
1358 UsageVector vector = base.Merge ();
1360 vector.MergeBreakOrigins (break_origins);
1366 public class FlowBranchingException : FlowBranching
1368 ExceptionStatement stmt;
1369 UsageVector current_vector;
1370 UsageVector catch_vectors;
1371 UsageVector finally_vector;
1372 UsageVector finally_origins;
1376 public FlowBranchingException (FlowBranching parent,
1377 ExceptionStatement stmt)
1378 : base (parent, BranchingType.Exception, SiblingType.Try,
1382 this.emit_finally = true;
1385 protected override void AddSibling (UsageVector sibling)
1387 if (sibling.Type == SiblingType.Try) {
1388 sibling.Next = catch_vectors;
1389 catch_vectors = sibling;
1391 } else if (sibling.Type == SiblingType.Catch) {
1392 sibling.Next = catch_vectors;
1393 catch_vectors = sibling;
1395 } else if (sibling.Type == SiblingType.Finally) {
1396 sibling.MergeFinallyOrigins (finally_origins);
1397 finally_vector = sibling;
1400 throw new InvalidOperationException ();
1402 current_vector = sibling;
1405 public override UsageVector CurrentUsageVector {
1406 get { return current_vector; }
1409 public override bool InTryOrCatch (bool is_return)
1411 return finally_vector == null;
1414 public override bool InCatch ()
1416 return !in_try && (finally_vector == null);
1419 public override bool InFinally (bool is_return)
1421 return finally_vector != null;
1424 public override bool BreakCrossesTryCatchBoundary ()
1429 public override void AddFinallyVector (UsageVector vector)
1431 vector = vector.Clone ();
1432 vector.Next = finally_origins;
1433 finally_origins = vector;
1436 public override void StealFinallyClauses (ref ArrayList list)
1439 list = new ArrayList ();
1441 emit_finally = false;
1442 base.StealFinallyClauses (ref list);
1445 public bool EmitFinally {
1446 get { return emit_finally; }
1449 public override LabeledStatement LookupLabel (string name, Location loc)
1451 if (current_vector.Block == null)
1452 return base.LookupLabel (name, loc);
1454 LabeledStatement s = current_vector.Block.LookupLabel (name);
1458 if (finally_vector != null) {
1460 157, loc, "Control can not leave the body " +
1461 "of the finally block");
1465 return base.LookupLabel (name, loc);
1468 public override void Label (UsageVector origin_vectors)
1470 CurrentUsageVector.MergeJumpOrigins (origin_vectors);
1473 protected override UsageVector Merge ()
1475 UsageVector vector = Merge (catch_vectors);
1477 vector.MergeFinally (this, finally_vector, finally_origins);
1484 // This is used by the flow analysis code to keep track of the type of local variables
1487 // The flow code uses a BitVector to keep track of whether a variable has been assigned
1488 // or not. This is easy for fundamental types (int, char etc.) or reference types since
1489 // you can only assign the whole variable as such.
1491 // For structs, we also need to keep track of all its fields. To do this, we allocate one
1492 // bit for the struct itself (it's used if you assign/access the whole struct) followed by
1493 // one bit for each of its fields.
1495 // This class computes this `layout' for each type.
1497 public class TypeInfo
1499 public readonly Type Type;
1502 // Total number of bits a variable of this type consumes in the flow vector.
1504 public readonly int TotalLength;
1507 // Number of bits the simple fields of a variable of this type consume
1508 // in the flow vector.
1510 public readonly int Length;
1513 // This is only used by sub-structs.
1515 public readonly int Offset;
1518 // If this is a struct.
1520 public readonly bool IsStruct;
1523 // If this is a struct, all fields which are structs theirselves.
1525 public TypeInfo[] SubStructInfo;
1527 protected readonly StructInfo struct_info;
1528 private static Hashtable type_hash = new Hashtable ();
1530 public static TypeInfo GetTypeInfo (Type type)
1532 TypeInfo info = (TypeInfo) type_hash [type];
1536 info = new TypeInfo (type);
1537 type_hash.Add (type, info);
1541 public static TypeInfo GetTypeInfo (TypeContainer tc)
1543 TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
1547 info = new TypeInfo (tc);
1548 type_hash.Add (tc.TypeBuilder, info);
1552 private TypeInfo (Type type)
1556 struct_info = StructInfo.GetStructInfo (type);
1557 if (struct_info != null) {
1558 Length = struct_info.Length;
1559 TotalLength = struct_info.TotalLength;
1560 SubStructInfo = struct_info.StructFields;
1569 private TypeInfo (TypeContainer tc)
1571 this.Type = tc.TypeBuilder;
1573 struct_info = StructInfo.GetStructInfo (tc);
1574 if (struct_info != null) {
1575 Length = struct_info.Length;
1576 TotalLength = struct_info.TotalLength;
1577 SubStructInfo = struct_info.StructFields;
1586 protected TypeInfo (StructInfo struct_info, int offset)
1588 this.struct_info = struct_info;
1589 this.Offset = offset;
1590 this.Length = struct_info.Length;
1591 this.TotalLength = struct_info.TotalLength;
1592 this.SubStructInfo = struct_info.StructFields;
1593 this.Type = struct_info.Type;
1594 this.IsStruct = true;
1597 public int GetFieldIndex (string name)
1599 if (struct_info == null)
1602 return struct_info [name];
1605 public TypeInfo GetSubStruct (string name)
1607 if (struct_info == null)
1610 return struct_info.GetStructField (name);
1614 // A struct's constructor must always assign all fields.
1615 // This method checks whether it actually does so.
1617 public bool IsFullyInitialized (FlowBranching branching, VariableInfo vi, Location loc)
1619 if (struct_info == null)
1623 for (int i = 0; i < struct_info.Count; i++) {
1624 FieldInfo field = struct_info.Fields [i];
1626 if (!branching.IsFieldAssigned (vi, field.Name)) {
1627 Report.Error (171, loc,
1628 "Field `" + TypeManager.CSharpName (Type) +
1629 "." + field.Name + "' must be fully initialized " +
1630 "before control leaves the constructor");
1638 public override string ToString ()
1640 return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
1641 Type, Offset, Length, TotalLength);
1644 protected class StructInfo {
1645 public readonly Type Type;
1646 public readonly FieldInfo[] Fields;
1647 public readonly TypeInfo[] StructFields;
1648 public readonly int Count;
1649 public readonly int CountPublic;
1650 public readonly int CountNonPublic;
1651 public readonly int Length;
1652 public readonly int TotalLength;
1653 public readonly bool HasStructFields;
1655 private static Hashtable field_type_hash = new Hashtable ();
1656 private Hashtable struct_field_hash;
1657 private Hashtable field_hash;
1659 protected bool InTransit = false;
1661 // Private constructor. To save memory usage, we only need to create one instance
1662 // of this class per struct type.
1663 private StructInfo (Type type)
1667 field_type_hash.Add (type, this);
1669 if (type is TypeBuilder) {
1670 TypeContainer tc = TypeManager.LookupTypeContainer (type);
1672 ArrayList fields = tc.Fields;
1674 ArrayList public_fields = new ArrayList ();
1675 ArrayList non_public_fields = new ArrayList ();
1677 if (fields != null) {
1678 foreach (Field field in fields) {
1679 if ((field.ModFlags & Modifiers.STATIC) != 0)
1681 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
1682 public_fields.Add (field.FieldBuilder);
1684 non_public_fields.Add (field.FieldBuilder);
1688 CountPublic = public_fields.Count;
1689 CountNonPublic = non_public_fields.Count;
1690 Count = CountPublic + CountNonPublic;
1692 Fields = new FieldInfo [Count];
1693 public_fields.CopyTo (Fields, 0);
1694 non_public_fields.CopyTo (Fields, CountPublic);
1695 } else if (type is GenericTypeParameterBuilder) {
1696 CountPublic = CountNonPublic = Count = 0;
1698 Fields = new FieldInfo [0];
1700 FieldInfo[] public_fields = type.GetFields (
1701 BindingFlags.Instance|BindingFlags.Public);
1702 FieldInfo[] non_public_fields = type.GetFields (
1703 BindingFlags.Instance|BindingFlags.NonPublic);
1705 CountPublic = public_fields.Length;
1706 CountNonPublic = non_public_fields.Length;
1707 Count = CountPublic + CountNonPublic;
1709 Fields = new FieldInfo [Count];
1710 public_fields.CopyTo (Fields, 0);
1711 non_public_fields.CopyTo (Fields, CountPublic);
1714 struct_field_hash = new Hashtable ();
1715 field_hash = new Hashtable ();
1718 StructFields = new TypeInfo [Count];
1719 StructInfo[] sinfo = new StructInfo [Count];
1723 for (int i = 0; i < Count; i++) {
1724 FieldInfo field = (FieldInfo) Fields [i];
1726 sinfo [i] = GetStructInfo (field.FieldType);
1727 if (sinfo [i] == null)
1728 field_hash.Add (field.Name, ++Length);
1729 else if (sinfo [i].InTransit) {
1730 Report.Error (523, String.Format (
1731 "Struct member '{0}.{1}' of type '{2}' causes " +
1732 "a cycle in the structure layout",
1733 type, field.Name, sinfo [i].Type));
1741 TotalLength = Length + 1;
1742 for (int i = 0; i < Count; i++) {
1743 FieldInfo field = (FieldInfo) Fields [i];
1745 if (sinfo [i] == null)
1748 field_hash.Add (field.Name, TotalLength);
1750 HasStructFields = true;
1751 StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
1752 struct_field_hash.Add (field.Name, StructFields [i]);
1753 TotalLength += sinfo [i].TotalLength;
1757 public int this [string name] {
1759 if (field_hash.Contains (name))
1760 return (int) field_hash [name];
1766 public TypeInfo GetStructField (string name)
1768 return (TypeInfo) struct_field_hash [name];
1771 public static StructInfo GetStructInfo (Type type)
1773 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
1774 TypeManager.IsBuiltinType (type))
1777 StructInfo info = (StructInfo) field_type_hash [type];
1781 return new StructInfo (type);
1784 public static StructInfo GetStructInfo (TypeContainer tc)
1786 StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
1790 return new StructInfo (tc.TypeBuilder);
1796 // This is used by the flow analysis code to store information about a single local variable
1797 // or parameter. Depending on the variable's type, we need to allocate one or more elements
1798 // in the BitVector - if it's a fundamental or reference type, we just need to know whether
1799 // it has been assigned or not, but for structs, we need this information for each of its fields.
1801 public class VariableInfo {
1802 public readonly string Name;
1803 public readonly TypeInfo TypeInfo;
1806 // The bit offset of this variable in the flow vector.
1808 public readonly int Offset;
1811 // The number of bits this variable needs in the flow vector.
1812 // The first bit always specifies whether the variable as such has been assigned while
1813 // the remaining bits contain this information for each of a struct's fields.
1815 public readonly int Length;
1818 // If this is a parameter of local variable.
1820 public readonly bool IsParameter;
1822 public readonly LocalInfo LocalInfo;
1823 public readonly int ParameterIndex;
1825 readonly VariableInfo Parent;
1826 VariableInfo[] sub_info;
1828 protected VariableInfo (string name, Type type, int offset)
1831 this.Offset = offset;
1832 this.TypeInfo = TypeInfo.GetTypeInfo (type);
1834 Length = TypeInfo.TotalLength;
1839 protected VariableInfo (VariableInfo parent, TypeInfo type)
1841 this.Name = parent.Name;
1842 this.TypeInfo = type;
1843 this.Offset = parent.Offset + type.Offset;
1844 this.Parent = parent;
1845 this.Length = type.TotalLength;
1847 this.IsParameter = parent.IsParameter;
1848 this.LocalInfo = parent.LocalInfo;
1849 this.ParameterIndex = parent.ParameterIndex;
1854 protected void Initialize ()
1856 TypeInfo[] sub_fields = TypeInfo.SubStructInfo;
1857 if (sub_fields != null) {
1858 sub_info = new VariableInfo [sub_fields.Length];
1859 for (int i = 0; i < sub_fields.Length; i++) {
1860 if (sub_fields [i] != null)
1861 sub_info [i] = new VariableInfo (this, sub_fields [i]);
1864 sub_info = new VariableInfo [0];
1867 public VariableInfo (LocalInfo local_info, int offset)
1868 : this (local_info.Name, local_info.VariableType, offset)
1870 this.LocalInfo = local_info;
1871 this.IsParameter = false;
1874 public VariableInfo (string name, Type type, int param_idx, int offset)
1875 : this (name, type, offset)
1877 this.ParameterIndex = param_idx;
1878 this.IsParameter = true;
1881 public bool IsAssigned (EmitContext ec)
1883 return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (this);
1886 public bool IsAssigned (EmitContext ec, Location loc)
1888 if (IsAssigned (ec))
1891 Report.Error (165, loc,
1892 "Use of unassigned local variable `" + Name + "'");
1893 ec.CurrentBranching.SetAssigned (this);
1897 public bool IsAssigned (MyBitVector vector)
1899 if (vector [Offset])
1902 for (VariableInfo parent = Parent; parent != null; parent = parent.Parent)
1903 if (vector [parent.Offset])
1906 // Return unless this is a struct.
1907 if (!TypeInfo.IsStruct)
1910 // Ok, so each field must be assigned.
1911 for (int i = 0; i < TypeInfo.Length; i++) {
1912 if (!vector [Offset + i + 1])
1916 // Ok, now check all fields which are structs.
1917 for (int i = 0; i < sub_info.Length; i++) {
1918 VariableInfo sinfo = sub_info [i];
1922 if (!sinfo.IsAssigned (vector))
1926 vector [Offset] = true;
1930 public void SetAssigned (EmitContext ec)
1932 if (ec.DoFlowAnalysis)
1933 ec.CurrentBranching.SetAssigned (this);
1936 public void SetAssigned (MyBitVector vector)
1938 vector [Offset] = true;
1941 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
1943 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsFieldAssigned (this, name))
1946 Report.Error (170, loc,
1947 "Use of possibly unassigned field `" + name + "'");
1948 ec.CurrentBranching.SetFieldAssigned (this, name);
1952 public bool IsFieldAssigned (MyBitVector vector, string field_name)
1954 int field_idx = TypeInfo.GetFieldIndex (field_name);
1959 return vector [Offset + field_idx];
1962 public void SetFieldAssigned (EmitContext ec, string name)
1964 if (ec.DoFlowAnalysis)
1965 ec.CurrentBranching.SetFieldAssigned (this, name);
1968 public void SetFieldAssigned (MyBitVector vector, string field_name)
1970 int field_idx = TypeInfo.GetFieldIndex (field_name);
1975 vector [Offset + field_idx] = true;
1978 public VariableInfo GetSubStruct (string name)
1980 TypeInfo type = TypeInfo.GetSubStruct (name);
1985 return new VariableInfo (this, type);
1988 public override string ToString ()
1990 return String.Format ("VariableInfo ({0}:{1}:{2}:{3}:{4})",
1991 Name, TypeInfo, Offset, Length, IsParameter);
1996 // This is used by the flow code to hold the `layout' of the flow vector for
1997 // all locals and all parameters (ie. we create one instance of this class for the
1998 // locals and another one for the params).
2000 public class VariableMap {
2002 // The number of variables in the map.
2004 public readonly int Count;
2007 // Total length of the flow vector for this map.
2009 public readonly int Length;
2013 public VariableMap (InternalParameters ip)
2015 Count = ip != null ? ip.Count : 0;
2017 // Dont bother allocating anything!
2023 for (int i = 0; i < Count; i++) {
2024 Parameter.Modifier mod = ip.ParameterModifier (i);
2026 if ((mod & Parameter.Modifier.OUT) == 0)
2029 // Dont allocate till we find an out var.
2031 map = new VariableInfo [Count];
2033 map [i] = new VariableInfo (ip.ParameterName (i),
2034 TypeManager.GetElementType (ip.ParameterType (i)), i, Length);
2036 Length += map [i].Length;
2040 public VariableMap (LocalInfo[] locals)
2041 : this (null, locals)
2044 public VariableMap (VariableMap parent, LocalInfo[] locals)
2046 int offset = 0, start = 0;
2047 if (parent != null && parent.map != null) {
2048 offset = parent.Length;
2049 start = parent.Count;
2052 Count = locals.Length + start;
2057 map = new VariableInfo [Count];
2060 if (parent != null && parent.map != null) {
2061 parent.map.CopyTo (map, 0);
2064 for (int i = start; i < Count; i++) {
2065 LocalInfo li = locals [i-start];
2067 if (li.VariableType == null)
2070 map [i] = li.VariableInfo = new VariableInfo (li, Length);
2071 Length += map [i].Length;
2076 // Returns the VariableInfo for variable @index or null if we don't need to
2077 // compute assignment info for this variable.
2079 public VariableInfo this [int index] {
2088 public override string ToString ()
2090 return String.Format ("VariableMap ({0}:{1})", Count, Length);
2095 // This is a special bit vector which can inherit from another bit vector doing a
2096 // copy-on-write strategy. The inherited vector may have a smaller size than the
2099 public class MyBitVector {
2100 public readonly int Count;
2101 public readonly MyBitVector InheritsFrom;
2106 public MyBitVector (int Count)
2107 : this (null, Count)
2110 public MyBitVector (MyBitVector InheritsFrom, int Count)
2112 this.InheritsFrom = InheritsFrom;
2117 // Checks whether this bit vector has been modified. After setting this to true,
2118 // we won't use the inherited vector anymore, but our own copy of it.
2120 public bool IsDirty {
2127 initialize_vector ();
2132 // Get/set bit `index' in the bit vector.
2134 public bool this [int index]
2138 throw new ArgumentOutOfRangeException ();
2140 // We're doing a "copy-on-write" strategy here; as long
2141 // as nobody writes to the array, we can use our parent's
2142 // copy instead of duplicating the vector.
2145 return vector [index];
2146 else if (InheritsFrom != null) {
2147 BitArray inherited = InheritsFrom.Vector;
2149 if (index < inherited.Count)
2150 return inherited [index];
2159 throw new ArgumentOutOfRangeException ();
2161 // Only copy the vector if we're actually modifying it.
2163 if (this [index] != value) {
2164 initialize_vector ();
2166 vector [index] = value;
2172 // If you explicitly convert the MyBitVector to a BitArray, you will get a deep
2173 // copy of the bit vector.
2175 public static explicit operator BitArray (MyBitVector vector)
2177 vector.initialize_vector ();
2178 return vector.Vector;
2182 // Performs an `or' operation on the bit vector. The `new_vector' may have a
2183 // different size than the current one.
2185 public void Or (MyBitVector new_vector)
2187 BitArray new_array = new_vector.Vector;
2189 initialize_vector ();
2192 if (vector.Count < new_array.Count)
2193 upper = vector.Count;
2195 upper = new_array.Count;
2197 for (int i = 0; i < upper; i++)
2198 vector [i] = vector [i] | new_array [i];
2202 // Perfonrms an `and' operation on the bit vector. The `new_vector' may have
2203 // a different size than the current one.
2205 public void And (MyBitVector new_vector)
2207 BitArray new_array = new_vector.Vector;
2209 initialize_vector ();
2212 if (vector.Count < new_array.Count)
2213 lower = upper = vector.Count;
2215 lower = new_array.Count;
2216 upper = vector.Count;
2219 for (int i = 0; i < lower; i++)
2220 vector [i] = vector [i] & new_array [i];
2222 for (int i = lower; i < upper; i++)
2226 public static void And (ref MyBitVector target, MyBitVector vector)
2229 target.And (vector);
2231 target = vector.Clone ();
2234 public static void Or (ref MyBitVector target, MyBitVector vector)
2239 target = vector.Clone ();
2243 // This does a deep copy of the bit vector.
2245 public MyBitVector Clone ()
2247 MyBitVector retval = new MyBitVector (Count);
2249 retval.Vector = Vector;
2258 else if (!is_dirty && (InheritsFrom != null))
2259 return InheritsFrom.Vector;
2261 initialize_vector ();
2267 initialize_vector ();
2269 for (int i = 0; i < System.Math.Min (vector.Count, value.Count); i++)
2270 vector [i] = value [i];
2274 void initialize_vector ()
2279 vector = new BitArray (Count, false);
2280 if (InheritsFrom != null)
2281 Vector = InheritsFrom.Vector;
2286 public override string ToString ()
2288 StringBuilder sb = new StringBuilder ("{");
2290 BitArray vector = Vector;
2293 for (int i = 0; i < vector.Count; i++) {
2294 sb.Append (vector [i] ? "1" : "0");
2298 return sb.ToString ();