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)
44 Report.Debug (8, "MARK", this, loc);
48 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
54 if (e.Type != TypeManager.bool_type){
55 e = Expression.ConvertImplicit (ec, e, TypeManager.bool_type,
61 31, loc, "Can not convert the expression to a boolean");
70 /// Encapsulates the emission of a boolean test and jumping to a
73 /// This will emit the bool expression in `bool_expr' and if
74 /// `target_is_for_true' is true, then the code will generate a
75 /// brtrue to the target. Otherwise a brfalse.
77 public static void EmitBoolExpression (EmitContext ec, Expression bool_expr,
78 Label target, bool target_is_for_true)
80 ILGenerator ig = ec.ig;
83 if (bool_expr is Unary){
84 Unary u = (Unary) bool_expr;
86 if (u.Oper == Unary.Operator.LogicalNot){
89 u.EmitLogicalNot (ec);
91 } else if (bool_expr is Binary){
92 Binary b = (Binary) bool_expr;
94 if (b.EmitBranchable (ec, target, target_is_for_true))
101 if (target_is_for_true){
103 ig.Emit (OpCodes.Brfalse, target);
105 ig.Emit (OpCodes.Brtrue, target);
108 ig.Emit (OpCodes.Brtrue, target);
110 ig.Emit (OpCodes.Brfalse, target);
114 public static void Warning_DeadCodeFound (Location loc)
116 Report.Warning (162, loc, "Unreachable code detected");
120 public class EmptyStatement : Statement {
121 public override bool Resolve (EmitContext ec)
126 protected override bool DoEmit (EmitContext ec)
132 public class If : Statement {
134 public Statement TrueStatement;
135 public Statement FalseStatement;
137 public If (Expression expr, Statement trueStatement, Location l)
140 TrueStatement = trueStatement;
144 public If (Expression expr,
145 Statement trueStatement,
146 Statement falseStatement,
150 TrueStatement = trueStatement;
151 FalseStatement = falseStatement;
155 public override bool Resolve (EmitContext ec)
157 Report.Debug (1, "START IF BLOCK", loc);
159 expr = ResolveBoolean (ec, expr, loc);
164 ec.StartFlowBranching (FlowBranchingType.BLOCK, loc);
166 if (!TrueStatement.Resolve (ec)) {
167 ec.KillFlowBranching ();
171 ec.CurrentBranching.CreateSibling ();
173 if ((FalseStatement != null) && !FalseStatement.Resolve (ec)) {
174 ec.KillFlowBranching ();
178 ec.EndFlowBranching ();
180 Report.Debug (1, "END IF BLOCK", loc);
185 protected override bool DoEmit (EmitContext ec)
187 ILGenerator ig = ec.ig;
188 Label false_target = ig.DefineLabel ();
190 bool is_true_ret, is_false_ret;
193 // Dead code elimination
195 if (expr is BoolConstant){
196 bool take = ((BoolConstant) expr).Value;
199 if (FalseStatement != null){
200 Warning_DeadCodeFound (FalseStatement.loc);
202 return TrueStatement.Emit (ec);
204 Warning_DeadCodeFound (TrueStatement.loc);
205 if (FalseStatement != null)
206 return FalseStatement.Emit (ec);
210 EmitBoolExpression (ec, expr, false_target, false);
212 is_true_ret = TrueStatement.Emit (ec);
213 is_false_ret = is_true_ret;
215 if (FalseStatement != null){
216 bool branch_emitted = false;
218 end = ig.DefineLabel ();
220 ig.Emit (OpCodes.Br, end);
221 branch_emitted = true;
224 ig.MarkLabel (false_target);
225 is_false_ret = FalseStatement.Emit (ec);
230 ig.MarkLabel (false_target);
231 is_false_ret = false;
234 return is_true_ret && is_false_ret;
238 public class Do : Statement {
239 public Expression expr;
240 public readonly Statement EmbeddedStatement;
241 bool infinite, may_return;
243 public Do (Statement statement, Expression boolExpr, Location l)
246 EmbeddedStatement = statement;
250 public override bool Resolve (EmitContext ec)
254 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
256 if (!EmbeddedStatement.Resolve (ec))
259 expr = ResolveBoolean (ec, expr, loc);
262 else if (expr is BoolConstant){
263 bool res = ((BoolConstant) expr).Value;
269 ec.CurrentBranching.Infinite = infinite;
270 FlowReturns returns = ec.EndFlowBranching ();
271 may_return = returns != FlowReturns.NEVER;
276 protected override bool DoEmit (EmitContext ec)
278 ILGenerator ig = ec.ig;
279 Label loop = ig.DefineLabel ();
280 Label old_begin = ec.LoopBegin;
281 Label old_end = ec.LoopEnd;
282 bool old_inloop = ec.InLoop;
283 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
285 ec.LoopBegin = ig.DefineLabel ();
286 ec.LoopEnd = ig.DefineLabel ();
288 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
291 EmbeddedStatement.Emit (ec);
292 ig.MarkLabel (ec.LoopBegin);
295 // Dead code elimination
297 if (expr is BoolConstant){
298 bool res = ((BoolConstant) expr).Value;
301 ec.ig.Emit (OpCodes.Br, loop);
303 EmitBoolExpression (ec, expr, loop, true);
305 ig.MarkLabel (ec.LoopEnd);
307 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
308 ec.LoopBegin = old_begin;
309 ec.LoopEnd = old_end;
310 ec.InLoop = old_inloop;
313 return may_return == false;
319 public class While : Statement {
320 public Expression expr;
321 public readonly Statement Statement;
322 bool may_return, empty, infinite;
324 public While (Expression boolExpr, Statement statement, Location l)
326 this.expr = boolExpr;
327 Statement = statement;
331 public override bool Resolve (EmitContext ec)
335 expr = ResolveBoolean (ec, expr, loc);
339 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
342 // Inform whether we are infinite or not
344 if (expr is BoolConstant){
345 BoolConstant bc = (BoolConstant) expr;
347 if (bc.Value == false){
348 Warning_DeadCodeFound (Statement.loc);
354 // We are not infinite, so the loop may or may not be executed.
356 ec.CurrentBranching.CreateSibling ();
359 if (!Statement.Resolve (ec))
363 ec.KillFlowBranching ();
365 ec.CurrentBranching.Infinite = infinite;
366 FlowReturns returns = ec.EndFlowBranching ();
367 may_return = returns != FlowReturns.NEVER;
373 protected override bool DoEmit (EmitContext ec)
378 ILGenerator ig = ec.ig;
379 Label old_begin = ec.LoopBegin;
380 Label old_end = ec.LoopEnd;
381 bool old_inloop = ec.InLoop;
382 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
385 ec.LoopBegin = ig.DefineLabel ();
386 ec.LoopEnd = ig.DefineLabel ();
388 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
391 // Inform whether we are infinite or not
393 if (expr is BoolConstant){
394 BoolConstant bc = (BoolConstant) expr;
396 ig.MarkLabel (ec.LoopBegin);
398 ig.Emit (OpCodes.Br, ec.LoopBegin);
401 // Inform that we are infinite (ie, `we return'), only
402 // if we do not `break' inside the code.
404 ret = may_return == false;
405 ig.MarkLabel (ec.LoopEnd);
407 Label while_loop = ig.DefineLabel ();
409 ig.Emit (OpCodes.Br, ec.LoopBegin);
410 ig.MarkLabel (while_loop);
414 ig.MarkLabel (ec.LoopBegin);
416 EmitBoolExpression (ec, expr, while_loop, true);
417 ig.MarkLabel (ec.LoopEnd);
422 ec.LoopBegin = old_begin;
423 ec.LoopEnd = old_end;
424 ec.InLoop = old_inloop;
425 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
431 public class For : Statement {
433 readonly Statement InitStatement;
434 readonly Statement Increment;
435 readonly Statement Statement;
436 bool may_return, infinite, empty;
438 public For (Statement initStatement,
444 InitStatement = initStatement;
446 Increment = increment;
447 Statement = statement;
451 public override bool Resolve (EmitContext ec)
455 if (InitStatement != null){
456 if (!InitStatement.Resolve (ec))
461 Test = ResolveBoolean (ec, Test, loc);
464 else if (Test is BoolConstant){
465 BoolConstant bc = (BoolConstant) Test;
467 if (bc.Value == false){
468 Warning_DeadCodeFound (Statement.loc);
476 if (Increment != null){
477 if (!Increment.Resolve (ec))
481 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
483 ec.CurrentBranching.CreateSibling ();
485 if (!Statement.Resolve (ec))
489 ec.KillFlowBranching ();
491 ec.CurrentBranching.Infinite = infinite;
492 FlowReturns returns = ec.EndFlowBranching ();
493 may_return = returns != FlowReturns.NEVER;
499 protected override bool DoEmit (EmitContext ec)
504 ILGenerator ig = ec.ig;
505 Label old_begin = ec.LoopBegin;
506 Label old_end = ec.LoopEnd;
507 bool old_inloop = ec.InLoop;
508 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
509 Label loop = ig.DefineLabel ();
510 Label test = ig.DefineLabel ();
512 if (InitStatement != null)
513 if (! (InitStatement is EmptyStatement))
514 InitStatement.Emit (ec);
516 ec.LoopBegin = ig.DefineLabel ();
517 ec.LoopEnd = ig.DefineLabel ();
519 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
521 ig.Emit (OpCodes.Br, test);
525 ig.MarkLabel (ec.LoopBegin);
526 if (!(Increment is EmptyStatement))
531 // If test is null, there is no test, and we are just
536 // The Resolve code already catches the case for Test == BoolConstant (false)
537 // so we know that this is true
539 if (Test is BoolConstant)
540 ig.Emit (OpCodes.Br, loop);
542 EmitBoolExpression (ec, Test, loop, true);
544 ig.Emit (OpCodes.Br, loop);
545 ig.MarkLabel (ec.LoopEnd);
547 ec.LoopBegin = old_begin;
548 ec.LoopEnd = old_end;
549 ec.InLoop = old_inloop;
550 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
553 // Inform whether we are infinite or not
556 if (Test is BoolConstant){
557 BoolConstant bc = (BoolConstant) Test;
560 return may_return == false;
564 return may_return == false;
568 public class StatementExpression : Statement {
571 public StatementExpression (ExpressionStatement expr, Location l)
577 public override bool Resolve (EmitContext ec)
579 expr = (Expression) expr.Resolve (ec);
583 protected override bool DoEmit (EmitContext ec)
585 ILGenerator ig = ec.ig;
587 if (expr is ExpressionStatement)
588 ((ExpressionStatement) expr).EmitStatement (ec);
591 ig.Emit (OpCodes.Pop);
597 public override string ToString ()
599 return "StatementExpression (" + expr + ")";
604 /// Implements the return statement
606 public class Return : Statement {
607 public Expression Expr;
609 public Return (Expression expr, Location l)
615 public override bool Resolve (EmitContext ec)
618 Expr = Expr.Resolve (ec);
623 FlowBranching.UsageVector vector = ec.CurrentBranching.CurrentUsageVector;
625 if (ec.CurrentBranching.InTryBlock ())
626 ec.CurrentBranching.AddFinallyVector (vector);
628 vector.CheckOutParameters (ec.CurrentBranching);
630 vector.Returns = FlowReturns.ALWAYS;
631 vector.Breaks = FlowReturns.ALWAYS;
635 protected override bool DoEmit (EmitContext ec)
638 Report.Error (157,loc,"Control can not leave the body of the finally block");
642 if (ec.ReturnType == null){
644 Report.Error (127, loc, "Return with a value not allowed here");
649 Report.Error (126, loc, "An object of type `" +
650 TypeManager.CSharpName (ec.ReturnType) + "' is " +
651 "expected for the return statement");
655 if (Expr.Type != ec.ReturnType)
656 Expr = Expression.ConvertImplicitRequired (
657 ec, Expr, ec.ReturnType, loc);
664 if (ec.InTry || ec.InCatch)
665 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
668 if (ec.InTry || ec.InCatch) {
669 if (!ec.HasReturnLabel) {
670 ec.ReturnLabel = ec.ig.DefineLabel ();
671 ec.HasReturnLabel = true;
673 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
675 ec.ig.Emit (OpCodes.Ret);
681 public class Goto : Statement {
684 LabeledStatement label;
686 public override bool Resolve (EmitContext ec)
688 label = block.LookupLabel (target);
692 "No such label `" + target + "' in this scope");
696 // If this is a forward goto.
697 if (!label.IsDefined)
698 label.AddUsageVector (ec.CurrentBranching.CurrentUsageVector);
700 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
701 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.ALWAYS;
706 public Goto (Block parent_block, string label, Location l)
708 block = parent_block;
713 public string Target {
719 protected override bool DoEmit (EmitContext ec)
721 Label l = label.LabelTarget (ec);
722 ec.ig.Emit (OpCodes.Br, l);
728 public class LabeledStatement : Statement {
729 public readonly Location Location;
737 public LabeledStatement (string label_name, Location l)
739 this.label_name = label_name;
743 public Label LabelTarget (EmitContext ec)
747 label = ec.ig.DefineLabel ();
753 public bool IsDefined {
759 public bool HasBeenReferenced {
765 public void AddUsageVector (FlowBranching.UsageVector vector)
768 vectors = new ArrayList ();
770 vectors.Add (vector.Clone ());
773 public override bool Resolve (EmitContext ec)
776 ec.CurrentBranching.CurrentUsageVector.MergeJumpOrigins (vectors);
778 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.NEVER;
779 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.NEVER;
787 protected override bool DoEmit (EmitContext ec)
790 ec.ig.MarkLabel (label);
798 /// `goto default' statement
800 public class GotoDefault : Statement {
802 public GotoDefault (Location l)
807 public override bool Resolve (EmitContext ec)
809 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
810 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.ALWAYS;
814 protected override bool DoEmit (EmitContext ec)
816 if (ec.Switch == null){
817 Report.Error (153, loc, "goto default is only valid in a switch statement");
821 if (!ec.Switch.GotDefault){
822 Report.Error (159, loc, "No default target on switch statement");
825 ec.ig.Emit (OpCodes.Br, ec.Switch.DefaultTarget);
831 /// `goto case' statement
833 public class GotoCase : Statement {
837 public GotoCase (Expression e, Location l)
843 public override bool Resolve (EmitContext ec)
845 if (ec.Switch == null){
846 Report.Error (153, loc, "goto case is only valid in a switch statement");
850 expr = expr.Resolve (ec);
854 if (!(expr is Constant)){
855 Report.Error (159, loc, "Target expression for goto case is not constant");
859 object val = Expression.ConvertIntLiteral (
860 (Constant) expr, ec.Switch.SwitchType, loc);
865 SwitchLabel sl = (SwitchLabel) ec.Switch.Elements [val];
870 "No such label 'case " + val + "': for the goto case");
873 label = sl.ILLabelCode;
875 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.UNREACHABLE;
876 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.ALWAYS;
880 protected override bool DoEmit (EmitContext ec)
882 ec.ig.Emit (OpCodes.Br, label);
887 public class Throw : Statement {
890 public Throw (Expression expr, Location l)
896 public override bool Resolve (EmitContext ec)
899 expr = expr.Resolve (ec);
903 ExprClass eclass = expr.eclass;
905 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
906 eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
907 expr.Error118 ("value, variable, property or indexer access ");
913 if ((t != TypeManager.exception_type) &&
914 !t.IsSubclassOf (TypeManager.exception_type) &&
915 !(expr is NullLiteral)) {
916 Report.Error (155, loc,
917 "The type caught or thrown must be derived " +
918 "from System.Exception");
923 ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.EXCEPTION;
924 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.EXCEPTION;
928 protected override bool DoEmit (EmitContext ec)
932 ec.ig.Emit (OpCodes.Rethrow);
936 "A throw statement with no argument is only " +
937 "allowed in a catch clause");
944 ec.ig.Emit (OpCodes.Throw);
950 public class Break : Statement {
952 public Break (Location l)
957 public override bool Resolve (EmitContext ec)
959 ec.CurrentBranching.MayLeaveLoop = true;
960 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
964 protected override bool DoEmit (EmitContext ec)
966 ILGenerator ig = ec.ig;
968 if (ec.InLoop == false && ec.Switch == null){
969 Report.Error (139, loc, "No enclosing loop or switch to continue to");
973 if (ec.InTry || ec.InCatch)
974 ig.Emit (OpCodes.Leave, ec.LoopEnd);
976 ig.Emit (OpCodes.Br, ec.LoopEnd);
982 public class Continue : Statement {
984 public Continue (Location l)
989 public override bool Resolve (EmitContext ec)
991 ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
995 protected override bool DoEmit (EmitContext ec)
997 Label begin = ec.LoopBegin;
1000 Report.Error (139, loc, "No enclosing loop to continue to");
1005 // UGH: Non trivial. This Br might cross a try/catch boundary
1009 // try { ... } catch { continue; }
1013 // try {} catch { while () { continue; }}
1015 if (ec.TryCatchLevel > ec.LoopBeginTryCatchLevel)
1016 ec.ig.Emit (OpCodes.Leave, begin);
1017 else if (ec.TryCatchLevel < ec.LoopBeginTryCatchLevel)
1018 throw new Exception ("Should never happen");
1020 ec.ig.Emit (OpCodes.Br, begin);
1026 // This is used in the control flow analysis code to specify whether the
1027 // current code block may return to its enclosing block before reaching
1030 public enum FlowReturns {
1031 // It can never return.
1034 // This means that the block contains a conditional return statement
1038 // The code always returns, ie. there's an unconditional return / break
1042 // The code always throws an exception.
1045 // The current code block is unreachable. This happens if it's immediately
1046 // following a FlowReturns.ALWAYS block.
1051 // This is a special bit vector which can inherit from another bit vector doing a
1052 // copy-on-write strategy. The inherited vector may have a smaller size than the
1055 public class MyBitVector {
1056 public readonly int Count;
1057 public readonly MyBitVector InheritsFrom;
1062 public MyBitVector (int Count)
1063 : this (null, Count)
1066 public MyBitVector (MyBitVector InheritsFrom, int Count)
1068 this.InheritsFrom = InheritsFrom;
1073 // Checks whether this bit vector has been modified. After setting this to true,
1074 // we won't use the inherited vector anymore, but our own copy of it.
1076 public bool IsDirty {
1083 initialize_vector ();
1088 // Get/set bit `index' in the bit vector.
1090 public bool this [int index]
1094 throw new ArgumentOutOfRangeException ();
1096 // We're doing a "copy-on-write" strategy here; as long
1097 // as nobody writes to the array, we can use our parent's
1098 // copy instead of duplicating the vector.
1101 return vector [index];
1102 else if (InheritsFrom != null) {
1103 BitArray inherited = InheritsFrom.Vector;
1105 if (index < inherited.Count)
1106 return inherited [index];
1115 throw new ArgumentOutOfRangeException ();
1117 // Only copy the vector if we're actually modifying it.
1119 if (this [index] != value) {
1120 initialize_vector ();
1122 vector [index] = value;
1128 // If you explicitly convert the MyBitVector to a BitArray, you will get a deep
1129 // copy of the bit vector.
1131 public static explicit operator BitArray (MyBitVector vector)
1133 vector.initialize_vector ();
1134 return vector.Vector;
1138 // Performs an `or' operation on the bit vector. The `new_vector' may have a
1139 // different size than the current one.
1141 public void Or (MyBitVector new_vector)
1143 BitArray new_array = new_vector.Vector;
1145 initialize_vector ();
1148 if (vector.Count < new_array.Count)
1149 upper = vector.Count;
1151 upper = new_array.Count;
1153 for (int i = 0; i < upper; i++)
1154 vector [i] = vector [i] | new_array [i];
1158 // Perfonrms an `and' operation on the bit vector. The `new_vector' may have
1159 // a different size than the current one.
1161 public void And (MyBitVector new_vector)
1163 BitArray new_array = new_vector.Vector;
1165 initialize_vector ();
1168 if (vector.Count < new_array.Count)
1169 lower = upper = vector.Count;
1171 lower = new_array.Count;
1172 upper = vector.Count;
1175 for (int i = 0; i < lower; i++)
1176 vector [i] = vector [i] & new_array [i];
1178 for (int i = lower; i < upper; i++)
1183 // This does a deep copy of the bit vector.
1185 public MyBitVector Clone ()
1187 MyBitVector retval = new MyBitVector (Count);
1189 retval.Vector = Vector;
1198 else if (!is_dirty && (InheritsFrom != null))
1199 return InheritsFrom.Vector;
1201 initialize_vector ();
1207 initialize_vector ();
1209 for (int i = 0; i < Math.Min (vector.Count, value.Count); i++)
1210 vector [i] = value [i];
1214 void initialize_vector ()
1219 vector = new BitArray (Count, false);
1220 if (InheritsFrom != null)
1221 Vector = InheritsFrom.Vector;
1226 public override string ToString ()
1228 StringBuilder sb = new StringBuilder ("MyBitVector (");
1230 BitArray vector = Vector;
1234 sb.Append ("INHERITED - ");
1235 for (int i = 0; i < vector.Count; i++) {
1238 sb.Append (vector [i]);
1242 return sb.ToString ();
1247 // The type of a FlowBranching.
1249 public enum FlowBranchingType {
1250 // Normal (conditional or toplevel) block.
1267 // A new instance of this class is created every time a new block is resolved
1268 // and if there's branching in the block's control flow.
1270 public class FlowBranching {
1272 // The type of this flow branching.
1274 public readonly FlowBranchingType Type;
1277 // The block this branching is contained in. This may be null if it's not
1278 // a top-level block and it doesn't declare any local variables.
1280 public readonly Block Block;
1283 // The parent of this branching or null if this is the top-block.
1285 public readonly FlowBranching Parent;
1288 // Start-Location of this flow branching.
1290 public readonly Location Location;
1293 // A list of UsageVectors. A new vector is added each time control flow may
1294 // take a different path.
1296 public UsageVector[] Siblings;
1299 // If this is an infinite loop.
1301 public bool Infinite;
1304 // If we may leave the current loop.
1306 public bool MayLeaveLoop;
1311 InternalParameters param_info;
1313 MyStructInfo[] struct_params;
1315 ArrayList finally_vectors;
1317 static int next_id = 0;
1321 // Performs an `And' operation on the FlowReturns status
1322 // (for instance, a block only returns ALWAYS if all its siblings
1325 public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
1327 if (b == FlowReturns.UNREACHABLE)
1331 case FlowReturns.NEVER:
1332 if (b == FlowReturns.NEVER)
1333 return FlowReturns.NEVER;
1335 return FlowReturns.SOMETIMES;
1337 case FlowReturns.SOMETIMES:
1338 return FlowReturns.SOMETIMES;
1340 case FlowReturns.ALWAYS:
1341 if ((b == FlowReturns.ALWAYS) || (b == FlowReturns.EXCEPTION))
1342 return FlowReturns.ALWAYS;
1344 return FlowReturns.SOMETIMES;
1346 case FlowReturns.EXCEPTION:
1347 if (b == FlowReturns.EXCEPTION)
1348 return FlowReturns.EXCEPTION;
1349 else if (b == FlowReturns.ALWAYS)
1350 return FlowReturns.ALWAYS;
1352 return FlowReturns.SOMETIMES;
1359 // The vector contains a BitArray with information about which local variables
1360 // and parameters are already initialized at the current code position.
1362 public class UsageVector {
1364 // If this is true, then the usage vector has been modified and must be
1365 // merged when we're done with this branching.
1367 public bool IsDirty;
1370 // The number of parameters in this block.
1372 public readonly int CountParameters;
1375 // The number of locals in this block.
1377 public readonly int CountLocals;
1380 // If not null, then we inherit our state from this vector and do a
1381 // copy-on-write. If null, then we're the first sibling in a top-level
1382 // block and inherit from the empty vector.
1384 public readonly UsageVector InheritsFrom;
1389 MyBitVector locals, parameters;
1390 FlowReturns real_returns, real_breaks;
1393 static int next_id = 0;
1397 // Normally, you should not use any of these constructors.
1399 public UsageVector (UsageVector parent, int num_params, int num_locals)
1401 this.InheritsFrom = parent;
1402 this.CountParameters = num_params;
1403 this.CountLocals = num_locals;
1404 this.real_returns = FlowReturns.NEVER;
1405 this.real_breaks = FlowReturns.NEVER;
1407 if (parent != null) {
1408 locals = new MyBitVector (parent.locals, CountLocals);
1410 parameters = new MyBitVector (parent.parameters, num_params);
1411 real_returns = parent.Returns;
1412 real_breaks = parent.Breaks;
1414 locals = new MyBitVector (null, CountLocals);
1416 parameters = new MyBitVector (null, num_params);
1422 public UsageVector (UsageVector parent)
1423 : this (parent, parent.CountParameters, parent.CountLocals)
1427 // This does a deep copy of the usage vector.
1429 public UsageVector Clone ()
1431 UsageVector retval = new UsageVector (null, CountParameters, CountLocals);
1433 retval.locals = locals.Clone ();
1434 if (parameters != null)
1435 retval.parameters = parameters.Clone ();
1436 retval.real_returns = real_returns;
1437 retval.real_breaks = real_breaks;
1443 // State of parameter `number'.
1445 public bool this [int number]
1450 else if (number == 0)
1451 throw new ArgumentException ();
1453 return parameters [number - 1];
1459 else if (number == 0)
1460 throw new ArgumentException ();
1462 parameters [number - 1] = value;
1467 // State of the local variable `vi'.
1468 // If the local variable is a struct, use a non-zero `field_idx'
1469 // to check an individual field in it.
1471 public bool this [VariableInfo vi, int field_idx]
1474 if (vi.Number == -1)
1476 else if (vi.Number == 0)
1477 throw new ArgumentException ();
1479 return locals [vi.Number + field_idx - 1];
1483 if (vi.Number == -1)
1485 else if (vi.Number == 0)
1486 throw new ArgumentException ();
1488 locals [vi.Number + field_idx - 1] = value;
1493 // Specifies when the current block returns.
1494 // If this is FlowReturns.UNREACHABLE, then control can never reach the
1495 // end of the method (so that we don't need to emit a return statement).
1496 // The same applies for FlowReturns.EXCEPTION, but in this case the return
1497 // value will never be used.
1499 public FlowReturns Returns {
1501 return real_returns;
1505 real_returns = value;
1510 // Specifies whether control may return to our containing block
1511 // before reaching the end of this block. This happens if there
1512 // is a break/continue/goto/return in it.
1513 // This can also be used to find out whether the statement immediately
1514 // following the current block may be reached or not.
1516 public FlowReturns Breaks {
1522 real_breaks = value;
1526 public bool AlwaysBreaks {
1528 return (Breaks == FlowReturns.ALWAYS) ||
1529 (Breaks == FlowReturns.EXCEPTION) ||
1530 (Breaks == FlowReturns.UNREACHABLE);
1534 public bool MayBreak {
1536 return Breaks != FlowReturns.NEVER;
1540 public bool AlwaysReturns {
1542 return (Returns == FlowReturns.ALWAYS) ||
1543 (Returns == FlowReturns.EXCEPTION);
1547 public bool MayReturn {
1549 return (Returns == FlowReturns.SOMETIMES) ||
1550 (Returns == FlowReturns.ALWAYS);
1555 // Merge a child branching.
1557 public FlowReturns MergeChildren (FlowBranching branching, UsageVector[] children)
1559 MyBitVector new_locals = null;
1560 MyBitVector new_params = null;
1562 FlowReturns new_returns = FlowReturns.NEVER;
1563 FlowReturns new_breaks = FlowReturns.NEVER;
1564 bool new_returns_set = false, new_breaks_set = false;
1566 Report.Debug (2, "MERGING CHILDREN", branching, branching.Type,
1567 this, children.Length);
1569 foreach (UsageVector child in children) {
1570 Report.Debug (2, " MERGING CHILD", child, child.is_finally);
1572 if (!child.is_finally) {
1573 if (child.Breaks != FlowReturns.UNREACHABLE) {
1574 // If Returns is already set, perform an
1575 // `And' operation on it, otherwise just set just.
1576 if (!new_returns_set) {
1577 new_returns = child.Returns;
1578 new_returns_set = true;
1580 new_returns = AndFlowReturns (
1581 new_returns, child.Returns);
1584 // If Breaks is already set, perform an
1585 // `And' operation on it, otherwise just set just.
1586 if (!new_breaks_set) {
1587 new_breaks = child.Breaks;
1588 new_breaks_set = true;
1590 new_breaks = AndFlowReturns (
1591 new_breaks, child.Breaks);
1594 // Ignore unreachable children.
1595 if (child.Returns == FlowReturns.UNREACHABLE)
1598 // A local variable is initialized after a flow branching if it
1599 // has been initialized in all its branches which do neither
1600 // always return or always throw an exception.
1602 // If a branch may return, but does not always return, then we
1603 // can treat it like a never-returning branch here: control will
1604 // only reach the code position after the branching if we did not
1607 // It's important to distinguish between always and sometimes
1608 // returning branches here:
1611 // 2 if (something) {
1615 // 6 Console.WriteLine (a);
1617 // The if block in lines 3-4 always returns, so we must not look
1618 // at the initialization of `a' in line 4 - thus it'll still be
1619 // uninitialized in line 6.
1621 // On the other hand, the following is allowed:
1628 // 6 Console.WriteLine (a);
1630 // Here, `a' is initialized in line 3 and we must not look at
1631 // line 5 since it always returns.
1633 if (child.is_finally) {
1634 if (new_locals == null)
1635 new_locals = locals.Clone ();
1636 new_locals.Or (child.locals);
1638 if (parameters != null) {
1639 if (new_params == null)
1640 new_params = parameters.Clone ();
1641 new_params.Or (child.parameters);
1645 if (!child.AlwaysReturns && !child.AlwaysBreaks) {
1646 if (new_locals != null)
1647 new_locals.And (child.locals);
1649 new_locals = locals.Clone ();
1650 new_locals.Or (child.locals);
1652 } else if (children.Length == 1) {
1653 new_locals = locals.Clone ();
1654 new_locals.Or (child.locals);
1657 // An `out' parameter must be assigned in all branches which do
1658 // not always throw an exception.
1659 if (parameters != null) {
1660 if (child.Breaks != FlowReturns.EXCEPTION) {
1661 if (new_params != null)
1662 new_params.And (child.parameters);
1664 new_params = parameters.Clone ();
1665 new_params.Or (child.parameters);
1667 } else if (children.Length == 1) {
1668 new_params = parameters.Clone ();
1669 new_params.Or (child.parameters);
1675 Returns = new_returns;
1676 if ((branching.Type == FlowBranchingType.BLOCK) ||
1677 (branching.Type == FlowBranchingType.EXCEPTION) ||
1678 (new_breaks == FlowReturns.UNREACHABLE) ||
1679 (new_breaks == FlowReturns.EXCEPTION))
1680 Breaks = new_breaks;
1681 else if (branching.Type == FlowBranchingType.SWITCH_SECTION)
1682 Breaks = new_returns;
1683 else if (branching.Type == FlowBranchingType.SWITCH){
1684 if (new_breaks == FlowReturns.ALWAYS)
1685 Breaks = FlowReturns.ALWAYS;
1689 // We've now either reached the point after the branching or we will
1690 // never get there since we always return or always throw an exception.
1692 // If we can reach the point after the branching, mark all locals and
1693 // parameters as initialized which have been initialized in all branches
1694 // we need to look at (see above).
1697 if (((new_breaks != FlowReturns.ALWAYS) &&
1698 (new_breaks != FlowReturns.EXCEPTION) &&
1699 (new_breaks != FlowReturns.UNREACHABLE)) ||
1700 (children.Length == 1)) {
1701 if (new_locals != null)
1702 locals.Or (new_locals);
1704 if (new_params != null)
1705 parameters.Or (new_params);
1708 Report.Debug (2, "MERGING CHILDREN DONE", branching.Type,
1709 new_params, new_locals, new_returns, new_breaks,
1710 branching.Infinite, branching.MayLeaveLoop, this);
1712 if (branching.Type == FlowBranchingType.SWITCH_SECTION) {
1713 if ((new_breaks != FlowReturns.ALWAYS) &&
1714 (new_breaks != FlowReturns.EXCEPTION) &&
1715 (new_breaks != FlowReturns.UNREACHABLE))
1716 Report.Error (163, branching.Location,
1717 "Control cannot fall through from one " +
1718 "case label to another");
1721 if (branching.Infinite && !branching.MayLeaveLoop) {
1722 Report.Debug (1, "INFINITE", new_returns, new_breaks,
1723 Returns, Breaks, this);
1725 // We're actually infinite.
1726 if (new_returns == FlowReturns.NEVER) {
1727 Breaks = FlowReturns.UNREACHABLE;
1728 return FlowReturns.UNREACHABLE;
1731 // If we're an infinite loop and do not break, the code after
1732 // the loop can never be reached. However, if we may return
1733 // from the loop, then we do always return (or stay in the loop
1735 if ((new_returns == FlowReturns.SOMETIMES) ||
1736 (new_returns == FlowReturns.ALWAYS)) {
1737 Returns = FlowReturns.ALWAYS;
1738 return FlowReturns.ALWAYS;
1746 // Tells control flow analysis that the current code position may be reached with
1747 // a forward jump from any of the origins listed in `origin_vectors' which is a
1748 // list of UsageVectors.
1750 // This is used when resolving forward gotos - in the following example, the
1751 // variable `a' is uninitialized in line 8 becase this line may be reached via
1752 // the goto in line 4:
1762 // 8 Console.WriteLine (a);
1765 public void MergeJumpOrigins (ICollection origin_vectors)
1767 Report.Debug (1, "MERGING JUMP ORIGIN", this);
1769 real_breaks = FlowReturns.NEVER;
1770 real_returns = FlowReturns.NEVER;
1772 foreach (UsageVector vector in origin_vectors) {
1773 Report.Debug (1, " MERGING JUMP ORIGIN", vector);
1775 locals.And (vector.locals);
1776 if (parameters != null)
1777 parameters.And (vector.parameters);
1778 Breaks = AndFlowReturns (Breaks, vector.Breaks);
1779 Returns = AndFlowReturns (Returns, vector.Returns);
1782 Report.Debug (1, "MERGING JUMP ORIGIN DONE", this);
1786 // This is used at the beginning of a finally block if there were
1787 // any return statements in the try block or one of the catch blocks.
1789 public void MergeFinallyOrigins (ICollection finally_vectors)
1791 Report.Debug (1, "MERGING FINALLY ORIGIN", this);
1793 real_breaks = FlowReturns.NEVER;
1795 foreach (UsageVector vector in finally_vectors) {
1796 Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
1798 if (parameters != null)
1799 parameters.And (vector.parameters);
1800 Breaks = AndFlowReturns (Breaks, vector.Breaks);
1805 Report.Debug (1, "MERGING FINALLY ORIGIN DONE", this);
1808 public void CheckOutParameters (FlowBranching branching)
1810 if (parameters != null)
1811 branching.CheckOutParameters (parameters, branching.Location);
1815 // Performs an `or' operation on the locals and the parameters.
1817 public void Or (UsageVector new_vector)
1819 locals.Or (new_vector.locals);
1820 if (parameters != null)
1821 parameters.Or (new_vector.parameters);
1825 // Performs an `and' operation on the locals.
1827 public void AndLocals (UsageVector new_vector)
1829 locals.And (new_vector.locals);
1833 // Returns a deep copy of the parameters.
1835 public MyBitVector Parameters {
1837 if (parameters != null)
1838 return parameters.Clone ();
1845 // Returns a deep copy of the locals.
1847 public MyBitVector Locals {
1849 return locals.Clone ();
1857 public override string ToString ()
1859 StringBuilder sb = new StringBuilder ();
1861 sb.Append ("Vector (");
1864 sb.Append (Returns);
1867 if (parameters != null) {
1869 sb.Append (parameters);
1875 return sb.ToString ();
1879 FlowBranching (FlowBranchingType type, Location loc)
1882 this.Location = loc;
1888 // Creates a new flow branching for `block'.
1889 // This is used from Block.Resolve to create the top-level branching of
1892 public FlowBranching (Block block, InternalParameters ip, Location loc)
1893 : this (FlowBranchingType.BLOCK, loc)
1898 int count = (ip != null) ? ip.Count : 0;
1901 param_map = new int [count];
1902 struct_params = new MyStructInfo [count];
1905 for (int i = 0; i < count; i++) {
1906 Parameter.Modifier mod = param_info.ParameterModifier (i);
1908 if ((mod & Parameter.Modifier.OUT) == 0)
1911 param_map [i] = ++num_params;
1913 Type param_type = param_info.ParameterType (i);
1915 struct_params [i] = MyStructInfo.GetStructInfo (param_type);
1916 if (struct_params [i] != null)
1917 num_params += struct_params [i].Count;
1920 AddSibling (new UsageVector (null, num_params, block.CountVariables));
1924 // Creates a new flow branching which is contained in `parent'.
1925 // You should only pass non-null for the `block' argument if this block
1926 // introduces any new variables - in this case, we need to create a new
1927 // usage vector with a different size than our parent's one.
1929 public FlowBranching (FlowBranching parent, FlowBranchingType type,
1930 Block block, Location loc)
1936 if (parent != null) {
1937 param_info = parent.param_info;
1938 param_map = parent.param_map;
1939 struct_params = parent.struct_params;
1940 num_params = parent.num_params;
1945 vector = new UsageVector (parent.CurrentUsageVector, num_params,
1946 Block.CountVariables);
1948 vector = new UsageVector (Parent.CurrentUsageVector);
1950 AddSibling (vector);
1953 case FlowBranchingType.EXCEPTION:
1954 finally_vectors = new ArrayList ();
1962 void AddSibling (UsageVector uv)
1964 if (Siblings != null) {
1965 UsageVector[] ns = new UsageVector [Siblings.Length + 1];
1966 for (int i = 0; i < Siblings.Length; ++i)
1967 ns [i] = Siblings [i];
1970 Siblings = new UsageVector [1];
1972 Siblings [Siblings.Length - 1] = uv;
1976 // Returns the branching's current usage vector.
1978 public UsageVector CurrentUsageVector
1981 return Siblings [Siblings.Length - 1];
1986 // Creates a sibling of the current usage vector.
1988 public void CreateSibling ()
1990 AddSibling (new UsageVector (Parent.CurrentUsageVector));
1992 Report.Debug (1, "CREATED SIBLING", CurrentUsageVector);
1996 // Creates a sibling for a `finally' block.
1998 public void CreateSiblingForFinally ()
2000 if (Type != FlowBranchingType.EXCEPTION)
2001 throw new NotSupportedException ();
2005 CurrentUsageVector.MergeFinallyOrigins (finally_vectors);
2009 // Check whether all `out' parameters have been assigned.
2011 public void CheckOutParameters (MyBitVector parameters, Location loc)
2016 for (int i = 0; i < param_map.Length; i++) {
2017 int index = param_map [i];
2022 if (parameters [index - 1])
2025 // If it's a struct, we must ensure that all its fields have
2026 // been assigned. If the struct has any non-public fields, this
2027 // can only be done by assigning the whole struct.
2029 MyStructInfo struct_info = struct_params [i];
2030 if ((struct_info == null) || struct_info.HasNonPublicFields) {
2032 177, loc, "The out parameter `" +
2033 param_info.ParameterName (i) + "' must be " +
2034 "assigned before control leave the current method.");
2040 for (int j = 0; j < struct_info.Count; j++) {
2041 if (!parameters [index + j]) {
2043 177, loc, "The out parameter `" +
2044 param_info.ParameterName (i) + "' must be " +
2045 "assigned before control leaves the current method.");
2054 // Merge a child branching.
2056 public FlowReturns MergeChild (FlowBranching child)
2058 FlowReturns returns = CurrentUsageVector.MergeChildren (child, child.Siblings);
2060 if ((child.Type != FlowBranchingType.LOOP_BLOCK) &&
2061 (child.Type != FlowBranchingType.SWITCH_SECTION))
2062 MayLeaveLoop |= child.MayLeaveLoop;
2064 MayLeaveLoop = false;
2070 // Does the toplevel merging.
2072 public FlowReturns MergeTopBlock ()
2074 if ((Type != FlowBranchingType.BLOCK) || (Block == null))
2075 throw new NotSupportedException ();
2077 UsageVector vector = new UsageVector (null, num_params, Block.CountVariables);
2079 Report.Debug (1, "MERGING TOP BLOCK", Location, vector);
2081 vector.MergeChildren (this, Siblings);
2083 if (Siblings.Length == 1)
2084 Siblings [0] = vector;
2087 AddSibling (vector);
2090 Report.Debug (1, "MERGING TOP BLOCK DONE", Location, vector);
2092 if (vector.Breaks != FlowReturns.EXCEPTION) {
2093 if (!vector.AlwaysBreaks)
2094 CheckOutParameters (CurrentUsageVector.Parameters, Location);
2095 return vector.AlwaysBreaks ? FlowReturns.ALWAYS : vector.Returns;
2097 return FlowReturns.EXCEPTION;
2100 public bool InTryBlock ()
2102 if (finally_vectors != null)
2104 else if (Parent != null)
2105 return Parent.InTryBlock ();
2110 public void AddFinallyVector (UsageVector vector)
2112 if (finally_vectors != null) {
2113 finally_vectors.Add (vector.Clone ());
2118 Parent.AddFinallyVector (vector);
2120 throw new NotSupportedException ();
2123 public bool IsVariableAssigned (VariableInfo vi)
2125 if (CurrentUsageVector.AlwaysBreaks)
2128 return CurrentUsageVector [vi, 0];
2131 public bool IsVariableAssigned (VariableInfo vi, int field_idx)
2133 if (CurrentUsageVector.AlwaysBreaks)
2136 return CurrentUsageVector [vi, field_idx];
2139 public void SetVariableAssigned (VariableInfo vi)
2141 if (CurrentUsageVector.AlwaysBreaks)
2144 CurrentUsageVector [vi, 0] = true;
2147 public void SetVariableAssigned (VariableInfo vi, int field_idx)
2149 if (CurrentUsageVector.AlwaysBreaks)
2152 CurrentUsageVector [vi, field_idx] = true;
2155 public bool IsParameterAssigned (int number)
2157 int index = param_map [number];
2162 if (CurrentUsageVector [index])
2165 // Parameter is not assigned, so check whether it's a struct.
2166 // If it's either not a struct or a struct which non-public
2167 // fields, return false.
2168 MyStructInfo struct_info = struct_params [number];
2169 if ((struct_info == null) || struct_info.HasNonPublicFields)
2172 // Ok, so each field must be assigned.
2173 for (int i = 0; i < struct_info.Count; i++)
2174 if (!CurrentUsageVector [index + i])
2180 public bool IsParameterAssigned (int number, string field_name)
2182 int index = param_map [number];
2187 MyStructInfo info = (MyStructInfo) struct_params [number];
2191 int field_idx = info [field_name];
2193 return CurrentUsageVector [index + field_idx];
2196 public void SetParameterAssigned (int number)
2198 if (param_map [number] == 0)
2201 if (!CurrentUsageVector.AlwaysBreaks)
2202 CurrentUsageVector [param_map [number]] = true;
2205 public void SetParameterAssigned (int number, string field_name)
2207 int index = param_map [number];
2212 MyStructInfo info = (MyStructInfo) struct_params [number];
2216 int field_idx = info [field_name];
2218 if (!CurrentUsageVector.AlwaysBreaks)
2219 CurrentUsageVector [index + field_idx] = true;
2222 public bool IsReachable ()
2227 case FlowBranchingType.SWITCH_SECTION:
2228 // The code following a switch block is reachable unless the switch
2229 // block always returns.
2230 reachable = !CurrentUsageVector.AlwaysReturns;
2233 case FlowBranchingType.LOOP_BLOCK:
2234 // The code following a loop is reachable unless the loop always
2235 // returns or it's an infinite loop without any `break's in it.
2236 reachable = !CurrentUsageVector.AlwaysReturns &&
2237 (CurrentUsageVector.Breaks != FlowReturns.UNREACHABLE);
2241 // The code following a block or exception is reachable unless the
2242 // block either always returns or always breaks.
2243 reachable = !CurrentUsageVector.AlwaysBreaks &&
2244 !CurrentUsageVector.AlwaysReturns;
2248 Report.Debug (1, "REACHABLE", Type, CurrentUsageVector.Returns,
2249 CurrentUsageVector.Breaks, CurrentUsageVector, reachable);
2254 public override string ToString ()
2256 StringBuilder sb = new StringBuilder ("FlowBranching (");
2261 if (Block != null) {
2263 sb.Append (Block.ID);
2265 sb.Append (Block.StartLocation);
2268 sb.Append (Siblings.Length);
2270 sb.Append (CurrentUsageVector);
2272 return sb.ToString ();
2276 public class MyStructInfo {
2277 public readonly Type Type;
2278 public readonly FieldInfo[] Fields;
2279 public readonly FieldInfo[] NonPublicFields;
2280 public readonly int Count;
2281 public readonly int CountNonPublic;
2282 public readonly bool HasNonPublicFields;
2284 private static Hashtable field_type_hash = new Hashtable ();
2285 private Hashtable field_hash;
2287 // Private constructor. To save memory usage, we only need to create one instance
2288 // of this class per struct type.
2289 private MyStructInfo (Type type)
2293 if (type is TypeBuilder) {
2294 TypeContainer tc = TypeManager.LookupTypeContainer (type);
2296 ArrayList fields = tc.Fields;
2297 if (fields != null) {
2298 foreach (Field field in fields) {
2299 if ((field.ModFlags & Modifiers.STATIC) != 0)
2301 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
2308 Fields = new FieldInfo [Count];
2309 NonPublicFields = new FieldInfo [CountNonPublic];
2311 Count = CountNonPublic = 0;
2312 if (fields != null) {
2313 foreach (Field field in fields) {
2314 if ((field.ModFlags & Modifiers.STATIC) != 0)
2316 if ((field.ModFlags & Modifiers.PUBLIC) != 0)
2317 Fields [Count++] = field.FieldBuilder;
2319 NonPublicFields [CountNonPublic++] =
2325 Fields = type.GetFields (BindingFlags.Instance|BindingFlags.Public);
2326 Count = Fields.Length;
2328 NonPublicFields = type.GetFields (BindingFlags.Instance|BindingFlags.NonPublic);
2329 CountNonPublic = NonPublicFields.Length;
2332 Count += NonPublicFields.Length;
2335 field_hash = new Hashtable ();
2336 foreach (FieldInfo field in Fields)
2337 field_hash.Add (field.Name, ++number);
2339 if (NonPublicFields.Length != 0)
2340 HasNonPublicFields = true;
2342 foreach (FieldInfo field in NonPublicFields)
2343 field_hash.Add (field.Name, ++number);
2346 public int this [string name] {
2348 if (field_hash.Contains (name))
2349 return (int) field_hash [name];
2355 public FieldInfo this [int index] {
2357 if (index >= Fields.Length)
2358 return NonPublicFields [index - Fields.Length];
2360 return Fields [index];
2364 public static MyStructInfo GetStructInfo (Type type)
2366 if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type))
2369 if (!(type is TypeBuilder) && TypeManager.IsBuiltinType (type))
2372 MyStructInfo info = (MyStructInfo) field_type_hash [type];
2376 info = new MyStructInfo (type);
2377 field_type_hash.Add (type, info);
2381 public static MyStructInfo GetStructInfo (TypeContainer tc)
2383 MyStructInfo info = (MyStructInfo) field_type_hash [tc.TypeBuilder];
2387 info = new MyStructInfo (tc.TypeBuilder);
2388 field_type_hash.Add (tc.TypeBuilder, info);
2393 public class VariableInfo : IVariable {
2394 public Expression Type;
2395 public LocalBuilder LocalBuilder;
2396 public Type VariableType;
2397 public readonly string Name;
2398 public readonly Location Location;
2399 public readonly int Block;
2404 public bool Assigned;
2405 public bool ReadOnly;
2407 public VariableInfo (Expression type, string name, int block, Location l)
2412 LocalBuilder = null;
2416 public VariableInfo (TypeContainer tc, int block, Location l)
2418 VariableType = tc.TypeBuilder;
2419 struct_info = MyStructInfo.GetStructInfo (tc);
2421 LocalBuilder = null;
2425 MyStructInfo struct_info;
2426 public MyStructInfo StructInfo {
2432 public bool IsAssigned (EmitContext ec, Location loc)
2434 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsVariableAssigned (this))
2437 MyStructInfo struct_info = StructInfo;
2438 if ((struct_info == null) || (struct_info.HasNonPublicFields && (Name != null))) {
2439 Report.Error (165, loc, "Use of unassigned local variable `" + Name + "'");
2440 ec.CurrentBranching.SetVariableAssigned (this);
2444 int count = struct_info.Count;
2446 for (int i = 0; i < count; i++) {
2447 if (!ec.CurrentBranching.IsVariableAssigned (this, i+1)) {
2449 Report.Error (165, loc,
2450 "Use of unassigned local variable `" +
2452 ec.CurrentBranching.SetVariableAssigned (this);
2456 FieldInfo field = struct_info [i];
2457 Report.Error (171, loc,
2458 "Field `" + TypeManager.CSharpName (VariableType) +
2459 "." + field.Name + "' must be fully initialized " +
2460 "before control leaves the constructor");
2468 public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
2470 if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsVariableAssigned (this) ||
2471 (struct_info == null))
2474 int field_idx = StructInfo [name];
2478 if (!ec.CurrentBranching.IsVariableAssigned (this, field_idx)) {
2479 Report.Error (170, loc,
2480 "Use of possibly unassigned field `" + name + "'");
2481 ec.CurrentBranching.SetVariableAssigned (this, field_idx);
2488 public void SetAssigned (EmitContext ec)
2490 if (ec.DoFlowAnalysis)
2491 ec.CurrentBranching.SetVariableAssigned (this);
2494 public void SetFieldAssigned (EmitContext ec, string name)
2496 if (ec.DoFlowAnalysis && (struct_info != null))
2497 ec.CurrentBranching.SetVariableAssigned (this, StructInfo [name]);
2500 public bool Resolve (DeclSpace decl)
2502 if (struct_info != null)
2505 if (VariableType == null)
2506 VariableType = decl.ResolveType (Type, false, Location);
2508 if (VariableType == null)
2511 struct_info = MyStructInfo.GetStructInfo (VariableType);
2516 public void MakePinned ()
2518 TypeManager.MakePinned (LocalBuilder);
2521 public override string ToString ()
2523 return "VariableInfo (" + Number + "," + Type + "," + Location + ")";
2528 /// Block represents a C# block.
2532 /// This class is used in a number of places: either to represent
2533 /// explicit blocks that the programmer places or implicit blocks.
2535 /// Implicit blocks are used as labels or to introduce variable
2538 public class Block : Statement {
2539 public readonly Block Parent;
2540 public readonly bool Implicit;
2541 public readonly Location StartLocation;
2542 public Location EndLocation = Location.Null;
2545 // The statements in this block
2547 ArrayList statements;
2550 // An array of Blocks. We keep track of children just
2551 // to generate the local variable declarations.
2553 // Statements and child statements are handled through the
2559 // Labels. (label, block) pairs.
2564 // Keeps track of (name, type) pairs
2566 Hashtable variables;
2569 // Keeps track of constants
2570 Hashtable constants;
2573 // Maps variable names to ILGenerator.LocalBuilders
2575 Hashtable local_builders;
2583 public Block (Block parent)
2584 : this (parent, false, Location.Null, Location.Null)
2587 public Block (Block parent, bool implicit_block)
2588 : this (parent, implicit_block, Location.Null, Location.Null)
2591 public Block (Block parent, bool implicit_block, Parameters parameters)
2592 : this (parent, implicit_block, parameters, Location.Null, Location.Null)
2595 public Block (Block parent, Location start, Location end)
2596 : this (parent, false, start, end)
2599 public Block (Block parent, Parameters parameters, Location start, Location end)
2600 : this (parent, false, parameters, start, end)
2603 public Block (Block parent, bool implicit_block, Location start, Location end)
2604 : this (parent, implicit_block, Parameters.EmptyReadOnlyParameters,
2608 public Block (Block parent, bool implicit_block, Parameters parameters,
2609 Location start, Location end)
2612 parent.AddChild (this);
2614 this.Parent = parent;
2615 this.Implicit = implicit_block;
2616 this.parameters = parameters;
2617 this.StartLocation = start;
2618 this.EndLocation = end;
2621 statements = new ArrayList ();
2630 void AddChild (Block b)
2632 if (children == null)
2633 children = new ArrayList ();
2638 public void SetEndLocation (Location loc)
2644 /// Adds a label to the current block.
2648 /// false if the name already exists in this block. true
2652 public bool AddLabel (string name, LabeledStatement target)
2655 labels = new Hashtable ();
2656 if (labels.Contains (name))
2659 labels.Add (name, target);
2663 public LabeledStatement LookupLabel (string name)
2665 if (labels != null){
2666 if (labels.Contains (name))
2667 return ((LabeledStatement) labels [name]);
2671 return Parent.LookupLabel (name);
2676 VariableInfo this_variable = null;
2679 // Returns the "this" instance variable of this block.
2680 // See AddThisVariable() for more information.
2682 public VariableInfo ThisVariable {
2684 if (this_variable != null)
2685 return this_variable;
2686 else if (Parent != null)
2687 return Parent.ThisVariable;
2693 Hashtable child_variable_names;
2696 // Marks a variable with name @name as being used in a child block.
2697 // If a variable name has been used in a child block, it's illegal to
2698 // declare a variable with the same name in the current block.
2700 public void AddChildVariableName (string name)
2702 if (child_variable_names == null)
2703 child_variable_names = new Hashtable ();
2705 if (!child_variable_names.Contains (name))
2706 child_variable_names.Add (name, true);
2710 // Marks all variables from block @block and all its children as being
2711 // used in a child block.
2713 public void AddChildVariableNames (Block block)
2715 if (block.Variables != null) {
2716 foreach (string name in block.Variables.Keys)
2717 AddChildVariableName (name);
2720 foreach (Block child in block.children) {
2721 if (child.Variables != null) {
2722 foreach (string name in child.Variables.Keys)
2723 AddChildVariableName (name);
2729 // Checks whether a variable name has already been used in a child block.
2731 public bool IsVariableNameUsedInChildBlock (string name)
2733 if (child_variable_names == null)
2736 return child_variable_names.Contains (name);
2740 // This is used by non-static `struct' constructors which do not have an
2741 // initializer - in this case, the constructor must initialize all of the
2742 // struct's fields. To do this, we add a "this" variable and use the flow
2743 // analysis code to ensure that it's been fully initialized before control
2744 // leaves the constructor.
2746 public VariableInfo AddThisVariable (TypeContainer tc, Location l)
2748 if (this_variable != null)
2749 return this_variable;
2751 this_variable = new VariableInfo (tc, ID, l);
2753 if (variables == null)
2754 variables = new Hashtable ();
2755 variables.Add ("this", this_variable);
2757 return this_variable;
2760 public VariableInfo AddVariable (Expression type, string name, Parameters pars, Location l)
2762 if (variables == null)
2763 variables = new Hashtable ();
2765 VariableInfo vi = GetVariableInfo (name);
2768 Report.Error (136, l, "A local variable named `" + name + "' " +
2769 "cannot be declared in this scope since it would " +
2770 "give a different meaning to `" + name + "', which " +
2771 "is already used in a `parent or current' scope to " +
2772 "denote something else");
2774 Report.Error (128, l, "A local variable `" + name + "' is already " +
2775 "defined in this scope");
2779 if (IsVariableNameUsedInChildBlock (name)) {
2780 Report.Error (136, l, "A local variable named `" + name + "' " +
2781 "cannot be declared in this scope since it would " +
2782 "give a different meaning to `" + name + "', which " +
2783 "is already used in a `child' scope to denote something " +
2790 Parameter p = pars.GetParameterByName (name, out idx);
2792 Report.Error (136, l, "A local variable named `" + name + "' " +
2793 "cannot be declared in this scope since it would " +
2794 "give a different meaning to `" + name + "', which " +
2795 "is already used in a `parent or current' scope to " +
2796 "denote something else");
2801 vi = new VariableInfo (type, name, ID, l);
2803 variables.Add (name, vi);
2805 if (variables_initialized)
2806 throw new Exception ();
2808 // Console.WriteLine ("Adding {0} to {1}", name, ID);
2812 public bool AddConstant (Expression type, string name, Expression value, Parameters pars, Location l)
2814 if (AddVariable (type, name, pars, l) == null)
2817 if (constants == null)
2818 constants = new Hashtable ();
2820 constants.Add (name, value);
2824 public Hashtable Variables {
2830 public VariableInfo GetVariableInfo (string name)
2832 if (variables != null) {
2834 temp = variables [name];
2837 return (VariableInfo) temp;
2842 return Parent.GetVariableInfo (name);
2847 public Expression GetVariableType (string name)
2849 VariableInfo vi = GetVariableInfo (name);
2857 public Expression GetConstantExpression (string name)
2859 if (constants != null) {
2861 temp = constants [name];
2864 return (Expression) temp;
2868 return Parent.GetConstantExpression (name);
2874 /// True if the variable named @name is a constant
2876 public bool IsConstant (string name)
2878 Expression e = null;
2880 e = GetConstantExpression (name);
2886 /// Use to fetch the statement associated with this label
2888 public Statement this [string name] {
2890 return (Statement) labels [name];
2894 Parameters parameters = null;
2895 public Parameters Parameters {
2898 return Parent.Parameters;
2905 /// A list of labels that were not used within this block
2907 public string [] GetUnreferenced ()
2909 // FIXME: Implement me
2913 public void AddStatement (Statement s)
2930 bool variables_initialized = false;
2931 int count_variables = 0, first_variable = 0;
2933 void UpdateVariableInfo (EmitContext ec)
2935 DeclSpace ds = ec.DeclSpace;
2940 first_variable += Parent.CountVariables;
2942 count_variables = first_variable;
2943 if (variables != null) {
2944 foreach (VariableInfo vi in variables.Values) {
2945 if (!vi.Resolve (ds)) {
2950 vi.Number = ++count_variables;
2952 if (vi.StructInfo != null)
2953 count_variables += vi.StructInfo.Count;
2957 variables_initialized = true;
2962 // The number of local variables in this block
2964 public int CountVariables
2967 if (!variables_initialized)
2968 throw new Exception ();
2970 return count_variables;
2975 /// Emits the variable declarations and labels.
2978 /// tc: is our typecontainer (to resolve type references)
2979 /// ig: is the code generator:
2980 /// toplevel: the toplevel block. This is used for checking
2981 /// that no two labels with the same name are used.
2983 public void EmitMeta (EmitContext ec, Block toplevel)
2985 DeclSpace ds = ec.DeclSpace;
2986 ILGenerator ig = ec.ig;
2988 if (!variables_initialized)
2989 UpdateVariableInfo (ec);
2992 // Process this block variables
2994 if (variables != null){
2995 local_builders = new Hashtable ();
2997 foreach (DictionaryEntry de in variables){
2998 string name = (string) de.Key;
2999 VariableInfo vi = (VariableInfo) de.Value;
3001 if (vi.VariableType == null)
3004 vi.LocalBuilder = ig.DeclareLocal (vi.VariableType);
3006 if (CodeGen.SymbolWriter != null)
3007 vi.LocalBuilder.SetLocalSymInfo (name);
3009 if (constants == null)
3012 Expression cv = (Expression) constants [name];
3016 Expression e = cv.Resolve (ec);
3020 if (!(e is Constant)){
3021 Report.Error (133, vi.Location,
3022 "The expression being assigned to `" +
3023 name + "' must be constant (" + e + ")");
3027 constants.Remove (name);
3028 constants.Add (name, e);
3033 // Now, handle the children
3035 if (children != null){
3036 foreach (Block b in children)
3037 b.EmitMeta (ec, toplevel);
3041 public void UsageWarning ()
3045 if (variables != null){
3046 foreach (DictionaryEntry de in variables){
3047 VariableInfo vi = (VariableInfo) de.Value;
3052 name = (string) de.Key;
3056 219, vi.Location, "The variable `" + name +
3057 "' is assigned but its value is never used");
3060 168, vi.Location, "The variable `" +
3062 "' is declared but never used");
3067 if (children != null)
3068 foreach (Block b in children)
3072 bool has_ret = false;
3074 public override bool Resolve (EmitContext ec)
3076 Block prev_block = ec.CurrentBlock;
3079 ec.CurrentBlock = this;
3080 ec.StartFlowBranching (this);
3082 Report.Debug (1, "RESOLVE BLOCK", StartLocation, ec.CurrentBranching);
3084 if (!variables_initialized)
3085 UpdateVariableInfo (ec);
3087 ArrayList new_statements = new ArrayList ();
3088 bool unreachable = false, warning_shown = false;
3090 foreach (Statement s in statements){
3091 if (unreachable && !(s is LabeledStatement)) {
3092 if (!warning_shown && !(s is EmptyStatement)) {
3093 warning_shown = true;
3094 Warning_DeadCodeFound (s.loc);
3100 if (s.Resolve (ec) == false) {
3105 if (s is LabeledStatement)
3106 unreachable = false;
3108 unreachable = ! ec.CurrentBranching.IsReachable ();
3110 new_statements.Add (s);
3113 statements = new_statements;
3115 Report.Debug (1, "RESOLVE BLOCK DONE", StartLocation, ec.CurrentBranching);
3117 FlowReturns returns = ec.EndFlowBranching ();
3118 ec.CurrentBlock = prev_block;
3120 // If we're a non-static `struct' constructor which doesn't have an
3121 // initializer, then we must initialize all of the struct's fields.
3122 if ((this_variable != null) && (returns != FlowReturns.EXCEPTION) &&
3123 !this_variable.IsAssigned (ec, loc))
3126 if ((labels != null) && (RootContext.WarningLevel >= 2)) {
3127 foreach (LabeledStatement label in labels.Values)
3128 if (!label.HasBeenReferenced)
3129 Report.Warning (164, label.Location,
3130 "This label has not been referenced");
3133 if ((returns == FlowReturns.ALWAYS) ||
3134 (returns == FlowReturns.EXCEPTION) ||
3135 (returns == FlowReturns.UNREACHABLE))
3141 protected override bool DoEmit (EmitContext ec)
3143 Block prev_block = ec.CurrentBlock;
3145 ec.CurrentBlock = this;
3147 ec.Mark (StartLocation);
3148 foreach (Statement s in statements)
3150 ec.Mark (EndLocation);
3152 ec.CurrentBlock = prev_block;
3157 public class SwitchLabel {
3160 public Location loc;
3161 public Label ILLabel;
3162 public Label ILLabelCode;
3165 // if expr == null, then it is the default case.
3167 public SwitchLabel (Expression expr, Location l)
3173 public Expression Label {
3179 public object Converted {
3186 // Resolves the expression, reduces it to a literal if possible
3187 // and then converts it to the requested type.
3189 public bool ResolveAndReduce (EmitContext ec, Type required_type)
3191 ILLabel = ec.ig.DefineLabel ();
3192 ILLabelCode = ec.ig.DefineLabel ();
3197 Expression e = label.Resolve (ec);
3202 if (!(e is Constant)){
3203 Report.Error (150, loc, "A constant value is expected, got: " + e);
3207 if (e is StringConstant || e is NullLiteral){
3208 if (required_type == TypeManager.string_type){
3210 ILLabel = ec.ig.DefineLabel ();
3215 converted = Expression.ConvertIntLiteral ((Constant) e, required_type, loc);
3216 if (converted == null)
3223 public class SwitchSection {
3224 // An array of SwitchLabels.
3225 public readonly ArrayList Labels;
3226 public readonly Block Block;
3228 public SwitchSection (ArrayList labels, Block block)
3235 public class Switch : Statement {
3236 public readonly ArrayList Sections;
3237 public Expression Expr;
3240 /// Maps constants whose type type SwitchType to their SwitchLabels.
3242 public Hashtable Elements;
3245 /// The governing switch type
3247 public Type SwitchType;
3253 Label default_target;
3254 Expression new_expr;
3257 // The types allowed to be implicitly cast from
3258 // on the governing type
3260 static Type [] allowed_types;
3262 public Switch (Expression e, ArrayList sects, Location l)
3269 public bool GotDefault {
3275 public Label DefaultTarget {
3277 return default_target;
3282 // Determines the governing type for a switch. The returned
3283 // expression might be the expression from the switch, or an
3284 // expression that includes any potential conversions to the
3285 // integral types or to string.
3287 Expression SwitchGoverningType (EmitContext ec, Type t)
3289 if (t == TypeManager.int32_type ||
3290 t == TypeManager.uint32_type ||
3291 t == TypeManager.char_type ||
3292 t == TypeManager.byte_type ||
3293 t == TypeManager.sbyte_type ||
3294 t == TypeManager.ushort_type ||
3295 t == TypeManager.short_type ||
3296 t == TypeManager.uint64_type ||
3297 t == TypeManager.int64_type ||
3298 t == TypeManager.string_type ||
3299 t == TypeManager.bool_type ||
3300 t.IsSubclassOf (TypeManager.enum_type))
3303 if (allowed_types == null){
3304 allowed_types = new Type [] {
3305 TypeManager.sbyte_type,
3306 TypeManager.byte_type,
3307 TypeManager.short_type,
3308 TypeManager.ushort_type,
3309 TypeManager.int32_type,
3310 TypeManager.uint32_type,
3311 TypeManager.int64_type,
3312 TypeManager.uint64_type,
3313 TypeManager.char_type,
3314 TypeManager.bool_type,
3315 TypeManager.string_type
3320 // Try to find a *user* defined implicit conversion.
3322 // If there is no implicit conversion, or if there are multiple
3323 // conversions, we have to report an error
3325 Expression converted = null;
3326 foreach (Type tt in allowed_types){
3329 e = Expression.ImplicitUserConversion (ec, Expr, tt, loc);
3333 if (converted != null){
3334 Report.Error (-12, loc, "More than one conversion to an integral " +
3335 " type exists for type `" +
3336 TypeManager.CSharpName (Expr.Type)+"'");
3344 void error152 (string n)
3347 152, "The label `" + n + ":' " +
3348 "is already present on this switch statement");
3352 // Performs the basic sanity checks on the switch statement
3353 // (looks for duplicate keys and non-constant expressions).
3355 // It also returns a hashtable with the keys that we will later
3356 // use to compute the switch tables
3358 bool CheckSwitch (EmitContext ec)
3362 Elements = new Hashtable ();
3364 got_default = false;
3366 if (TypeManager.IsEnumType (SwitchType)){
3367 compare_type = TypeManager.EnumToUnderlying (SwitchType);
3369 compare_type = SwitchType;
3371 foreach (SwitchSection ss in Sections){
3372 foreach (SwitchLabel sl in ss.Labels){
3373 if (!sl.ResolveAndReduce (ec, SwitchType)){
3378 if (sl.Label == null){
3380 error152 ("default");
3387 object key = sl.Converted;
3389 if (key is Constant)
3390 key = ((Constant) key).GetValue ();
3393 key = NullLiteral.Null;
3395 string lname = null;
3396 if (compare_type == TypeManager.uint64_type){
3397 ulong v = (ulong) key;
3399 if (Elements.Contains (v))
3400 lname = v.ToString ();
3402 Elements.Add (v, sl);
3403 } else if (compare_type == TypeManager.int64_type){
3404 long v = (long) key;
3406 if (Elements.Contains (v))
3407 lname = v.ToString ();
3409 Elements.Add (v, sl);
3410 } else if (compare_type == TypeManager.uint32_type){
3411 uint v = (uint) key;
3413 if (Elements.Contains (v))
3414 lname = v.ToString ();
3416 Elements.Add (v, sl);
3417 } else if (compare_type == TypeManager.char_type){
3418 char v = (char) key;
3420 if (Elements.Contains (v))
3421 lname = v.ToString ();
3423 Elements.Add (v, sl);
3424 } else if (compare_type == TypeManager.byte_type){
3425 byte v = (byte) key;
3427 if (Elements.Contains (v))
3428 lname = v.ToString ();
3430 Elements.Add (v, sl);
3431 } else if (compare_type == TypeManager.sbyte_type){
3432 sbyte v = (sbyte) key;
3434 if (Elements.Contains (v))
3435 lname = v.ToString ();
3437 Elements.Add (v, sl);
3438 } else if (compare_type == TypeManager.short_type){
3439 short v = (short) key;
3441 if (Elements.Contains (v))
3442 lname = v.ToString ();
3444 Elements.Add (v, sl);
3445 } else if (compare_type == TypeManager.ushort_type){
3446 ushort v = (ushort) key;
3448 if (Elements.Contains (v))
3449 lname = v.ToString ();
3451 Elements.Add (v, sl);
3452 } else if (compare_type == TypeManager.string_type){
3453 if (key is NullLiteral){
3454 if (Elements.Contains (NullLiteral.Null))
3457 Elements.Add (NullLiteral.Null, null);
3459 string s = (string) key;
3461 if (Elements.Contains (s))
3464 Elements.Add (s, sl);
3466 } else if (compare_type == TypeManager.int32_type) {
3469 if (Elements.Contains (v))
3470 lname = v.ToString ();
3472 Elements.Add (v, sl);
3473 } else if (compare_type == TypeManager.bool_type) {
3474 bool v = (bool) key;
3476 if (Elements.Contains (v))
3477 lname = v.ToString ();
3479 Elements.Add (v, sl);
3483 throw new Exception ("Unknown switch type!" +
3484 SwitchType + " " + compare_type);
3488 error152 ("case + " + lname);
3499 void EmitObjectInteger (ILGenerator ig, object k)
3502 IntConstant.EmitInt (ig, (int) k);
3503 else if (k is Constant) {
3504 EmitObjectInteger (ig, ((Constant) k).GetValue ());
3507 IntConstant.EmitInt (ig, unchecked ((int) (uint) k));
3510 if ((long) k >= int.MinValue && (long) k <= int.MaxValue)
3512 IntConstant.EmitInt (ig, (int) (long) k);
3513 ig.Emit (OpCodes.Conv_I8);
3516 LongConstant.EmitLong (ig, (long) k);
3518 else if (k is ulong)
3520 if ((ulong) k < (1L<<32))
3522 IntConstant.EmitInt (ig, (int) (long) k);
3523 ig.Emit (OpCodes.Conv_U8);
3527 LongConstant.EmitLong (ig, unchecked ((long) (ulong) k));
3531 IntConstant.EmitInt (ig, (int) ((char) k));
3532 else if (k is sbyte)
3533 IntConstant.EmitInt (ig, (int) ((sbyte) k));
3535 IntConstant.EmitInt (ig, (int) ((byte) k));
3536 else if (k is short)
3537 IntConstant.EmitInt (ig, (int) ((short) k));
3538 else if (k is ushort)
3539 IntConstant.EmitInt (ig, (int) ((ushort) k));
3541 IntConstant.EmitInt (ig, ((bool) k) ? 1 : 0);
3543 throw new Exception ("Unhandled case");
3546 // structure used to hold blocks of keys while calculating table switch
3547 class KeyBlock : IComparable
3549 public KeyBlock (long _nFirst)
3551 nFirst = nLast = _nFirst;
3555 public ArrayList rgKeys = null;
3558 get { return (int) (nLast - nFirst + 1); }
3560 public static long TotalLength (KeyBlock kbFirst, KeyBlock kbLast)
3562 return kbLast.nLast - kbFirst.nFirst + 1;
3564 public int CompareTo (object obj)
3566 KeyBlock kb = (KeyBlock) obj;
3567 int nLength = Length;
3568 int nLengthOther = kb.Length;
3569 if (nLengthOther == nLength)
3570 return (int) (kb.nFirst - nFirst);
3571 return nLength - nLengthOther;
3576 /// This method emits code for a lookup-based switch statement (non-string)
3577 /// Basically it groups the cases into blocks that are at least half full,
3578 /// and then spits out individual lookup opcodes for each block.
3579 /// It emits the longest blocks first, and short blocks are just
3580 /// handled with direct compares.
3582 /// <param name="ec"></param>
3583 /// <param name="val"></param>
3584 /// <returns></returns>
3585 bool TableSwitchEmit (EmitContext ec, LocalBuilder val)
3587 int cElements = Elements.Count;
3588 object [] rgKeys = new object [cElements];
3589 Elements.Keys.CopyTo (rgKeys, 0);
3590 Array.Sort (rgKeys);
3592 // initialize the block list with one element per key
3593 ArrayList rgKeyBlocks = new ArrayList ();
3594 foreach (object key in rgKeys)
3595 rgKeyBlocks.Add (new KeyBlock (Convert.ToInt64 (key)));
3598 // iteratively merge the blocks while they are at least half full
3599 // there's probably a really cool way to do this with a tree...
3600 while (rgKeyBlocks.Count > 1)
3602 ArrayList rgKeyBlocksNew = new ArrayList ();
3603 kbCurr = (KeyBlock) rgKeyBlocks [0];
3604 for (int ikb = 1; ikb < rgKeyBlocks.Count; ikb++)
3606 KeyBlock kb = (KeyBlock) rgKeyBlocks [ikb];
3607 if ((kbCurr.Length + kb.Length) * 2 >= KeyBlock.TotalLength (kbCurr, kb))
3610 kbCurr.nLast = kb.nLast;
3614 // start a new block
3615 rgKeyBlocksNew.Add (kbCurr);
3619 rgKeyBlocksNew.Add (kbCurr);
3620 if (rgKeyBlocks.Count == rgKeyBlocksNew.Count)
3622 rgKeyBlocks = rgKeyBlocksNew;
3625 // initialize the key lists
3626 foreach (KeyBlock kb in rgKeyBlocks)
3627 kb.rgKeys = new ArrayList ();
3629 // fill the key lists
3631 if (rgKeyBlocks.Count > 0) {
3632 kbCurr = (KeyBlock) rgKeyBlocks [0];
3633 foreach (object key in rgKeys)
3635 bool fNextBlock = (key is UInt64) ? (ulong) key > (ulong) kbCurr.nLast : Convert.ToInt64 (key) > kbCurr.nLast;
3637 kbCurr = (KeyBlock) rgKeyBlocks [++iBlockCurr];
3638 kbCurr.rgKeys.Add (key);
3642 // sort the blocks so we can tackle the largest ones first
3643 rgKeyBlocks.Sort ();
3645 // okay now we can start...
3646 ILGenerator ig = ec.ig;
3647 Label lblEnd = ig.DefineLabel (); // at the end ;-)
3648 Label lblDefault = ig.DefineLabel ();
3650 Type typeKeys = null;
3651 if (rgKeys.Length > 0)
3652 typeKeys = rgKeys [0].GetType (); // used for conversions
3654 for (int iBlock = rgKeyBlocks.Count - 1; iBlock >= 0; --iBlock)
3656 KeyBlock kb = ((KeyBlock) rgKeyBlocks [iBlock]);
3657 lblDefault = (iBlock == 0) ? DefaultTarget : ig.DefineLabel ();
3660 foreach (object key in kb.rgKeys)
3662 ig.Emit (OpCodes.Ldloc, val);
3663 EmitObjectInteger (ig, key);
3664 SwitchLabel sl = (SwitchLabel) Elements [key];
3665 ig.Emit (OpCodes.Beq, sl.ILLabel);
3670 // TODO: if all the keys in the block are the same and there are
3671 // no gaps/defaults then just use a range-check.
3672 if (SwitchType == TypeManager.int64_type ||
3673 SwitchType == TypeManager.uint64_type)
3675 // TODO: optimize constant/I4 cases
3677 // check block range (could be > 2^31)
3678 ig.Emit (OpCodes.Ldloc, val);
3679 EmitObjectInteger (ig, Convert.ChangeType (kb.nFirst, typeKeys));
3680 ig.Emit (OpCodes.Blt, lblDefault);
3681 ig.Emit (OpCodes.Ldloc, val);
3682 EmitObjectInteger (ig, Convert.ChangeType (kb.nFirst, typeKeys));
3683 ig.Emit (OpCodes.Bgt, lblDefault);
3686 ig.Emit (OpCodes.Ldloc, val);
3689 EmitObjectInteger (ig, Convert.ChangeType (kb.nFirst, typeKeys));
3690 ig.Emit (OpCodes.Sub);
3692 ig.Emit (OpCodes.Conv_I4); // assumes < 2^31 labels!
3697 ig.Emit (OpCodes.Ldloc, val);
3698 int nFirst = (int) kb.nFirst;
3701 IntConstant.EmitInt (ig, nFirst);
3702 ig.Emit (OpCodes.Sub);
3704 else if (nFirst < 0)
3706 IntConstant.EmitInt (ig, -nFirst);
3707 ig.Emit (OpCodes.Add);
3711 // first, build the list of labels for the switch
3713 int cJumps = kb.Length;
3714 Label [] rgLabels = new Label [cJumps];
3715 for (int iJump = 0; iJump < cJumps; iJump++)
3717 object key = kb.rgKeys [iKey];
3718 if (Convert.ToInt64 (key) == kb.nFirst + iJump)
3720 SwitchLabel sl = (SwitchLabel) Elements [key];
3721 rgLabels [iJump] = sl.ILLabel;
3725 rgLabels [iJump] = lblDefault;
3727 // emit the switch opcode
3728 ig.Emit (OpCodes.Switch, rgLabels);
3731 // mark the default for this block
3733 ig.MarkLabel (lblDefault);
3736 // TODO: find the default case and emit it here,
3737 // to prevent having to do the following jump.
3738 // make sure to mark other labels in the default section
3740 // the last default just goes to the end
3741 ig.Emit (OpCodes.Br, lblDefault);
3743 // now emit the code for the sections
3744 bool fFoundDefault = false;
3745 bool fAllReturn = true;
3746 foreach (SwitchSection ss in Sections)
3748 foreach (SwitchLabel sl in ss.Labels)
3750 ig.MarkLabel (sl.ILLabel);
3751 ig.MarkLabel (sl.ILLabelCode);
3752 if (sl.Label == null)
3754 ig.MarkLabel (lblDefault);
3755 fFoundDefault = true;
3758 bool returns = ss.Block.Emit (ec);
3759 fAllReturn &= returns;
3760 //ig.Emit (OpCodes.Br, lblEnd);
3763 if (!fFoundDefault) {
3764 ig.MarkLabel (lblDefault);
3767 ig.MarkLabel (lblEnd);
3772 // This simple emit switch works, but does not take advantage of the
3774 // TODO: remove non-string logic from here
3775 // TODO: binary search strings?
3777 bool SimpleSwitchEmit (EmitContext ec, LocalBuilder val)
3779 ILGenerator ig = ec.ig;
3780 Label end_of_switch = ig.DefineLabel ();
3781 Label next_test = ig.DefineLabel ();
3782 Label null_target = ig.DefineLabel ();
3783 bool default_found = false;
3784 bool first_test = true;
3785 bool pending_goto_end = false;
3786 bool all_return = true;
3787 bool is_string = false;
3791 // Special processing for strings: we cant compare
3794 if (SwitchType == TypeManager.string_type){
3795 ig.Emit (OpCodes.Ldloc, val);
3798 if (Elements.Contains (NullLiteral.Null)){
3799 ig.Emit (OpCodes.Brfalse, null_target);
3801 ig.Emit (OpCodes.Brfalse, default_target);
3803 ig.Emit (OpCodes.Ldloc, val);
3804 ig.Emit (OpCodes.Call, TypeManager.string_isinterneted_string);
3805 ig.Emit (OpCodes.Stloc, val);
3808 foreach (SwitchSection ss in Sections){
3809 Label sec_begin = ig.DefineLabel ();
3811 if (pending_goto_end)
3812 ig.Emit (OpCodes.Br, end_of_switch);
3814 int label_count = ss.Labels.Count;
3816 foreach (SwitchLabel sl in ss.Labels){
3817 ig.MarkLabel (sl.ILLabel);
3820 ig.MarkLabel (next_test);
3821 next_test = ig.DefineLabel ();
3824 // If we are the default target
3826 if (sl.Label == null){
3827 ig.MarkLabel (default_target);
3828 default_found = true;
3830 object lit = sl.Converted;
3832 if (lit is NullLiteral){
3834 if (label_count == 1)
3835 ig.Emit (OpCodes.Br, next_test);
3840 StringConstant str = (StringConstant) lit;
3842 ig.Emit (OpCodes.Ldloc, val);
3843 ig.Emit (OpCodes.Ldstr, str.Value);
3844 if (label_count == 1)
3845 ig.Emit (OpCodes.Bne_Un, next_test);
3847 ig.Emit (OpCodes.Beq, sec_begin);
3849 ig.Emit (OpCodes.Ldloc, val);
3850 EmitObjectInteger (ig, lit);
3851 ig.Emit (OpCodes.Ceq);
3852 if (label_count == 1)
3853 ig.Emit (OpCodes.Brfalse, next_test);
3855 ig.Emit (OpCodes.Brtrue, sec_begin);
3859 if (label_count != 1)
3860 ig.Emit (OpCodes.Br, next_test);
3863 ig.MarkLabel (null_target);
3864 ig.MarkLabel (sec_begin);
3865 foreach (SwitchLabel sl in ss.Labels)
3866 ig.MarkLabel (sl.ILLabelCode);
3868 bool returns = ss.Block.Emit (ec);
3870 pending_goto_end = false;
3873 pending_goto_end = true;
3877 if (!default_found){
3878 ig.MarkLabel (default_target);
3881 ig.MarkLabel (next_test);
3882 ig.MarkLabel (end_of_switch);
3887 public override bool Resolve (EmitContext ec)
3889 Expr = Expr.Resolve (ec);
3893 new_expr = SwitchGoverningType (ec, Expr.Type);
3894 if (new_expr == null){
3895 Report.Error (151, loc, "An integer type or string was expected for switch");
3900 SwitchType = new_expr.Type;
3902 if (!CheckSwitch (ec))
3905 Switch old_switch = ec.Switch;
3907 ec.Switch.SwitchType = SwitchType;
3909 ec.StartFlowBranching (FlowBranchingType.SWITCH, loc);
3912 foreach (SwitchSection ss in Sections){
3914 ec.CurrentBranching.CreateSibling ();
3918 if (ss.Block.Resolve (ec) != true)
3924 ec.CurrentBranching.CreateSibling ();
3926 ec.EndFlowBranching ();
3927 ec.Switch = old_switch;
3932 protected override bool DoEmit (EmitContext ec)
3934 // Store variable for comparission purposes
3935 LocalBuilder value = ec.ig.DeclareLocal (SwitchType);
3937 ec.ig.Emit (OpCodes.Stloc, value);
3939 ILGenerator ig = ec.ig;
3941 default_target = ig.DefineLabel ();
3944 // Setup the codegen context
3946 Label old_end = ec.LoopEnd;
3947 Switch old_switch = ec.Switch;
3949 ec.LoopEnd = ig.DefineLabel ();
3954 if (SwitchType == TypeManager.string_type)
3955 all_return = SimpleSwitchEmit (ec, value);
3957 all_return = TableSwitchEmit (ec, value);
3959 // Restore context state.
3960 ig.MarkLabel (ec.LoopEnd);
3963 // Restore the previous context
3965 ec.LoopEnd = old_end;
3966 ec.Switch = old_switch;
3972 public class Lock : Statement {
3974 Statement Statement;
3976 public Lock (Expression expr, Statement stmt, Location l)
3983 public override bool Resolve (EmitContext ec)
3985 expr = expr.Resolve (ec);
3986 return Statement.Resolve (ec) && expr != null;
3989 protected override bool DoEmit (EmitContext ec)
3991 Type type = expr.Type;
3994 if (type.IsValueType){
3995 Report.Error (185, loc, "lock statement requires the expression to be " +
3996 " a reference type (type is: `" +
3997 TypeManager.CSharpName (type) + "'");
4001 ILGenerator ig = ec.ig;
4002 LocalBuilder temp = ig.DeclareLocal (type);
4005 ig.Emit (OpCodes.Dup);
4006 ig.Emit (OpCodes.Stloc, temp);
4007 ig.Emit (OpCodes.Call, TypeManager.void_monitor_enter_object);
4010 Label end = ig.BeginExceptionBlock ();
4011 bool old_in_try = ec.InTry;
4013 Label finish = ig.DefineLabel ();
4014 val = Statement.Emit (ec);
4015 ec.InTry = old_in_try;
4016 // ig.Emit (OpCodes.Leave, finish);
4018 ig.MarkLabel (finish);
4021 ig.BeginFinallyBlock ();
4022 ig.Emit (OpCodes.Ldloc, temp);
4023 ig.Emit (OpCodes.Call, TypeManager.void_monitor_exit_object);
4024 ig.EndExceptionBlock ();
4030 public class Unchecked : Statement {
4031 public readonly Block Block;
4033 public Unchecked (Block b)
4038 public override bool Resolve (EmitContext ec)
4040 return Block.Resolve (ec);
4043 protected override bool DoEmit (EmitContext ec)
4045 bool previous_state = ec.CheckState;
4046 bool previous_state_const = ec.ConstantCheckState;
4049 ec.CheckState = false;
4050 ec.ConstantCheckState = false;
4051 val = Block.Emit (ec);
4052 ec.CheckState = previous_state;
4053 ec.ConstantCheckState = previous_state_const;
4059 public class Checked : Statement {
4060 public readonly Block Block;
4062 public Checked (Block b)
4067 public override bool Resolve (EmitContext ec)
4069 bool previous_state = ec.CheckState;
4070 bool previous_state_const = ec.ConstantCheckState;
4072 ec.CheckState = true;
4073 ec.ConstantCheckState = true;
4074 bool ret = Block.Resolve (ec);
4075 ec.CheckState = previous_state;
4076 ec.ConstantCheckState = previous_state_const;
4081 protected override bool DoEmit (EmitContext ec)
4083 bool previous_state = ec.CheckState;
4084 bool previous_state_const = ec.ConstantCheckState;
4087 ec.CheckState = true;
4088 ec.ConstantCheckState = true;
4089 val = Block.Emit (ec);
4090 ec.CheckState = previous_state;
4091 ec.ConstantCheckState = previous_state_const;
4097 public class Unsafe : Statement {
4098 public readonly Block Block;
4100 public Unsafe (Block b)
4105 public override bool Resolve (EmitContext ec)
4107 bool previous_state = ec.InUnsafe;
4111 val = Block.Resolve (ec);
4112 ec.InUnsafe = previous_state;
4117 protected override bool DoEmit (EmitContext ec)
4119 bool previous_state = ec.InUnsafe;
4123 val = Block.Emit (ec);
4124 ec.InUnsafe = previous_state;
4133 public class Fixed : Statement {
4135 ArrayList declarators;
4136 Statement statement;
4141 public bool is_object;
4142 public VariableInfo vi;
4143 public Expression expr;
4144 public Expression converted;
4147 public Fixed (Expression type, ArrayList decls, Statement stmt, Location l)
4150 declarators = decls;
4155 public override bool Resolve (EmitContext ec)
4157 expr_type = ec.DeclSpace.ResolveType (type, false, loc);
4158 if (expr_type == null)
4161 data = new FixedData [declarators.Count];
4163 if (!expr_type.IsPointer){
4164 Report.Error (209, loc, "Variables in a fixed statement must be pointers");
4169 foreach (Pair p in declarators){
4170 VariableInfo vi = (VariableInfo) p.First;
4171 Expression e = (Expression) p.Second;
4176 // The rules for the possible declarators are pretty wise,
4177 // but the production on the grammar is more concise.
4179 // So we have to enforce these rules here.
4181 // We do not resolve before doing the case 1 test,
4182 // because the grammar is explicit in that the token &
4183 // is present, so we need to test for this particular case.
4187 // Case 1: & object.
4189 if (e is Unary && ((Unary) e).Oper == Unary.Operator.AddressOf){
4190 Expression child = ((Unary) e).Expr;
4193 if (child is ParameterReference || child is LocalVariableReference){
4196 "No need to use fixed statement for parameters or " +
4197 "local variable declarations (address is already " +
4206 child = ((Unary) e).Expr;
4208 if (!TypeManager.VerifyUnManaged (child.Type, loc))
4211 data [i].is_object = true;
4213 data [i].converted = null;
4227 if (e.Type.IsArray){
4228 Type array_type = e.Type.GetElementType ();
4232 // Provided that array_type is unmanaged,
4234 if (!TypeManager.VerifyUnManaged (array_type, loc))
4238 // and T* is implicitly convertible to the
4239 // pointer type given in the fixed statement.
4241 ArrayPtr array_ptr = new ArrayPtr (e, loc);
4243 Expression converted = Expression.ConvertImplicitRequired (
4244 ec, array_ptr, vi.VariableType, loc);
4245 if (converted == null)
4248 data [i].is_object = false;
4250 data [i].converted = converted;
4260 if (e.Type == TypeManager.string_type){
4261 data [i].is_object = false;
4263 data [i].converted = null;
4269 return statement.Resolve (ec);
4272 protected override bool DoEmit (EmitContext ec)
4274 ILGenerator ig = ec.ig;
4276 bool is_ret = false;
4277 LocalBuilder [] clear_list = new LocalBuilder [data.Length];
4279 for (int i = 0; i < data.Length; i++) {
4280 VariableInfo vi = data [i].vi;
4283 // Case 1: & object.
4285 if (data [i].is_object) {
4287 // Store pointer in pinned location
4289 data [i].expr.Emit (ec);
4290 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4291 clear_list [i] = vi.LocalBuilder;
4298 if (data [i].expr.Type.IsArray){
4300 // Store pointer in pinned location
4302 data [i].converted.Emit (ec);
4304 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4305 clear_list [i] = vi.LocalBuilder;
4312 if (data [i].expr.Type == TypeManager.string_type){
4313 LocalBuilder pinned_string = ig.DeclareLocal (TypeManager.string_type);
4314 TypeManager.MakePinned (pinned_string);
4315 clear_list [i] = pinned_string;
4317 data [i].expr.Emit (ec);
4318 ig.Emit (OpCodes.Stloc, pinned_string);
4320 Expression sptr = new StringPtr (pinned_string, loc);
4321 Expression converted = Expression.ConvertImplicitRequired (
4322 ec, sptr, vi.VariableType, loc);
4324 if (converted == null)
4327 converted.Emit (ec);
4328 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4332 is_ret = statement.Emit (ec);
4337 // Clear the pinned variable
4339 for (int i = 0; i < data.Length; i++) {
4340 VariableInfo vi = data [i].vi;
4342 if (data [i].is_object || data [i].expr.Type.IsArray) {
4343 ig.Emit (OpCodes.Ldc_I4_0);
4344 ig.Emit (OpCodes.Conv_U);
4345 ig.Emit (OpCodes.Stloc, clear_list [i]);
4346 } else if (data [i].expr.Type == TypeManager.string_type){
4347 ig.Emit (OpCodes.Ldnull);
4348 ig.Emit (OpCodes.Stloc, clear_list [i]);
4356 public class Catch {
4357 public readonly string Name;
4358 public readonly Block Block;
4359 public readonly Location Location;
4361 Expression type_expr;
4364 public Catch (Expression type, string name, Block block, Location l)
4372 public Type CatchType {
4378 public bool IsGeneral {
4380 return type_expr == null;
4384 public bool Resolve (EmitContext ec)
4386 if (type_expr != null) {
4387 type = ec.DeclSpace.ResolveType (type_expr, false, Location);
4391 if (type != TypeManager.exception_type && !type.IsSubclassOf (TypeManager.exception_type)){
4392 Report.Error (155, Location,
4393 "The type caught or thrown must be derived " +
4394 "from System.Exception");
4400 if (!Block.Resolve (ec))
4407 public class Try : Statement {
4408 public readonly Block Fini, Block;
4409 public readonly ArrayList Specific;
4410 public readonly Catch General;
4413 // specific, general and fini might all be null.
4415 public Try (Block block, ArrayList specific, Catch general, Block fini, Location l)
4417 if (specific == null && general == null){
4418 Console.WriteLine ("CIR.Try: Either specific or general have to be non-null");
4422 this.Specific = specific;
4423 this.General = general;
4428 public override bool Resolve (EmitContext ec)
4432 ec.StartFlowBranching (FlowBranchingType.EXCEPTION, Block.StartLocation);
4434 Report.Debug (1, "START OF TRY BLOCK", Block.StartLocation);
4436 bool old_in_try = ec.InTry;
4439 if (!Block.Resolve (ec))
4442 ec.InTry = old_in_try;
4444 FlowBranching.UsageVector vector = ec.CurrentBranching.CurrentUsageVector;
4446 Report.Debug (1, "START OF CATCH BLOCKS", vector);
4448 foreach (Catch c in Specific){
4449 ec.CurrentBranching.CreateSibling ();
4450 Report.Debug (1, "STARTED SIBLING FOR CATCH", ec.CurrentBranching);
4452 if (c.Name != null) {
4453 VariableInfo vi = c.Block.GetVariableInfo (c.Name);
4455 throw new Exception ();
4460 bool old_in_catch = ec.InCatch;
4463 if (!c.Resolve (ec))
4466 ec.InCatch = old_in_catch;
4468 FlowBranching.UsageVector current = ec.CurrentBranching.CurrentUsageVector;
4470 if (!current.AlwaysReturns && !current.AlwaysBreaks)
4471 vector.AndLocals (current);
4473 vector.Or (current);
4476 Report.Debug (1, "END OF CATCH BLOCKS", ec.CurrentBranching);
4478 if (General != null){
4479 ec.CurrentBranching.CreateSibling ();
4480 Report.Debug (1, "STARTED SIBLING FOR GENERAL", ec.CurrentBranching);
4482 bool old_in_catch = ec.InCatch;
4485 if (!General.Resolve (ec))
4488 ec.InCatch = old_in_catch;
4490 FlowBranching.UsageVector current = ec.CurrentBranching.CurrentUsageVector;
4492 if (!current.AlwaysReturns && !current.AlwaysBreaks)
4493 vector.AndLocals (current);
4495 vector.Or (current);
4498 Report.Debug (1, "END OF GENERAL CATCH BLOCKS", ec.CurrentBranching);
4501 ec.CurrentBranching.CreateSiblingForFinally ();
4502 Report.Debug (1, "STARTED SIBLING FOR FINALLY", ec.CurrentBranching, vector);
4504 bool old_in_finally = ec.InFinally;
4505 ec.InFinally = true;
4507 if (!Fini.Resolve (ec))
4510 ec.InFinally = old_in_finally;
4513 FlowReturns returns = ec.EndFlowBranching ();
4515 FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
4517 Report.Debug (1, "END OF FINALLY", ec.CurrentBranching, returns, vector, f_vector);
4519 if ((returns == FlowReturns.SOMETIMES) || (returns == FlowReturns.ALWAYS)) {
4520 ec.CurrentBranching.CheckOutParameters (f_vector.Parameters, loc);
4523 ec.CurrentBranching.CurrentUsageVector.Or (vector);
4525 Report.Debug (1, "END OF TRY", ec.CurrentBranching);
4530 protected override bool DoEmit (EmitContext ec)
4532 ILGenerator ig = ec.ig;
4534 Label finish = ig.DefineLabel ();;
4538 end = ig.BeginExceptionBlock ();
4539 bool old_in_try = ec.InTry;
4541 returns = Block.Emit (ec);
4542 ec.InTry = old_in_try;
4545 // System.Reflection.Emit provides this automatically:
4546 // ig.Emit (OpCodes.Leave, finish);
4548 bool old_in_catch = ec.InCatch;
4550 DeclSpace ds = ec.DeclSpace;
4552 foreach (Catch c in Specific){
4555 ig.BeginCatchBlock (c.CatchType);
4557 if (c.Name != null){
4558 vi = c.Block.GetVariableInfo (c.Name);
4560 throw new Exception ("Variable does not exist in this block");
4562 ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
4564 ig.Emit (OpCodes.Pop);
4566 if (!c.Block.Emit (ec))
4570 if (General != null){
4571 ig.BeginCatchBlock (TypeManager.object_type);
4572 ig.Emit (OpCodes.Pop);
4573 if (!General.Block.Emit (ec))
4576 ec.InCatch = old_in_catch;
4578 ig.MarkLabel (finish);
4580 ig.BeginFinallyBlock ();
4581 bool old_in_finally = ec.InFinally;
4582 ec.InFinally = true;
4584 ec.InFinally = old_in_finally;
4587 ig.EndExceptionBlock ();
4590 if (!returns || ec.InTry || ec.InCatch)
4593 // Unfortunately, System.Reflection.Emit automatically emits a leave
4594 // to the end of the finally block. This is a problem if `returns'
4595 // is true since we may jump to a point after the end of the method.
4596 // As a workaround, emit an explicit ret here.
4598 if (ec.ReturnType != null)
4599 ec.ig.Emit (OpCodes.Ldloc, ec.TemporaryReturn ());
4600 ec.ig.Emit (OpCodes.Ret);
4606 public class Using : Statement {
4607 object expression_or_block;
4608 Statement Statement;
4613 Expression [] converted_vars;
4614 ExpressionStatement [] assign;
4616 public Using (object expression_or_block, Statement stmt, Location l)
4618 this.expression_or_block = expression_or_block;
4624 // Resolves for the case of using using a local variable declaration.
4626 bool ResolveLocalVariableDecls (EmitContext ec)
4628 bool need_conv = false;
4629 expr_type = ec.DeclSpace.ResolveType (expr, false, loc);
4632 if (expr_type == null)
4636 // The type must be an IDisposable or an implicit conversion
4639 converted_vars = new Expression [var_list.Count];
4640 assign = new ExpressionStatement [var_list.Count];
4641 if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
4642 foreach (DictionaryEntry e in var_list){
4643 Expression var = (Expression) e.Key;
4645 var = var.ResolveLValue (ec, new EmptyExpression ());
4649 converted_vars [i] = Expression.ConvertImplicitRequired (
4650 ec, var, TypeManager.idisposable_type, loc);
4652 if (converted_vars [i] == null)
4660 foreach (DictionaryEntry e in var_list){
4661 LocalVariableReference var = (LocalVariableReference) e.Key;
4662 Expression new_expr = (Expression) e.Value;
4665 a = new Assign (var, new_expr, loc);
4671 converted_vars [i] = var;
4672 assign [i] = (ExpressionStatement) a;
4679 bool ResolveExpression (EmitContext ec)
4681 if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
4682 conv = Expression.ConvertImplicitRequired (
4683 ec, expr, TypeManager.idisposable_type, loc);
4693 // Emits the code for the case of using using a local variable declaration.
4695 bool EmitLocalVariableDecls (EmitContext ec)
4697 ILGenerator ig = ec.ig;
4700 bool old_in_try = ec.InTry;
4702 for (i = 0; i < assign.Length; i++) {
4703 assign [i].EmitStatement (ec);
4705 ig.BeginExceptionBlock ();
4707 Statement.Emit (ec);
4708 ec.InTry = old_in_try;
4710 bool old_in_finally = ec.InFinally;
4711 ec.InFinally = true;
4712 var_list.Reverse ();
4713 foreach (DictionaryEntry e in var_list){
4714 LocalVariableReference var = (LocalVariableReference) e.Key;
4715 Label skip = ig.DefineLabel ();
4718 ig.BeginFinallyBlock ();
4721 ig.Emit (OpCodes.Brfalse, skip);
4722 converted_vars [i].Emit (ec);
4723 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4724 ig.MarkLabel (skip);
4725 ig.EndExceptionBlock ();
4727 ec.InFinally = old_in_finally;
4732 bool EmitExpression (EmitContext ec)
4735 // Make a copy of the expression and operate on that.
4737 ILGenerator ig = ec.ig;
4738 LocalBuilder local_copy = ig.DeclareLocal (expr_type);
4743 ig.Emit (OpCodes.Stloc, local_copy);
4745 bool old_in_try = ec.InTry;
4747 ig.BeginExceptionBlock ();
4748 Statement.Emit (ec);
4749 ec.InTry = old_in_try;
4751 Label skip = ig.DefineLabel ();
4752 bool old_in_finally = ec.InFinally;
4753 ig.BeginFinallyBlock ();
4754 ig.Emit (OpCodes.Ldloc, local_copy);
4755 ig.Emit (OpCodes.Brfalse, skip);
4756 ig.Emit (OpCodes.Ldloc, local_copy);
4757 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4758 ig.MarkLabel (skip);
4759 ec.InFinally = old_in_finally;
4760 ig.EndExceptionBlock ();
4765 public override bool Resolve (EmitContext ec)
4767 if (expression_or_block is DictionaryEntry){
4768 expr = (Expression) ((DictionaryEntry) expression_or_block).Key;
4769 var_list = (ArrayList)((DictionaryEntry)expression_or_block).Value;
4771 if (!ResolveLocalVariableDecls (ec))
4774 } else if (expression_or_block is Expression){
4775 expr = (Expression) expression_or_block;
4777 expr = expr.Resolve (ec);
4781 expr_type = expr.Type;
4783 if (!ResolveExpression (ec))
4787 return Statement.Resolve (ec);
4790 protected override bool DoEmit (EmitContext ec)
4792 if (expression_or_block is DictionaryEntry)
4793 return EmitLocalVariableDecls (ec);
4794 else if (expression_or_block is Expression)
4795 return EmitExpression (ec);
4802 /// Implementation of the foreach C# statement
4804 public class Foreach : Statement {
4806 LocalVariableReference variable;
4808 Statement statement;
4809 ForeachHelperMethods hm;
4810 Expression empty, conv;
4811 Type array_type, element_type;
4814 public Foreach (Expression type, LocalVariableReference var, Expression expr,
4815 Statement stmt, Location l)
4818 this.variable = var;
4824 public override bool Resolve (EmitContext ec)
4826 expr = expr.Resolve (ec);
4830 var_type = ec.DeclSpace.ResolveType (type, false, loc);
4831 if (var_type == null)
4835 // We need an instance variable. Not sure this is the best
4836 // way of doing this.
4838 // FIXME: When we implement propertyaccess, will those turn
4839 // out to return values in ExprClass? I think they should.
4841 if (!(expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.Value ||
4842 expr.eclass == ExprClass.PropertyAccess || expr.eclass == ExprClass.IndexerAccess)){
4843 error1579 (expr.Type);
4847 if (expr.Type.IsArray) {
4848 array_type = expr.Type;
4849 element_type = array_type.GetElementType ();
4851 empty = new EmptyExpression (element_type);
4853 hm = ProbeCollectionType (ec, expr.Type);
4855 error1579 (expr.Type);
4859 array_type = expr.Type;
4860 element_type = hm.element_type;
4862 empty = new EmptyExpression (hm.element_type);
4865 ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
4866 ec.CurrentBranching.CreateSibling ();
4870 // FIXME: maybe we can apply the same trick we do in the
4871 // array handling to avoid creating empty and conv in some cases.
4873 // Although it is not as important in this case, as the type
4874 // will not likely be object (what the enumerator will return).
4876 conv = Expression.ConvertExplicit (ec, empty, var_type, loc);
4880 if (variable.ResolveLValue (ec, empty) == null)
4883 if (!statement.Resolve (ec))
4886 FlowReturns returns = ec.EndFlowBranching ();
4892 // Retrieves a `public bool MoveNext ()' method from the Type `t'
4894 static MethodInfo FetchMethodMoveNext (Type t)
4896 MemberList move_next_list;
4898 move_next_list = TypeContainer.FindMembers (
4899 t, MemberTypes.Method,
4900 BindingFlags.Public | BindingFlags.Instance,
4901 Type.FilterName, "MoveNext");
4902 if (move_next_list.Count == 0)
4905 foreach (MemberInfo m in move_next_list){
4906 MethodInfo mi = (MethodInfo) m;
4909 args = TypeManager.GetArgumentTypes (mi);
4910 if (args != null && args.Length == 0){
4911 if (mi.ReturnType == TypeManager.bool_type)
4919 // Retrieves a `public T get_Current ()' method from the Type `t'
4921 static MethodInfo FetchMethodGetCurrent (Type t)
4923 MemberList move_next_list;
4925 move_next_list = TypeContainer.FindMembers (
4926 t, MemberTypes.Method,
4927 BindingFlags.Public | BindingFlags.Instance,
4928 Type.FilterName, "get_Current");
4929 if (move_next_list.Count == 0)
4932 foreach (MemberInfo m in move_next_list){
4933 MethodInfo mi = (MethodInfo) m;
4936 args = TypeManager.GetArgumentTypes (mi);
4937 if (args != null && args.Length == 0)
4944 // This struct records the helper methods used by the Foreach construct
4946 class ForeachHelperMethods {
4947 public EmitContext ec;
4948 public MethodInfo get_enumerator;
4949 public MethodInfo move_next;
4950 public MethodInfo get_current;
4951 public Type element_type;
4952 public Type enumerator_type;
4953 public bool is_disposable;
4955 public ForeachHelperMethods (EmitContext ec)
4958 this.element_type = TypeManager.object_type;
4959 this.enumerator_type = TypeManager.ienumerator_type;
4960 this.is_disposable = true;
4964 static bool GetEnumeratorFilter (MemberInfo m, object criteria)
4969 if (!(m is MethodInfo))
4972 if (m.Name != "GetEnumerator")
4975 MethodInfo mi = (MethodInfo) m;
4976 Type [] args = TypeManager.GetArgumentTypes (mi);
4978 if (args.Length != 0)
4981 ForeachHelperMethods hm = (ForeachHelperMethods) criteria;
4982 EmitContext ec = hm.ec;
4985 // Check whether GetEnumerator is accessible to us
4987 MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
4989 Type declaring = mi.DeclaringType;
4990 if (prot == MethodAttributes.Private){
4991 if (declaring != ec.ContainerType)
4993 } else if (prot == MethodAttributes.FamANDAssem){
4994 // If from a different assembly, false
4995 if (!(mi is MethodBuilder))
4998 // Are we being invoked from the same class, or from a derived method?
5000 if (ec.ContainerType != declaring){
5001 if (!ec.ContainerType.IsSubclassOf (declaring))
5004 } else if (prot == MethodAttributes.FamORAssem){
5005 if (!(mi is MethodBuilder ||
5006 ec.ContainerType == declaring ||
5007 ec.ContainerType.IsSubclassOf (declaring)))
5009 } if (prot == MethodAttributes.Family){
5010 if (!(ec.ContainerType == declaring ||
5011 ec.ContainerType.IsSubclassOf (declaring)))
5016 // Ok, we can access it, now make sure that we can do something
5017 // with this `GetEnumerator'
5020 if (mi.ReturnType == TypeManager.ienumerator_type ||
5021 TypeManager.ienumerator_type.IsAssignableFrom (mi.ReturnType) ||
5022 (!RootContext.StdLib && TypeManager.ImplementsInterface (mi.ReturnType, TypeManager.ienumerator_type))) {
5023 hm.move_next = TypeManager.bool_movenext_void;
5024 hm.get_current = TypeManager.object_getcurrent_void;
5029 // Ok, so they dont return an IEnumerable, we will have to
5030 // find if they support the GetEnumerator pattern.
5032 Type return_type = mi.ReturnType;
5034 hm.move_next = FetchMethodMoveNext (return_type);
5035 if (hm.move_next == null)
5037 hm.get_current = FetchMethodGetCurrent (return_type);
5038 if (hm.get_current == null)
5041 hm.element_type = hm.get_current.ReturnType;
5042 hm.enumerator_type = return_type;
5043 hm.is_disposable = TypeManager.ImplementsInterface (
5044 hm.enumerator_type, TypeManager.idisposable_type);
5050 /// This filter is used to find the GetEnumerator method
5051 /// on which IEnumerator operates
5053 static MemberFilter FilterEnumerator;
5057 FilterEnumerator = new MemberFilter (GetEnumeratorFilter);
5060 void error1579 (Type t)
5062 Report.Error (1579, loc,
5063 "foreach statement cannot operate on variables of type `" +
5064 t.FullName + "' because that class does not provide a " +
5065 " GetEnumerator method or it is inaccessible");
5068 static bool TryType (Type t, ForeachHelperMethods hm)
5072 mi = TypeContainer.FindMembers (t, MemberTypes.Method,
5073 BindingFlags.Public | BindingFlags.NonPublic |
5074 BindingFlags.Instance,
5075 FilterEnumerator, hm);
5080 hm.get_enumerator = (MethodInfo) mi [0];
5085 // Looks for a usable GetEnumerator in the Type, and if found returns
5086 // the three methods that participate: GetEnumerator, MoveNext and get_Current
5088 ForeachHelperMethods ProbeCollectionType (EmitContext ec, Type t)
5090 ForeachHelperMethods hm = new ForeachHelperMethods (ec);
5092 if (TryType (t, hm))
5096 // Now try to find the method in the interfaces
5099 Type [] ifaces = t.GetInterfaces ();
5101 foreach (Type i in ifaces){
5102 if (TryType (i, hm))
5107 // Since TypeBuilder.GetInterfaces only returns the interface
5108 // types for this type, we have to keep looping, but once
5109 // we hit a non-TypeBuilder (ie, a Type), then we know we are
5110 // done, because it returns all the types
5112 if ((t is TypeBuilder))
5122 // FIXME: possible optimization.
5123 // We might be able to avoid creating `empty' if the type is the sam
5125 bool EmitCollectionForeach (EmitContext ec)
5127 ILGenerator ig = ec.ig;
5128 LocalBuilder enumerator, disposable;
5130 enumerator = ig.DeclareLocal (hm.enumerator_type);
5131 if (hm.is_disposable)
5132 disposable = ig.DeclareLocal (TypeManager.idisposable_type);
5137 // Instantiate the enumerator
5139 if (expr.Type.IsValueType){
5140 if (expr is IMemoryLocation){
5141 IMemoryLocation ml = (IMemoryLocation) expr;
5143 ml.AddressOf (ec, AddressOp.Load);
5145 throw new Exception ("Expr " + expr + " of type " + expr.Type +
5146 " does not implement IMemoryLocation");
5147 ig.Emit (OpCodes.Call, hm.get_enumerator);
5150 ig.Emit (OpCodes.Callvirt, hm.get_enumerator);
5152 ig.Emit (OpCodes.Stloc, enumerator);
5155 // Protect the code in a try/finalize block, so that
5156 // if the beast implement IDisposable, we get rid of it
5159 bool old_in_try = ec.InTry;
5161 if (hm.is_disposable) {
5162 l = ig.BeginExceptionBlock ();
5166 Label end_try = ig.DefineLabel ();
5168 ig.MarkLabel (ec.LoopBegin);
5169 ig.Emit (OpCodes.Ldloc, enumerator);
5170 ig.Emit (OpCodes.Callvirt, hm.move_next);
5171 ig.Emit (OpCodes.Brfalse, end_try);
5172 ig.Emit (OpCodes.Ldloc, enumerator);
5173 ig.Emit (OpCodes.Callvirt, hm.get_current);
5174 variable.EmitAssign (ec, conv);
5175 statement.Emit (ec);
5176 ig.Emit (OpCodes.Br, ec.LoopBegin);
5177 ig.MarkLabel (end_try);
5178 ec.InTry = old_in_try;
5180 // The runtime provides this for us.
5181 // ig.Emit (OpCodes.Leave, end);
5184 // Now the finally block
5186 if (hm.is_disposable) {
5187 Label end_finally = ig.DefineLabel ();
5188 bool old_in_finally = ec.InFinally;
5189 ec.InFinally = true;
5190 ig.BeginFinallyBlock ();
5192 ig.Emit (OpCodes.Ldloc, enumerator);
5193 ig.Emit (OpCodes.Isinst, TypeManager.idisposable_type);
5194 ig.Emit (OpCodes.Stloc, disposable);
5195 ig.Emit (OpCodes.Ldloc, disposable);
5196 ig.Emit (OpCodes.Brfalse, end_finally);
5197 ig.Emit (OpCodes.Ldloc, disposable);
5198 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
5199 ig.MarkLabel (end_finally);
5200 ec.InFinally = old_in_finally;
5202 // The runtime generates this anyways.
5203 // ig.Emit (OpCodes.Endfinally);
5205 ig.EndExceptionBlock ();
5208 ig.MarkLabel (ec.LoopEnd);
5213 // FIXME: possible optimization.
5214 // We might be able to avoid creating `empty' if the type is the sam
5216 bool EmitArrayForeach (EmitContext ec)
5218 int rank = array_type.GetArrayRank ();
5219 ILGenerator ig = ec.ig;
5221 LocalBuilder copy = ig.DeclareLocal (array_type);
5224 // Make our copy of the array
5227 ig.Emit (OpCodes.Stloc, copy);
5230 LocalBuilder counter = ig.DeclareLocal (TypeManager.int32_type);
5234 ig.Emit (OpCodes.Ldc_I4_0);
5235 ig.Emit (OpCodes.Stloc, counter);
5236 test = ig.DefineLabel ();
5237 ig.Emit (OpCodes.Br, test);
5239 loop = ig.DefineLabel ();
5240 ig.MarkLabel (loop);
5242 ig.Emit (OpCodes.Ldloc, copy);
5243 ig.Emit (OpCodes.Ldloc, counter);
5244 ArrayAccess.EmitLoadOpcode (ig, var_type);
5246 variable.EmitAssign (ec, conv);
5248 statement.Emit (ec);
5250 ig.MarkLabel (ec.LoopBegin);
5251 ig.Emit (OpCodes.Ldloc, counter);
5252 ig.Emit (OpCodes.Ldc_I4_1);
5253 ig.Emit (OpCodes.Add);
5254 ig.Emit (OpCodes.Stloc, counter);
5256 ig.MarkLabel (test);
5257 ig.Emit (OpCodes.Ldloc, counter);
5258 ig.Emit (OpCodes.Ldloc, copy);
5259 ig.Emit (OpCodes.Ldlen);
5260 ig.Emit (OpCodes.Conv_I4);
5261 ig.Emit (OpCodes.Blt, loop);
5263 LocalBuilder [] dim_len = new LocalBuilder [rank];
5264 LocalBuilder [] dim_count = new LocalBuilder [rank];
5265 Label [] loop = new Label [rank];
5266 Label [] test = new Label [rank];
5269 for (dim = 0; dim < rank; dim++){
5270 dim_len [dim] = ig.DeclareLocal (TypeManager.int32_type);
5271 dim_count [dim] = ig.DeclareLocal (TypeManager.int32_type);
5272 test [dim] = ig.DefineLabel ();
5273 loop [dim] = ig.DefineLabel ();
5276 for (dim = 0; dim < rank; dim++){
5277 ig.Emit (OpCodes.Ldloc, copy);
5278 IntLiteral.EmitInt (ig, dim);
5279 ig.Emit (OpCodes.Callvirt, TypeManager.int_getlength_int);
5280 ig.Emit (OpCodes.Stloc, dim_len [dim]);
5283 for (dim = 0; dim < rank; dim++){
5284 ig.Emit (OpCodes.Ldc_I4_0);
5285 ig.Emit (OpCodes.Stloc, dim_count [dim]);
5286 ig.Emit (OpCodes.Br, test [dim]);
5287 ig.MarkLabel (loop [dim]);
5290 ig.Emit (OpCodes.Ldloc, copy);
5291 for (dim = 0; dim < rank; dim++)
5292 ig.Emit (OpCodes.Ldloc, dim_count [dim]);
5295 // FIXME: Maybe we can cache the computation of `get'?
5297 Type [] args = new Type [rank];
5300 for (int i = 0; i < rank; i++)
5301 args [i] = TypeManager.int32_type;
5303 ModuleBuilder mb = CodeGen.ModuleBuilder;
5304 get = mb.GetArrayMethod (
5306 CallingConventions.HasThis| CallingConventions.Standard,
5308 ig.Emit (OpCodes.Call, get);
5309 variable.EmitAssign (ec, conv);
5310 statement.Emit (ec);
5311 ig.MarkLabel (ec.LoopBegin);
5312 for (dim = rank - 1; dim >= 0; dim--){
5313 ig.Emit (OpCodes.Ldloc, dim_count [dim]);
5314 ig.Emit (OpCodes.Ldc_I4_1);
5315 ig.Emit (OpCodes.Add);
5316 ig.Emit (OpCodes.Stloc, dim_count [dim]);
5318 ig.MarkLabel (test [dim]);
5319 ig.Emit (OpCodes.Ldloc, dim_count [dim]);
5320 ig.Emit (OpCodes.Ldloc, dim_len [dim]);
5321 ig.Emit (OpCodes.Blt, loop [dim]);
5324 ig.MarkLabel (ec.LoopEnd);
5329 protected override bool DoEmit (EmitContext ec)
5333 ILGenerator ig = ec.ig;
5335 Label old_begin = ec.LoopBegin, old_end = ec.LoopEnd;
5336 bool old_inloop = ec.InLoop;
5337 int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
5338 ec.LoopBegin = ig.DefineLabel ();
5339 ec.LoopEnd = ig.DefineLabel ();
5341 ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
5344 ret_val = EmitCollectionForeach (ec);
5346 ret_val = EmitArrayForeach (ec);
5348 ec.LoopBegin = old_begin;
5349 ec.LoopEnd = old_end;
5350 ec.InLoop = old_inloop;
5351 ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;