/
statement.cs
executable file
·5167 lines (4255 loc) · 121 KB
/
statement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// statement.cs: Statement representation for the IL tree.
//
// Author:
// Miguel de Icaza (miguel@ximian.com)
// Martin Baulig (martin@gnome.org)
//
// (C) 2001, 2002 Ximian, Inc.
//
using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Diagnostics;
namespace Mono.CSharp {
using System.Collections;
public abstract class Statement {
public Location loc;
///
/// Resolves the statement, true means that all sub-statements
/// did resolve ok.
//
public virtual bool Resolve (EmitContext ec)
{
return true;
}
/// <summary>
/// Return value indicates whether all code paths emitted return.
/// </summary>
public abstract bool Emit (EmitContext ec);
public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
{
e = e.Resolve (ec);
if (e == null)
return null;
if (e.Type != TypeManager.bool_type){
e = Expression.ConvertImplicit (ec, e, TypeManager.bool_type,
new Location (-1));
}
if (e == null){
Report.Error (
31, loc, "Can not convert the expression to a boolean");
}
if (CodeGen.SymbolWriter != null)
ec.Mark (loc);
return e;
}
/// <remarks>
/// Encapsulates the emission of a boolean test and jumping to a
/// destination.
///
/// This will emit the bool expression in `bool_expr' and if
/// `target_is_for_true' is true, then the code will generate a
/// brtrue to the target. Otherwise a brfalse.
/// </remarks>
public static void EmitBoolExpression (EmitContext ec, Expression bool_expr,
Label target, bool target_is_for_true)
{
ILGenerator ig = ec.ig;
bool invert = false;
if (bool_expr is Unary){
Unary u = (Unary) bool_expr;
if (u.Oper == Unary.Operator.LogicalNot){
invert = true;
u.EmitLogicalNot (ec);
}
} else if (bool_expr is Binary){
Binary b = (Binary) bool_expr;
if (b.EmitBranchable (ec, target, target_is_for_true))
return;
}
if (!invert)
bool_expr.Emit (ec);
if (target_is_for_true){
if (invert)
ig.Emit (OpCodes.Brfalse, target);
else
ig.Emit (OpCodes.Brtrue, target);
} else {
if (invert)
ig.Emit (OpCodes.Brtrue, target);
else
ig.Emit (OpCodes.Brfalse, target);
}
}
public static void Warning_DeadCodeFound (Location loc)
{
Report.Warning (162, loc, "Unreachable code detected");
}
}
public class EmptyStatement : Statement {
public override bool Resolve (EmitContext ec)
{
return true;
}
public override bool Emit (EmitContext ec)
{
return false;
}
}
public class If : Statement {
Expression expr;
public Statement TrueStatement;
public Statement FalseStatement;
public If (Expression expr, Statement trueStatement, Location l)
{
this.expr = expr;
TrueStatement = trueStatement;
loc = l;
}
public If (Expression expr,
Statement trueStatement,
Statement falseStatement,
Location l)
{
this.expr = expr;
TrueStatement = trueStatement;
FalseStatement = falseStatement;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
Report.Debug (1, "START IF BLOCK", loc);
expr = ResolveBoolean (ec, expr, loc);
if (expr == null){
return false;
}
ec.StartFlowBranching (FlowBranchingType.BLOCK, loc);
if (!TrueStatement.Resolve (ec)) {
ec.KillFlowBranching ();
return false;
}
ec.CurrentBranching.CreateSibling ();
if ((FalseStatement != null) && !FalseStatement.Resolve (ec)) {
ec.KillFlowBranching ();
return false;
}
ec.EndFlowBranching ();
Report.Debug (1, "END IF BLOCK", loc);
return true;
}
public override bool Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
Label false_target = ig.DefineLabel ();
Label end;
bool is_true_ret, is_false_ret;
//
// Dead code elimination
//
if (expr is BoolConstant){
bool take = ((BoolConstant) expr).Value;
if (take){
if (FalseStatement != null){
Warning_DeadCodeFound (FalseStatement.loc);
}
return TrueStatement.Emit (ec);
} else {
Warning_DeadCodeFound (TrueStatement.loc);
if (FalseStatement != null)
return FalseStatement.Emit (ec);
}
}
EmitBoolExpression (ec, expr, false_target, false);
is_true_ret = TrueStatement.Emit (ec);
is_false_ret = is_true_ret;
if (FalseStatement != null){
bool branch_emitted = false;
end = ig.DefineLabel ();
if (!is_true_ret){
ig.Emit (OpCodes.Br, end);
branch_emitted = true;
}
ig.MarkLabel (false_target);
is_false_ret = FalseStatement.Emit (ec);
if (branch_emitted)
ig.MarkLabel (end);
} else {
ig.MarkLabel (false_target);
is_false_ret = false;
}
return is_true_ret && is_false_ret;
}
}
public class Do : Statement {
public Expression expr;
public readonly Statement EmbeddedStatement;
public Do (Statement statement, Expression boolExpr, Location l)
{
expr = boolExpr;
EmbeddedStatement = statement;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
bool ok = true;
ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
if (!EmbeddedStatement.Resolve (ec))
ok = false;
ec.EndFlowBranching ();
expr = ResolveBoolean (ec, expr, loc);
if (expr == null)
ok = false;
return ok;
}
public override bool Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
Label loop = ig.DefineLabel ();
Label old_begin = ec.LoopBegin;
Label old_end = ec.LoopEnd;
bool old_inloop = ec.InLoop;
bool old_breaks = ec.Breaks;
int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
ec.LoopBegin = ig.DefineLabel ();
ec.LoopEnd = ig.DefineLabel ();
ec.InLoop = true;
ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
ig.MarkLabel (loop);
ec.Breaks = false;
EmbeddedStatement.Emit (ec);
bool breaks = ec.Breaks;
ig.MarkLabel (ec.LoopBegin);
//
// Dead code elimination
//
if (expr is BoolConstant){
bool res = ((BoolConstant) expr).Value;
if (res)
ec.ig.Emit (OpCodes.Br, loop);
} else
EmitBoolExpression (ec, expr, loop, true);
ig.MarkLabel (ec.LoopEnd);
ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;
ec.InLoop = old_inloop;
ec.Breaks = old_breaks;
//
// Inform whether we are infinite or not
//
if (expr is BoolConstant){
BoolConstant bc = (BoolConstant) expr;
if (bc.Value == true)
return breaks == false;
}
return false;
}
}
public class While : Statement {
public Expression expr;
public readonly Statement Statement;
public While (Expression boolExpr, Statement statement, Location l)
{
this.expr = boolExpr;
Statement = statement;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
bool ok = true;
expr = ResolveBoolean (ec, expr, loc);
if (expr == null)
return false;
ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
if (!Statement.Resolve (ec))
ok = false;
ec.EndFlowBranching ();
return ok;
}
public override bool Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
Label old_begin = ec.LoopBegin;
Label old_end = ec.LoopEnd;
bool old_inloop = ec.InLoop;
bool old_breaks = ec.Breaks;
Label while_loop = ig.DefineLabel ();
int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
bool ret;
ec.LoopBegin = ig.DefineLabel ();
ec.LoopEnd = ig.DefineLabel ();
ec.InLoop = true;
ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
ig.Emit (OpCodes.Br, ec.LoopBegin);
ig.MarkLabel (while_loop);
//
// Inform whether we are infinite or not
//
if (expr is BoolConstant){
BoolConstant bc = (BoolConstant) expr;
ig.MarkLabel (ec.LoopBegin);
if (bc.Value == false){
Warning_DeadCodeFound (Statement.loc);
ret = false;
} else {
bool breaks;
ec.Breaks = false;
Statement.Emit (ec);
breaks = ec.Breaks;
ig.Emit (OpCodes.Br, ec.LoopBegin);
//
// Inform that we are infinite (ie, `we return'), only
// if we do not `break' inside the code.
//
ret = breaks == false;
}
ig.MarkLabel (ec.LoopEnd);
} else {
Statement.Emit (ec);
ig.MarkLabel (ec.LoopBegin);
EmitBoolExpression (ec, expr, while_loop, true);
ig.MarkLabel (ec.LoopEnd);
ret = false;
}
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;
ec.InLoop = old_inloop;
ec.Breaks = old_breaks;
ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
return ret;
}
}
public class For : Statement {
Expression Test;
readonly Statement InitStatement;
readonly Statement Increment;
readonly Statement Statement;
public For (Statement initStatement,
Expression test,
Statement increment,
Statement statement,
Location l)
{
InitStatement = initStatement;
Test = test;
Increment = increment;
Statement = statement;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
bool ok = true;
if (InitStatement != null){
if (!InitStatement.Resolve (ec))
ok = false;
}
if (Test != null){
Test = ResolveBoolean (ec, Test, loc);
if (Test == null)
ok = false;
}
if (Increment != null){
if (!Increment.Resolve (ec))
ok = false;
}
ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
if (!Statement.Resolve (ec))
ok = false;
ec.EndFlowBranching ();
return ok;
}
public override bool Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
Label old_begin = ec.LoopBegin;
Label old_end = ec.LoopEnd;
bool old_inloop = ec.InLoop;
bool old_breaks = ec.Breaks;
int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
Label loop = ig.DefineLabel ();
Label test = ig.DefineLabel ();
if (InitStatement != null)
if (! (InitStatement is EmptyStatement))
InitStatement.Emit (ec);
ec.LoopBegin = ig.DefineLabel ();
ec.LoopEnd = ig.DefineLabel ();
ec.InLoop = true;
ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
ig.Emit (OpCodes.Br, test);
ig.MarkLabel (loop);
ec.Breaks = false;
Statement.Emit (ec);
bool breaks = ec.Breaks;
ig.MarkLabel (ec.LoopBegin);
if (!(Increment is EmptyStatement))
Increment.Emit (ec);
ig.MarkLabel (test);
//
// If test is null, there is no test, and we are just
// an infinite loop
//
if (Test != null)
EmitBoolExpression (ec, Test, loop, true);
else
ig.Emit (OpCodes.Br, loop);
ig.MarkLabel (ec.LoopEnd);
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;
ec.InLoop = old_inloop;
ec.Breaks = old_breaks;
ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
//
// Inform whether we are infinite or not
//
if (Test != null){
if (Test is BoolConstant){
BoolConstant bc = (BoolConstant) Test;
if (bc.Value)
return breaks == false;
}
return false;
} else
return true;
}
}
public class StatementExpression : Statement {
Expression expr;
public StatementExpression (ExpressionStatement expr, Location l)
{
this.expr = expr;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
expr = (Expression) expr.Resolve (ec);
return expr != null;
}
public override bool Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
if (expr is ExpressionStatement)
((ExpressionStatement) expr).EmitStatement (ec);
else {
expr.Emit (ec);
ig.Emit (OpCodes.Pop);
}
return false;
}
public override string ToString ()
{
return "StatementExpression (" + expr + ")";
}
}
/// <summary>
/// Implements the return statement
/// </summary>
public class Return : Statement {
public Expression Expr;
public Return (Expression expr, Location l)
{
Expr = expr;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
if (Expr != null){
Expr = Expr.Resolve (ec);
if (Expr == null)
return false;
}
FlowBranching.UsageVector vector = ec.CurrentBranching.CurrentUsageVector;
if (ec.CurrentBranching.InTryBlock ())
ec.CurrentBranching.AddFinallyVector (vector);
vector.Returns = FlowReturns.ALWAYS;
return true;
}
public override bool Emit (EmitContext ec)
{
if (ec.InFinally){
Report.Error (157,loc,"Control can not leave the body of the finally block");
return false;
}
if (ec.ReturnType == null){
if (Expr != null){
Report.Error (127, loc, "Return with a value not allowed here");
return true;
}
} else {
if (Expr == null){
Report.Error (126, loc, "An object of type `" +
TypeManager.CSharpName (ec.ReturnType) + "' is " +
"expected for the return statement");
return true;
}
if (Expr.Type != ec.ReturnType)
Expr = Expression.ConvertImplicitRequired (
ec, Expr, ec.ReturnType, loc);
if (Expr == null)
return true;
Expr.Emit (ec);
if (ec.InTry || ec.InCatch)
ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
}
if (ec.InTry || ec.InCatch) {
if (!ec.HasReturnLabel) {
ec.ReturnLabel = ec.ig.DefineLabel ();
ec.HasReturnLabel = true;
}
ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
} else
ec.ig.Emit (OpCodes.Ret);
return true;
}
}
public class Goto : Statement {
string target;
Block block;
LabeledStatement label;
public override bool Resolve (EmitContext ec)
{
label = block.LookupLabel (target);
if (label == null){
Report.Error (
159, loc,
"No such label `" + target + "' in this scope");
return false;
}
// If this is a forward goto.
if (!label.IsDefined)
label.AddUsageVector (ec.CurrentBranching.CurrentUsageVector);
ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
return true;
}
public Goto (Block parent_block, string label, Location l)
{
block = parent_block;
loc = l;
target = label;
}
public string Target {
get {
return target;
}
}
public override bool Emit (EmitContext ec)
{
Label l = label.LabelTarget (ec);
ec.ig.Emit (OpCodes.Br, l);
return false;
}
}
public class LabeledStatement : Statement {
public readonly Location Location;
string label_name;
bool defined;
bool referenced;
Label label;
ArrayList vectors;
public LabeledStatement (string label_name, Location l)
{
this.label_name = label_name;
this.Location = l;
}
public Label LabelTarget (EmitContext ec)
{
if (defined)
return label;
label = ec.ig.DefineLabel ();
defined = true;
return label;
}
public bool IsDefined {
get {
return defined;
}
}
public bool HasBeenReferenced {
get {
return referenced;
}
}
public void AddUsageVector (FlowBranching.UsageVector vector)
{
if (vectors == null)
vectors = new ArrayList ();
vectors.Add (vector.Clone ());
}
public override bool Resolve (EmitContext ec)
{
if (vectors != null)
ec.CurrentBranching.CurrentUsageVector.MergeJumpOrigins (vectors);
referenced = true;
return true;
}
public override bool Emit (EmitContext ec)
{
LabelTarget (ec);
ec.ig.MarkLabel (label);
return false;
}
}
/// <summary>
/// `goto default' statement
/// </summary>
public class GotoDefault : Statement {
public GotoDefault (Location l)
{
loc = l;
}
public override bool Resolve (EmitContext ec)
{
ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.UNREACHABLE;
ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
return true;
}
public override bool Emit (EmitContext ec)
{
if (ec.Switch == null){
Report.Error (153, loc, "goto default is only valid in a switch statement");
return false;
}
if (!ec.Switch.GotDefault){
Report.Error (159, loc, "No default target on switch statement");
return false;
}
ec.ig.Emit (OpCodes.Br, ec.Switch.DefaultTarget);
return false;
}
}
/// <summary>
/// `goto case' statement
/// </summary>
public class GotoCase : Statement {
Expression expr;
Label label;
public GotoCase (Expression e, Location l)
{
expr = e;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
if (ec.Switch == null){
Report.Error (153, loc, "goto case is only valid in a switch statement");
return false;
}
expr = expr.Resolve (ec);
if (expr == null)
return false;
if (!(expr is Constant)){
Report.Error (159, loc, "Target expression for goto case is not constant");
return false;
}
object val = Expression.ConvertIntLiteral (
(Constant) expr, ec.Switch.SwitchType, loc);
if (val == null)
return false;
SwitchLabel sl = (SwitchLabel) ec.Switch.Elements [val];
if (sl == null){
Report.Error (
159, loc,
"No such label 'case " + val + "': for the goto case");
}
label = sl.ILLabelCode;
ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.UNREACHABLE;
ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
return true;
}
public override bool Emit (EmitContext ec)
{
ec.ig.Emit (OpCodes.Br, label);
return true;
}
}
public class Throw : Statement {
Expression expr;
public Throw (Expression expr, Location l)
{
this.expr = expr;
loc = l;
}
public override bool Resolve (EmitContext ec)
{
if (expr != null){
expr = expr.Resolve (ec);
if (expr == null)
return false;
ExprClass eclass = expr.eclass;
if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
expr.Error118 ("value, variable, property or indexer access ");
return false;
}
Type t = expr.Type;
if ((t != TypeManager.exception_type) &&
!t.IsSubclassOf (TypeManager.exception_type) &&
!(expr is NullLiteral)) {
Report.Error (155, loc,
"The type caught or thrown must be derived " +
"from System.Exception");
return false;
}
}
ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.EXCEPTION;
return true;
}
public override bool Emit (EmitContext ec)
{
if (expr == null){
if (ec.InCatch)
ec.ig.Emit (OpCodes.Rethrow);
else {
Report.Error (
156, loc,
"A throw statement with no argument is only " +
"allowed in a catch clause");
}
return false;
}
expr.Emit (ec);
ec.ig.Emit (OpCodes.Throw);
return true;
}
}
public class Break : Statement {
public Break (Location l)
{
loc = l;
}
public override bool Resolve (EmitContext ec)
{
ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
return true;
}
public override bool Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
if (ec.InLoop == false && ec.Switch == null){
Report.Error (139, loc, "No enclosing loop or switch to continue to");
return false;
}
ec.Breaks = true;
if (ec.InTry || ec.InCatch)
ig.Emit (OpCodes.Leave, ec.LoopEnd);
else
ig.Emit (OpCodes.Br, ec.LoopEnd);
return false;
}
}
public class Continue : Statement {
public Continue (Location l)
{
loc = l;
}
public override bool Resolve (EmitContext ec)
{
ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
return true;
}
public override bool Emit (EmitContext ec)
{
Label begin = ec.LoopBegin;
if (!ec.InLoop){
Report.Error (139, loc, "No enclosing loop to continue to");
return false;
}
//
// UGH: Non trivial. This Br might cross a try/catch boundary
// How can we tell?
//
// while () {
// try { ... } catch { continue; }
// }
//
// From:
// try {} catch { while () { continue; }}
//
if (ec.TryCatchLevel > ec.LoopBeginTryCatchLevel)
ec.ig.Emit (OpCodes.Leave, begin);
else if (ec.TryCatchLevel < ec.LoopBeginTryCatchLevel)
throw new Exception ("Should never happen");
else
ec.ig.Emit (OpCodes.Br, begin);
return false;
}
}
// <summary>
// This is used in the control flow analysis code to specify whether the
// current code block may return to its enclosing block before reaching
// its end.
// </summary>
public enum FlowReturns {
// It can never return.
NEVER,
// This means that the block contains a conditional return statement
// somewhere.
SOMETIMES,
// The code always returns, ie. there's an unconditional return / break
// statement in it.
ALWAYS,
// The code always throws an exception.
EXCEPTION,
// The current code block is unreachable. This happens if it's immediately
// following a FlowReturns.ALWAYS block.
UNREACHABLE
}
// <summary>
// This is a special bit vector which can inherit from another bit vector doing a
// copy-on-write strategy. The inherited vector may have a smaller size than the
// current one.
// </summary>
public class MyBitVector {
public readonly int Count;
public readonly MyBitVector InheritsFrom;
bool is_dirty;