2 // statement.cs: Statement representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Martin Baulig (martin@gnome.org)
8 // (C) 2001, 2002 Ximian, Inc.
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Diagnostics;
17 namespace Mono.CSharp {
19 using System.Collections;
21 public abstract class Statement {
25 /// Resolves the statement, true means that all sub-statements
28 public virtual bool Resolve (EmitContext ec)
34 /// Return value indicates whether all code paths emitted return.
36 protected abstract bool DoEmit (EmitContext ec);
39 /// Return value indicates whether all code paths emitted return.
41 public virtual bool Emit (EmitContext ec)
47 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
53 if (e.Type != TypeManager.bool_type){
54 e = Expression.ConvertImplicit (ec, e, TypeManager.bool_type,
60 31, loc, "Can not convert the expression to a boolean");
69 /// Encapsulates the emission of a boolean test and jumping to a
72 /// This will emit the bool expression in `bool_expr' and if
73 /// `target_is_for_true' is true, then the code will generate a
74 /// brtrue to the target. Otherwise a brfalse.
76 public static void EmitBoolExpression (EmitContext ec, Expression bool_expr,
77 Label target, bool target_is_for_true)
79 ILGenerator ig = ec.ig;
82 if (bool_expr is Unary){
83 Unary u = (Unary) bool_expr;
85 if (u.Oper == Unary.Operator.LogicalNot){
88 u.EmitLogicalNot (ec);
90 } else if (bool_expr is Binary){
91 Binary b = (Binary) bool_expr;
93 if (b.EmitBranchable (ec, target, target_is_for_true))
100 if (target_is_for_true){
102 ig.Emit (OpCodes.Brfalse, target);
104 ig.Emit (OpCodes.Brtrue, target);
107 ig.Emit (OpCodes.Brtrue, target);
109 ig.Emit (OpCodes.Brfalse, target);
113 public static void Warning_DeadCodeFound (Location loc)
115 Report.Warning (162, loc, "Unreachable code detected");
119 public class EmptyStatement : Statement {
120 public override bool Resolve (EmitContext ec)
125 protected override bool DoEmit (EmitContext ec)
131 public class If : Statement {
133 public Statement TrueStatement;
134 public Statement FalseStatement;
136 public If (Expression expr, Statement trueStatement, Location l)
139 TrueStatement = trueStatement;
143 public If (Expression expr,
144 Statement trueStatement,
145 Statement falseStatement,
149 TrueStatement = trueStatement;
150 FalseStatement = falseStatement;
154 public override bool Resolve (EmitContext ec)
156 Report.Debug (1, "START IF BLOCK", loc);
158 expr = ResolveBoolean (ec, expr, loc);
163 ec.StartFlowBranching (FlowBranchingType.BLOCK, loc);
165 if (!TrueStatement.Resolve (ec)) {
166 ec.KillFlowBranching ();
170 ec.CurrentBranching.CreateSibling ();
172 if ((FalseStatement != null) && !FalseStatement.Resolve (ec)) {
173 ec.KillFlowBranching ();
177 ec.EndFlowBranching ();
179 Report.Debug (1, "END IF BLOCK", loc);
184 protected override bool DoEmit (EmitContext ec)
186 ILGenerator ig = ec.ig;
187 Label false_target = ig.DefineLabel ();
189 bool is_true_ret, is_false_ret;
192 // Dead code elimination
194 if (expr is BoolConstant){
195 bool take = ((BoolConstant) expr).Value;
198 if (FalseStatement != null){
199 Warning_DeadCodeFound (FalseStatement.loc);
201 return TrueStatement.Emit (ec);
203 Warning_DeadCodeFound (TrueStatement.loc);
204 if (FalseStatement != null)
205 return FalseStatement.Emit (ec);
209 EmitBoolExpression (ec, expr, false_target, false);
211 is_true_ret = TrueStatement.Emit (ec);
212 is_false_ret = is_true_ret;
214 if (FalseStatement != null){
215 bool branch_emitted = false;
217 end = ig.DefineLabel ();
219 ig.Emit (OpCodes.Br, end);
220 branch_emitted = true;
223 ig.MarkLabel (false_target);
224 is_false_ret = FalseStatement.Emit (ec);
229 ig.MarkLabel (false_target);
230 is_false_ret = false;
233 return is_true_ret && is_false_ret;
237 public class Do : Statement {
238 public Expression expr;
239 public readonly Statement EmbeddedStatement;
240 bool infinite, may_return;
242 public Do (Statement statement, Expression boolExpr, Location l)
245 EmbeddedStatement = statement;
249 public override bool Resolve (EmitContext ec)
253 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
255 if (!EmbeddedStatement.Resolve (ec))
258 expr = ResolveBoolean (ec, expr, loc);
261 else if (expr is BoolConstant){
262 bool res = ((BoolConstant) expr).Value;
268 ec.CurrentBranching.Infinite = infinite;
269 FlowReturns returns = ec.EndFlowBranching ();
270 may_return = returns != FlowReturns.NEVER;
275 protected override bool DoEmit (EmitContext ec)
277 ILGenerator ig = ec.ig;
278 Label loop = ig.DefineLabel ();
279 Label old_begin = ec.LoopBegin;
280 Label old_end = ec.LoopEnd;
281 bool old_inloop = ec.InLoop;
282 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
284 ec.LoopBegin = ig.DefineLabel ();
285 ec.LoopEnd = ig.DefineLabel ();
287 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
290 EmbeddedStatement.Emit (ec);
291 ig.MarkLabel (ec.LoopBegin);
294 // Dead code elimination
296 if (expr is BoolConstant){
297 bool res = ((BoolConstant) expr).Value;
300 ec.ig.Emit (OpCodes.Br, loop);
302 EmitBoolExpression (ec, expr, loop, true);
304 ig.MarkLabel (ec.LoopEnd);
306 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
307 ec.LoopBegin = old_begin;
308 ec.LoopEnd = old_end;
309 ec.InLoop = old_inloop;
312 return may_return == false;
318 public class While : Statement {
319 public Expression expr;
320 public readonly Statement Statement;
321 bool may_return, empty, infinite;
323 public While (Expression boolExpr, Statement statement, Location l)
325 this.expr = boolExpr;
326 Statement = statement;
330 public override bool Resolve (EmitContext ec)
334 expr = ResolveBoolean (ec, expr, loc);
338 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
341 // Inform whether we are infinite or not
343 if (expr is BoolConstant){
344 BoolConstant bc = (BoolConstant) expr;
346 if (bc.Value == false){
347 Warning_DeadCodeFound (Statement.loc);
353 // We are not infinite, so the loop may or may not be executed.
355 ec.CurrentBranching.CreateSibling ();
358 if (!Statement.Resolve (ec))
362 ec.KillFlowBranching ();
364 ec.CurrentBranching.Infinite = infinite;
365 FlowReturns returns = ec.EndFlowBranching ();
366 may_return = returns != FlowReturns.NEVER;
372 protected override bool DoEmit (EmitContext ec)
377 ILGenerator ig = ec.ig;
378 Label old_begin = ec.LoopBegin;
379 Label old_end = ec.LoopEnd;
380 bool old_inloop = ec.InLoop;
381 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
384 ec.LoopBegin = ig.DefineLabel ();
385 ec.LoopEnd = ig.DefineLabel ();
387 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
390 // Inform whether we are infinite or not
392 if (expr is BoolConstant){
393 BoolConstant bc = (BoolConstant) expr;
395 ig.MarkLabel (ec.LoopBegin);
397 ig.Emit (OpCodes.Br, ec.LoopBegin);
400 // Inform that we are infinite (ie, `we return'), only
401 // if we do not `break' inside the code.
403 ret = may_return == false;
404 ig.MarkLabel (ec.LoopEnd);
406 Label while_loop = ig.DefineLabel ();
408 ig.Emit (OpCodes.Br, ec.LoopBegin);
409 ig.MarkLabel (while_loop);
413 ig.MarkLabel (ec.LoopBegin);
415 EmitBoolExpression (ec, expr, while_loop, true);
416 ig.MarkLabel (ec.LoopEnd);
421 ec.LoopBegin = old_begin;
422 ec.LoopEnd = old_end;
423 ec.InLoop = old_inloop;
424 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
430 public class For : Statement {
432 readonly Statement InitStatement;
433 readonly Statement Increment;
434 readonly Statement Statement;
435 bool may_return, infinite, empty;
437 public For (Statement initStatement,
443 InitStatement = initStatement;
445 Increment = increment;
446 Statement = statement;
450 public override bool Resolve (EmitContext ec)
454 if (InitStatement != null){
455 if (!InitStatement.Resolve (ec))
460 Test = ResolveBoolean (ec, Test, loc);
463 else if (Test is BoolConstant){
464 BoolConstant bc = (BoolConstant) Test;
466 if (bc.Value == false){
467 Warning_DeadCodeFound (Statement.loc);
475 if (Increment != null){
476 if (!Increment.Resolve (ec))
480 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
482 ec.CurrentBranching.CreateSibling ();
484 if (!Statement.Resolve (ec))
488 ec.KillFlowBranching ();
490 ec.CurrentBranching.Infinite = infinite;
491 FlowReturns returns = ec.EndFlowBranching ();
492 may_return = returns != FlowReturns.NEVER;
498 protected override bool DoEmit (EmitContext ec)
503 ILGenerator ig = ec.ig;
504 Label old_begin = ec.LoopBegin;
505 Label old_end = ec.LoopEnd;
506 bool old_inloop = ec.InLoop;
507 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
508 Label loop = ig.DefineLabel ();
509 Label test = ig.DefineLabel ();
511 if (InitStatement != null)
512 if (! (InitStatement is EmptyStatement))
513 InitStatement.Emit (ec);
515 ec.LoopBegin = ig.DefineLabel ();
516 ec.LoopEnd = ig.DefineLabel ();
518 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
520 ig.Emit (OpCodes.Br, test);
524 ig.MarkLabel (ec.LoopBegin);
525 if (!(Increment is EmptyStatement))
530 // If test is null, there is no test, and we are just
535 // The Resolve code already catches the case for Test == BoolConstant (false)
536 // so we know that this is true
538 if (Test is BoolConstant)
539 ig.Emit (OpCodes.Br, loop);
541 EmitBoolExpression (ec, Test, loop, true);
543 ig.Emit (OpCodes.Br, loop);
544 ig.MarkLabel (ec.LoopEnd);
546 ec.LoopBegin = old_begin;
547 ec.LoopEnd = old_end;
548 ec.InLoop = old_inloop;
549 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
552 // Inform whether we are infinite or not
555 if (Test is BoolConstant){
556 BoolConstant bc = (BoolConstant) Test;
559 return may_return == false;
563 return may_return == false;
567 public class StatementExpression : Statement {
570 public StatementExpression (ExpressionStatement expr, Location l)
576 public override bool Resolve (EmitContext ec)
578 expr = (Expression) expr.Resolve (ec);
582 protected override bool DoEmit (EmitContext ec)
584 ILGenerator ig = ec.ig;
586 if (expr is ExpressionStatement)
587 ((ExpressionStatement) expr).EmitStatement (ec);
590 ig.Emit (OpCodes.Pop);
596 public override string ToString ()
598 return "StatementExpression (" + expr + ")";
603 /// Implements the return statement
605 public class Return : Statement {
606 public Expression Expr;
608 public Return (Expression expr, Location l)
614 public override bool Resolve (EmitContext ec)
617 Expr = Expr.Resolve (ec);
622 FlowBranching.UsageVector vector = ec.CurrentBranching.CurrentUsageVector;
624 if (ec.CurrentBranching.InTryBlock ())
625 ec.CurrentBranching.AddFinallyVector (vector);
627 vector.CheckOutParameters (ec.CurrentBranching);
629 vector.Returns = FlowReturns.ALWAYS;
630 vector.Breaks = FlowReturns.ALWAYS;
634 protected override bool DoEmit (EmitContext ec)
637 Report.Error (157,loc,"Control can not leave the body of the finally block");
641 if (ec.ReturnType == null){
643 Report.Error (127, loc, "Return with a value not allowed here");
648 Report.Error (126, loc, "An object of type `" +
649 TypeManager.CSharpName (ec.ReturnType) + "' is " +
650 "expected for the return statement");
654 if (Expr.Type != ec.ReturnType)
655 Expr = Expression.ConvertImplicitRequired (
656 ec, Expr, ec.ReturnType, loc);
663 if (ec.InTry || ec.InCatch)
664 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
667 if (ec.InTry || ec.InCatch) {
668 if (!ec.HasReturnLabel) {
669 ec.ReturnLabel = ec.ig.DefineLabel ();
670 ec.HasReturnLabel = true;
672 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
674 ec.ig.Emit (OpCodes.Ret);
680 public class Goto : Statement {
683 LabeledStatement label;
685 public override bool Resolve (EmitContext ec)
687 label = block.LookupLabel (target);
691 "No such label `" + target + "' in this scope");
695 // If this is a forward goto.
696 if (!label.IsDefined)
697 label.AddUsageVector (ec.CurrentBranching.CurrentUsageVector);
699 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
700 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.ALWAYS;
705 public Goto (Block parent_block, string label, Location l)
707 block = parent_block;
712 public string Target {
718 protected override bool DoEmit (EmitContext ec)
720 Label l = label.LabelTarget (ec);
721 ec.ig.Emit (OpCodes.Br, l);
727 public class LabeledStatement : Statement {
728 public readonly Location Location;
736 public LabeledStatement (string label_name, Location l)
738 this.label_name = label_name;
742 public Label LabelTarget (EmitContext ec)
746 label = ec.ig.DefineLabel ();
752 public bool IsDefined {
758 public bool HasBeenReferenced {
764 public void AddUsageVector (FlowBranching.UsageVector vector)
767 vectors = new ArrayList ();
769 vectors.Add (vector.Clone ());
772 public override bool Resolve (EmitContext ec)
775 ec.CurrentBranching.CurrentUsageVector.MergeJumpOrigins (vectors);
777 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.NEVER;
778 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.NEVER;
786 protected override bool DoEmit (EmitContext ec)
789 ec.ig.MarkLabel (label);
797 /// `goto default' statement
799 public class GotoDefault : Statement {
801 public GotoDefault (Location l)
806 public override bool Resolve (EmitContext ec)
808 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
809 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.ALWAYS;
813 protected override bool DoEmit (EmitContext ec)
815 if (ec.Switch == null){
816 Report.Error (153, loc, "goto default is only valid in a switch statement");
820 if (!ec.Switch.GotDefault){
821 Report.Error (159, loc, "No default target on switch statement");
824 ec.ig.Emit (OpCodes.Br, ec.Switch.DefaultTarget);
830 /// `goto case' statement
832 public class GotoCase : Statement {
836 public GotoCase (Expression e, Location l)
842 public override bool Resolve (EmitContext ec)
844 if (ec.Switch == null){
845 Report.Error (153, loc, "goto case is only valid in a switch statement");
849 expr = expr.Resolve (ec);
853 if (!(expr is Constant)){
854 Report.Error (159, loc, "Target expression for goto case is not constant");
858 object val = Expression.ConvertIntLiteral (
859 (Constant) expr, ec.Switch.SwitchType, loc);
864 SwitchLabel sl = (SwitchLabel) ec.Switch.Elements [val];
869 "No such label 'case " + val + "': for the goto case");
872 label = sl.ILLabelCode;
874 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.UNREACHABLE;
875 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.ALWAYS;
879 protected override bool DoEmit (EmitContext ec)
881 ec.ig.Emit (OpCodes.Br, label);
886 public class Throw : Statement {
889 public Throw (Expression expr, Location l)
895 public override bool Resolve (EmitContext ec)
898 expr = expr.Resolve (ec);
902 ExprClass eclass = expr.eclass;
904 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
905 eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
906 expr.Error118 ("value, variable, property or indexer access ");
912 if ((t != TypeManager.exception_type) &&
913 !t.IsSubclassOf (TypeManager.exception_type) &&
914 !(expr is NullLiteral)) {
915 Report.Error (155, loc,
916 "The type caught or thrown must be derived " +
917 "from System.Exception");
922 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.EXCEPTION;
923 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.EXCEPTION;
927 protected override bool DoEmit (EmitContext ec)
931 ec.ig.Emit (OpCodes.Rethrow);
935 "A throw statement with no argument is only " +
936 "allowed in a catch clause");
943 ec.ig.Emit (OpCodes.Throw);
949 public class Break : Statement {
951 public Break (Location l)
956 public override bool Resolve (EmitContext ec)
958 ec.CurrentBranching.MayLeaveLoop = true;
959 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
963 protected override bool DoEmit (EmitContext ec)
965 ILGenerator ig = ec.ig;
967 if (ec.InLoop == false && ec.Switch == null){
968 Report.Error (139, loc, "No enclosing loop or switch to continue to");
972 if (ec.InTry || ec.InCatch)
973 ig.Emit (OpCodes.Leave, ec.LoopEnd);
975 ig.Emit (OpCodes.Br, ec.LoopEnd);
981 public class Continue : Statement {
983 public Continue (Location l)
988 public override bool Resolve (EmitContext ec)
990 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
994 protected override bool DoEmit (EmitContext ec)
996 Label begin = ec.LoopBegin;
999 Report.Error (139, loc, "No enclosing loop to continue to");
1004 // UGH: Non trivial. This Br might cross a try/catch boundary
1008 // try { ... } catch { continue; }
1012 // try {} catch { while () { continue; }}
1014 if (ec.TryCatchLevel > ec.LoopBeginTryCatchLevel)
1015 ec.ig.Emit (OpCodes.Leave, begin);
1016 else if (ec.TryCatchLevel < ec.LoopBeginTryCatchLevel)
1017 throw new Exception ("Should never happen");
1019 ec.ig.Emit (OpCodes.Br, begin);
1025 // This is used in the control flow analysis code to specify whether the
1026 // current code block may return to its enclosing block before reaching
1029 public enum FlowReturns {
1030 // It can never return.
1033 // This means that the block contains a conditional return statement
1037 // The code always returns, ie. there's an unconditional return / break
1041 // The code always throws an exception.
1044 // The current code block is unreachable. This happens if it's immediately
1045 // following a FlowReturns.ALWAYS block.
1050 // This is a special bit vector which can inherit from another bit vector doing a
1051 // copy-on-write strategy. The inherited vector may have a smaller size than the
1054 public class MyBitVector {
1055 public readonly int Count;
1056 public readonly MyBitVector InheritsFrom;
1061 public MyBitVector (int Count)
1062 : this (null, Count)
1065 public MyBitVector (MyBitVector InheritsFrom, int Count)
1067 this.InheritsFrom = InheritsFrom;
1072 // Checks whether this bit vector has been modified. After setting this to true,
1073 // we won't use the inherited vector anymore, but our own copy of it.
1075 public bool IsDirty {
1082 initialize_vector ();
1087 // Get/set bit `index' in the bit vector.
1089 public bool this [int index]
1093 throw new ArgumentOutOfRangeException ();
1095 // We're doing a "copy-on-write" strategy here; as long
1096 // as nobody writes to the array, we can use our parent's
1097 // copy instead of duplicating the vector.
1100 return vector [index];
1101 else if (InheritsFrom != null) {
1102 BitArray inherited = InheritsFrom.Vector;
1104 if (index < inherited.Count)
1105 return inherited [index];
1114 throw new ArgumentOutOfRangeException ();
1116 // Only copy the vector if we're actually modifying it.
1118 if (this [index] != value) {
1119 initialize_vector ();
1121 vector [index] = value;
1127 // If you explicitly convert the MyBitVector to a BitArray, you will get a deep
1128 // copy of the bit vector.
1130 public static explicit operator BitArray (MyBitVector vector)
1132 vector.initialize_vector ();
1133 return vector.Vector;
1137 // Performs an `or' operation on the bit vector. The `new_vector' may have a
1138 // different size than the current one.
1140 public void Or (MyBitVector new_vector)
1142 BitArray new_array = new_vector.Vector;
1144 initialize_vector ();
1147 if (vector.Count < new_array.Count)
1148 upper = vector.Count;
1150 upper = new_array.Count;
1152 for (int i = 0; i < upper; i++)
1153 vector [i] = vector [i] | new_array [i];
1157 // Perfonrms an `and' operation on the bit vector. The `new_vector' may have
1158 // a different size than the current one.
1160 public void And (MyBitVector new_vector)
1162 BitArray new_array = new_vector.Vector;
1164 initialize_vector ();
1167 if (vector.Count < new_array.Count)
1168 lower = upper = vector.Count;
1170 lower = new_array.Count;
1171 upper = vector.Count;
1174 for (int i = 0; i < lower; i++)
1175 vector [i] = vector [i] & new_array [i];
1177 for (int i = lower; i < upper; i++)
1182 // This does a deep copy of the bit vector.
1184 public MyBitVector Clone ()
1186 MyBitVector retval = new MyBitVector (Count);
1188 retval.Vector = Vector;
1197 else if (!is_dirty && (InheritsFrom != null))
1198 return InheritsFrom.Vector;
1200 initialize_vector ();
1206 initialize_vector ();
1208 for (int i = 0; i < Math.Min (vector.Count, value.Count); i++)
1209 vector [i] = value [i];
1213 void initialize_vector ()
1218 vector = new BitArray (Count, false);
1219 if (InheritsFrom != null)
1220 Vector = InheritsFrom.Vector;
1225 public override string ToString ()
1227 StringBuilder sb = new StringBuilder ("MyBitVector (");
1229 BitArray vector = Vector;
1233 sb.Append ("INHERITED - ");
1234 for (int i = 0; i < vector.Count; i++) {
1237 sb.Append (vector [i]);
1241 return sb.ToString ();
1246 // The type of a FlowBranching.
1248 public enum FlowBranchingType {
1249 // Normal (conditional or toplevel) block.
1266 // A new instance of this class is created every time a new block is resolved
1267 // and if there's branching in the block's control flow.
1269 public class FlowBranching {
1271 // The type of this flow branching.
1273 public readonly FlowBranchingType Type;
1276 // The block this branching is contained in. This may be null if it's not
1277 // a top-level block and it doesn't declare any local variables.
1279 public readonly Block Block;
1282 // The parent of this branching or null if this is the top-block.
1284 public readonly FlowBranching Parent;
1287 // Start-Location of this flow branching.
1289 public readonly Location Location;
1292 // A list of UsageVectors. A new vector is added each time control flow may
1293 // take a different path.
1295 public UsageVector[] Siblings;
1298 // If this is an infinite loop.
1300 public bool Infinite;
1303 // If we may leave the current loop.
1305 public bool MayLeaveLoop;
1310 InternalParameters param_info;
1312 MyStructInfo[] struct_params;
1314 ArrayList finally_vectors;
1316 static int next_id = 0;
1320 // Performs an `And' operation on the FlowReturns status
1321 // (for instance, a block only returns ALWAYS if all its siblings
1324 public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
1326 if (b == FlowReturns.UNREACHABLE)
1330 case FlowReturns.NEVER:
1331 if (b == FlowReturns.NEVER)
1332 return FlowReturns.NEVER;
1334 return FlowReturns.SOMETIMES;
1336 case FlowReturns.SOMETIMES:
1337 return FlowReturns.SOMETIMES;
1339 case FlowReturns.ALWAYS:
1340 if ((b == FlowReturns.ALWAYS) || (b == FlowReturns.EXCEPTION))
1341 return FlowReturns.ALWAYS;
1343 return FlowReturns.SOMETIMES;
1345 case FlowReturns.EXCEPTION:
1346 if (b == FlowReturns.EXCEPTION)
1347 return FlowReturns.EXCEPTION;
1348 else if (b == FlowReturns.ALWAYS)
1349 return FlowReturns.ALWAYS;
1351 return FlowReturns.SOMETIMES;
1358 // The vector contains a BitArray with information about which local variables
1359 // and parameters are already initialized at the current code position.
1361 public class UsageVector {
1363 // If this is true, then the usage vector has been modified and must be
1364 // merged when we're done with this branching.
1366 public bool IsDirty;
1369 // The number of parameters in this block.
1371 public readonly int CountParameters;
1374 // The number of locals in this block.
1376 public readonly int CountLocals;
1379 // If not null, then we inherit our state from this vector and do a
1380 // copy-on-write. If null, then we're the first sibling in a top-level
1381 // block and inherit from the empty vector.
1383 public readonly UsageVector InheritsFrom;
1388 MyBitVector locals, parameters;
1389 FlowReturns real_returns, real_breaks;
1392 static int next_id = 0;
1396 // Normally, you should not use any of these constructors.
1398 public UsageVector (UsageVector parent, int num_params, int num_locals)
1400 this.InheritsFrom = parent;
1401 this.CountParameters = num_params;
1402 this.CountLocals = num_locals;
1403 this.real_returns = FlowReturns.NEVER;
1404 this.real_breaks = FlowReturns.NEVER;
1406 if (parent != null) {
1407 locals = new MyBitVector (parent.locals, CountLocals);
1409 parameters = new MyBitVector (parent.parameters, num_params);
1410 real_returns = parent.Returns;
1411 real_breaks = parent.Breaks;
1413 locals = new MyBitVector (null, CountLocals);
1415 parameters = new MyBitVector (null, num_params);
1421 public UsageVector (UsageVector parent)
1422 : this (parent, parent.CountParameters, parent.CountLocals)
1426 // This does a deep copy of the usage vector.
1428 public UsageVector Clone ()
1430 UsageVector retval = new UsageVector (null, CountParameters, CountLocals);
1432 retval.locals = locals.Clone ();
1433 if (parameters != null)
1434 retval.parameters = parameters.Clone ();
1435 retval.real_returns = real_returns;
1436 retval.real_breaks = real_breaks;
1442 // State of parameter `number'.
1444 public bool this [int number]
1449 else if (number == 0)
1450 throw new ArgumentException ();
1452 return parameters [number - 1];
1458 else if (number == 0)
1459 throw new ArgumentException ();
1461 parameters [number - 1] = value;
1466 // State of the local variable `vi'.
1467 // If the local variable is a struct, use a non-zero `field_idx'
1468 // to check an individual field in it.
1470 public bool this [VariableInfo vi, int field_idx]
1473 if (vi.Number == -1)
1475 else if (vi.Number == 0)
1476 throw new ArgumentException ();
1478 return locals [vi.Number + field_idx - 1];
1482 if (vi.Number == -1)
1484 else if (vi.Number == 0)
1485 throw new ArgumentException ();
1487 locals [vi.Number + field_idx - 1] = value;
1492 // Specifies when the current block returns.
1493 // If this is FlowReturns.UNREACHABLE, then control can never reach the
1494 // end of the method (so that we don't need to emit a return statement).
1495 // The same applies for FlowReturns.EXCEPTION, but in this case the return
1496 // value will never be used.
1498 public FlowReturns Returns {
1500 return real_returns;
1504 real_returns = value;
1509 // Specifies whether control may return to our containing block
1510 // before reaching the end of this block. This happens if there
1511 // is a break/continue/goto/return in it.
1512 // This can also be used to find out whether the statement immediately
1513 // following the current block may be reached or not.
1515 public FlowReturns Breaks {
1521 real_breaks = value;
1525 public bool AlwaysBreaks {
1527 return (Breaks == FlowReturns.ALWAYS) ||
1528 (Breaks == FlowReturns.EXCEPTION) ||
1529 (Breaks == FlowReturns.UNREACHABLE);
1533 public bool MayBreak {
1535 return Breaks != FlowReturns.NEVER;
1539 public bool AlwaysReturns {
1541 return (Returns == FlowReturns.ALWAYS) ||
1542 (Returns == FlowReturns.EXCEPTION);
1546 public bool MayReturn {
1548 return (Returns == FlowReturns.SOMETIMES) ||
1549 (Returns == FlowReturns.ALWAYS);
1554 // Merge a child branching.
1556 public FlowReturns MergeChildren (FlowBranching branching, UsageVector[] children)
1558 MyBitVector new_locals = null;
1559 MyBitVector new_params = null;
1561 FlowReturns new_returns = FlowReturns.NEVER;
1562 FlowReturns new_breaks = FlowReturns.NEVER;
1563 bool new_returns_set = false, new_breaks_set = false;
1565 Report.Debug (2, "MERGING CHILDREN", branching, branching.Type,
1566 this, children.Length);
1568 foreach (UsageVector child in children) {
1569 Report.Debug (2, " MERGING CHILD", child, child.is_finally);
1571 if (!child.is_finally) {
1572 if (child.Breaks != FlowReturns.UNREACHABLE) {
1573 // If Returns is already set, perform an
1574 // `And' operation on it, otherwise just set just.
1575 if (!new_returns_set) {
1576 new_returns = child.Returns;
1577 new_returns_set = true;
1579 new_returns = AndFlowReturns (
1580 new_returns, child.Returns);
1583 // If Breaks is already set, perform an
1584 // `And' operation on it, otherwise just set just.
1585 if (!new_breaks_set) {
1586 new_breaks = child.Breaks;
1587 new_breaks_set = true;
1589 new_breaks = AndFlowReturns (
1590 new_breaks, child.Breaks);
1593 // Ignore unreachable children.
1594 if (child.Returns == FlowReturns.UNREACHABLE)
1597 // A local variable is initialized after a flow branching if it
1598 // has been initialized in all its branches which do neither
1599 // always return or always throw an exception.
1601 // If a branch may return, but does not always return, then we
1602 // can treat it like a never-returning branch here: control will
1603 // only reach the code position after the branching if we did not
1606 // It's important to distinguish between always and sometimes
1607 // returning branches here:
1610 // 2 if (something) {
1614 // 6 Console.WriteLine (a);
1616 // The if block in lines 3-4 always returns, so we must not look
1617 // at the initialization of `a' in line 4 - thus it'll still be
1618 // uninitialized in line 6.
1620 // On the other hand, the following is allowed:
1627 // 6 Console.WriteLine (a);
1629 // Here, `a' is initialized in line 3 and we must not look at
1630 // line 5 since it always returns.
1632 if (child.is_finally) {
1633 if (new_locals == null)
1634 new_locals = locals.Clone ();
1635 new_locals.Or (child.locals);
1637 if (parameters != null) {
1638 if (new_params == null)
1639 new_params = parameters.Clone ();
1640 new_params.Or (child.parameters);
1644 if (!child.AlwaysReturns && !child.AlwaysBreaks) {
1645 if (new_locals != null)
1646 new_locals.And (child.locals);
1648 new_locals = locals.Clone ();
1649 new_locals.Or (child.locals);
1651 } else if (children.Length == 1) {
1652 new_locals = locals.Clone ();
1653 new_locals.Or (child.locals);
1656 // An `out' parameter must be assigned in all branches which do
1657 // not always throw an exception.
1658 if (parameters != null) {
1659 if (child.Breaks != FlowReturns.EXCEPTION) {
1660 if (new_params != null)
1661 new_params.And (child.parameters);
1663 new_params = parameters.Clone ();
1664 new_params.Or (child.parameters);
1666 } else if (children.Length == 1) {
1667 new_params = parameters.Clone ();
1668 new_params.Or (child.parameters);
1674 Returns = new_returns;
1675 if ((branching.Type == FlowBranchingType.BLOCK) ||
1676 (branching.Type == FlowBranchingType.EXCEPTION) ||
1677 (new_breaks == FlowReturns.UNREACHABLE) ||
1678 (new_breaks == FlowReturns.EXCEPTION))
1679 Breaks = new_breaks;
1680 else if (branching.Type == FlowBranchingType.SWITCH_SECTION)
1681 Breaks = new_returns;
1682 else if (branching.Type == FlowBranchingType.SWITCH){
1683 if (new_breaks == FlowReturns.ALWAYS)
1684 Breaks = FlowReturns.ALWAYS;
1688 // We've now either reached the point after the branching or we will
1689 // never get there since we always return or always throw an exception.
1691 // If we can reach the point after the branching, mark all locals and
1692 // parameters as initialized which have been initialized in all branches
1693 // we need to look at (see above).
1696 if (((new_breaks != FlowReturns.ALWAYS) &&
1697 (new_breaks != FlowReturns.EXCEPTION) &&
1698 (new_breaks != FlowReturns.UNREACHABLE)) ||
1699 (children.Length == 1)) {
1700 if (new_locals != null)
1701 locals.Or (new_locals);
1703 if (new_params != null)
1704 parameters.Or (new_params);
1707 Report.Debug (2, "MERGING CHILDREN DONE", branching.Type,
1708 new_params, new_locals, new_returns, new_breaks,
1709 branching.Infinite, branching.MayLeaveLoop, this);
1711 if (branching.Type == FlowBranchingType.SWITCH_SECTION) {
1712 if ((new_breaks != FlowReturns.ALWAYS) &&
1713 (new_breaks != FlowReturns.EXCEPTION) &&
1714 (new_breaks != FlowReturns.UNREACHABLE))
1715 Report.Error (163, branching.Location,
1716 "Control cannot fall through from one " +
1717 "case label to another");
1720 if (branching.Infinite && !branching.MayLeaveLoop) {
1721 Report.Debug (1, "INFINITE", new_returns, new_breaks,
1722 Returns, Breaks, this);
1724 // We're actually infinite.
1725 if (new_returns == FlowReturns.NEVER) {
1726 Breaks = FlowReturns.UNREACHABLE;
1727 return FlowReturns.UNREACHABLE;
1730 // If we're an infinite loop and do not break, the code after
1731 // the loop can never be reached. However, if we may return
1732 // from the loop, then we do always return (or stay in the loop
1734 if ((new_returns == FlowReturns.SOMETIMES) ||
1735 (new_returns == FlowReturns.ALWAYS)) {
1736 Returns = FlowReturns.ALWAYS;
1737 return FlowReturns.ALWAYS;
1741 if ((branching.Type == FlowBranchingType.LOOP_BLOCK) &&
1742 branching.MayLeaveLoop && (new_returns == FlowReturns.ALWAYS)) {
1743 Returns = FlowReturns.SOMETIMES;
1744 return FlowReturns.SOMETIMES;
1751 // Tells control flow analysis that the current code position may be reached with
1752 // a forward jump from any of the origins listed in `origin_vectors' which is a
1753 // list of UsageVectors.
1755 // This is used when resolving forward gotos - in the following example, the
1756 // variable `a' is uninitialized in line 8 becase this line may be reached via
1757 // the goto in line 4:
1767 // 8 Console.WriteLine (a);
1770 public void MergeJumpOrigins (ICollection origin_vectors)
1772 Report.Debug (1, "MERGING JUMP ORIGIN", this);
1774 real_breaks = FlowReturns.NEVER;
1775 real_returns = FlowReturns.NEVER;
1777 foreach (UsageVector vector in origin_vectors) {
1778 Report.Debug (1, " MERGING JUMP ORIGIN", vector);
1780 locals.And (vector.locals);
1781 if (parameters != null)
1782 parameters.And (vector.parameters);
1783 Breaks = AndFlowReturns (Breaks, vector.Breaks);
1784 Returns = AndFlowReturns (Returns, vector.Returns);
1787 Report.Debug (1, "MERGING JUMP ORIGIN DONE", this);
1791 // This is used at the beginning of a finally block if there were
1792 // any return statements in the try block or one of the catch blocks.
1794 public void MergeFinallyOrigins (ICollection finally_vectors)
1796 Report.Debug (1, "MERGING FINALLY ORIGIN", this);
1798 real_breaks = FlowReturns.NEVER;
1800 foreach (UsageVector vector in finally_vectors) {
1801 Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
1803 if (parameters != null)
1804 parameters.And (vector.parameters);
1805 Breaks = AndFlowReturns (Breaks, vector.Breaks);
1810 Report.Debug (1, "MERGING FINALLY ORIGIN DONE", this);
1813 public void CheckOutParameters (FlowBranching branching)
1815 if (parameters != null)
1816 branching.CheckOutParameters (parameters, branching.Location);
1820 // Performs an `or' operation on the locals and the parameters.
1822 public void Or (UsageVector new_vector)
1824 locals.Or (new_vector.locals);
1825 if (parameters != null)
1826 parameters.Or (new_vector.parameters);
1830 // Performs an `and' operation on the locals.
1832 public void AndLocals (UsageVector new_vector)
1834 locals.And (new_vector.locals);
1838 // Returns a deep copy of the parameters.
1840 public MyBitVector Parameters {
1842 if (parameters != null)
1843 return parameters.Clone ();
1850 // Returns a deep copy of the locals.
1852 public MyBitVector Locals {
1854 return locals.Clone ();
1862 public override string ToString ()
1864 StringBuilder sb = new StringBuilder ();
1866 sb.Append ("Vector (");
1869 sb.Append (Returns);
1872 if (parameters != null) {
1874 sb.Append (parameters);
1880 return sb.ToString ();
1884 FlowBranching (FlowBranchingType type, Location loc)
1887 this.Location = loc;
1893 // Creates a new flow branching for `block'.
1894 // This is used from Block.Resolve to create the top-level branching of
1897 public FlowBranching (Block block, InternalParameters ip, Location loc)
1898 : this (FlowBranchingType.BLOCK, loc)
1903 int count = (ip != null) ? ip.Count : 0;
1906 param_map = new int [count];
1907 struct_params = new MyStructInfo [count];
1910 for (int i = 0; i < count; i++) {
1911 Parameter.Modifier mod = param_info.ParameterModifier (i);
1913 if ((mod & Parameter.Modifier.OUT) == 0)
1916 param_map [i] = ++num_params;
1918 Type param_type = param_info.ParameterType (i);
1920 struct_params [i] = MyStructInfo.GetStructInfo (param_type);
1921 if (struct_params [i] != null)
1922 num_params += struct_params [i].Count;
1925 AddSibling (new UsageVector (null, num_params, block.CountVariables));
1929 // Creates a new flow branching which is contained in `parent'.
1930 // You should only pass non-null for the `block' argument if this block
1931 // introduces any new variables - in this case, we need to create a new
1932 // usage vector with a different size than our parent's one.
1934 public FlowBranching (FlowBranching parent, FlowBranchingType type,
1935 Block block, Location loc)
1941 if (parent != null) {
1942 param_info = parent.param_info;
1943 param_map = parent.param_map;
1944 struct_params = parent.struct_params;
1945 num_params = parent.num_params;
1950 vector = new UsageVector (parent.CurrentUsageVector, num_params,
1951 Block.CountVariables);
1953 vector = new UsageVector (Parent.CurrentUsageVector);
1955 AddSibling (vector);
1958 case FlowBranchingType.EXCEPTION:
1959 finally_vectors = new ArrayList ();
1967 void AddSibling (UsageVector uv)
1969 if (Siblings != null) {
1970 UsageVector[] ns = new UsageVector [Siblings.Length + 1];
1971 for (int i = 0; i < Siblings.Length; ++i)
1972 ns [i] = Siblings [i];
1975 Siblings = new UsageVector [1];
1977 Siblings [Siblings.Length - 1] = uv;
1981 // Returns the branching's current usage vector.
1983 public UsageVector CurrentUsageVector
1986 return Siblings [Siblings.Length - 1];
1991 // Creates a sibling of the current usage vector.
1993 public void CreateSibling ()
1995 AddSibling (new UsageVector (Parent.CurrentUsageVector));
1997 Report.Debug (1, "CREATED SIBLING", CurrentUsageVector);
2001 // Creates a sibling for a `finally' block.
2003 public void CreateSiblingForFinally ()
2005 if (Type != FlowBranchingType.EXCEPTION)
2006 throw new NotSupportedException ();
2010 CurrentUsageVector.MergeFinallyOrigins (finally_vectors);
2014 // Check whether all `out' parameters have been assigned.
2016 public void CheckOutParameters (MyBitVector parameters, Location loc)
2021 for (int i = 0; i < param_map.Length; i++) {
2022 int index = param_map [i];
2027 if (parameters [index - 1])
2030 // If it's a struct, we must ensure that all its fields have
2031 // been assigned. If the struct has any non-public fields, this
2032 // can only be done by assigning the whole struct.
2034 MyStructInfo struct_info = struct_params [i];
2035 if ((struct_info == null) || struct_info.HasNonPublicFields) {
2037 177, loc, "The out parameter `" +
2038 param_info.ParameterName (i) + "' must be " +
2039 "assigned before control leave the current method.");
2045 for (int j = 0; j < struct_info.Count; j++) {
2046 if (!parameters [index + j]) {
2048 177, loc, "The out parameter `" +
2049 param_info.ParameterName (i) + "' must be " +
2050 "assigned before control leaves the current method.");
2059 // Merge a child branching.
2061 public FlowReturns MergeChild (FlowBranching child)
2063 FlowReturns returns = CurrentUsageVector.MergeChildren (child, child.Siblings);
2065 if ((child.Type != FlowBranchingType.LOOP_BLOCK) &&
2066 (child.Type != FlowBranchingType.SWITCH_SECTION))
2067 MayLeaveLoop |= child.MayLeaveLoop;
2069 MayLeaveLoop = false;
2075 // Does the toplevel merging.
2077 public FlowReturns MergeTopBlock ()
2079 if ((Type != FlowBranchingType.BLOCK) || (Block == null))
2080 throw new NotSupportedException ();
2082 UsageVector vector = new UsageVector (null, num_params, Block.CountVariables);
2084 Report.Debug (1, "MERGING TOP BLOCK", Location, vector);
2086 vector.MergeChildren (this, Siblings);
2088 if (Siblings.Length == 1)
2089 Siblings [0] = vector;
2092 AddSibling (vector);
2095 Report.Debug (1, "MERGING TOP BLOCK DONE", Location, vector);
2097 if (vector.Breaks != FlowReturns.EXCEPTION) {
2098 if (!vector.AlwaysBreaks)
2099 CheckOutParameters (CurrentUsageVector.Parameters, Location);
2100 return vector.AlwaysBreaks ? FlowReturns.ALWAYS : vector.Returns;
2102 return FlowReturns.EXCEPTION;
2105 public bool InTryBlock ()
2107 if (finally_vectors != null)
2109 else if (Parent != null)
2110 return Parent.InTryBlock ();
2115 public void AddFinallyVector (UsageVector vector)
2117 if (finally_vectors != null) {
2118 finally_vectors.Add (vector.Clone ());
2123 Parent.AddFinallyVector (vector);
2125 throw new NotSupportedException ();
2128 public bool IsVariableAssigned (VariableInfo vi)
2130 if (CurrentUsageVector.AlwaysBreaks)
2133 return CurrentUsageVector [vi, 0];
2136 public bool IsVariableAssigned (VariableInfo vi, int field_idx)
2138 if (CurrentUsageVector.AlwaysBreaks)
2141 return CurrentUsageVector [vi, field_idx];
2144 public void SetVariableAssigned (VariableInfo vi)
2146 if (CurrentUsageVector.AlwaysBreaks)
2149 CurrentUsageVector [vi, 0] = true;
2152 public void SetVariableAssigned (VariableInfo vi, int field_idx)
2154 if (CurrentUsageVector.AlwaysBreaks)
2157 CurrentUsageVector [vi, field_idx] = true;
2160 public bool IsParameterAssigned (int number)
2162 int index = param_map [number];
2167 if (CurrentUsageVector [index])
2170 // Parameter is not assigned, so check whether it's a struct.
2171 // If it's either not a struct or a struct which non-public
2172 // fields, return false.
2173 MyStructInfo struct_info = struct_params [number];
2174 if ((struct_info == null) || struct_info.HasNonPublicFields)
2177 // Ok, so each field must be assigned.
2178 for (int i = 0; i < struct_info.Count; i++)
2179 if (!CurrentUsageVector [index + i])
2185 public bool IsParameterAssigned (int number, string field_name)
2187 int index = param_map [number];
2192 MyStructInfo info = (MyStructInfo) struct_params [number];
2196 int field_idx = info [field_name];
2198 return CurrentUsageVector [index + field_idx];
2201 public void SetParameterAssigned (int number)
2203 if (param_map [number] == 0)
2206 if (!CurrentUsageVector.AlwaysBreaks)
2207 CurrentUsageVector [param_map [number]] = true;
2210 public void SetParameterAssigned (int number, string field_name)
2212 int index = param_map [number];
2217 MyStructInfo info = (MyStructInfo) struct_params [number];
2221 int field_idx = info [field_name];
2223 if (!CurrentUsageVector.AlwaysBreaks)
2224 CurrentUsageVector [index + field_idx] = true;
2227 public bool IsReachable ()
2232 case FlowBranchingType.SWITCH_SECTION:
2233 // The code following a switch block is reachable unless the switch
2234 // block always returns.
2235 reachable = !CurrentUsageVector.AlwaysReturns;
2238 case FlowBranchingType.LOOP_BLOCK:
2239 // The code following a loop is reachable unless the loop always
2240 // returns or it's an infinite loop without any `break's in it.
2241 reachable = !CurrentUsageVector.AlwaysReturns &&
2242 (CurrentUsageVector.Breaks != FlowReturns.UNREACHABLE);
2246 // The code following a block or exception is reachable unless the
2247 // block either always returns or always breaks.
2251 reachable = !CurrentUsageVector.AlwaysBreaks &&
2252 !CurrentUsageVector.AlwaysReturns;
2256 Report.Debug (1, "REACHABLE", this, Type, CurrentUsageVector.Returns,
2257 CurrentUsageVector.Breaks, CurrentUsageVector, MayLeaveLoop,
2263 public override string ToString ()
2265 StringBuilder sb = new StringBuilder ("FlowBranching (");
2270 if (Block != null) {
2272 sb.Append (Block.ID);
2274 sb.Append (Block.StartLocation);
2277 sb.Append (Siblings.Length);
2279 sb.Append (CurrentUsageVector);
2281 return sb.ToString ();
2285 public class MyStructInfo {
2286 public readonly Type Type;
2287 public readonly FieldInfo[] Fields;
2288 public readonly FieldInfo[] NonPublicFields;
2289 public readonly int Count;
2290 public readonly int CountNonPublic;
2291 public readonly bool HasNonPublicFields;
2293 private static Hashtable field_type_hash = new Hashtable ();
2294 private Hashtable field_hash;
2296 // Private constructor. To save memory usage, we only need to create one instance
2297 // of this class per struct type.
2298 private MyStructInfo (Type type)
2302 if (type is TypeBuilder) {
2303 TypeContainer tc = TypeManager.LookupTypeContainer (type);
2305 ArrayList fields = tc.Fields;
2306 if (fields != null) {
2307 foreach (Field field in fields) {
2308 if ((field.ModFlags & Modifiers.STATIC) != 0)
2310 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
2317 Fields = new FieldInfo [Count];
2318 NonPublicFields = new FieldInfo [CountNonPublic];
2320 Count = CountNonPublic = 0;
2321 if (fields != null) {
2322 foreach (Field field in fields) {
2323 if ((field.ModFlags & Modifiers.STATIC) != 0)
2325 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
2326 Fields [Count++] = field.FieldBuilder;
2328 NonPublicFields [CountNonPublic++] =
2334 Fields = type.GetFields (BindingFlags.Instance|BindingFlags.Public);
2335 Count = Fields.Length;
2337 NonPublicFields = type.GetFields (BindingFlags.Instance|BindingFlags.NonPublic);
2338 CountNonPublic = NonPublicFields.Length;
2341 Count += NonPublicFields.Length;
2344 field_hash = new Hashtable ();
2345 foreach (FieldInfo field in Fields)
2346 field_hash.Add (field.Name, ++number);
2348 if (NonPublicFields.Length != 0)
2349 HasNonPublicFields = true;
2351 foreach (FieldInfo field in NonPublicFields)
2352 field_hash.Add (field.Name, ++number);
2355 public int this [string name] {
2357 if (field_hash.Contains (name))
2358 return (int) field_hash [name];
2364 public FieldInfo this [int index] {
2366 if (index >= Fields.Length)
2367 return NonPublicFields [index - Fields.Length];
2369 return Fields [index];
2373 public static MyStructInfo GetStructInfo (Type type)
2375 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type))
2378 if (!(type is TypeBuilder) && TypeManager.IsBuiltinType (type))
2381 MyStructInfo info = (MyStructInfo) field_type_hash [type];
2385 info = new MyStructInfo (type);
2386 field_type_hash.Add (type, info);
2390 public static MyStructInfo GetStructInfo (TypeContainer tc)
2392 MyStructInfo info = (MyStructInfo) field_type_hash [tc.TypeBuilder];
2396 info = new MyStructInfo (tc.TypeBuilder);
2397 field_type_hash.Add (tc.TypeBuilder, info);
2402 public class VariableInfo : IVariable {
2403 public Expression Type;
2404 public LocalBuilder LocalBuilder;
2405 public Type VariableType;
2406 public readonly string Name;
2407 public readonly Location Location;
2408 public readonly int Block;
2413 public bool Assigned;
2414 public bool ReadOnly;
2416 public VariableInfo (Expression type, string name, int block, Location l)
2421 LocalBuilder = null;
2425 public VariableInfo (TypeContainer tc, int block, Location l)
2427 VariableType = tc.TypeBuilder;
2428 struct_info = MyStructInfo.GetStructInfo (tc);
2430 LocalBuilder = null;
2434 MyStructInfo struct_info;
2435 public MyStructInfo StructInfo {
2441 public bool IsAssigned (EmitContext ec, Location loc)
2443 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsVariableAssigned (this))
2446 MyStructInfo struct_info = StructInfo;
2447 if ((struct_info == null) || (struct_info.HasNonPublicFields && (Name != null))) {
2448 Report.Error (165, loc, "Use of unassigned local variable `" + Name + "'");
2449 ec.CurrentBranching.SetVariableAssigned (this);
2453 int count = struct_info.Count;
2455 for (int i = 0; i < count; i++) {
2456 if (!ec.CurrentBranching.IsVariableAssigned (this, i+1)) {
2458 Report.Error (165, loc,
2459 "Use of unassigned local variable `" +
2461 ec.CurrentBranching.SetVariableAssigned (this);
2465 FieldInfo field = struct_info [i];
2466 Report.Error (171, loc,
2467 "Field `" + TypeManager.CSharpName (VariableType) +
2468 "." + field.Name + "' must be fully initialized " +
2469 "before control leaves the constructor");
2477 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
2479 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsVariableAssigned (this) ||
2480 (struct_info == null))
2483 int field_idx = StructInfo [name];
2487 if (!ec.CurrentBranching.IsVariableAssigned (this, field_idx)) {
2488 Report.Error (170, loc,
2489 "Use of possibly unassigned field `" + name + "'");
2490 ec.CurrentBranching.SetVariableAssigned (this, field_idx);
2497 public void SetAssigned (EmitContext ec)
2499 if (ec.DoFlowAnalysis)
2500 ec.CurrentBranching.SetVariableAssigned (this);
2503 public void SetFieldAssigned (EmitContext ec, string name)
2505 if (ec.DoFlowAnalysis && (struct_info != null))
2506 ec.CurrentBranching.SetVariableAssigned (this, StructInfo [name]);
2509 public bool Resolve (DeclSpace decl)
2511 if (struct_info != null)
2514 if (VariableType == null)
2515 VariableType = decl.ResolveType (Type, false, Location);
2517 if (VariableType == null)
2520 struct_info = MyStructInfo.GetStructInfo (VariableType);
2525 public void MakePinned ()
2527 TypeManager.MakePinned (LocalBuilder);
2530 public override string ToString ()
2532 return "VariableInfo (" + Number + "," + Type + "," + Location + ")";
2537 /// Block represents a C# block.
2541 /// This class is used in a number of places: either to represent
2542 /// explicit blocks that the programmer places or implicit blocks.
2544 /// Implicit blocks are used as labels or to introduce variable
2547 public class Block : Statement {
2548 public readonly Block Parent;
2549 public readonly bool Implicit;
2550 public readonly Location StartLocation;
2551 public Location EndLocation = Location.Null;
2554 // The statements in this block
2556 ArrayList statements;
2559 // An array of Blocks. We keep track of children just
2560 // to generate the local variable declarations.
2562 // Statements and child statements are handled through the
2568 // Labels. (label, block) pairs.
2573 // Keeps track of (name, type) pairs
2575 Hashtable variables;
2578 // Keeps track of constants
2579 Hashtable constants;
2587 public Block (Block parent)
2588 : this (parent, false, Location.Null, Location.Null)
2591 public Block (Block parent, bool implicit_block)
2592 : this (parent, implicit_block, Location.Null, Location.Null)
2595 public Block (Block parent, bool implicit_block, Parameters parameters)
2596 : this (parent, implicit_block, parameters, Location.Null, Location.Null)
2599 public Block (Block parent, Location start, Location end)
2600 : this (parent, false, start, end)
2603 public Block (Block parent, Parameters parameters, Location start, Location end)
2604 : this (parent, false, parameters, start, end)
2607 public Block (Block parent, bool implicit_block, Location start, Location end)
2608 : this (parent, implicit_block, Parameters.EmptyReadOnlyParameters,
2612 public Block (Block parent, bool implicit_block, Parameters parameters,
2613 Location start, Location end)
2616 parent.AddChild (this);
2618 this.Parent = parent;
2619 this.Implicit = implicit_block;
2620 this.parameters = parameters;
2621 this.StartLocation = start;
2622 this.EndLocation = end;
2625 statements = new ArrayList ();
2634 void AddChild (Block b)
2636 if (children == null)
2637 children = new ArrayList ();
2642 public void SetEndLocation (Location loc)
2648 /// Adds a label to the current block.
2652 /// false if the name already exists in this block. true
2656 public bool AddLabel (string name, LabeledStatement target)
2659 labels = new Hashtable ();
2660 if (labels.Contains (name))
2663 labels.Add (name, target);
2667 public LabeledStatement LookupLabel (string name)
2669 if (labels != null){
2670 if (labels.Contains (name))
2671 return ((LabeledStatement) labels [name]);
2675 return Parent.LookupLabel (name);
2680 VariableInfo this_variable = null;
2683 // Returns the "this" instance variable of this block.
2684 // See AddThisVariable() for more information.
2686 public VariableInfo ThisVariable {
2688 if (this_variable != null)
2689 return this_variable;
2690 else if (Parent != null)
2691 return Parent.ThisVariable;
2697 Hashtable child_variable_names;
2700 // Marks a variable with name @name as being used in a child block.
2701 // If a variable name has been used in a child block, it's illegal to
2702 // declare a variable with the same name in the current block.
2704 public void AddChildVariableName (string name)
2706 if (child_variable_names == null)
2707 child_variable_names = new Hashtable ();
2709 if (!child_variable_names.Contains (name))
2710 child_variable_names.Add (name, true);
2714 // Marks all variables from block @block and all its children as being
2715 // used in a child block.
2717 public void AddChildVariableNames (Block block)
2719 if (block.Variables != null) {
2720 foreach (string name in block.Variables.Keys)
2721 AddChildVariableName (name);
2724 if (block.children != null) {
2725 foreach (Block child in block.children)
2726 AddChildVariableNames (child);
2731 // Checks whether a variable name has already been used in a child block.
2733 public bool IsVariableNameUsedInChildBlock (string name)
2735 if (child_variable_names == null)
2738 return child_variable_names.Contains (name);
2742 // This is used by non-static `struct' constructors which do not have an
2743 // initializer - in this case, the constructor must initialize all of the
2744 // struct's fields. To do this, we add a "this" variable and use the flow
2745 // analysis code to ensure that it's been fully initialized before control
2746 // leaves the constructor.
2748 public VariableInfo AddThisVariable (TypeContainer tc, Location l)
2750 if (this_variable != null)
2751 return this_variable;
2753 this_variable = new VariableInfo (tc, ID, l);
2755 if (variables == null)
2756 variables = new Hashtable ();
2757 variables.Add ("this", this_variable);
2759 return this_variable;
2762 public VariableInfo AddVariable (Expression type, string name, Parameters pars, Location l)
2764 if (variables == null)
2765 variables = new Hashtable ();
2767 VariableInfo vi = GetVariableInfo (name);
2770 Report.Error (136, l, "A local variable named `" + name + "' " +
2771 "cannot be declared in this scope since it would " +
2772 "give a different meaning to `" + name + "', which " +
2773 "is already used in a `parent or current' scope to " +
2774 "denote something else");
2776 Report.Error (128, l, "A local variable `" + name + "' is already " +
2777 "defined in this scope");
2781 if (IsVariableNameUsedInChildBlock (name)) {
2782 Report.Error (136, l, "A local variable named `" + name + "' " +
2783 "cannot be declared in this scope since it would " +
2784 "give a different meaning to `" + name + "', which " +
2785 "is already used in a `child' scope to denote something " +
2792 Parameter p = pars.GetParameterByName (name, out idx);
2794 Report.Error (136, l, "A local variable named `" + name + "' " +
2795 "cannot be declared in this scope since it would " +
2796 "give a different meaning to `" + name + "', which " +
2797 "is already used in a `parent or current' scope to " +
2798 "denote something else");
2803 vi = new VariableInfo (type, name, ID, l);
2805 variables.Add (name, vi);
2807 if (variables_initialized)
2808 throw new Exception ();
2810 // Console.WriteLine ("Adding {0} to {1}", name, ID);
2814 public bool AddConstant (Expression type, string name, Expression value, Parameters pars, Location l)
2816 if (AddVariable (type, name, pars, l) == null)
2819 if (constants == null)
2820 constants = new Hashtable ();
2822 constants.Add (name, value);
2826 public Hashtable Variables {
2832 public VariableInfo GetVariableInfo (string name)
2834 if (variables != null) {
2836 temp = variables [name];
2839 return (VariableInfo) temp;
2844 return Parent.GetVariableInfo (name);
2849 public Expression GetVariableType (string name)
2851 VariableInfo vi = GetVariableInfo (name);
2859 public Expression GetConstantExpression (string name)
2861 if (constants != null) {
2863 temp = constants [name];
2866 return (Expression) temp;
2870 return Parent.GetConstantExpression (name);
2876 /// True if the variable named @name is a constant
2878 public bool IsConstant (string name)
2880 Expression e = null;
2882 e = GetConstantExpression (name);
2888 /// Use to fetch the statement associated with this label
2890 public Statement this [string name] {
2892 return (Statement) labels [name];
2896 Parameters parameters = null;
2897 public Parameters Parameters {
2900 return Parent.Parameters;
2907 /// A list of labels that were not used within this block
2909 public string [] GetUnreferenced ()
2911 // FIXME: Implement me
2915 public void AddStatement (Statement s)
2932 bool variables_initialized = false;
2933 int count_variables = 0, first_variable = 0;
2935 void UpdateVariableInfo (EmitContext ec)
2937 DeclSpace ds = ec.DeclSpace;
2942 first_variable += Parent.CountVariables;
2944 count_variables = first_variable;
2945 if (variables != null) {
2946 foreach (VariableInfo vi in variables.Values) {
2947 if (!vi.Resolve (ds)) {
2952 vi.Number = ++count_variables;
2954 if (vi.StructInfo != null)
2955 count_variables += vi.StructInfo.Count;
2959 variables_initialized = true;
2964 // The number of local variables in this block
2966 public int CountVariables
2969 if (!variables_initialized)
2970 throw new Exception ();
2972 return count_variables;
2977 /// Emits the variable declarations and labels.
2980 /// tc: is our typecontainer (to resolve type references)
2981 /// ig: is the code generator:
2982 /// toplevel: the toplevel block. This is used for checking
2983 /// that no two labels with the same name are used.
2985 public void EmitMeta (EmitContext ec, Block toplevel)
2987 DeclSpace ds = ec.DeclSpace;
2988 ILGenerator ig = ec.ig;
2990 if (!variables_initialized)
2991 UpdateVariableInfo (ec);
2994 // Process this block variables
2996 if (variables != null){
2997 foreach (DictionaryEntry de in variables){
2998 string name = (string) de.Key;
2999 VariableInfo vi = (VariableInfo) de.Value;
3001 if (vi.VariableType == null)
3004 Type variable_type = vi.VariableType;
3006 if (variable_type.IsPointer){
3008 // Am not really convinced that this test is required (Microsoft does it)
3009 // but the fact is that you would not be able to use the pointer variable
3012 if (!TypeManager.VerifyUnManaged (variable_type.GetElementType (), vi.Location))
3016 vi.LocalBuilder = ig.DeclareLocal (vi.VariableType);
3018 if (constants == null)
3021 Expression cv = (Expression) constants [name];
3025 Expression e = cv.Resolve (ec);
3029 if (!(e is Constant)){
3030 Report.Error (133, vi.Location,
3031 "The expression being assigned to `" +
3032 name + "' must be constant (" + e + ")");
3036 constants.Remove (name);
3037 constants.Add (name, e);
3042 // Now, handle the children
3044 if (children != null){
3045 foreach (Block b in children)
3046 b.EmitMeta (ec, toplevel);
3050 public void UsageWarning ()
3054 if (variables != null){
3055 foreach (DictionaryEntry de in variables){
3056 VariableInfo vi = (VariableInfo) de.Value;
3061 name = (string) de.Key;
3065 219, vi.Location, "The variable `" + name +
3066 "' is assigned but its value is never used");
3069 168, vi.Location, "The variable `" +
3071 "' is declared but never used");
3076 if (children != null)
3077 foreach (Block b in children)
3081 bool has_ret = false;
3083 public override bool Resolve (EmitContext ec)
3085 Block prev_block = ec.CurrentBlock;
3088 ec.CurrentBlock = this;
3089 ec.StartFlowBranching (this);
3091 Report.Debug (1, "RESOLVE BLOCK", StartLocation, ec.CurrentBranching);
3093 if (!variables_initialized)
3094 UpdateVariableInfo (ec);
3096 ArrayList new_statements = new ArrayList ();
3097 bool unreachable = false, warning_shown = false;
3099 foreach (Statement s in statements){
3100 if (unreachable && !(s is LabeledStatement)) {
3101 if (!warning_shown && !(s is EmptyStatement)) {
3102 warning_shown = true;
3103 Warning_DeadCodeFound (s.loc);
3109 if (s.Resolve (ec) == false) {
3114 if (s is LabeledStatement)
3115 unreachable = false;
3117 unreachable = ! ec.CurrentBranching.IsReachable ();
3119 new_statements.Add (s);
3122 statements = new_statements;
3124 Report.Debug (1, "RESOLVE BLOCK DONE", StartLocation, ec.CurrentBranching);
3126 FlowReturns returns = ec.EndFlowBranching ();
3127 ec.CurrentBlock = prev_block;
3129 // If we're a non-static `struct' constructor which doesn't have an
3130 // initializer, then we must initialize all of the struct's fields.
3131 if ((this_variable != null) && (returns != FlowReturns.EXCEPTION) &&
3132 !this_variable.IsAssigned (ec, loc))
3135 if ((labels != null) && (RootContext.WarningLevel >= 2)) {
3136 foreach (LabeledStatement label in labels.Values)
3137 if (!label.HasBeenReferenced)
3138 Report.Warning (164, label.Location,
3139 "This label has not been referenced");
3142 if ((returns == FlowReturns.ALWAYS) ||
3143 (returns == FlowReturns.EXCEPTION) ||
3144 (returns == FlowReturns.UNREACHABLE))
3150 protected override bool DoEmit (EmitContext ec)
3152 foreach (Statement s in statements)
3158 public override bool Emit (EmitContext ec)
3160 Block prev_block = ec.CurrentBlock;
3162 ec.CurrentBlock = this;
3164 bool emit_debug_info = (CodeGen.SymbolWriter != null);
3165 bool is_lexical_block = !Implicit && (Parent != null);
3166 if (emit_debug_info) {
3167 if (is_lexical_block)
3168 ec.ig.BeginScope ();
3170 if (variables != null) {
3171 foreach (DictionaryEntry de in variables) {
3172 string name = (string) de.Key;
3173 VariableInfo vi = (VariableInfo) de.Value;
3175 if (vi.LocalBuilder == null)
3178 vi.LocalBuilder.SetLocalSymInfo (name);
3183 ec.Mark (StartLocation);
3184 bool retval = DoEmit (ec);
3185 ec.Mark (EndLocation);
3187 if (emit_debug_info && is_lexical_block)
3190 ec.CurrentBlock = prev_block;
3196 public class SwitchLabel {
3199 public Location loc;
3200 public Label ILLabel;
3201 public Label ILLabelCode;
3204 // if expr == null, then it is the default case.
3206 public SwitchLabel (Expression expr, Location l)
3212 public Expression Label {
3218 public object Converted {
3225 // Resolves the expression, reduces it to a literal if possible
3226 // and then converts it to the requested type.
3228 public bool ResolveAndReduce (EmitContext ec, Type required_type)
3230 ILLabel = ec.ig.DefineLabel ();
3231 ILLabelCode = ec.ig.DefineLabel ();
3236 Expression e = label.Resolve (ec);
3241 if (!(e is Constant)){
3242 Report.Error (150, loc, "A constant value is expected, got: " + e);
3246 if (e is StringConstant || e is NullLiteral){
3247 if (required_type == TypeManager.string_type){
3249 ILLabel = ec.ig.DefineLabel ();
3254 converted = Expression.ConvertIntLiteral ((Constant) e, required_type, loc);
3255 if (converted == null)
3262 public class SwitchSection {
3263 // An array of SwitchLabels.
3264 public readonly ArrayList Labels;
3265 public readonly Block Block;
3267 public SwitchSection (ArrayList labels, Block block)
3274 public class Switch : Statement {
3275 public readonly ArrayList Sections;
3276 public Expression Expr;
3279 /// Maps constants whose type type SwitchType to their SwitchLabels.
3281 public Hashtable Elements;
3284 /// The governing switch type
3286 public Type SwitchType;
3292 Label default_target;
3293 Expression new_expr;
3296 // The types allowed to be implicitly cast from
3297 // on the governing type
3299 static Type [] allowed_types;
3301 public Switch (Expression e, ArrayList sects, Location l)
3308 public bool GotDefault {
3314 public Label DefaultTarget {
3316 return default_target;
3321 // Determines the governing type for a switch. The returned
3322 // expression might be the expression from the switch, or an
3323 // expression that includes any potential conversions to the
3324 // integral types or to string.
3326 Expression SwitchGoverningType (EmitContext ec, Type t)
3328 if (t == TypeManager.int32_type ||
3329 t == TypeManager.uint32_type ||
3330 t == TypeManager.char_type ||
3331 t == TypeManager.byte_type ||
3332 t == TypeManager.sbyte_type ||
3333 t == TypeManager.ushort_type ||
3334 t == TypeManager.short_type ||
3335 t == TypeManager.uint64_type ||
3336 t == TypeManager.int64_type ||
3337 t == TypeManager.string_type ||
3338 t == TypeManager.bool_type ||
3339 t.IsSubclassOf (TypeManager.enum_type))
3342 if (allowed_types == null){
3343 allowed_types = new Type [] {
3344 TypeManager.sbyte_type,
3345 TypeManager.byte_type,
3346 TypeManager.short_type,
3347 TypeManager.ushort_type,
3348 TypeManager.int32_type,
3349 TypeManager.uint32_type,
3350 TypeManager.int64_type,
3351 TypeManager.uint64_type,
3352 TypeManager.char_type,
3353 TypeManager.bool_type,
3354 TypeManager.string_type
3359 // Try to find a *user* defined implicit conversion.
3361 // If there is no implicit conversion, or if there are multiple
3362 // conversions, we have to report an error
3364 Expression converted = null;
3365 foreach (Type tt in allowed_types){
3368 e = Expression.ImplicitUserConversion (ec, Expr, tt, loc);
3372 if (converted != null){
3373 Report.Error (-12, loc, "More than one conversion to an integral " +
3374 " type exists for type `" +
3375 TypeManager.CSharpName (Expr.Type)+"'");
3383 void error152 (string n)
3386 152, "The label `" + n + ":' " +
3387 "is already present on this switch statement");
3391 // Performs the basic sanity checks on the switch statement
3392 // (looks for duplicate keys and non-constant expressions).
3394 // It also returns a hashtable with the keys that we will later
3395 // use to compute the switch tables
3397 bool CheckSwitch (EmitContext ec)
3401 Elements = new Hashtable ();
3403 got_default = false;
3405 if (TypeManager.IsEnumType (SwitchType)){
3406 compare_type = TypeManager.EnumToUnderlying (SwitchType);
3408 compare_type = SwitchType;
3410 foreach (SwitchSection ss in Sections){
3411 foreach (SwitchLabel sl in ss.Labels){
3412 if (!sl.ResolveAndReduce (ec, SwitchType)){
3417 if (sl.Label == null){
3419 error152 ("default");
3426 object key = sl.Converted;
3428 if (key is Constant)
3429 key = ((Constant) key).GetValue ();
3432 key = NullLiteral.Null;
3434 string lname = null;
3435 if (compare_type == TypeManager.uint64_type){
3436 ulong v = (ulong) key;
3438 if (Elements.Contains (v))
3439 lname = v.ToString ();
3441 Elements.Add (v, sl);
3442 } else if (compare_type == TypeManager.int64_type){
3443 long v = (long) key;
3445 if (Elements.Contains (v))
3446 lname = v.ToString ();
3448 Elements.Add (v, sl);
3449 } else if (compare_type == TypeManager.uint32_type){
3450 uint v = (uint) key;
3452 if (Elements.Contains (v))
3453 lname = v.ToString ();
3455 Elements.Add (v, sl);
3456 } else if (compare_type == TypeManager.char_type){
3457 char v = (char) key;
3459 if (Elements.Contains (v))
3460 lname = v.ToString ();
3462 Elements.Add (v, sl);
3463 } else if (compare_type == TypeManager.byte_type){
3464 byte v = (byte) key;
3466 if (Elements.Contains (v))
3467 lname = v.ToString ();
3469 Elements.Add (v, sl);
3470 } else if (compare_type == TypeManager.sbyte_type){
3471 sbyte v = (sbyte) key;
3473 if (Elements.Contains (v))
3474 lname = v.ToString ();
3476 Elements.Add (v, sl);
3477 } else if (compare_type == TypeManager.short_type){
3478 short v = (short) key;
3480 if (Elements.Contains (v))
3481 lname = v.ToString ();
3483 Elements.Add (v, sl);
3484 } else if (compare_type == TypeManager.ushort_type){
3485 ushort v = (ushort) key;
3487 if (Elements.Contains (v))
3488 lname = v.ToString ();
3490 Elements.Add (v, sl);
3491 } else if (compare_type == TypeManager.string_type){
3492 if (key is NullLiteral){
3493 if (Elements.Contains (NullLiteral.Null))
3496 Elements.Add (NullLiteral.Null, null);
3498 string s = (string) key;
3500 if (Elements.Contains (s))
3503 Elements.Add (s, sl);
3505 } else if (compare_type == TypeManager.int32_type) {
3508 if (Elements.Contains (v))
3509 lname = v.ToString ();
3511 Elements.Add (v, sl);
3512 } else if (compare_type == TypeManager.bool_type) {
3513 bool v = (bool) key;
3515 if (Elements.Contains (v))
3516 lname = v.ToString ();
3518 Elements.Add (v, sl);
3522 throw new Exception ("Unknown switch type!" +
3523 SwitchType + " " + compare_type);
3527 error152 ("case + " + lname);
3538 void EmitObjectInteger (ILGenerator ig, object k)
3541 IntConstant.EmitInt (ig, (int) k);
3542 else if (k is Constant) {
3543 EmitObjectInteger (ig, ((Constant) k).GetValue ());
3546 IntConstant.EmitInt (ig, unchecked ((int) (uint) k));
3549 if ((long) k >= int.MinValue && (long) k <= int.MaxValue)
3551 IntConstant.EmitInt (ig, (int) (long) k);
3552 ig.Emit (OpCodes.Conv_I8);
3555 LongConstant.EmitLong (ig, (long) k);
3557 else if (k is ulong)
3559 if ((ulong) k < (1L<<32))
3561 IntConstant.EmitInt (ig, (int) (long) k);
3562 ig.Emit (OpCodes.Conv_U8);
3566 LongConstant.EmitLong (ig, unchecked ((long) (ulong) k));
3570 IntConstant.EmitInt (ig, (int) ((char) k));
3571 else if (k is sbyte)
3572 IntConstant.EmitInt (ig, (int) ((sbyte) k));
3574 IntConstant.EmitInt (ig, (int) ((byte) k));
3575 else if (k is short)
3576 IntConstant.EmitInt (ig, (int) ((short) k));
3577 else if (k is ushort)
3578 IntConstant.EmitInt (ig, (int) ((ushort) k));
3580 IntConstant.EmitInt (ig, ((bool) k) ? 1 : 0);
3582 throw new Exception ("Unhandled case");
3585 // structure used to hold blocks of keys while calculating table switch
3586 class KeyBlock : IComparable
3588 public KeyBlock (long _nFirst)
3590 nFirst = nLast = _nFirst;
3594 public ArrayList rgKeys = null;
3597 get { return (int) (nLast - nFirst + 1); }
3599 public static long TotalLength (KeyBlock kbFirst, KeyBlock kbLast)
3601 return kbLast.nLast - kbFirst.nFirst + 1;
3603 public int CompareTo (object obj)
3605 KeyBlock kb = (KeyBlock) obj;
3606 int nLength = Length;
3607 int nLengthOther = kb.Length;
3608 if (nLengthOther == nLength)
3609 return (int) (kb.nFirst - nFirst);
3610 return nLength - nLengthOther;
3615 /// This method emits code for a lookup-based switch statement (non-string)
3616 /// Basically it groups the cases into blocks that are at least half full,
3617 /// and then spits out individual lookup opcodes for each block.
3618 /// It emits the longest blocks first, and short blocks are just
3619 /// handled with direct compares.
3621 /// <param name="ec"></param>
3622 /// <param name="val"></param>
3623 /// <returns></returns>
3624 bool TableSwitchEmit (EmitContext ec, LocalBuilder val)
3626 int cElements = Elements.Count;
3627 object [] rgKeys = new object [cElements];
3628 Elements.Keys.CopyTo (rgKeys, 0);
3629 Array.Sort (rgKeys);
3631 // initialize the block list with one element per key
3632 ArrayList rgKeyBlocks = new ArrayList ();
3633 foreach (object key in rgKeys)
3634 rgKeyBlocks.Add (new KeyBlock (Convert.ToInt64 (key)));
3637 // iteratively merge the blocks while they are at least half full
3638 // there's probably a really cool way to do this with a tree...
3639 while (rgKeyBlocks.Count > 1)
3641 ArrayList rgKeyBlocksNew = new ArrayList ();
3642 kbCurr = (KeyBlock) rgKeyBlocks [0];
3643 for (int ikb = 1; ikb < rgKeyBlocks.Count; ikb++)
3645 KeyBlock kb = (KeyBlock) rgKeyBlocks [ikb];
3646 if ((kbCurr.Length + kb.Length) * 2 >= KeyBlock.TotalLength (kbCurr, kb))
3649 kbCurr.nLast = kb.nLast;
3653 // start a new block
3654 rgKeyBlocksNew.Add (kbCurr);
3658 rgKeyBlocksNew.Add (kbCurr);
3659 if (rgKeyBlocks.Count == rgKeyBlocksNew.Count)
3661 rgKeyBlocks = rgKeyBlocksNew;
3664 // initialize the key lists
3665 foreach (KeyBlock kb in rgKeyBlocks)
3666 kb.rgKeys = new ArrayList ();
3668 // fill the key lists
3670 if (rgKeyBlocks.Count > 0) {
3671 kbCurr = (KeyBlock) rgKeyBlocks [0];
3672 foreach (object key in rgKeys)
3674 bool fNextBlock = (key is UInt64) ? (ulong) key > (ulong) kbCurr.nLast : Convert.ToInt64 (key) > kbCurr.nLast;
3676 kbCurr = (KeyBlock) rgKeyBlocks [++iBlockCurr];
3677 kbCurr.rgKeys.Add (key);
3681 // sort the blocks so we can tackle the largest ones first
3682 rgKeyBlocks.Sort ();
3684 // okay now we can start...
3685 ILGenerator ig = ec.ig;
3686 Label lblEnd = ig.DefineLabel (); // at the end ;-)
3687 Label lblDefault = ig.DefineLabel ();
3689 Type typeKeys = null;
3690 if (rgKeys.Length > 0)
3691 typeKeys = rgKeys [0].GetType (); // used for conversions
3693 for (int iBlock = rgKeyBlocks.Count - 1; iBlock >= 0; --iBlock)
3695 KeyBlock kb = ((KeyBlock) rgKeyBlocks [iBlock]);
3696 lblDefault = (iBlock == 0) ? DefaultTarget : ig.DefineLabel ();
3699 foreach (object key in kb.rgKeys)
3701 ig.Emit (OpCodes.Ldloc, val);
3702 EmitObjectInteger (ig, key);
3703 SwitchLabel sl = (SwitchLabel) Elements [key];
3704 ig.Emit (OpCodes.Beq, sl.ILLabel);
3709 // TODO: if all the keys in the block are the same and there are
3710 // no gaps/defaults then just use a range-check.
3711 if (SwitchType == TypeManager.int64_type ||
3712 SwitchType == TypeManager.uint64_type)
3714 // TODO: optimize constant/I4 cases
3716 // check block range (could be > 2^31)
3717 ig.Emit (OpCodes.Ldloc, val);
3718 EmitObjectInteger (ig, Convert.ChangeType (kb.nFirst, typeKeys));
3719 ig.Emit (OpCodes.Blt, lblDefault);
3720 ig.Emit (OpCodes.Ldloc, val);
3721 EmitObjectInteger (ig, Convert.ChangeType (kb.nFirst, typeKeys));
3722 ig.Emit (OpCodes.Bgt, lblDefault);
3725 ig.Emit (OpCodes.Ldloc, val);
3728 EmitObjectInteger (ig, Convert.ChangeType (kb.nFirst, typeKeys));
3729 ig.Emit (OpCodes.Sub);
3731 ig.Emit (OpCodes.Conv_I4); // assumes < 2^31 labels!
3736 ig.Emit (OpCodes.Ldloc, val);
3737 int nFirst = (int) kb.nFirst;
3740 IntConstant.EmitInt (ig, nFirst);
3741 ig.Emit (OpCodes.Sub);
3743 else if (nFirst < 0)
3745 IntConstant.EmitInt (ig, -nFirst);
3746 ig.Emit (OpCodes.Add);
3750 // first, build the list of labels for the switch
3752 int cJumps = kb.Length;
3753 Label [] rgLabels = new Label [cJumps];
3754 for (int iJump = 0; iJump < cJumps; iJump++)
3756 object key = kb.rgKeys [iKey];
3757 if (Convert.ToInt64 (key) == kb.nFirst + iJump)
3759 SwitchLabel sl = (SwitchLabel) Elements [key];
3760 rgLabels [iJump] = sl.ILLabel;
3764 rgLabels [iJump] = lblDefault;
3766 // emit the switch opcode
3767 ig.Emit (OpCodes.Switch, rgLabels);
3770 // mark the default for this block
3772 ig.MarkLabel (lblDefault);
3775 // TODO: find the default case and emit it here,
3776 // to prevent having to do the following jump.
3777 // make sure to mark other labels in the default section
3779 // the last default just goes to the end
3780 ig.Emit (OpCodes.Br, lblDefault);
3782 // now emit the code for the sections
3783 bool fFoundDefault = false;
3784 bool fAllReturn = true;
3785 foreach (SwitchSection ss in Sections)
3787 foreach (SwitchLabel sl in ss.Labels)
3789 ig.MarkLabel (sl.ILLabel);
3790 ig.MarkLabel (sl.ILLabelCode);
3791 if (sl.Label == null)
3793 ig.MarkLabel (lblDefault);
3794 fFoundDefault = true;
3797 bool returns = ss.Block.Emit (ec);
3798 fAllReturn &= returns;
3799 //ig.Emit (OpCodes.Br, lblEnd);
3802 if (!fFoundDefault) {
3803 ig.MarkLabel (lblDefault);
3806 ig.MarkLabel (lblEnd);
3811 // This simple emit switch works, but does not take advantage of the
3813 // TODO: remove non-string logic from here
3814 // TODO: binary search strings?
3816 bool SimpleSwitchEmit (EmitContext ec, LocalBuilder val)
3818 ILGenerator ig = ec.ig;
3819 Label end_of_switch = ig.DefineLabel ();
3820 Label next_test = ig.DefineLabel ();
3821 Label null_target = ig.DefineLabel ();
3822 bool default_found = false;
3823 bool first_test = true;
3824 bool pending_goto_end = false;
3825 bool all_return = true;
3826 bool is_string = false;
3830 // Special processing for strings: we cant compare
3833 if (SwitchType == TypeManager.string_type){
3834 ig.Emit (OpCodes.Ldloc, val);
3837 if (Elements.Contains (NullLiteral.Null)){
3838 ig.Emit (OpCodes.Brfalse, null_target);
3840 ig.Emit (OpCodes.Brfalse, default_target);
3842 ig.Emit (OpCodes.Ldloc, val);
3843 ig.Emit (OpCodes.Call, TypeManager.string_isinterneted_string);
3844 ig.Emit (OpCodes.Stloc, val);
3847 foreach (SwitchSection ss in Sections){
3848 Label sec_begin = ig.DefineLabel ();
3850 if (pending_goto_end)
3851 ig.Emit (OpCodes.Br, end_of_switch);
3853 int label_count = ss.Labels.Count;
3855 foreach (SwitchLabel sl in ss.Labels){
3856 ig.MarkLabel (sl.ILLabel);
3859 ig.MarkLabel (next_test);
3860 next_test = ig.DefineLabel ();
3863 // If we are the default target
3865 if (sl.Label == null){
3866 ig.MarkLabel (default_target);
3867 default_found = true;
3869 object lit = sl.Converted;
3871 if (lit is NullLiteral){
3873 if (label_count == 1)
3874 ig.Emit (OpCodes.Br, next_test);
3879 StringConstant str = (StringConstant) lit;
3881 ig.Emit (OpCodes.Ldloc, val);
3882 ig.Emit (OpCodes.Ldstr, str.Value);
3883 if (label_count == 1)
3884 ig.Emit (OpCodes.Bne_Un, next_test);
3886 ig.Emit (OpCodes.Beq, sec_begin);
3888 ig.Emit (OpCodes.Ldloc, val);
3889 EmitObjectInteger (ig, lit);
3890 ig.Emit (OpCodes.Ceq);
3891 if (label_count == 1)
3892 ig.Emit (OpCodes.Brfalse, next_test);
3894 ig.Emit (OpCodes.Brtrue, sec_begin);
3898 if (label_count != 1)
3899 ig.Emit (OpCodes.Br, next_test);
3902 ig.MarkLabel (null_target);
3903 ig.MarkLabel (sec_begin);
3904 foreach (SwitchLabel sl in ss.Labels)
3905 ig.MarkLabel (sl.ILLabelCode);
3907 bool returns = ss.Block.Emit (ec);
3909 pending_goto_end = false;
3912 pending_goto_end = true;
3916 if (!default_found){
3917 ig.MarkLabel (default_target);
3920 ig.MarkLabel (next_test);
3921 ig.MarkLabel (end_of_switch);
3926 public override bool Resolve (EmitContext ec)
3928 Expr = Expr.Resolve (ec);
3932 new_expr = SwitchGoverningType (ec, Expr.Type);
3933 if (new_expr == null){
3934 Report.Error (151, loc, "An integer type or string was expected for switch");
3939 SwitchType = new_expr.Type;
3941 if (!CheckSwitch (ec))
3944 Switch old_switch = ec.Switch;
3946 ec.Switch.SwitchType = SwitchType;
3948 ec.StartFlowBranching (FlowBranchingType.SWITCH, loc);
3951 foreach (SwitchSection ss in Sections){
3953 ec.CurrentBranching.CreateSibling ();
3957 if (ss.Block.Resolve (ec) != true)
3963 ec.CurrentBranching.CreateSibling ();
3965 ec.EndFlowBranching ();
3966 ec.Switch = old_switch;
3971 protected override bool DoEmit (EmitContext ec)
3973 // Store variable for comparission purposes
3974 LocalBuilder value = ec.ig.DeclareLocal (SwitchType);
3976 ec.ig.Emit (OpCodes.Stloc, value);
3978 ILGenerator ig = ec.ig;
3980 default_target = ig.DefineLabel ();
3983 // Setup the codegen context
3985 Label old_end = ec.LoopEnd;
3986 Switch old_switch = ec.Switch;
3988 ec.LoopEnd = ig.DefineLabel ();
3993 if (SwitchType == TypeManager.string_type)
3994 all_return = SimpleSwitchEmit (ec, value);
3996 all_return = TableSwitchEmit (ec, value);
3998 // Restore context state.
3999 ig.MarkLabel (ec.LoopEnd);
4002 // Restore the previous context
4004 ec.LoopEnd = old_end;
4005 ec.Switch = old_switch;
4011 public class Lock : Statement {
4013 Statement Statement;
4015 public Lock (Expression expr, Statement stmt, Location l)
4022 public override bool Resolve (EmitContext ec)
4024 expr = expr.Resolve (ec);
4025 return Statement.Resolve (ec) && expr != null;
4028 protected override bool DoEmit (EmitContext ec)
4030 Type type = expr.Type;
4033 if (type.IsValueType){
4034 Report.Error (185, loc, "lock statement requires the expression to be " +
4035 " a reference type (type is: `" +
4036 TypeManager.CSharpName (type) + "'");
4040 ILGenerator ig = ec.ig;
4041 LocalBuilder temp = ig.DeclareLocal (type);
4044 ig.Emit (OpCodes.Dup);
4045 ig.Emit (OpCodes.Stloc, temp);
4046 ig.Emit (OpCodes.Call, TypeManager.void_monitor_enter_object);
4049 Label end = ig.BeginExceptionBlock ();
4050 bool old_in_try = ec.InTry;
4052 Label finish = ig.DefineLabel ();
4053 val = Statement.Emit (ec);
4054 ec.InTry = old_in_try;
4055 // ig.Emit (OpCodes.Leave, finish);
4057 ig.MarkLabel (finish);
4060 ig.BeginFinallyBlock ();
4061 ig.Emit (OpCodes.Ldloc, temp);
4062 ig.Emit (OpCodes.Call, TypeManager.void_monitor_exit_object);
4063 ig.EndExceptionBlock ();
4069 public class Unchecked : Statement {
4070 public readonly Block Block;
4072 public Unchecked (Block b)
4077 public override bool Resolve (EmitContext ec)
4079 bool previous_state = ec.CheckState;
4080 bool previous_state_const = ec.ConstantCheckState;
4082 ec.CheckState = false;
4083 ec.ConstantCheckState = false;
4084 bool ret = Block.Resolve (ec);
4085 ec.CheckState = previous_state;
4086 ec.ConstantCheckState = previous_state_const;
4091 protected override bool DoEmit (EmitContext ec)
4093 bool previous_state = ec.CheckState;
4094 bool previous_state_const = ec.ConstantCheckState;
4097 ec.CheckState = false;
4098 ec.ConstantCheckState = false;
4099 val = Block.Emit (ec);
4100 ec.CheckState = previous_state;
4101 ec.ConstantCheckState = previous_state_const;
4107 public class Checked : Statement {
4108 public readonly Block Block;
4110 public Checked (Block b)
4115 public override bool Resolve (EmitContext ec)
4117 bool previous_state = ec.CheckState;
4118 bool previous_state_const = ec.ConstantCheckState;
4120 ec.CheckState = true;
4121 ec.ConstantCheckState = true;
4122 bool ret = Block.Resolve (ec);
4123 ec.CheckState = previous_state;
4124 ec.ConstantCheckState = previous_state_const;
4129 protected override bool DoEmit (EmitContext ec)
4131 bool previous_state = ec.CheckState;
4132 bool previous_state_const = ec.ConstantCheckState;
4135 ec.CheckState = true;
4136 ec.ConstantCheckState = true;
4137 val = Block.Emit (ec);
4138 ec.CheckState = previous_state;
4139 ec.ConstantCheckState = previous_state_const;
4145 public class Unsafe : Statement {
4146 public readonly Block Block;
4148 public Unsafe (Block b)
4153 public override bool Resolve (EmitContext ec)
4155 bool previous_state = ec.InUnsafe;
4159 val = Block.Resolve (ec);
4160 ec.InUnsafe = previous_state;
4165 protected override bool DoEmit (EmitContext ec)
4167 bool previous_state = ec.InUnsafe;
4171 val = Block.Emit (ec);
4172 ec.InUnsafe = previous_state;
4181 public class Fixed : Statement {
4183 ArrayList declarators;
4184 Statement statement;
4189 public bool is_object;
4190 public VariableInfo vi;
4191 public Expression expr;
4192 public Expression converted;
4195 public Fixed (Expression type, ArrayList decls, Statement stmt, Location l)
4198 declarators = decls;
4203 public override bool Resolve (EmitContext ec)
4205 expr_type = ec.DeclSpace.ResolveType (type, false, loc);
4206 if (expr_type == null)
4209 data = new FixedData [declarators.Count];
4211 if (!expr_type.IsPointer){
4212 Report.Error (209, loc, "Variables in a fixed statement must be pointers");
4217 foreach (Pair p in declarators){
4218 VariableInfo vi = (VariableInfo) p.First;
4219 Expression e = (Expression) p.Second;
4224 // The rules for the possible declarators are pretty wise,
4225 // but the production on the grammar is more concise.
4227 // So we have to enforce these rules here.
4229 // We do not resolve before doing the case 1 test,
4230 // because the grammar is explicit in that the token &
4231 // is present, so we need to test for this particular case.
4235 // Case 1: & object.
4237 if (e is Unary && ((Unary) e).Oper == Unary.Operator.AddressOf){
4238 Expression child = ((Unary) e).Expr;
4241 if (child is ParameterReference || child is LocalVariableReference){
4244 "No need to use fixed statement for parameters or " +
4245 "local variable declarations (address is already " +
4254 child = ((Unary) e).Expr;
4256 if (!TypeManager.VerifyUnManaged (child.Type, loc))
4259 data [i].is_object = true;
4261 data [i].converted = null;
4275 if (e.Type.IsArray){
4276 Type array_type = e.Type.GetElementType ();
4280 // Provided that array_type is unmanaged,
4282 if (!TypeManager.VerifyUnManaged (array_type, loc))
4286 // and T* is implicitly convertible to the
4287 // pointer type given in the fixed statement.
4289 ArrayPtr array_ptr = new ArrayPtr (e, loc);
4291 Expression converted = Expression.ConvertImplicitRequired (
4292 ec, array_ptr, vi.VariableType, loc);
4293 if (converted == null)
4296 data [i].is_object = false;
4298 data [i].converted = converted;
4308 if (e.Type == TypeManager.string_type){
4309 data [i].is_object = false;
4311 data [i].converted = null;
4317 return statement.Resolve (ec);
4320 protected override bool DoEmit (EmitContext ec)
4322 ILGenerator ig = ec.ig;
4324 bool is_ret = false;
4325 LocalBuilder [] clear_list = new LocalBuilder [data.Length];
4327 for (int i = 0; i < data.Length; i++) {
4328 VariableInfo vi = data [i].vi;
4331 // Case 1: & object.
4333 if (data [i].is_object) {
4335 // Store pointer in pinned location
4337 data [i].expr.Emit (ec);
4338 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4339 clear_list [i] = vi.LocalBuilder;
4346 if (data [i].expr.Type.IsArray){
4348 // Store pointer in pinned location
4350 data [i].converted.Emit (ec);
4352 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4353 clear_list [i] = vi.LocalBuilder;
4360 if (data [i].expr.Type == TypeManager.string_type){
4361 LocalBuilder pinned_string = ig.DeclareLocal (TypeManager.string_type);
4362 TypeManager.MakePinned (pinned_string);
4363 clear_list [i] = pinned_string;
4365 data [i].expr.Emit (ec);
4366 ig.Emit (OpCodes.Stloc, pinned_string);
4368 Expression sptr = new StringPtr (pinned_string, loc);
4369 Expression converted = Expression.ConvertImplicitRequired (
4370 ec, sptr, vi.VariableType, loc);
4372 if (converted == null)
4375 converted.Emit (ec);
4376 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4380 is_ret = statement.Emit (ec);
4385 // Clear the pinned variable
4387 for (int i = 0; i < data.Length; i++) {
4388 VariableInfo vi = data [i].vi;
4390 if (data [i].is_object || data [i].expr.Type.IsArray) {
4391 ig.Emit (OpCodes.Ldc_I4_0);
4392 ig.Emit (OpCodes.Conv_U);
4393 ig.Emit (OpCodes.Stloc, clear_list [i]);
4394 } else if (data [i].expr.Type == TypeManager.string_type){
4395 ig.Emit (OpCodes.Ldnull);
4396 ig.Emit (OpCodes.Stloc, clear_list [i]);
4404 public class Catch {
4405 public readonly string Name;
4406 public readonly Block Block;
4407 public readonly Location Location;
4409 Expression type_expr;
4412 public Catch (Expression type, string name, Block block, Location l)
4420 public Type CatchType {
4426 public bool IsGeneral {
4428 return type_expr == null;
4432 public bool Resolve (EmitContext ec)
4434 if (type_expr != null) {
4435 type = ec.DeclSpace.ResolveType (type_expr, false, Location);
4439 if (type != TypeManager.exception_type && !type.IsSubclassOf (TypeManager.exception_type)){
4440 Report.Error (155, Location,
4441 "The type caught or thrown must be derived " +
4442 "from System.Exception");
4448 if (!Block.Resolve (ec))
4455 public class Try : Statement {
4456 public readonly Block Fini, Block;
4457 public readonly ArrayList Specific;
4458 public readonly Catch General;
4461 // specific, general and fini might all be null.
4463 public Try (Block block, ArrayList specific, Catch general, Block fini, Location l)
4465 if (specific == null && general == null){
4466 Console.WriteLine ("CIR.Try: Either specific or general have to be non-null");
4470 this.Specific = specific;
4471 this.General = general;
4476 public override bool Resolve (EmitContext ec)
4480 ec.StartFlowBranching (FlowBranchingType.EXCEPTION, Block.StartLocation);
4482 Report.Debug (1, "START OF TRY BLOCK", Block.StartLocation);
4484 bool old_in_try = ec.InTry;
4487 if (!Block.Resolve (ec))
4490 ec.InTry = old_in_try;
4492 FlowBranching.UsageVector vector = ec.CurrentBranching.CurrentUsageVector;
4494 Report.Debug (1, "START OF CATCH BLOCKS", vector);
4496 foreach (Catch c in Specific){
4497 ec.CurrentBranching.CreateSibling ();
4498 Report.Debug (1, "STARTED SIBLING FOR CATCH", ec.CurrentBranching);
4500 if (c.Name != null) {
4501 VariableInfo vi = c.Block.GetVariableInfo (c.Name);
4503 throw new Exception ();
4508 bool old_in_catch = ec.InCatch;
4511 if (!c.Resolve (ec))
4514 ec.InCatch = old_in_catch;
4516 FlowBranching.UsageVector current = ec.CurrentBranching.CurrentUsageVector;
4518 if (!current.AlwaysReturns && !current.AlwaysBreaks)
4519 vector.AndLocals (current);
4521 vector.Or (current);
4524 Report.Debug (1, "END OF CATCH BLOCKS", ec.CurrentBranching);
4526 if (General != null){
4527 ec.CurrentBranching.CreateSibling ();
4528 Report.Debug (1, "STARTED SIBLING FOR GENERAL", ec.CurrentBranching);
4530 bool old_in_catch = ec.InCatch;
4533 if (!General.Resolve (ec))
4536 ec.InCatch = old_in_catch;
4538 FlowBranching.UsageVector current = ec.CurrentBranching.CurrentUsageVector;
4540 if (!current.AlwaysReturns && !current.AlwaysBreaks)
4541 vector.AndLocals (current);
4543 vector.Or (current);
4546 Report.Debug (1, "END OF GENERAL CATCH BLOCKS", ec.CurrentBranching);
4549 ec.CurrentBranching.CreateSiblingForFinally ();
4550 Report.Debug (1, "STARTED SIBLING FOR FINALLY", ec.CurrentBranching, vector);
4552 bool old_in_finally = ec.InFinally;
4553 ec.InFinally = true;
4555 if (!Fini.Resolve (ec))
4558 ec.InFinally = old_in_finally;
4561 FlowReturns returns = ec.EndFlowBranching ();
4563 FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
4565 Report.Debug (1, "END OF FINALLY", ec.CurrentBranching, returns, vector, f_vector);
4567 if ((returns == FlowReturns.SOMETIMES) || (returns == FlowReturns.ALWAYS)) {
4568 ec.CurrentBranching.CheckOutParameters (f_vector.Parameters, loc);
4571 ec.CurrentBranching.CurrentUsageVector.Or (vector);
4573 Report.Debug (1, "END OF TRY", ec.CurrentBranching);
4578 protected override bool DoEmit (EmitContext ec)
4580 ILGenerator ig = ec.ig;
4582 Label finish = ig.DefineLabel ();;
4586 end = ig.BeginExceptionBlock ();
4587 bool old_in_try = ec.InTry;
4589 returns = Block.Emit (ec);
4590 ec.InTry = old_in_try;
4593 // System.Reflection.Emit provides this automatically:
4594 // ig.Emit (OpCodes.Leave, finish);
4596 bool old_in_catch = ec.InCatch;
4598 DeclSpace ds = ec.DeclSpace;
4600 foreach (Catch c in Specific){
4603 ig.BeginCatchBlock (c.CatchType);
4605 if (c.Name != null){
4606 vi = c.Block.GetVariableInfo (c.Name);
4608 throw new Exception ("Variable does not exist in this block");
4610 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4612 ig.Emit (OpCodes.Pop);
4614 if (!c.Block.Emit (ec))
4618 if (General != null){
4619 ig.BeginCatchBlock (TypeManager.object_type);
4620 ig.Emit (OpCodes.Pop);
4621 if (!General.Block.Emit (ec))
4624 ec.InCatch = old_in_catch;
4626 ig.MarkLabel (finish);
4628 ig.BeginFinallyBlock ();
4629 bool old_in_finally = ec.InFinally;
4630 ec.InFinally = true;
4632 ec.InFinally = old_in_finally;
4635 ig.EndExceptionBlock ();
4638 if (!returns || ec.InTry || ec.InCatch)
4641 // Unfortunately, System.Reflection.Emit automatically emits a leave
4642 // to the end of the finally block. This is a problem if `returns'
4643 // is true since we may jump to a point after the end of the method.
4644 // As a workaround, emit an explicit ret here.
4646 if (ec.ReturnType != null)
4647 ec.ig.Emit (OpCodes.Ldloc, ec.TemporaryReturn ());
4648 ec.ig.Emit (OpCodes.Ret);
4654 public class Using : Statement {
4655 object expression_or_block;
4656 Statement Statement;
4661 Expression [] converted_vars;
4662 ExpressionStatement [] assign;
4664 public Using (object expression_or_block, Statement stmt, Location l)
4666 this.expression_or_block = expression_or_block;
4672 // Resolves for the case of using using a local variable declaration.
4674 bool ResolveLocalVariableDecls (EmitContext ec)
4676 bool need_conv = false;
4677 expr_type = ec.DeclSpace.ResolveType (expr, false, loc);
4680 if (expr_type == null)
4684 // The type must be an IDisposable or an implicit conversion
4687 converted_vars = new Expression [var_list.Count];
4688 assign = new ExpressionStatement [var_list.Count];
4689 if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
4690 foreach (DictionaryEntry e in var_list){
4691 Expression var = (Expression) e.Key;
4693 var = var.ResolveLValue (ec, new EmptyExpression ());
4697 converted_vars [i] = Expression.ConvertImplicitRequired (
4698 ec, var, TypeManager.idisposable_type, loc);
4700 if (converted_vars [i] == null)
4708 foreach (DictionaryEntry e in var_list){
4709 LocalVariableReference var = (LocalVariableReference) e.Key;
4710 Expression new_expr = (Expression) e.Value;
4713 a = new Assign (var, new_expr, loc);
4719 converted_vars [i] = var;
4720 assign [i] = (ExpressionStatement) a;
4727 bool ResolveExpression (EmitContext ec)
4729 if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
4730 conv = Expression.ConvertImplicitRequired (
4731 ec, expr, TypeManager.idisposable_type, loc);
4741 // Emits the code for the case of using using a local variable declaration.
4743 bool EmitLocalVariableDecls (EmitContext ec)
4745 ILGenerator ig = ec.ig;
4748 bool old_in_try = ec.InTry;
4750 for (i = 0; i < assign.Length; i++) {
4751 assign [i].EmitStatement (ec);
4753 ig.BeginExceptionBlock ();
4755 Statement.Emit (ec);
4756 ec.InTry = old_in_try;
4758 bool old_in_finally = ec.InFinally;
4759 ec.InFinally = true;
4760 var_list.Reverse ();
4761 foreach (DictionaryEntry e in var_list){
4762 LocalVariableReference var = (LocalVariableReference) e.Key;
4763 Label skip = ig.DefineLabel ();
4766 ig.BeginFinallyBlock ();
4769 ig.Emit (OpCodes.Brfalse, skip);
4770 converted_vars [i].Emit (ec);
4771 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4772 ig.MarkLabel (skip);
4773 ig.EndExceptionBlock ();
4775 ec.InFinally = old_in_finally;
4780 bool EmitExpression (EmitContext ec)
4783 // Make a copy of the expression and operate on that.
4785 ILGenerator ig = ec.ig;
4786 LocalBuilder local_copy = ig.DeclareLocal (expr_type);
4791 ig.Emit (OpCodes.Stloc, local_copy);
4793 bool old_in_try = ec.InTry;
4795 ig.BeginExceptionBlock ();
4796 Statement.Emit (ec);
4797 ec.InTry = old_in_try;
4799 Label skip = ig.DefineLabel ();
4800 bool old_in_finally = ec.InFinally;
4801 ig.BeginFinallyBlock ();
4802 ig.Emit (OpCodes.Ldloc, local_copy);
4803 ig.Emit (OpCodes.Brfalse, skip);
4804 ig.Emit (OpCodes.Ldloc, local_copy);
4805 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4806 ig.MarkLabel (skip);
4807 ec.InFinally = old_in_finally;
4808 ig.EndExceptionBlock ();
4813 public override bool Resolve (EmitContext ec)
4815 if (expression_or_block is DictionaryEntry){
4816 expr = (Expression) ((DictionaryEntry) expression_or_block).Key;
4817 var_list = (ArrayList)((DictionaryEntry)expression_or_block).Value;
4819 if (!ResolveLocalVariableDecls (ec))
4822 } else if (expression_or_block is Expression){
4823 expr = (Expression) expression_or_block;
4825 expr = expr.Resolve (ec);
4829 expr_type = expr.Type;
4831 if (!ResolveExpression (ec))
4835 return Statement.Resolve (ec);
4838 protected override bool DoEmit (EmitContext ec)
4840 if (expression_or_block is DictionaryEntry)
4841 return EmitLocalVariableDecls (ec);
4842 else if (expression_or_block is Expression)
4843 return EmitExpression (ec);
4850 /// Implementation of the foreach C# statement
4852 public class Foreach : Statement {
4854 LocalVariableReference variable;
4856 Statement statement;
4857 ForeachHelperMethods hm;
4858 Expression empty, conv;
4859 Type array_type, element_type;
4862 public Foreach (Expression type, LocalVariableReference var, Expression expr,
4863 Statement stmt, Location l)
4866 this.variable = var;
4872 public override bool Resolve (EmitContext ec)
4874 expr = expr.Resolve (ec);
4878 var_type = ec.DeclSpace.ResolveType (type, false, loc);
4879 if (var_type == null)
4883 // We need an instance variable. Not sure this is the best
4884 // way of doing this.
4886 // FIXME: When we implement propertyaccess, will those turn
4887 // out to return values in ExprClass? I think they should.
4889 if (!(expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.Value ||
4890 expr.eclass == ExprClass.PropertyAccess || expr.eclass == ExprClass.IndexerAccess)){
4891 error1579 (expr.Type);
4895 if (expr.Type.IsArray) {
4896 array_type = expr.Type;
4897 element_type = array_type.GetElementType ();
4899 empty = new EmptyExpression (element_type);
4901 hm = ProbeCollectionType (ec, expr.Type);
4903 error1579 (expr.Type);
4907 array_type = expr.Type;
4908 element_type = hm.element_type;
4910 empty = new EmptyExpression (hm.element_type);
4913 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
4914 ec.CurrentBranching.CreateSibling ();
4918 // FIXME: maybe we can apply the same trick we do in the
4919 // array handling to avoid creating empty and conv in some cases.
4921 // Although it is not as important in this case, as the type
4922 // will not likely be object (what the enumerator will return).
4924 conv = Expression.ConvertExplicit (ec, empty, var_type, loc);
4928 if (variable.ResolveLValue (ec, empty) == null)
4931 if (!statement.Resolve (ec))
4934 FlowReturns returns = ec.EndFlowBranching ();
4940 // Retrieves a `public bool MoveNext ()' method from the Type `t'
4942 static MethodInfo FetchMethodMoveNext (Type t)
4944 MemberList move_next_list;
4946 move_next_list = TypeContainer.FindMembers (
4947 t, MemberTypes.Method,
4948 BindingFlags.Public | BindingFlags.Instance,
4949 Type.FilterName, "MoveNext");
4950 if (move_next_list.Count == 0)
4953 foreach (MemberInfo m in move_next_list){
4954 MethodInfo mi = (MethodInfo) m;
4957 args = TypeManager.GetArgumentTypes (mi);
4958 if (args != null && args.Length == 0){
4959 if (mi.ReturnType == TypeManager.bool_type)
4967 // Retrieves a `public T get_Current ()' method from the Type `t'
4969 static MethodInfo FetchMethodGetCurrent (Type t)
4971 MemberList move_next_list;
4973 move_next_list = TypeContainer.FindMembers (
4974 t, MemberTypes.Method,
4975 BindingFlags.Public | BindingFlags.Instance,
4976 Type.FilterName, "get_Current");
4977 if (move_next_list.Count == 0)
4980 foreach (MemberInfo m in move_next_list){
4981 MethodInfo mi = (MethodInfo) m;
4984 args = TypeManager.GetArgumentTypes (mi);
4985 if (args != null && args.Length == 0)
4992 // This struct records the helper methods used by the Foreach construct
4994 class ForeachHelperMethods {
4995 public EmitContext ec;
4996 public MethodInfo get_enumerator;
4997 public MethodInfo move_next;
4998 public MethodInfo get_current;
4999 public Type element_type;
5000 public Type enumerator_type;
5001 public bool is_disposable;
5003 public ForeachHelperMethods (EmitContext ec)
5006 this.element_type = TypeManager.object_type;
5007 this.enumerator_type = TypeManager.ienumerator_type;
5008 this.is_disposable = true;
5012 static bool GetEnumeratorFilter (MemberInfo m, object criteria)
5017 if (!(m is MethodInfo))
5020 if (m.Name != "GetEnumerator")
5023 MethodInfo mi = (MethodInfo) m;
5024 Type [] args = TypeManager.GetArgumentTypes (mi);
5026 if (args.Length != 0)
5029 ForeachHelperMethods hm = (ForeachHelperMethods) criteria;
5030 EmitContext ec = hm.ec;
5033 // Check whether GetEnumerator is accessible to us
5035 MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
5037 Type declaring = mi.DeclaringType;
5038 if (prot == MethodAttributes.Private){
5039 if (declaring != ec.ContainerType)
5041 } else if (prot == MethodAttributes.FamANDAssem){
5042 // If from a different assembly, false
5043 if (!(mi is MethodBuilder))
5046 // Are we being invoked from the same class, or from a derived method?
5048 if (ec.ContainerType != declaring){
5049 if (!ec.ContainerType.IsSubclassOf (declaring))
5052 } else if (prot == MethodAttributes.FamORAssem){
5053 if (!(mi is MethodBuilder ||
5054 ec.ContainerType == declaring ||
5055 ec.ContainerType.IsSubclassOf (declaring)))
5057 } if (prot == MethodAttributes.Family){
5058 if (!(ec.ContainerType == declaring ||
5059 ec.ContainerType.IsSubclassOf (declaring)))
5064 // Ok, we can access it, now make sure that we can do something
5065 // with this `GetEnumerator'
5068 if (mi.ReturnType == TypeManager.ienumerator_type ||
5069 TypeManager.ienumerator_type.IsAssignableFrom (mi.ReturnType) ||
5070 (!RootContext.StdLib && TypeManager.ImplementsInterface (mi.ReturnType, TypeManager.ienumerator_type))) {
5071 hm.move_next = TypeManager.bool_movenext_void;
5072 hm.get_current = TypeManager.object_getcurrent_void;
5077 // Ok, so they dont return an IEnumerable, we will have to
5078 // find if they support the GetEnumerator pattern.
5080 Type return_type = mi.ReturnType;
5082 hm.move_next = FetchMethodMoveNext (return_type);
5083 if (hm.move_next == null)
5085 hm.get_current = FetchMethodGetCurrent (return_type);
5086 if (hm.get_current == null)
5089 hm.element_type = hm.get_current.ReturnType;
5090 hm.enumerator_type = return_type;
5091 hm.is_disposable = TypeManager.ImplementsInterface (
5092 hm.enumerator_type, TypeManager.idisposable_type);
5098 /// This filter is used to find the GetEnumerator method
5099 /// on which IEnumerator operates
5101 static MemberFilter FilterEnumerator;
5105 FilterEnumerator = new MemberFilter (GetEnumeratorFilter);
5108 void error1579 (Type t)
5110 Report.Error (1579, loc,
5111 "foreach statement cannot operate on variables of type `" +
5112 t.FullName + "' because that class does not provide a " +
5113 " GetEnumerator method or it is inaccessible");
5116 static bool TryType (Type t, ForeachHelperMethods hm)
5120 mi = TypeContainer.FindMembers (t, MemberTypes.Method,
5121 BindingFlags.Public | BindingFlags.NonPublic |
5122 BindingFlags.Instance,
5123 FilterEnumerator, hm);
5128 hm.get_enumerator = (MethodInfo) mi [0];
5133 // Looks for a usable GetEnumerator in the Type, and if found returns
5134 // the three methods that participate: GetEnumerator, MoveNext and get_Current
5136 ForeachHelperMethods ProbeCollectionType (EmitContext ec, Type t)
5138 ForeachHelperMethods hm = new ForeachHelperMethods (ec);
5140 if (TryType (t, hm))
5144 // Now try to find the method in the interfaces
5147 Type [] ifaces = t.GetInterfaces ();
5149 foreach (Type i in ifaces){
5150 if (TryType (i, hm))
5155 // Since TypeBuilder.GetInterfaces only returns the interface
5156 // types for this type, we have to keep looping, but once
5157 // we hit a non-TypeBuilder (ie, a Type), then we know we are
5158 // done, because it returns all the types
5160 if ((t is TypeBuilder))
5170 // FIXME: possible optimization.
5171 // We might be able to avoid creating `empty' if the type is the sam
5173 bool EmitCollectionForeach (EmitContext ec)
5175 ILGenerator ig = ec.ig;
5176 LocalBuilder enumerator, disposable;
5178 enumerator = ig.DeclareLocal (hm.enumerator_type);
5179 if (hm.is_disposable)
5180 disposable = ig.DeclareLocal (TypeManager.idisposable_type);
5185 // Instantiate the enumerator
5187 if (expr.Type.IsValueType){
5188 if (expr is IMemoryLocation){
5189 IMemoryLocation ml = (IMemoryLocation) expr;
5191 ml.AddressOf (ec, AddressOp.Load);
5193 throw new Exception ("Expr " + expr + " of type " + expr.Type +
5194 " does not implement IMemoryLocation");
5195 ig.Emit (OpCodes.Call, hm.get_enumerator);
5198 ig.Emit (OpCodes.Callvirt, hm.get_enumerator);
5200 ig.Emit (OpCodes.Stloc, enumerator);
5203 // Protect the code in a try/finalize block, so that
5204 // if the beast implement IDisposable, we get rid of it
5207 bool old_in_try = ec.InTry;
5209 if (hm.is_disposable) {
5210 l = ig.BeginExceptionBlock ();
5214 Label end_try = ig.DefineLabel ();
5216 ig.MarkLabel (ec.LoopBegin);
5217 ig.Emit (OpCodes.Ldloc, enumerator);
5218 ig.Emit (OpCodes.Callvirt, hm.move_next);
5219 ig.Emit (OpCodes.Brfalse, end_try);
5220 ig.Emit (OpCodes.Ldloc, enumerator);
5221 ig.Emit (OpCodes.Callvirt, hm.get_current);
5222 variable.EmitAssign (ec, conv);
5223 statement.Emit (ec);
5224 ig.Emit (OpCodes.Br, ec.LoopBegin);
5225 ig.MarkLabel (end_try);
5226 ec.InTry = old_in_try;
5228 // The runtime provides this for us.
5229 // ig.Emit (OpCodes.Leave, end);
5232 // Now the finally block
5234 if (hm.is_disposable) {
5235 Label end_finally = ig.DefineLabel ();
5236 bool old_in_finally = ec.InFinally;
5237 ec.InFinally = true;
5238 ig.BeginFinallyBlock ();
5240 ig.Emit (OpCodes.Ldloc, enumerator);
5241 ig.Emit (OpCodes.Isinst, TypeManager.idisposable_type);
5242 ig.Emit (OpCodes.Stloc, disposable);
5243 ig.Emit (OpCodes.Ldloc, disposable);
5244 ig.Emit (OpCodes.Brfalse, end_finally);
5245 ig.Emit (OpCodes.Ldloc, disposable);
5246 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
5247 ig.MarkLabel (end_finally);
5248 ec.InFinally = old_in_finally;
5250 // The runtime generates this anyways.
5251 // ig.Emit (OpCodes.Endfinally);
5253 ig.EndExceptionBlock ();
5256 ig.MarkLabel (ec.LoopEnd);
5261 // FIXME: possible optimization.
5262 // We might be able to avoid creating `empty' if the type is the sam
5264 bool EmitArrayForeach (EmitContext ec)
5266 int rank = array_type.GetArrayRank ();
5267 ILGenerator ig = ec.ig;
5269 LocalBuilder copy = ig.DeclareLocal (array_type);
5272 // Make our copy of the array
5275 ig.Emit (OpCodes.Stloc, copy);
5278 LocalBuilder counter = ig.DeclareLocal (TypeManager.int32_type);
5282 ig.Emit (OpCodes.Ldc_I4_0);
5283 ig.Emit (OpCodes.Stloc, counter);
5284 test = ig.DefineLabel ();
5285 ig.Emit (OpCodes.Br, test);
5287 loop = ig.DefineLabel ();
5288 ig.MarkLabel (loop);
5290 ig.Emit (OpCodes.Ldloc, copy);
5291 ig.Emit (OpCodes.Ldloc, counter);
5292 ArrayAccess.EmitLoadOpcode (ig, var_type);
5294 variable.EmitAssign (ec, conv);
5296 statement.Emit (ec);
5298 ig.MarkLabel (ec.LoopBegin);
5299 ig.Emit (OpCodes.Ldloc, counter);
5300 ig.Emit (OpCodes.Ldc_I4_1);
5301 ig.Emit (OpCodes.Add);
5302 ig.Emit (OpCodes.Stloc, counter);
5304 ig.MarkLabel (test);
5305 ig.Emit (OpCodes.Ldloc, counter);
5306 ig.Emit (OpCodes.Ldloc, copy);
5307 ig.Emit (OpCodes.Ldlen);
5308 ig.Emit (OpCodes.Conv_I4);
5309 ig.Emit (OpCodes.Blt, loop);
5311 LocalBuilder [] dim_len = new LocalBuilder [rank];
5312 LocalBuilder [] dim_count = new LocalBuilder [rank];
5313 Label [] loop = new Label [rank];
5314 Label [] test = new Label [rank];
5317 for (dim = 0; dim < rank; dim++){
5318 dim_len [dim] = ig.DeclareLocal (TypeManager.int32_type);
5319 dim_count [dim] = ig.DeclareLocal (TypeManager.int32_type);
5320 test [dim] = ig.DefineLabel ();
5321 loop [dim] = ig.DefineLabel ();
5324 for (dim = 0; dim < rank; dim++){
5325 ig.Emit (OpCodes.Ldloc, copy);
5326 IntLiteral.EmitInt (ig, dim);
5327 ig.Emit (OpCodes.Callvirt, TypeManager.int_getlength_int);
5328 ig.Emit (OpCodes.Stloc, dim_len [dim]);
5331 for (dim = 0; dim < rank; dim++){
5332 ig.Emit (OpCodes.Ldc_I4_0);
5333 ig.Emit (OpCodes.Stloc, dim_count [dim]);
5334 ig.Emit (OpCodes.Br, test [dim]);
5335 ig.MarkLabel (loop [dim]);
5338 ig.Emit (OpCodes.Ldloc, copy);
5339 for (dim = 0; dim < rank; dim++)
5340 ig.Emit (OpCodes.Ldloc, dim_count [dim]);
5343 // FIXME: Maybe we can cache the computation of `get'?
5345 Type [] args = new Type [rank];
5348 for (int i = 0; i < rank; i++)
5349 args [i] = TypeManager.int32_type;
5351 ModuleBuilder mb = CodeGen.ModuleBuilder;
5352 get = mb.GetArrayMethod (
5354 CallingConventions.HasThis| CallingConventions.Standard,
5356 ig.Emit (OpCodes.Call, get);
5357 variable.EmitAssign (ec, conv);
5358 statement.Emit (ec);
5359 ig.MarkLabel (ec.LoopBegin);
5360 for (dim = rank - 1; dim >= 0; dim--){
5361 ig.Emit (OpCodes.Ldloc, dim_count [dim]);
5362 ig.Emit (OpCodes.Ldc_I4_1);
5363 ig.Emit (OpCodes.Add);
5364 ig.Emit (OpCodes.Stloc, dim_count [dim]);
5366 ig.MarkLabel (test [dim]);
5367 ig.Emit (OpCodes.Ldloc, dim_count [dim]);
5368 ig.Emit (OpCodes.Ldloc, dim_len [dim]);
5369 ig.Emit (OpCodes.Blt, loop [dim]);
5372 ig.MarkLabel (ec.LoopEnd);
5377 protected override bool DoEmit (EmitContext ec)
5381 ILGenerator ig = ec.ig;
5383 Label old_begin = ec.LoopBegin, old_end = ec.LoopEnd;
5384 bool old_inloop = ec.InLoop;
5385 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
5386 ec.LoopBegin = ig.DefineLabel ();
5387 ec.LoopEnd = ig.DefineLabel ();
5389 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
5392 ret_val = EmitCollectionForeach (ec);
5394 ret_val = EmitArrayForeach (ec);
5396 ec.LoopBegin = old_begin;
5397 ec.LoopEnd = old_end;
5398 ec.InLoop = old_inloop;
5399 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;