9a3121ecae27fc9f2cd6ecef0fde5f0eaae55588
[mono.git] / mcs / mcs / statement.cs
1 //
2 // statement.cs: Statement representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Martin Baulig (martin@ximian.com)
7 //   Marek Safar (marek.safar@seznam.cz)
8 //
9 // (C) 2001, 2002, 2003 Ximian, Inc.
10 // (C) 2003, 2004 Novell, Inc.
11 //
12
13 using System;
14 using System.Text;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.Diagnostics;
18 using System.Collections;
19 using System.Collections.Specialized;
20
21 namespace Mono.CSharp {
22         
23         public abstract class Statement {
24                 public Location loc;
25                 
26                 /// <summary>
27                 ///   Resolves the statement, true means that all sub-statements
28                 ///   did resolve ok.
29                 //  </summary>
30                 public virtual bool Resolve (EmitContext ec)
31                 {
32                         return true;
33                 }
34
35                 /// <summary>
36                 ///   We already know that the statement is unreachable, but we still
37                 ///   need to resolve it to catch errors.
38                 /// </summary>
39                 public virtual bool ResolveUnreachable (EmitContext ec, bool warn)
40                 {
41                         //
42                         // This conflicts with csc's way of doing this, but IMHO it's
43                         // the right thing to do.
44                         //
45                         // If something is unreachable, we still check whether it's
46                         // correct.  This means that you cannot use unassigned variables
47                         // in unreachable code, for instance.
48                         //
49
50                         if (warn)
51                                 Report.Warning (162, 2, loc, "Unreachable code detected");
52
53                         ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
54                         bool ok = Resolve (ec);
55                         ec.KillFlowBranching ();
56
57                         return ok;
58                 }
59                                 
60                 /// <summary>
61                 ///   Return value indicates whether all code paths emitted return.
62                 /// </summary>
63                 protected abstract void DoEmit (EmitContext ec);
64
65                 /// <summary>
66                 ///   Utility wrapper routine for Error, just to beautify the code
67                 /// </summary>
68                 public void Error (int error, string format, params object[] args)
69                 {
70                         Error (error, String.Format (format, args));
71                 }
72
73                 public void Error (int error, string s)
74                 {
75                         if (!loc.IsNull)
76                                 Report.Error (error, loc, s);
77                         else
78                                 Report.Error (error, s);
79                 }
80
81                 /// <summary>
82                 ///   Return value indicates whether all code paths emitted return.
83                 /// </summary>
84                 public virtual void Emit (EmitContext ec)
85                 {
86                         ec.Mark (loc, true);
87                         DoEmit (ec);
88                 }
89
90                 //
91                 // This routine must be overrided in derived classes and make copies
92                 // of all the data that might be modified if resolved
93                 // 
94                 protected virtual void CloneTo (CloneContext clonectx, Statement target)
95                 {
96                         throw new InternalErrorException ("{0} does not implement Statement.CloneTo", this.GetType ());
97                 }
98
99                 public Statement Clone (CloneContext clonectx)
100                 {
101                         Statement s = (Statement) this.MemberwiseClone ();
102                         CloneTo (clonectx, s);
103                         return s;
104                 }
105
106                 public virtual Expression CreateExpressionTree (EmitContext ec)
107                 {
108                         Report.Error (834, loc, "A lambda expression with statement body cannot be converted to an expresion tree");
109                         return null;
110                 }
111
112                 public Statement PerformClone ()
113                 {
114                         CloneContext clonectx = new CloneContext ();
115
116                         return Clone (clonectx);
117                 }
118
119         }
120
121         //
122         // This class is used during the Statement.Clone operation
123         // to remap objects that have been cloned.
124         //
125         // Since blocks are cloned by Block.Clone, we need a way for
126         // expressions that must reference the block to be cloned
127         // pointing to the new cloned block.
128         //
129         public class CloneContext {
130                 Hashtable block_map = new Hashtable ();
131                 Hashtable variable_map;
132                 
133                 public void AddBlockMap (Block from, Block to)
134                 {
135                         if (block_map.Contains (from))
136                                 return;
137                         block_map [from] = to;
138                 }
139                 
140                 public Block LookupBlock (Block from)
141                 {
142                         Block result = (Block) block_map [from];
143
144                         if (result == null){
145                                 result = (Block) from.Clone (this);
146                                 block_map [from] = result;
147                         }
148
149                         return result;
150                 }
151
152                 ///
153                 /// Remaps block to cloned copy if one exists.
154                 ///
155                 public Block RemapBlockCopy (Block from)
156                 {
157                         Block mapped_to = (Block)block_map[from];
158                         if (mapped_to == null)
159                                 return from;
160
161                         return mapped_to;
162                 }
163
164                 public void AddVariableMap (LocalInfo from, LocalInfo to)
165                 {
166                         if (variable_map == null)
167                                 variable_map = new Hashtable ();
168                         
169                         if (variable_map.Contains (from))
170                                 return;
171                         variable_map [from] = to;
172                 }
173                 
174                 public LocalInfo LookupVariable (LocalInfo from)
175                 {
176                         LocalInfo result = (LocalInfo) variable_map [from];
177
178                         if (result == null)
179                                 throw new Exception ("LookupVariable: looking up a variable that has not been registered yet");
180
181                         return result;
182                 }
183         }
184         
185         public sealed class EmptyStatement : Statement {
186                 
187                 private EmptyStatement () {}
188                 
189                 public static readonly EmptyStatement Value = new EmptyStatement ();
190                 
191                 public override bool Resolve (EmitContext ec)
192                 {
193                         return true;
194                 }
195
196                 public override bool ResolveUnreachable (EmitContext ec, bool warn)
197                 {
198                         return true;
199                 }
200
201                 protected override void DoEmit (EmitContext ec)
202                 {
203                 }
204
205                 protected override void CloneTo (CloneContext clonectx, Statement target)
206                 {
207                         // nothing needed.
208                 }
209         }
210         
211         public class If : Statement {
212                 Expression expr;
213                 public Statement TrueStatement;
214                 public Statement FalseStatement;
215
216                 bool is_true_ret;
217                 
218                 public If (Expression expr, Statement true_statement, Location l)
219                 {
220                         this.expr = expr;
221                         TrueStatement = true_statement;
222                         loc = l;
223                 }
224
225                 public If (Expression expr,
226                            Statement true_statement,
227                            Statement false_statement,
228                            Location l)
229                 {
230                         this.expr = expr;
231                         TrueStatement = true_statement;
232                         FalseStatement = false_statement;
233                         loc = l;
234                 }
235
236                 public override bool Resolve (EmitContext ec)
237                 {
238                         bool ok = true;
239
240                         Report.Debug (1, "START IF BLOCK", loc);
241
242                         expr = Expression.ResolveBoolean (ec, expr, loc);
243                         if (expr == null){
244                                 ok = false;
245                                 goto skip;
246                         }
247
248                         Assign ass = expr as Assign;
249                         if (ass != null && ass.Source is Constant) {
250                                 Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
251                         }
252
253                         //
254                         // Dead code elimination
255                         //
256                         if (expr is Constant){
257                                 bool take = !((Constant) expr).IsDefaultValue;
258
259                                 if (take){
260                                         if (!TrueStatement.Resolve (ec))
261                                                 return false;
262
263                                         if ((FalseStatement != null) &&
264                                             !FalseStatement.ResolveUnreachable (ec, true))
265                                                 return false;
266                                         FalseStatement = null;
267                                 } else {
268                                         if (!TrueStatement.ResolveUnreachable (ec, true))
269                                                 return false;
270                                         TrueStatement = null;
271
272                                         if ((FalseStatement != null) &&
273                                             !FalseStatement.Resolve (ec))
274                                                 return false;
275                                 }
276
277                                 return true;
278                         }
279                 skip:
280                         ec.StartFlowBranching (FlowBranching.BranchingType.Conditional, loc);
281                         
282                         ok &= TrueStatement.Resolve (ec);
283
284                         is_true_ret = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
285
286                         ec.CurrentBranching.CreateSibling ();
287
288                         if (FalseStatement != null)
289                                 ok &= FalseStatement.Resolve (ec);
290                                         
291                         ec.EndFlowBranching ();
292
293                         Report.Debug (1, "END IF BLOCK", loc);
294
295                         return ok;
296                 }
297                 
298                 protected override void DoEmit (EmitContext ec)
299                 {
300                         ILGenerator ig = ec.ig;
301                         Label false_target = ig.DefineLabel ();
302                         Label end;
303
304                         //
305                         // If we're a boolean constant, Resolve() already
306                         // eliminated dead code for us.
307                         //
308                         if (expr is Constant){
309                                 
310                                 //
311                                 // Simple bool constant
312                                 //
313                                 if (expr is BoolConstant) {
314                                         bool take = ((BoolConstant) expr).Value;
315
316                                         if (take)
317                                                 TrueStatement.Emit (ec);
318                                         else if (FalseStatement != null)
319                                                 FalseStatement.Emit (ec);
320                                         
321                                         return;
322                                 }
323
324                                 //
325                                 // Bool constant with side-effects
326                                 //
327                                 expr.Emit (ec);
328                                 ig.Emit (OpCodes.Pop);
329
330                                 if (TrueStatement != null)
331                                         TrueStatement.Emit (ec);
332                                 if (FalseStatement != null)
333                                         FalseStatement.Emit (ec);
334
335                                 return;
336                         }
337                         
338                         expr.EmitBranchable (ec, false_target, false);
339                         
340                         TrueStatement.Emit (ec);
341
342                         if (FalseStatement != null){
343                                 bool branch_emitted = false;
344                                 
345                                 end = ig.DefineLabel ();
346                                 if (!is_true_ret){
347                                         ig.Emit (OpCodes.Br, end);
348                                         branch_emitted = true;
349                                 }
350
351                                 ig.MarkLabel (false_target);
352                                 FalseStatement.Emit (ec);
353
354                                 if (branch_emitted)
355                                         ig.MarkLabel (end);
356                         } else {
357                                 ig.MarkLabel (false_target);
358                         }
359                 }
360
361                 protected override void CloneTo (CloneContext clonectx, Statement t)
362                 {
363                         If target = (If) t;
364
365                         target.expr = expr.Clone (clonectx);
366                         target.TrueStatement = TrueStatement.Clone (clonectx);
367                         if (FalseStatement != null)
368                                 target.FalseStatement = FalseStatement.Clone (clonectx);
369                 }
370         }
371
372         public class Do : Statement {
373                 public Expression expr;
374                 public Statement  EmbeddedStatement;
375                 bool infinite;
376                 
377                 public Do (Statement statement, Expression bool_expr, Location l)
378                 {
379                         expr = bool_expr;
380                         EmbeddedStatement = statement;
381                         loc = l;
382                 }
383
384                 public override bool Resolve (EmitContext ec)
385                 {
386                         bool ok = true;
387
388                         ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
389
390                         bool was_unreachable = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
391
392                         ec.StartFlowBranching (FlowBranching.BranchingType.Embedded, loc);
393                         if (!EmbeddedStatement.Resolve (ec))
394                                 ok = false;
395                         ec.EndFlowBranching ();
396
397                         if (ec.CurrentBranching.CurrentUsageVector.IsUnreachable && !was_unreachable)
398                                 Report.Warning (162, 2, expr.Location, "Unreachable code detected");
399
400                         expr = Expression.ResolveBoolean (ec, expr, loc);
401                         if (expr == null)
402                                 ok = false;
403                         else if (expr is BoolConstant){
404                                 bool res = ((BoolConstant) expr).Value;
405
406                                 if (res)
407                                         infinite = true;
408                         }
409                         if (infinite)
410                                 ec.CurrentBranching.CurrentUsageVector.Goto ();
411
412                         ec.EndFlowBranching ();
413
414                         return ok;
415                 }
416                 
417                 protected override void DoEmit (EmitContext ec)
418                 {
419                         ILGenerator ig = ec.ig;
420                         Label loop = ig.DefineLabel ();
421                         Label old_begin = ec.LoopBegin;
422                         Label old_end = ec.LoopEnd;
423                         
424                         ec.LoopBegin = ig.DefineLabel ();
425                         ec.LoopEnd = ig.DefineLabel ();
426                                 
427                         ig.MarkLabel (loop);
428                         EmbeddedStatement.Emit (ec);
429                         ig.MarkLabel (ec.LoopBegin);
430
431                         //
432                         // Dead code elimination
433                         //
434                         if (expr is BoolConstant){
435                                 bool res = ((BoolConstant) expr).Value;
436
437                                 if (res)
438                                         ec.ig.Emit (OpCodes.Br, loop); 
439                         } else
440                                 expr.EmitBranchable (ec, loop, true);
441                         
442                         ig.MarkLabel (ec.LoopEnd);
443
444                         ec.LoopBegin = old_begin;
445                         ec.LoopEnd = old_end;
446                 }
447
448                 protected override void CloneTo (CloneContext clonectx, Statement t)
449                 {
450                         Do target = (Do) t;
451
452                         target.EmbeddedStatement = EmbeddedStatement.Clone (clonectx);
453                         target.expr = expr.Clone (clonectx);
454                 }
455         }
456
457         public class While : Statement {
458                 public Expression expr;
459                 public Statement Statement;
460                 bool infinite, empty;
461                 
462                 public While (Expression bool_expr, Statement statement, Location l)
463                 {
464                         this.expr = bool_expr;
465                         Statement = statement;
466                         loc = l;
467                 }
468
469                 public override bool Resolve (EmitContext ec)
470                 {
471                         bool ok = true;
472
473                         expr = Expression.ResolveBoolean (ec, expr, loc);
474                         if (expr == null)
475                                 return false;
476
477                         //
478                         // Inform whether we are infinite or not
479                         //
480                         if (expr is BoolConstant){
481                                 BoolConstant bc = (BoolConstant) expr;
482
483                                 if (bc.Value == false){
484                                         if (!Statement.ResolveUnreachable (ec, true))
485                                                 return false;
486                                         empty = true;
487                                         return true;
488                                 } else
489                                         infinite = true;
490                         }
491
492                         ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
493                         if (!infinite)
494                                 ec.CurrentBranching.CreateSibling ();
495
496                         ec.StartFlowBranching (FlowBranching.BranchingType.Embedded, loc);
497                         if (!Statement.Resolve (ec))
498                                 ok = false;
499                         ec.EndFlowBranching ();
500
501                         // There's no direct control flow from the end of the embedded statement to the end of the loop
502                         ec.CurrentBranching.CurrentUsageVector.Goto ();
503
504                         ec.EndFlowBranching ();
505
506                         return ok;
507                 }
508                 
509                 protected override void DoEmit (EmitContext ec)
510                 {
511                         if (empty)
512                                 return;
513
514                         ILGenerator ig = ec.ig;
515                         Label old_begin = ec.LoopBegin;
516                         Label old_end = ec.LoopEnd;
517                         
518                         ec.LoopBegin = ig.DefineLabel ();
519                         ec.LoopEnd = ig.DefineLabel ();
520
521                         //
522                         // Inform whether we are infinite or not
523                         //
524                         if (expr is BoolConstant){
525                                 ig.MarkLabel (ec.LoopBegin);
526                                 Statement.Emit (ec);
527                                 ig.Emit (OpCodes.Br, ec.LoopBegin);
528                                         
529                                 //
530                                 // Inform that we are infinite (ie, `we return'), only
531                                 // if we do not `break' inside the code.
532                                 //
533                                 ig.MarkLabel (ec.LoopEnd);
534                         } else {
535                                 Label while_loop = ig.DefineLabel ();
536
537                                 ig.Emit (OpCodes.Br, ec.LoopBegin);
538                                 ig.MarkLabel (while_loop);
539
540                                 Statement.Emit (ec);
541                         
542                                 ig.MarkLabel (ec.LoopBegin);
543                                 ec.Mark (loc, true);
544
545                                 expr.EmitBranchable (ec, while_loop, true);
546                                 
547                                 ig.MarkLabel (ec.LoopEnd);
548                         }       
549
550                         ec.LoopBegin = old_begin;
551                         ec.LoopEnd = old_end;
552                 }
553
554                 public override void Emit (EmitContext ec)
555                 {
556                         DoEmit (ec);
557                 }
558
559                 protected override void CloneTo (CloneContext clonectx, Statement t)
560                 {
561                         While target = (While) t;
562
563                         target.expr = expr.Clone (clonectx);
564                         target.Statement = Statement.Clone (clonectx);
565                 }
566         }
567
568         public class For : Statement {
569                 Expression Test;
570                 Statement InitStatement;
571                 Statement Increment;
572                 public Statement Statement;
573                 bool infinite, empty;
574                 
575                 public For (Statement init_statement,
576                             Expression test,
577                             Statement increment,
578                             Statement statement,
579                             Location l)
580                 {
581                         InitStatement = init_statement;
582                         Test = test;
583                         Increment = increment;
584                         Statement = statement;
585                         loc = l;
586                 }
587
588                 public override bool Resolve (EmitContext ec)
589                 {
590                         bool ok = true;
591
592                         if (InitStatement != null){
593                                 if (!InitStatement.Resolve (ec))
594                                         ok = false;
595                         }
596
597                         if (Test != null){
598                                 Test = Expression.ResolveBoolean (ec, Test, loc);
599                                 if (Test == null)
600                                         ok = false;
601                                 else if (Test is BoolConstant){
602                                         BoolConstant bc = (BoolConstant) Test;
603
604                                         if (bc.Value == false){
605                                                 if (!Statement.ResolveUnreachable (ec, true))
606                                                         return false;
607                                                 if ((Increment != null) &&
608                                                     !Increment.ResolveUnreachable (ec, false))
609                                                         return false;
610                                                 empty = true;
611                                                 return true;
612                                         } else
613                                                 infinite = true;
614                                 }
615                         } else
616                                 infinite = true;
617
618                         ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
619                         if (!infinite)
620                                 ec.CurrentBranching.CreateSibling ();
621
622                         bool was_unreachable = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
623
624                         ec.StartFlowBranching (FlowBranching.BranchingType.Embedded, loc);
625                         if (!Statement.Resolve (ec))
626                                 ok = false;
627                         ec.EndFlowBranching ();
628
629                         if (Increment != null){
630                                 if (ec.CurrentBranching.CurrentUsageVector.IsUnreachable) {
631                                         if (!Increment.ResolveUnreachable (ec, !was_unreachable))
632                                                 ok = false;
633                                 } else {
634                                         if (!Increment.Resolve (ec))
635                                                 ok = false;
636                                 }
637                         }
638
639                         // There's no direct control flow from the end of the embedded statement to the end of the loop
640                         ec.CurrentBranching.CurrentUsageVector.Goto ();
641
642                         ec.EndFlowBranching ();
643
644                         return ok;
645                 }
646                 
647                 protected override void DoEmit (EmitContext ec)
648                 {
649                         if (empty)
650                                 return;
651
652                         ILGenerator ig = ec.ig;
653                         Label old_begin = ec.LoopBegin;
654                         Label old_end = ec.LoopEnd;
655                         Label loop = ig.DefineLabel ();
656                         Label test = ig.DefineLabel ();
657                         
658                         if (InitStatement != null && InitStatement != EmptyStatement.Value)
659                                 InitStatement.Emit (ec);
660
661                         ec.LoopBegin = ig.DefineLabel ();
662                         ec.LoopEnd = ig.DefineLabel ();
663
664                         ig.Emit (OpCodes.Br, test);
665                         ig.MarkLabel (loop);
666                         Statement.Emit (ec);
667
668                         ig.MarkLabel (ec.LoopBegin);
669                         if (Increment != EmptyStatement.Value)
670                                 Increment.Emit (ec);
671
672                         ig.MarkLabel (test);
673                         //
674                         // If test is null, there is no test, and we are just
675                         // an infinite loop
676                         //
677                         if (Test != null){
678                                 //
679                                 // The Resolve code already catches the case for
680                                 // Test == BoolConstant (false) so we know that
681                                 // this is true
682                                 //
683                                 if (Test is BoolConstant)
684                                         ig.Emit (OpCodes.Br, loop);
685                                 else
686                                         Test.EmitBranchable (ec, loop, true);
687                                 
688                         } else
689                                 ig.Emit (OpCodes.Br, loop);
690                         ig.MarkLabel (ec.LoopEnd);
691
692                         ec.LoopBegin = old_begin;
693                         ec.LoopEnd = old_end;
694                 }
695
696                 protected override void CloneTo (CloneContext clonectx, Statement t)
697                 {
698                         For target = (For) t;
699
700                         if (InitStatement != null)
701                                 target.InitStatement = InitStatement.Clone (clonectx);
702                         if (Test != null)
703                                 target.Test = Test.Clone (clonectx);
704                         if (Increment != null)
705                                 target.Increment = Increment.Clone (clonectx);
706                         target.Statement = Statement.Clone (clonectx);
707                 }
708         }
709         
710         public class StatementExpression : Statement {
711                 ExpressionStatement expr;
712                 
713                 public StatementExpression (ExpressionStatement expr)
714                 {
715                         this.expr = expr;
716                         loc = expr.Location;
717                 }
718
719                 public override bool Resolve (EmitContext ec)
720                 {
721                         if (expr != null)
722                                 expr = expr.ResolveStatement (ec);
723                         return expr != null;
724                 }
725                 
726                 protected override void DoEmit (EmitContext ec)
727                 {
728                         expr.EmitStatement (ec);
729                 }
730
731                 public override string ToString ()
732                 {
733                         return "StatementExpression (" + expr + ")";
734                 }
735
736                 protected override void CloneTo (CloneContext clonectx, Statement t)
737                 {
738                         StatementExpression target = (StatementExpression) t;
739
740                         target.expr = (ExpressionStatement) expr.Clone (clonectx);
741                 }
742         }
743
744         /// <summary>
745         ///   Implements the return statement
746         /// </summary>
747         public class Return : Statement {
748                 protected Expression Expr;
749                 bool unwind_protect;
750                 
751                 public Return (Expression expr, Location l)
752                 {
753                         Expr = expr;
754                         loc = l;
755                 }
756                 
757                 bool DoResolve (EmitContext ec)
758                 {
759                         if (Expr == null) {
760                                 if (ec.ReturnType == TypeManager.void_type)
761                                         return true;
762                                 
763                                 Error (126, "An object of a type convertible to `{0}' is required " +
764                                            "for the return statement",
765                                            TypeManager.CSharpName (ec.ReturnType));
766                                 return false;
767                         }
768
769                         AnonymousContainer am = ec.CurrentAnonymousMethod;
770                         if ((am != null) && am.IsIterator && ec.InIterator) {
771                                 Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
772                                                   "statement to return a value, or yield break to end the iteration");
773                         }
774
775                         if (am == null && ec.ReturnType == TypeManager.void_type) {
776                                 MemberCore mc = ec.ResolveContext as MemberCore;
777                                 Report.Error (127, loc, "`{0}': A return keyword must not be followed by any expression when method returns void",
778                                         mc.GetSignatureForError ());
779                         }
780
781                         Expr = Expr.Resolve (ec);
782                         if (Expr == null)
783                                 return false;
784
785                         if (Expr.Type != ec.ReturnType) {
786                                 if (ec.InferReturnType) {
787                                         //
788                                         // void cannot be used in contextual return
789                                         //
790                                         if (Expr.Type == TypeManager.void_type)
791                                                 return false;
792
793                                         ec.ReturnType = Expr.Type;
794                                 } else {
795                                         Expr = Convert.ImplicitConversionRequired (
796                                                 ec, Expr, ec.ReturnType, loc);
797
798                                         if (Expr == null) {
799                                                 if (am != null) {
800                                                         Report.Error (1662, loc,
801                                                                 "Cannot convert `{0}' to delegate type `{1}' because some of the return types in the block are not implicitly convertible to the delegate return type",
802                                                                 am.ContainerType, am.GetSignatureForError ());
803                                                 }
804                                                 return false;
805                                         }
806                                 }
807                         }
808
809                         return true;                    
810                 }
811
812                 public override bool Resolve (EmitContext ec)
813                 {
814                         if (!DoResolve (ec))
815                                 return false;
816                         
817                         unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
818                         if (unwind_protect)
819                                 ec.NeedReturnLabel ();
820                         ec.CurrentBranching.CurrentUsageVector.Goto ();
821                         return true;
822                 }
823                 
824                 protected override void DoEmit (EmitContext ec)
825                 {
826                         if (Expr != null) {
827                                 Expr.Emit (ec);
828
829                                 if (unwind_protect)
830                                         ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
831                         }
832
833                         if (unwind_protect)
834                                 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
835                         else
836                                 ec.ig.Emit (OpCodes.Ret);
837                 }
838
839                 protected override void CloneTo (CloneContext clonectx, Statement t)
840                 {
841                         Return target = (Return) t;
842                         // It's null for simple return;
843                         if (Expr != null)
844                                 target.Expr = Expr.Clone (clonectx);
845                 }
846         }
847
848         public class Goto : Statement {
849                 string target;
850                 LabeledStatement label;
851                 bool unwind_protect;
852                 
853                 public override bool Resolve (EmitContext ec)
854                 {
855                         int errors = Report.Errors;
856                         unwind_protect = ec.CurrentBranching.AddGotoOrigin (ec.CurrentBranching.CurrentUsageVector, this);
857                         ec.CurrentBranching.CurrentUsageVector.Goto ();
858                         return errors == Report.Errors;
859                 }
860                 
861                 public Goto (string label, Location l)
862                 {
863                         loc = l;
864                         target = label;
865                 }
866
867                 public string Target {
868                         get { return target; }
869                 }
870
871                 public void SetResolvedTarget (LabeledStatement label)
872                 {
873                         this.label = label;
874                         label.AddReference ();
875                 }
876
877                 protected override void DoEmit (EmitContext ec)
878                 {
879                         if (label == null)
880                                 throw new InternalErrorException ("goto emitted before target resolved");
881                         Label l = label.LabelTarget (ec);
882                         ec.ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, l);
883                 }
884         }
885
886         public class LabeledStatement : Statement {
887                 string name;
888                 bool defined;
889                 bool referenced;
890                 Label label;
891                 ILGenerator ig;
892
893                 FlowBranching.UsageVector vectors;
894                 
895                 public LabeledStatement (string name, Location l)
896                 {
897                         this.name = name;
898                         this.loc = l;
899                 }
900
901                 public Label LabelTarget (EmitContext ec)
902                 {
903                         if (defined)
904                                 return label;
905                         ig = ec.ig;
906                         label = ec.ig.DefineLabel ();
907                         defined = true;
908
909                         return label;
910                 }
911
912                 public string Name {
913                         get { return name; }
914                 }
915
916                 public bool IsDefined {
917                         get { return defined; }
918                 }
919
920                 public bool HasBeenReferenced {
921                         get { return referenced; }
922                 }
923
924                 public FlowBranching.UsageVector JumpOrigins {
925                         get { return vectors; }
926                 }
927
928                 public void AddUsageVector (FlowBranching.UsageVector vector)
929                 {
930                         vector = vector.Clone ();
931                         vector.Next = vectors;
932                         vectors = vector;
933                 }
934
935                 public override bool Resolve (EmitContext ec)
936                 {
937                         // this flow-branching will be terminated when the surrounding block ends
938                         ec.StartFlowBranching (this);
939                         return true;
940                 }
941
942                 protected override void DoEmit (EmitContext ec)
943                 {
944                         if (ig != null && ig != ec.ig)
945                                 throw new InternalErrorException ("cannot happen");
946                         LabelTarget (ec);
947                         ec.ig.MarkLabel (label);
948                 }
949
950                 public void AddReference ()
951                 {
952                         referenced = true;
953                 }
954         }
955         
956
957         /// <summary>
958         ///   `goto default' statement
959         /// </summary>
960         public class GotoDefault : Statement {
961                 
962                 public GotoDefault (Location l)
963                 {
964                         loc = l;
965                 }
966
967                 public override bool Resolve (EmitContext ec)
968                 {
969                         ec.CurrentBranching.CurrentUsageVector.Goto ();
970                         return true;
971                 }
972
973                 protected override void DoEmit (EmitContext ec)
974                 {
975                         if (ec.Switch == null){
976                                 Report.Error (153, loc, "A goto case is only valid inside a switch statement");
977                                 return;
978                         }
979
980                         if (!ec.Switch.GotDefault){
981                                 FlowBranchingBlock.Error_UnknownLabel (loc, "default");
982                                 return;
983                         }
984                         ec.ig.Emit (OpCodes.Br, ec.Switch.DefaultTarget);
985                 }
986         }
987
988         /// <summary>
989         ///   `goto case' statement
990         /// </summary>
991         public class GotoCase : Statement {
992                 Expression expr;
993                 SwitchLabel sl;
994                 
995                 public GotoCase (Expression e, Location l)
996                 {
997                         expr = e;
998                         loc = l;
999                 }
1000
1001                 public override bool Resolve (EmitContext ec)
1002                 {
1003                         if (ec.Switch == null){
1004                                 Report.Error (153, loc, "A goto case is only valid inside a switch statement");
1005                                 return false;
1006                         }
1007
1008                         expr = expr.Resolve (ec);
1009                         if (expr == null)
1010                                 return false;
1011
1012                         Constant c = expr as Constant;
1013                         if (c == null) {
1014                                 Error (150, "A constant value is expected");
1015                                 return false;
1016                         }
1017
1018                         Type type = ec.Switch.SwitchType;
1019                         if (!Convert.ImplicitStandardConversionExists (c, type))
1020                                 Report.Warning (469, 2, loc, "The `goto case' value is not implicitly " +
1021                                                 "convertible to type `{0}'", TypeManager.CSharpName (type));
1022
1023                         bool fail = false;
1024                         object val = c.GetValue ();
1025                         if ((val != null) && (c.Type != type) && (c.Type != TypeManager.object_type))
1026                                 val = TypeManager.ChangeType (val, type, out fail);
1027
1028                         if (fail) {
1029                                 Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
1030                                               c.GetSignatureForError (), TypeManager.CSharpName (type));
1031                                 return false;
1032                         }
1033
1034                         if (val == null)
1035                                 val = SwitchLabel.NullStringCase;
1036                                         
1037                         sl = (SwitchLabel) ec.Switch.Elements [val];
1038
1039                         if (sl == null){
1040                                 FlowBranchingBlock.Error_UnknownLabel (loc, "case " + 
1041                                         (c.GetValue () == null ? "null" : val.ToString ()));
1042                                 return false;
1043                         }
1044
1045                         ec.CurrentBranching.CurrentUsageVector.Goto ();
1046                         return true;
1047                 }
1048
1049                 protected override void DoEmit (EmitContext ec)
1050                 {
1051                         ec.ig.Emit (OpCodes.Br, sl.GetILLabelCode (ec));
1052                 }
1053
1054                 protected override void CloneTo (CloneContext clonectx, Statement t)
1055                 {
1056                         GotoCase target = (GotoCase) t;
1057
1058                         target.expr = expr.Clone (clonectx);
1059                         target.sl = sl.Clone (clonectx);
1060                 }
1061         }
1062         
1063         public class Throw : Statement {
1064                 Expression expr;
1065                 
1066                 public Throw (Expression expr, Location l)
1067                 {
1068                         this.expr = expr;
1069                         loc = l;
1070                 }
1071
1072                 public override bool Resolve (EmitContext ec)
1073                 {
1074                         ec.CurrentBranching.CurrentUsageVector.Goto ();
1075
1076                         if (expr != null){
1077                                 expr = expr.Resolve (ec);
1078                                 if (expr == null)
1079                                         return false;
1080
1081                                 ExprClass eclass = expr.eclass;
1082
1083                                 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
1084                                         eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
1085                                         expr.Error_UnexpectedKind (ec.DeclContainer, "value, variable, property or indexer access ", loc);
1086                                         return false;
1087                                 }
1088
1089                                 Type t = expr.Type;
1090                                 
1091                                 if ((t != TypeManager.exception_type) &&
1092                                     !TypeManager.IsSubclassOf (t, TypeManager.exception_type) &&
1093                                     !(expr is NullLiteral)) {
1094                                         Error (155,
1095                                                 "The type caught or thrown must be derived " +
1096                                                 "from System.Exception");
1097                                         return false;
1098                                 }
1099                                 return true;
1100                         }
1101
1102                         if (!ec.InCatch) {
1103                                 Error (156, "A throw statement with no arguments is not allowed outside of a catch clause");
1104                                 return false;
1105                         }
1106
1107                         if (ec.InFinally) {
1108                                 Error (724, "A throw statement with no arguments is not allowed inside of a finally clause nested inside of the innermost catch clause");
1109                                 return false;
1110                         }
1111                         return true;
1112                 }
1113                         
1114                 protected override void DoEmit (EmitContext ec)
1115                 {
1116                         if (expr == null)
1117                                 ec.ig.Emit (OpCodes.Rethrow);
1118                         else {
1119                                 expr.Emit (ec);
1120
1121                                 ec.ig.Emit (OpCodes.Throw);
1122                         }
1123                 }
1124
1125                 protected override void CloneTo (CloneContext clonectx, Statement t)
1126                 {
1127                         Throw target = (Throw) t;
1128
1129                         if (expr != null)
1130                                 target.expr = expr.Clone (clonectx);
1131                 }
1132         }
1133
1134         public class Break : Statement {
1135                 
1136                 public Break (Location l)
1137                 {
1138                         loc = l;
1139                 }
1140
1141                 bool unwind_protect;
1142
1143                 public override bool Resolve (EmitContext ec)
1144                 {
1145                         int errors = Report.Errors;
1146                         unwind_protect = ec.CurrentBranching.AddBreakOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
1147                         ec.CurrentBranching.CurrentUsageVector.Goto ();
1148                         return errors == Report.Errors;
1149                 }
1150
1151                 protected override void DoEmit (EmitContext ec)
1152                 {
1153                         ec.ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, ec.LoopEnd);
1154                 }
1155                 
1156                 protected override void CloneTo (CloneContext clonectx, Statement t)
1157                 {
1158                         // nothing needed
1159                 }
1160         }
1161
1162         public class Continue : Statement {
1163                 
1164                 public Continue (Location l)
1165                 {
1166                         loc = l;
1167                 }
1168
1169                 bool unwind_protect;
1170
1171                 public override bool Resolve (EmitContext ec)
1172                 {
1173                         int errors = Report.Errors;
1174                         unwind_protect = ec.CurrentBranching.AddContinueOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
1175                         ec.CurrentBranching.CurrentUsageVector.Goto ();
1176                         return errors == Report.Errors;
1177                 }
1178
1179                 protected override void DoEmit (EmitContext ec)
1180                 {
1181                         ec.ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, ec.LoopBegin);
1182                 }
1183
1184                 protected override void CloneTo (CloneContext clonectx, Statement t)
1185                 {
1186                         // nothing needed.
1187                 }
1188         }
1189
1190         public abstract class Variable
1191         {
1192                 public abstract Type Type {
1193                         get;
1194                 }
1195
1196                 public abstract bool HasInstance {
1197                         get;
1198                 }
1199
1200                 public abstract bool NeedsTemporary {
1201                         get;
1202                 }
1203
1204                 public abstract void EmitInstance (EmitContext ec);
1205
1206                 public abstract void Emit (EmitContext ec);
1207
1208                 public abstract void EmitAssign (EmitContext ec);
1209
1210                 public abstract void EmitAddressOf (EmitContext ec);
1211         }
1212
1213         public interface IKnownVariable {
1214                 Block Block { get; }
1215                 Location Location { get; }
1216         }
1217
1218         //
1219         // The information about a user-perceived local variable
1220         //
1221         public class LocalInfo : IKnownVariable {
1222                 public Expression Type;
1223
1224                 public Type VariableType;
1225                 public readonly string Name;
1226                 public readonly Location Location;
1227                 public readonly Block Block;
1228
1229                 public VariableInfo VariableInfo;
1230
1231                 Variable var;
1232                 public Variable Variable {
1233                         get { return var; }
1234                 }
1235
1236                 [Flags]
1237                 enum Flags : byte {
1238                         Used = 1,
1239                         ReadOnly = 2,
1240                         Pinned = 4,
1241                         IsThis = 8,
1242                         Captured = 16,
1243                         AddressTaken = 32,
1244                         CompilerGenerated = 64,
1245                         IsConstant = 128
1246                 }
1247
1248                 public enum ReadOnlyContext: byte {
1249                         Using,
1250                         Foreach,
1251                         Fixed
1252                 }
1253
1254                 Flags flags;
1255                 ReadOnlyContext ro_context;
1256                 LocalBuilder builder;
1257                 
1258                 public LocalInfo (Expression type, string name, Block block, Location l)
1259                 {
1260                         Type = type;
1261                         Name = name;
1262                         Block = block;
1263                         Location = l;
1264                 }
1265
1266                 public LocalInfo (DeclSpace ds, Block block, Location l)
1267                 {
1268                         VariableType = ds.IsGeneric ? ds.CurrentType : ds.TypeBuilder;
1269                         Block = block;
1270                         Location = l;
1271                 }
1272
1273                 public void ResolveVariable (EmitContext ec)
1274                 {
1275                         Block theblock = Block;
1276                         if (theblock.ScopeInfo != null)
1277                                 var = theblock.ScopeInfo.GetCapturedVariable (this);
1278
1279                         if (var == null) {
1280                                 if (Pinned)
1281                                         //
1282                                         // This is needed to compile on both .NET 1.x and .NET 2.x
1283                                         // the later introduced `DeclareLocal (Type t, bool pinned)'
1284                                         //
1285                                         builder = TypeManager.DeclareLocalPinned (ec.ig, VariableType);
1286                                 else
1287                                         builder = ec.ig.DeclareLocal (VariableType);
1288
1289                                 var = new LocalVariable (this, builder);
1290                         }
1291                 }
1292
1293                 public void EmitSymbolInfo (EmitContext ec, string name)
1294                 {
1295                         if (builder != null)
1296                                 ec.DefineLocalVariable (Name, builder);
1297                 }
1298
1299                 public bool IsThisAssigned (EmitContext ec)
1300                 {
1301                         if (VariableInfo == null)
1302                                 throw new Exception ();
1303
1304                         if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (VariableInfo))
1305                                 return true;
1306
1307                         return VariableInfo.TypeInfo.IsFullyInitialized (ec.CurrentBranching, VariableInfo, ec.loc);
1308                 }
1309
1310                 public bool IsAssigned (EmitContext ec)
1311                 {
1312                         if (VariableInfo == null)
1313                                 throw new Exception ();
1314
1315                         return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (VariableInfo);
1316                 }
1317
1318                 public bool Resolve (EmitContext ec)
1319                 {
1320                         if (VariableType == null) {
1321                                 TypeExpr texpr = Type.ResolveAsContextualType (ec, false);
1322                                 if (texpr == null)
1323                                         return false;
1324                                 
1325                                 VariableType = texpr.Type;
1326                         }
1327
1328                         if (TypeManager.IsGenericParameter (VariableType))
1329                                 return true;
1330
1331                         if (VariableType == TypeManager.void_type) {
1332                                 Expression.Error_VoidInvalidInTheContext (Location);
1333                                 return false;
1334                         }
1335
1336                         if (VariableType.IsAbstract && VariableType.IsSealed) {
1337                                 FieldBase.Error_VariableOfStaticClass (Location, Name, VariableType);
1338                                 return false;
1339                         }
1340
1341                         if (VariableType.IsPointer && !ec.InUnsafe)
1342                                 Expression.UnsafeError (Location);
1343
1344                         return true;
1345                 }
1346
1347                 public bool IsCaptured {
1348                         get { return (flags & Flags.Captured) != 0; }
1349                         set { flags |= Flags.Captured; }
1350                 }
1351
1352                 public bool IsConstant {
1353                         get { return (flags & Flags.IsConstant) != 0; }
1354                         set { flags |= Flags.IsConstant; }
1355                 }
1356
1357                 public bool AddressTaken {
1358                         get { return (flags & Flags.AddressTaken) != 0; }
1359                         set { flags |= Flags.AddressTaken; }
1360                 }
1361
1362                 public bool CompilerGenerated {
1363                         get { return (flags & Flags.CompilerGenerated) != 0; }
1364                         set { flags |= Flags.CompilerGenerated; }
1365                 }
1366
1367                 public override string ToString ()
1368                 {
1369                         return String.Format ("LocalInfo ({0},{1},{2},{3})",
1370                                               Name, Type, VariableInfo, Location);
1371                 }
1372
1373                 public bool Used {
1374                         get { return (flags & Flags.Used) != 0; }
1375                         set { flags = value ? (flags | Flags.Used) : (unchecked (flags & ~Flags.Used)); }
1376                 }
1377
1378                 public bool ReadOnly {
1379                         get { return (flags & Flags.ReadOnly) != 0; }
1380                 }
1381
1382                 public void SetReadOnlyContext (ReadOnlyContext context)
1383                 {
1384                         flags |= Flags.ReadOnly;
1385                         ro_context = context;
1386                 }
1387
1388                 public string GetReadOnlyContext ()
1389                 {
1390                         if (!ReadOnly)
1391                                 throw new InternalErrorException ("Variable is not readonly");
1392
1393                         switch (ro_context) {
1394                                 case ReadOnlyContext.Fixed:
1395                                         return "fixed variable";
1396                                 case ReadOnlyContext.Foreach:
1397                                         return "foreach iteration variable";
1398                                 case ReadOnlyContext.Using:
1399                                         return "using variable";
1400                         }
1401                         throw new NotImplementedException ();
1402                 }
1403
1404                 //
1405                 // Whether the variable is pinned, if Pinned the variable has been 
1406                 // allocated in a pinned slot with DeclareLocal.
1407                 //
1408                 public bool Pinned {
1409                         get { return (flags & Flags.Pinned) != 0; }
1410                         set { flags = value ? (flags | Flags.Pinned) : (flags & ~Flags.Pinned); }
1411                 }
1412
1413                 public bool IsThis {
1414                         get { return (flags & Flags.IsThis) != 0; }
1415                         set { flags = value ? (flags | Flags.IsThis) : (flags & ~Flags.IsThis); }
1416                 }
1417
1418                 Block IKnownVariable.Block {
1419                         get { return Block; }
1420                 }
1421
1422                 Location IKnownVariable.Location {
1423                         get { return Location; }
1424                 }
1425
1426                 protected class LocalVariable : Variable
1427                 {
1428                         public readonly LocalInfo LocalInfo;
1429                         LocalBuilder builder;
1430
1431                         public LocalVariable (LocalInfo local, LocalBuilder builder)
1432                         {
1433                                 this.LocalInfo = local;
1434                                 this.builder = builder;
1435                         }
1436
1437                         public override Type Type {
1438                                 get { return LocalInfo.VariableType; }
1439                         }
1440
1441                         public override bool HasInstance {
1442                                 get { return false; }
1443                         }
1444
1445                         public override bool NeedsTemporary {
1446                                 get { return false; }
1447                         }
1448
1449                         public override void EmitInstance (EmitContext ec)
1450                         {
1451                                 // Do nothing.
1452                         }
1453
1454                         public override void Emit (EmitContext ec)
1455                         {
1456                                 ec.ig.Emit (OpCodes.Ldloc, builder);
1457                         }
1458
1459                         public override void EmitAssign (EmitContext ec)
1460                         {
1461                                 ec.ig.Emit (OpCodes.Stloc, builder);
1462                         }
1463
1464                         public override void EmitAddressOf (EmitContext ec)
1465                         {
1466                                 ec.ig.Emit (OpCodes.Ldloca, builder);
1467                         }
1468                 }
1469
1470                 public LocalInfo Clone (CloneContext clonectx)
1471                 {
1472                         //
1473                         // Variables in anonymous block are not resolved yet
1474                         //
1475                         if (VariableType == null)
1476                                 return new LocalInfo (Type.Clone (clonectx), Name, clonectx.LookupBlock (Block), Location);
1477
1478                         //
1479                         // Variables in method block are resolved
1480                         //
1481                         LocalInfo li = new LocalInfo (null, Name, clonectx.LookupBlock (Block), Location);
1482                         li.VariableType = VariableType;
1483                         return li;                      
1484                 }
1485         }
1486
1487         /// <summary>
1488         ///   Block represents a C# block.
1489         /// </summary>
1490         ///
1491         /// <remarks>
1492         ///   This class is used in a number of places: either to represent
1493         ///   explicit blocks that the programmer places or implicit blocks.
1494         ///
1495         ///   Implicit blocks are used as labels or to introduce variable
1496         ///   declarations.
1497         ///
1498         ///   Top-level blocks derive from Block, and they are called ToplevelBlock
1499         ///   they contain extra information that is not necessary on normal blocks.
1500         /// </remarks>
1501         public class Block : Statement {
1502                 public Block    Parent;
1503                 public readonly Location  StartLocation;
1504                 public Location EndLocation = Location.Null;
1505
1506                 public ExplicitBlock Explicit;
1507                 public ToplevelBlock Toplevel;
1508
1509                 [Flags]
1510                 public enum Flags : byte {
1511                         Unchecked = 1,
1512                         BlockUsed = 2,
1513                         VariablesInitialized = 4,
1514                         HasRet = 8,
1515                         IsDestructor = 16,
1516                         Unsafe = 32,
1517                         HasVarargs = 64, // Used in ToplevelBlock
1518                         IsIterator = 128
1519                 }
1520                 protected Flags flags;
1521
1522                 public bool Unchecked {
1523                         get { return (flags & Flags.Unchecked) != 0; }
1524                         set { flags |= Flags.Unchecked; }
1525                 }
1526
1527                 public bool Unsafe {
1528                         get { return (flags & Flags.Unsafe) != 0; }
1529                         set { flags |= Flags.Unsafe; }
1530                 }
1531
1532                 //
1533                 // The statements in this block
1534                 //
1535                 protected ArrayList statements;
1536                 int num_statements;
1537
1538                 //
1539                 // An array of Blocks.  We keep track of children just
1540                 // to generate the local variable declarations.
1541                 //
1542                 // Statements and child statements are handled through the
1543                 // statements.
1544                 //
1545                 ArrayList children;
1546
1547                 //
1548                 // Labels.  (label, block) pairs.
1549                 //
1550                 HybridDictionary labels;
1551
1552                 //
1553                 // Keeps track of (name, type) pairs
1554                 //
1555                 IDictionary variables;
1556
1557                 //
1558                 // Keeps track of constants
1559                 HybridDictionary constants;
1560
1561                 //
1562                 // Temporary variables.
1563                 //
1564                 ArrayList temporary_variables;
1565                 
1566                 //
1567                 // If this is a switch section, the enclosing switch block.
1568                 //
1569                 Block switch_block;
1570
1571                 // TODO: merge with scope_initializers
1572                 ExpressionStatement scope_init;
1573                 ArrayList scope_initializers;
1574
1575                 ArrayList anonymous_children;
1576
1577                 protected static int id;
1578
1579                 int this_id;
1580
1581                 int assignable_slots;
1582                 protected ScopeInfo scope_info;
1583                 bool unreachable_shown;
1584                 bool unreachable;
1585                 
1586                 public Block (Block parent)
1587                         : this (parent, (Flags) 0, Location.Null, Location.Null)
1588                 { }
1589
1590                 public Block (Block parent, Flags flags)
1591                         : this (parent, flags, Location.Null, Location.Null)
1592                 { }
1593
1594                 public Block (Block parent, Location start, Location end)
1595                         : this (parent, (Flags) 0, start, end)
1596                 { }
1597
1598                 public Block (Block parent, Flags flags, Location start, Location end)
1599                 {
1600                         if (parent != null) {
1601                                 parent.AddChild (this);
1602
1603                                 // the appropriate constructors will fixup these fields
1604                                 Toplevel = parent.Toplevel;
1605                                 Explicit = parent.Explicit;
1606                         }
1607                         
1608                         this.Parent = parent;
1609                         this.flags = flags;
1610                         this.StartLocation = start;
1611                         this.EndLocation = end;
1612                         this.loc = start;
1613                         this_id = id++;
1614                         statements = new ArrayList (4);
1615                 }
1616
1617                 public Block CreateSwitchBlock (Location start)
1618                 {
1619                         // FIXME: should this be implicit?
1620                         Block new_block = new ExplicitBlock (this, start, start);
1621                         new_block.switch_block = this;
1622                         return new_block;
1623                 }
1624
1625                 public int ID {
1626                         get { return this_id; }
1627                 }
1628
1629                 public IDictionary Variables {
1630                         get {
1631                                 if (variables == null)
1632                                         variables = new ListDictionary ();
1633                                 return variables;
1634                         }
1635                 }
1636
1637                 void AddChild (Block b)
1638                 {
1639                         if (children == null)
1640                                 children = new ArrayList (1);
1641                         
1642                         children.Add (b);
1643                 }
1644
1645                 public void SetEndLocation (Location loc)
1646                 {
1647                         EndLocation = loc;
1648                 }
1649
1650                 protected static void Error_158 (string name, Location loc)
1651                 {
1652                         Report.Error (158, loc, "The label `{0}' shadows another label " +
1653                                       "by the same name in a contained scope", name);
1654                 }
1655
1656                 /// <summary>
1657                 ///   Adds a label to the current block. 
1658                 /// </summary>
1659                 ///
1660                 /// <returns>
1661                 ///   false if the name already exists in this block. true
1662                 ///   otherwise.
1663                 /// </returns>
1664                 ///
1665                 public bool AddLabel (LabeledStatement target)
1666                 {
1667                         if (switch_block != null)
1668                                 return switch_block.AddLabel (target);
1669
1670                         string name = target.Name;
1671
1672                         Block cur = this;
1673                         while (cur != null) {
1674                                 LabeledStatement s = cur.DoLookupLabel (name);
1675                                 if (s != null) {
1676                                         Report.SymbolRelatedToPreviousError (s.loc, s.Name);
1677                                         Report.Error (140, target.loc, "The label `{0}' is a duplicate", name);
1678                                         return false;
1679                                 }
1680
1681                                 if (this == Explicit)
1682                                         break;
1683
1684                                 cur = cur.Parent;
1685                         }
1686
1687                         while (cur != null) {
1688                                 if (cur.DoLookupLabel (name) != null) {
1689                                         Error_158 (name, target.loc);
1690                                         return false;
1691                                 }
1692
1693                                 if (children != null) {
1694                                         foreach (Block b in children) {
1695                                                 LabeledStatement s = b.DoLookupLabel (name);
1696                                                 if (s == null)
1697                                                         continue;
1698
1699                                                 Report.SymbolRelatedToPreviousError (s.loc, s.Name);
1700                                                 Error_158 (name, target.loc);
1701                                                 return false;
1702                                         }
1703                                 }
1704
1705                                 cur = cur.Parent;
1706                         }
1707
1708                         Toplevel.CheckError158 (name, target.loc);
1709
1710                         if (labels == null)
1711                                 labels = new HybridDictionary();
1712
1713                         labels.Add (name, target);
1714                         return true;
1715                 }
1716
1717                 public LabeledStatement LookupLabel (string name)
1718                 {
1719                         LabeledStatement s = DoLookupLabel (name);
1720                         if (s != null)
1721                                 return s;
1722
1723                         if (children == null)
1724                                 return null;
1725
1726                         foreach (Block child in children) {
1727                                 if (Explicit != child.Explicit)
1728                                         continue;
1729
1730                                 s = child.LookupLabel (name);
1731                                 if (s != null)
1732                                         return s;
1733                         }
1734
1735                         return null;
1736                 }
1737
1738                 LabeledStatement DoLookupLabel (string name)
1739                 {
1740                         if (switch_block != null)
1741                                 return switch_block.LookupLabel (name);
1742
1743                         if (labels != null)
1744                                 if (labels.Contains (name))
1745                                         return ((LabeledStatement) labels [name]);
1746
1747                         return null;
1748                 }
1749
1750                 public bool CheckInvariantMeaningInBlock (string name, Expression e, Location loc)
1751                 {
1752                         Block b = this;
1753                         IKnownVariable kvi = b.Explicit.GetKnownVariable (name);
1754                         while (kvi == null) {
1755                                 b = b.Explicit.Parent;
1756                                 if (b == null)
1757                                         return true;
1758                                 kvi = b.Explicit.GetKnownVariable (name);
1759                         }
1760
1761                         if (kvi.Block == b)
1762                                 return true;
1763
1764                         // Is kvi.Block nested inside 'b'
1765                         if (b.Explicit != kvi.Block.Explicit) {
1766                                 //
1767                                 // If a variable by the same name it defined in a nested block of this
1768                                 // block, we violate the invariant meaning in a block.
1769                                 //
1770                                 if (b == this) {
1771                                         Report.SymbolRelatedToPreviousError (kvi.Location, name);
1772                                         Report.Error (135, loc, "`{0}' conflicts with a declaration in a child block", name);
1773                                         return false;
1774                                 }
1775
1776                                 //
1777                                 // It's ok if the definition is in a nested subblock of b, but not
1778                                 // nested inside this block -- a definition in a sibling block
1779                                 // should not affect us.
1780                                 //
1781                                 return true;
1782                         }
1783
1784                         //
1785                         // Block 'b' and kvi.Block are the same textual block.
1786                         // However, different variables are extant.
1787                         //
1788                         // Check if the variable is in scope in both blocks.  We use
1789                         // an indirect check that depends on AddVariable doing its
1790                         // part in maintaining the invariant-meaning-in-block property.
1791                         //
1792                         if (e is VariableReference || (e is Constant && b.GetLocalInfo (name) != null))
1793                                 return true;
1794
1795                         //
1796                         // Even though we detected the error when the name is used, we
1797                         // treat it as if the variable declaration was in error.
1798                         //
1799                         Report.SymbolRelatedToPreviousError (loc, name);
1800                         Error_AlreadyDeclared (kvi.Location, name, "parent or current");
1801                         return false;
1802                 }
1803
1804                 public LocalInfo AddVariable (Expression type, string name, Location l)
1805                 {
1806                         LocalInfo vi = GetLocalInfo (name);
1807                         if (vi != null) {
1808                                 Report.SymbolRelatedToPreviousError (vi.Location, name);
1809                                 if (Explicit == vi.Block.Explicit)
1810                                         Error_AlreadyDeclared (l, name, null);
1811                                 else
1812                                         Error_AlreadyDeclared (l, name, "parent");
1813                                 return null;
1814                         }
1815
1816                         ToplevelParameterInfo pi = Toplevel.GetParameterInfo (name);
1817                         if (pi != null) {
1818                                 Report.SymbolRelatedToPreviousError (pi.Location, name);
1819                                 Error_AlreadyDeclared (loc, name,
1820                                         pi.Block == Toplevel ? "method argument" : "parent or current");
1821                                 return null;
1822                         }
1823                         
1824                         if (Toplevel.GenericMethod != null) {
1825                                 foreach (TypeParameter tp in Toplevel.GenericMethod.CurrentTypeParameters) {
1826                                         if (tp.Name == name) {
1827                                                 Report.SymbolRelatedToPreviousError (tp);
1828                                                 Error_AlreadyDeclaredTypeParameter (loc, name);
1829                                                 return null;
1830                                         }
1831                                 }
1832                         }                       
1833
1834                         IKnownVariable kvi = Explicit.GetKnownVariable (name);
1835                         if (kvi != null) {
1836                                 Report.SymbolRelatedToPreviousError (kvi.Location, name);
1837                                 Error_AlreadyDeclared (l, name, "child");
1838                                 return null;
1839                         }
1840
1841                         vi = new LocalInfo (type, name, this, l);
1842                         AddVariable (vi);
1843
1844                         if ((flags & Flags.VariablesInitialized) != 0)
1845                                 throw new InternalErrorException ("block has already been resolved");
1846
1847                         return vi;
1848                 }
1849                 
1850                 protected virtual void AddVariable (LocalInfo li)
1851                 {
1852                         Variables.Add (li.Name, li);
1853                         Explicit.AddKnownVariable (li.Name, li);
1854                 }
1855
1856                 protected virtual void Error_AlreadyDeclared (Location loc, string var, string reason)
1857                 {
1858                         if (reason == null) {
1859                                 Error_AlreadyDeclared (loc, var);
1860                                 return;
1861                         }
1862                         
1863                         Report.Error (136, loc, "A local variable named `{0}' cannot be declared " +
1864                                       "in this scope because it would give a different meaning " +
1865                                       "to `{0}', which is already used in a `{1}' scope " +
1866                                       "to denote something else", var, reason);
1867                 }
1868
1869                 protected virtual void Error_AlreadyDeclared (Location loc, string name)
1870                 {
1871                         Report.Error (128, loc,
1872                                 "A local variable named `{0}' is already defined in this scope", name);
1873                 }
1874                                         
1875                 protected virtual void Error_AlreadyDeclaredTypeParameter (Location loc, string name)
1876                 {
1877                         GenericMethod.Error_ParameterNameCollision (loc, name, "local variable");
1878                 }                                       
1879
1880                 public bool AddConstant (Expression type, string name, Expression value, Location l)
1881                 {
1882                         if (AddVariable (type, name, l) == null)
1883                                 return false;
1884                         
1885                         if (constants == null)
1886                                 constants = new HybridDictionary();
1887
1888                         constants.Add (name, value);
1889
1890                         // A block is considered used if we perform an initialization in a local declaration, even if it is constant.
1891                         Use ();
1892                         return true;
1893                 }
1894
1895                 static int next_temp_id = 0;
1896
1897                 public LocalInfo AddTemporaryVariable (TypeExpr te, Location loc)
1898                 {
1899                         Report.Debug (64, "ADD TEMPORARY", this, Toplevel, loc);
1900
1901                         if (temporary_variables == null)
1902                                 temporary_variables = new ArrayList ();
1903
1904                         int id = ++next_temp_id;
1905                         string name = "$s_" + id.ToString ();
1906
1907                         LocalInfo li = new LocalInfo (te, name, this, loc);
1908                         li.CompilerGenerated = true;
1909                         temporary_variables.Add (li);
1910                         return li;
1911                 }
1912
1913                 public LocalInfo GetLocalInfo (string name)
1914                 {
1915                         for (Block b = this; b != null; b = b.Parent) {
1916                                 if (b.variables != null) {
1917                                         LocalInfo ret = b.variables [name] as LocalInfo;
1918                                         if (ret != null)
1919                                                 return ret;
1920                                 }
1921                         }
1922                         return null;
1923                 }
1924
1925                 public Expression GetVariableType (string name)
1926                 {
1927                         LocalInfo vi = GetLocalInfo (name);
1928                         return vi == null ? null : vi.Type;
1929                 }
1930
1931                 public Expression GetConstantExpression (string name)
1932                 {
1933                         for (Block b = this; b != null; b = b.Parent) {
1934                                 if (b.constants != null) {
1935                                         Expression ret = b.constants [name] as Expression;
1936                                         if (ret != null)
1937                                                 return ret;
1938                                 }
1939                         }
1940                         return null;
1941                 }
1942
1943                 //
1944                 // It should be used by expressions which require to
1945                 // register a statement during resolve process.
1946                 //
1947                 public void AddScopeStatement (StatementExpression s)
1948                 {
1949                         if (scope_initializers == null)
1950                                 scope_initializers = new ArrayList ();
1951
1952                         scope_initializers.Add (s);
1953                 }
1954                 
1955                 public void AddStatement (Statement s)
1956                 {
1957                         statements.Add (s);
1958                         flags |= Flags.BlockUsed;
1959                 }
1960
1961                 public bool Used {
1962                         get { return (flags & Flags.BlockUsed) != 0; }
1963                 }
1964
1965                 public void Use ()
1966                 {
1967                         flags |= Flags.BlockUsed;
1968                 }
1969
1970                 public bool HasRet {
1971                         get { return (flags & Flags.HasRet) != 0; }
1972                 }
1973
1974                 public bool IsDestructor {
1975                         get { return (flags & Flags.IsDestructor) != 0; }
1976                 }
1977
1978                 public void SetDestructor ()
1979                 {
1980                         flags |= Flags.IsDestructor;
1981                 }
1982
1983                 public int AssignableSlots {
1984                         get {
1985                                 if ((flags & Flags.VariablesInitialized) == 0)
1986                                         throw new Exception ("Variables have not been initialized yet");
1987                                 return assignable_slots;
1988                         }
1989                 }
1990
1991                 public ScopeInfo ScopeInfo {
1992                         get { return scope_info; }
1993                 }
1994
1995                 public ScopeInfo CreateScopeInfo ()
1996                 {
1997                         if (scope_info == null)
1998                                 scope_info = ScopeInfo.CreateScope (this);
1999
2000                         return scope_info;
2001                 }
2002
2003                 public ArrayList AnonymousChildren {
2004                         get { return anonymous_children; }
2005                 }
2006
2007                 public void AddAnonymousChild (ToplevelBlock b)
2008                 {
2009                         if (anonymous_children == null)
2010                                 anonymous_children = new ArrayList ();
2011
2012                         anonymous_children.Add (b);
2013                 }
2014
2015                 void DoResolveConstants (EmitContext ec)
2016                 {
2017                         if (constants == null)
2018                                 return;
2019
2020                         if (variables == null)
2021                                 throw new InternalErrorException ("cannot happen");
2022
2023                         foreach (DictionaryEntry de in variables) {
2024                                 string name = (string) de.Key;
2025                                 LocalInfo vi = (LocalInfo) de.Value;
2026                                 Type variable_type = vi.VariableType;
2027
2028                                 if (variable_type == null) {
2029                                         if (vi.Type is VarExpr)
2030                                                 Report.Error (822, vi.Type.Location, "An implicitly typed local variable cannot be a constant");
2031
2032                                         continue;
2033                                 }
2034
2035                                 Expression cv = (Expression) constants [name];
2036                                 if (cv == null)
2037                                         continue;
2038
2039                                 // Don't let 'const int Foo = Foo;' succeed.
2040                                 // Removing the name from 'constants' ensures that we get a LocalVariableReference below,
2041                                 // which in turn causes the 'must be constant' error to be triggered.
2042                                 constants.Remove (name);
2043
2044                                 if (!Const.IsConstantTypeValid (variable_type)) {
2045                                         Const.Error_InvalidConstantType (variable_type, loc);
2046                                         continue;
2047                                 }
2048
2049                                 ec.CurrentBlock = this;
2050                                 Expression e;
2051                                 using (ec.With (EmitContext.Flags.ConstantCheckState, (flags & Flags.Unchecked) == 0)) {
2052                                         e = cv.Resolve (ec);
2053                                 }
2054                                 if (e == null)
2055                                         continue;
2056
2057                                 Constant ce = e as Constant;
2058                                 if (ce == null) {
2059                                         Const.Error_ExpressionMustBeConstant (vi.Location, name);
2060                                         continue;
2061                                 }
2062
2063                                 e = ce.ConvertImplicitly (variable_type);
2064                                 if (e == null) {
2065                                         if (!variable_type.IsValueType && variable_type != TypeManager.string_type && !ce.IsDefaultValue)
2066                                                 Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
2067                                         else
2068                                                 ce.Error_ValueCannotBeConverted (null, vi.Location, variable_type, false);
2069                                         continue;
2070                                 }
2071
2072                                 constants.Add (name, e);
2073                                 vi.IsConstant = true;
2074                         }
2075                 }
2076
2077                 protected void ResolveMeta (EmitContext ec, int offset)
2078                 {
2079                         Report.Debug (64, "BLOCK RESOLVE META", this, Parent);
2080
2081                         // If some parent block was unsafe, we remain unsafe even if this block
2082                         // isn't explicitly marked as such.
2083                         using (ec.With (EmitContext.Flags.InUnsafe, ec.InUnsafe | Unsafe)) {
2084                                 flags |= Flags.VariablesInitialized;
2085
2086                                 if (variables != null) {
2087                                         foreach (LocalInfo li in variables.Values) {
2088                                                 if (!li.Resolve (ec))
2089                                                         continue;
2090                                                 li.VariableInfo = new VariableInfo (li, offset);
2091                                                 offset += li.VariableInfo.Length;
2092                                         }
2093                                 }
2094                                 assignable_slots = offset;
2095
2096                                 DoResolveConstants (ec);
2097
2098                                 if (children == null)
2099                                         return;
2100                                 foreach (Block b in children)
2101                                         b.ResolveMeta (ec, offset);
2102                         }
2103                 }
2104
2105                 //
2106                 // Emits the local variable declarations for a block
2107                 //
2108                 public virtual void EmitMeta (EmitContext ec)
2109                 {
2110                         Report.Debug (64, "BLOCK EMIT META", this, Parent, Toplevel, ScopeInfo, ec);
2111                         if (ScopeInfo != null) {
2112                                 scope_init = ScopeInfo.GetScopeInitializer (ec);
2113                                 Report.Debug (64, "BLOCK EMIT META #1", this, Toplevel, ScopeInfo,
2114                                               ec, scope_init);
2115                         }
2116
2117                         if (variables != null){
2118                                 foreach (LocalInfo vi in variables.Values)
2119                                         vi.ResolveVariable (ec);
2120                         }
2121
2122                         if (temporary_variables != null) {
2123                                 for (int i = 0; i < temporary_variables.Count; i++)
2124                                         ((LocalInfo)temporary_variables[i]).ResolveVariable(ec);
2125                         }
2126
2127                         if (children != null){
2128                                 for (int i = 0; i < children.Count; i++)
2129                                         ((Block)children[i]).EmitMeta(ec);
2130                         }
2131                 }
2132
2133                 void UsageWarning (FlowBranching.UsageVector vector)
2134                 {
2135                         string name;
2136
2137                         if ((variables != null) && (Report.WarningLevel >= 3)) {
2138                                 foreach (DictionaryEntry de in variables){
2139                                         LocalInfo vi = (LocalInfo) de.Value;
2140
2141                                         if (vi.Used)
2142                                                 continue;
2143
2144                                         name = (string) de.Key;
2145
2146                                         // vi.VariableInfo can be null for 'catch' variables
2147                                         if (vi.VariableInfo != null && vector.IsAssigned (vi.VariableInfo, true)){
2148                                                 Report.Warning (219, 3, vi.Location, "The variable `{0}' is assigned but its value is never used", name);
2149                                         } else {
2150                                                 Report.Warning (168, 3, vi.Location, "The variable `{0}' is declared but never used", name);
2151                                         }
2152                                 }
2153                         }
2154                 }
2155
2156                 private void CheckPossibleMistakenEmptyStatement (Statement s)
2157                 {
2158                         Statement body;
2159
2160                         // Some statements are wrapped by a Block. Since
2161                         // others' internal could be changed, here I treat
2162                         // them as possibly wrapped by Block equally.
2163                         Block b = s as Block;
2164                         if (b != null && b.statements.Count == 1)
2165                                 s = (Statement) b.statements [0];
2166
2167                         if (s is Lock)
2168                                 body = ((Lock) s).Statement;
2169                         else if (s is For)
2170                                 body = ((For) s).Statement;
2171                         else if (s is Foreach)
2172                                 body = ((Foreach) s).Statement;
2173                         else if (s is While)
2174                                 body = ((While) s).Statement;
2175                         else if (s is Using)
2176                                 body = ((Using) s).Statement;
2177                         else if (s is Fixed)
2178                                 body = ((Fixed) s).Statement;
2179                         else
2180                                 return;
2181
2182                         if (body == null || body is EmptyStatement)
2183                                 Report.Warning (642, 3, s.loc, "Possible mistaken empty statement");
2184                 }
2185
2186                 public override bool Resolve (EmitContext ec)
2187                 {
2188                         Block prev_block = ec.CurrentBlock;
2189                         bool ok = true;
2190
2191                         int errors = Report.Errors;
2192
2193                         ec.CurrentBlock = this;
2194                         ec.StartFlowBranching (this);
2195
2196                         Report.Debug (4, "RESOLVE BLOCK", StartLocation, ec.CurrentBranching);
2197
2198                         //
2199                         // This flag is used to notate nested statements as unreachable from the beginning of this block.
2200                         // For the purposes of this resolution, it doesn't matter that the whole block is unreachable 
2201                         // from the beginning of the function.  The outer Resolve() that detected the unreachability is
2202                         // responsible for handling the situation.
2203                         //
2204                         int statement_count = statements.Count;
2205                         for (int ix = 0; ix < statement_count; ix++){
2206                                 Statement s = (Statement) statements [ix];
2207                                 // Check possible empty statement (CS0642)
2208                                 if (Report.WarningLevel >= 3 &&
2209                                         ix + 1 < statement_count &&
2210                                                 statements [ix + 1] is Block)
2211                                         CheckPossibleMistakenEmptyStatement (s);
2212
2213                                 //
2214                                 // Warn if we detect unreachable code.
2215                                 //
2216                                 if (unreachable) {
2217                                         if (s is EmptyStatement)
2218                                                 continue;
2219
2220                                         if (s is Block)
2221                                                 ((Block) s).unreachable = true;
2222
2223                                         if (!unreachable_shown && !(s is LabeledStatement)) {
2224                                                 Report.Warning (162, 2, s.loc, "Unreachable code detected");
2225                                                 unreachable_shown = true;
2226                                         }
2227                                 }
2228
2229                                 //
2230                                 // Note that we're not using ResolveUnreachable() for unreachable
2231                                 // statements here.  ResolveUnreachable() creates a temporary
2232                                 // flow branching and kills it afterwards.  This leads to problems
2233                                 // if you have two unreachable statements where the first one
2234                                 // assigns a variable and the second one tries to access it.
2235                                 //
2236
2237                                 if (!s.Resolve (ec)) {
2238                                         ok = false;
2239                                         if (ec.IsInProbingMode)
2240                                                 break;
2241
2242                                         statements [ix] = EmptyStatement.Value;
2243                                         continue;
2244                                 }
2245
2246                                 if (unreachable && !(s is LabeledStatement) && !(s is Block))
2247                                         statements [ix] = EmptyStatement.Value;
2248
2249                                 num_statements = ix + 1;
2250
2251                                 unreachable = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
2252                                 if (unreachable && s is LabeledStatement)
2253                                         throw new InternalErrorException ("should not happen");
2254                         }
2255
2256                         Report.Debug (4, "RESOLVE BLOCK DONE", StartLocation,
2257                                       ec.CurrentBranching, statement_count, num_statements);
2258
2259                         while (ec.CurrentBranching is FlowBranchingLabeled)
2260                                 ec.EndFlowBranching ();
2261
2262                         FlowBranching.UsageVector vector = ec.DoEndFlowBranching ();
2263
2264                         ec.CurrentBlock = prev_block;
2265
2266                         // If we're a non-static `struct' constructor which doesn't have an
2267                         // initializer, then we must initialize all of the struct's fields.
2268                         if (this == Toplevel && !Toplevel.IsThisAssigned (ec) && !vector.IsUnreachable)
2269                                 ok = false;
2270
2271                         if ((labels != null) && (Report.WarningLevel >= 2)) {
2272                                 foreach (LabeledStatement label in labels.Values)
2273                                         if (!label.HasBeenReferenced)
2274                                                 Report.Warning (164, 2, label.loc,
2275                                                                 "This label has not been referenced");
2276                         }
2277
2278                         Report.Debug (4, "RESOLVE BLOCK DONE #2", StartLocation, vector);
2279
2280                         if (vector.IsUnreachable)
2281                                 flags |= Flags.HasRet;
2282
2283                         if (ok && (errors == Report.Errors)) {
2284                                 UsageWarning (vector);
2285                         }
2286
2287                         return ok;
2288                 }
2289
2290                 public override bool ResolveUnreachable (EmitContext ec, bool warn)
2291                 {
2292                         unreachable_shown = true;
2293                         unreachable = true;
2294
2295                         if (warn)
2296                                 Report.Warning (162, 2, loc, "Unreachable code detected");
2297
2298                         ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
2299                         bool ok = Resolve (ec);
2300                         ec.KillFlowBranching ();
2301
2302                         return ok;
2303                 }
2304                 
2305                 protected override void DoEmit (EmitContext ec)
2306                 {
2307                         for (int ix = 0; ix < num_statements; ix++){
2308                                 Statement s = (Statement) statements [ix];
2309                                 s.Emit (ec);
2310                         }
2311                 }
2312
2313                 public override void Emit (EmitContext ec)
2314                 {
2315                         Block prev_block = ec.CurrentBlock;
2316
2317                         ec.CurrentBlock = this;
2318
2319                         bool emit_debug_info = SymbolWriter.HasSymbolWriter;
2320                         bool is_lexical_block = (this == Explicit) && (Parent != null) &&
2321                                 ((flags & Flags.IsIterator) == 0);
2322
2323                         bool omit_debug_info = ec.OmitDebuggingInfo;
2324
2325                         if (emit_debug_info) {
2326                                 if (is_lexical_block)
2327                                         ec.BeginScope ();
2328                         }
2329
2330                         if ((scope_init != null) || (scope_initializers != null))
2331                                 SymbolWriter.OpenCompilerGeneratedBlock (ec.ig);
2332
2333                         if (scope_init != null) {
2334                                 ec.OmitDebuggingInfo = true;
2335                                 scope_init.EmitStatement (ec);
2336                                 ec.OmitDebuggingInfo = omit_debug_info;
2337                         }
2338                         if (scope_initializers != null) {
2339                                 ec.OmitDebuggingInfo = true;
2340                                 foreach (StatementExpression s in scope_initializers)
2341                                         s.Emit (ec);
2342                                 ec.OmitDebuggingInfo = omit_debug_info;
2343                         }
2344
2345                         if ((scope_init != null) || (scope_initializers != null))
2346                                 SymbolWriter.CloseCompilerGeneratedBlock (ec.ig);
2347
2348                         ec.Mark (StartLocation, true);
2349                         DoEmit (ec);
2350
2351                         if (emit_debug_info) {
2352                                 EmitSymbolInfo (ec);
2353
2354                                 if (is_lexical_block)
2355                                         ec.EndScope ();
2356                         }
2357
2358                         ec.CurrentBlock = prev_block;
2359                 }
2360
2361                 protected virtual void EmitSymbolInfo (EmitContext ec)
2362                 {
2363                         if (variables != null) {
2364                                 foreach (DictionaryEntry de in variables) {
2365                                         string name = (string) de.Key;
2366                                         LocalInfo vi = (LocalInfo) de.Value;
2367
2368                                         vi.EmitSymbolInfo (ec, name);
2369                                 }
2370                         }
2371                 }
2372
2373                 public override string ToString ()
2374                 {
2375                         return String.Format ("{0} ({1}:{2})", GetType (),ID, StartLocation);
2376                 }
2377
2378                 protected override void CloneTo (CloneContext clonectx, Statement t)
2379                 {
2380                         Block target = (Block) t;
2381
2382                         clonectx.AddBlockMap (this, target);
2383
2384                         //target.Toplevel = (ToplevelBlock) clonectx.LookupBlock (Toplevel);
2385                         target.Explicit = (ExplicitBlock) clonectx.LookupBlock (Explicit);
2386                         if (Parent != null)
2387                                 target.Parent = clonectx.RemapBlockCopy (Parent);
2388
2389                         if (variables != null){
2390                                 target.variables = new Hashtable ();
2391
2392                                 foreach (DictionaryEntry de in variables){
2393                                         LocalInfo newlocal = ((LocalInfo) de.Value).Clone (clonectx);
2394                                         target.variables [de.Key] = newlocal;
2395                                         clonectx.AddVariableMap ((LocalInfo) de.Value, newlocal);
2396                                 }
2397                         }
2398
2399                         target.statements = new ArrayList (statements.Count);
2400                         foreach (Statement s in statements)
2401                                 target.statements.Add (s.Clone (clonectx));
2402
2403                         if (target.children != null){
2404                                 target.children = new ArrayList (children.Count);
2405                                 foreach (Block b in children){
2406                                         target.children.Add (clonectx.LookupBlock (b));
2407                                 }
2408                         }
2409
2410                         //
2411                         // TODO: labels, switch_block, constants (?), anonymous_children
2412                         //
2413                 }
2414         }
2415
2416         public class ExplicitBlock : Block {
2417                 public ExplicitBlock (Block parent, Location start, Location end)
2418                         : this (parent, (Flags) 0, start, end)
2419                 {
2420                 }
2421
2422                 public ExplicitBlock (Block parent, Flags flags, Location start, Location end)
2423                         : base (parent, flags, start, end)
2424                 {
2425                         this.Explicit = this;
2426                 }
2427
2428                 public bool IsIterator {
2429                         get { return (flags & Flags.IsIterator) != 0; }
2430                 }
2431
2432                 HybridDictionary known_variables;
2433
2434                 // <summary>
2435                 //   Marks a variable with name @name as being used in this or a child block.
2436                 //   If a variable name has been used in a child block, it's illegal to
2437                 //   declare a variable with the same name in the current block.
2438                 // </summary>
2439                 internal void AddKnownVariable (string name, IKnownVariable info)
2440                 {
2441                         if (known_variables == null)
2442                                 known_variables = new HybridDictionary();
2443
2444                         known_variables [name] = info;
2445
2446                         if (Parent != null)
2447                                 Parent.Explicit.AddKnownVariable (name, info);
2448                 }
2449
2450                 internal IKnownVariable GetKnownVariable (string name)
2451                 {
2452                         return known_variables == null ? null : (IKnownVariable) known_variables [name];
2453                 }
2454
2455                 protected override void CloneTo (CloneContext clonectx, Statement t)
2456                 {
2457                         ExplicitBlock target = (ExplicitBlock) t;
2458                         target.known_variables = null;
2459                         base.CloneTo (clonectx, t);
2460                 }
2461         }
2462
2463         public class ToplevelParameterInfo : IKnownVariable {
2464                 public readonly ToplevelBlock Block;
2465                 public readonly int Index;
2466                 public VariableInfo VariableInfo;
2467
2468                 Block IKnownVariable.Block {
2469                         get { return Block; }
2470                 }
2471                 public Parameter Parameter {
2472                         get { return Block.Parameters [Index]; }
2473                 }
2474                 public Location Location {
2475                         get { return Parameter.Location; }
2476                 }
2477
2478                 public ToplevelParameterInfo (ToplevelBlock block, int idx)
2479                 {
2480                         this.Block = block;
2481                         this.Index = idx;
2482                 }
2483         }
2484
2485         //
2486         // A toplevel block contains extra information, the split is done
2487         // only to separate information that would otherwise bloat the more
2488         // lightweight Block.
2489         //
2490         // In particular, this was introduced when the support for Anonymous
2491         // Methods was implemented. 
2492         // 
2493         public class ToplevelBlock : ExplicitBlock {
2494                 GenericMethod generic;
2495                 FlowBranchingToplevel top_level_branching;
2496                 AnonymousContainer anonymous_container;
2497                 RootScopeInfo root_scope;
2498                 Parameters parameters;
2499                 ToplevelParameterInfo[] parameter_info;
2500
2501                 public bool HasVarargs {
2502                         get { return (flags & Flags.HasVarargs) != 0; }
2503                         set { flags |= Flags.HasVarargs; }
2504                 }
2505
2506                 //
2507                 // The parameters for the block.
2508                 //
2509                 public Parameters Parameters {
2510                         get { return parameters; }
2511                 }
2512
2513                 public bool CompleteContexts (EmitContext ec)
2514                 {
2515                         Report.Debug (64, "TOPLEVEL COMPLETE CONTEXTS", this, Parent, root_scope);
2516
2517                         if (root_scope != null)
2518                                 root_scope.LinkScopes ();
2519
2520                         if (Parent == null && root_scope != null) {
2521                                 Report.Debug (64, "TOPLEVEL COMPLETE CONTEXTS #1", this, root_scope);
2522
2523                                 if (root_scope.DefineType () == null)
2524                                         return false;
2525                                 if (!root_scope.ResolveType ())
2526                                         return false;
2527                                 if (!root_scope.ResolveMembers ())
2528                                         return false;
2529                                 if (!root_scope.DefineMembers ())
2530                                         return false;
2531                         }
2532
2533                         return true;
2534                 }
2535
2536                 public GenericMethod GenericMethod {
2537                         get { return generic; }
2538                 }
2539
2540                 public ToplevelBlock Container {
2541                         get { return Parent == null ? null : Parent.Toplevel; }
2542                 }
2543
2544                 public AnonymousContainer AnonymousContainer {
2545                         get { return anonymous_container; }
2546                         set { anonymous_container = value; }
2547                 }
2548
2549                 public ToplevelBlock (Block parent, Parameters parameters, Location start) :
2550                         this (parent, (Flags) 0, parameters, start)
2551                 {
2552                 }
2553
2554                 public ToplevelBlock (Block parent, Parameters parameters, GenericMethod generic, Location start) :
2555                         this (parent, parameters, start)
2556                 {
2557                         this.generic = generic;
2558                 }
2559                 
2560                 public ToplevelBlock (Parameters parameters, Location start) :
2561                         this (null, (Flags) 0, parameters, start)
2562                 {
2563                 }
2564
2565                 public ToplevelBlock (Flags flags, Parameters parameters, Location start) :
2566                         this (null, flags, parameters, start)
2567                 {
2568                 }
2569
2570                 // We use 'Parent' to hook up to the containing block, but don't want to register the current block as a child.
2571                 // So, we use a two-stage setup -- first pass a null parent to the base constructor, and then override 'Parent'.
2572                 public ToplevelBlock (Block parent, Flags flags, Parameters parameters, Location start) :
2573                         base (null, flags, start, Location.Null)
2574                 {
2575                         this.Toplevel = this;
2576
2577                         this.parameters = parameters == null ? Parameters.EmptyReadOnlyParameters : parameters;
2578                         this.Parent = parent;
2579                         if (parent != null)
2580                                 parent.AddAnonymousChild (this);
2581
2582                         if (this.parameters.Count != 0)
2583                                 ProcessParameters ();
2584                 }
2585
2586                 public ToplevelBlock (Location loc) : this (null, (Flags) 0, null, loc)
2587                 {
2588                 }
2589
2590                 protected override void CloneTo (CloneContext clonectx, Statement t)
2591                 {
2592                         ToplevelBlock target = (ToplevelBlock) t;
2593                         base.CloneTo (clonectx, t);
2594
2595                         if (parameters.Count != 0)
2596                                 target.parameter_info = new ToplevelParameterInfo [parameters.Count];
2597                         for (int i = 0; i < parameters.Count; ++i)
2598                                 target.parameter_info [i] = new ToplevelParameterInfo (target, i);
2599                 }
2600
2601                 public bool CheckError158 (string name, Location loc)
2602                 {
2603                         if (AnonymousChildren != null) {
2604                                 foreach (ToplevelBlock child in AnonymousChildren) {
2605                                         if (!child.CheckError158 (name, loc))
2606                                                 return false;
2607                                 }
2608                         }
2609
2610                         for (ToplevelBlock c = Container; c != null; c = c.Container) {
2611                                 if (!c.DoCheckError158 (name, loc))
2612                                         return false;
2613                         }
2614
2615                         return true;
2616                 }
2617
2618                 public virtual Expression GetTransparentIdentifier (string name)
2619                 {
2620                         return null;
2621                 }
2622
2623                 void ProcessParameters ()
2624                 {
2625                         int n = parameters.Count;
2626                         parameter_info = new ToplevelParameterInfo [n];
2627                         for (int i = 0; i < n; ++i) {
2628                                 parameter_info [i] = new ToplevelParameterInfo (this, i);
2629
2630                                 Parameter p = parameters [i];
2631                                 if (p == null)
2632                                         continue;
2633
2634                                 string name = p.Name;
2635                                 LocalInfo vi = GetLocalInfo (name);
2636                                 if (vi != null) {
2637                                         Report.SymbolRelatedToPreviousError (vi.Location, name);
2638                                         Error_AlreadyDeclared (loc, name, "parent or current");
2639                                         continue;
2640                                 }
2641
2642                                 ToplevelParameterInfo pi = Parent == null ? null : Parent.Toplevel.GetParameterInfo (name);
2643                                 if (pi != null) {
2644                                         Report.SymbolRelatedToPreviousError (pi.Location, name);
2645                                         Error_AlreadyDeclared (loc, name, "parent or current");
2646                                         continue;
2647                                 }
2648
2649                                 AddKnownVariable (name, parameter_info [i]);
2650                         }
2651
2652                         // mark this block as "used" so that we create local declarations in a sub-block
2653                         // FIXME: This appears to uncover a lot of bugs
2654                         //this.Use ();
2655                 }
2656
2657                 bool DoCheckError158 (string name, Location loc)
2658                 {
2659                         LabeledStatement s = LookupLabel (name);
2660                         if (s != null) {
2661                                 Report.SymbolRelatedToPreviousError (s.loc, s.Name);
2662                                 Error_158 (name, loc);
2663                                 return false;
2664                         }
2665
2666                         return true;
2667                 }
2668
2669                 public RootScopeInfo CreateRootScope (TypeContainer host)
2670                 {
2671                         if (root_scope != null)
2672                                 return root_scope;
2673
2674                         if (Container == null)
2675                                 root_scope = new RootScopeInfo (
2676                                         this, host, generic, StartLocation);
2677
2678                         if (scope_info != null)
2679                                 throw new InternalErrorException ();
2680
2681                         scope_info = root_scope;
2682                         return root_scope;
2683                 }
2684
2685                 public override Expression CreateExpressionTree (EmitContext ec)
2686                 {
2687                         return ((Statement) statements [0]).CreateExpressionTree (ec);
2688                 }
2689
2690                 public void CreateIteratorHost (RootScopeInfo root)
2691                 {
2692                         Report.Debug (64, "CREATE ITERATOR HOST", this, root, Parent, root_scope);
2693
2694                         if (Parent != null || root_scope != null)
2695                                 throw new InternalErrorException ();
2696
2697                         scope_info = root_scope = root;
2698                 }
2699
2700                 public RootScopeInfo RootScope {
2701                         get {
2702                                 if (root_scope != null)
2703                                         return root_scope;
2704                                 else if (Container != null)
2705                                         return Container.RootScope;
2706                                 else
2707                                         return null;
2708                         }
2709                 }
2710
2711                 public FlowBranchingToplevel TopLevelBranching {
2712                         get { return top_level_branching; }
2713                 }
2714
2715                 //
2716                 // This is used if anonymous methods are used inside an iterator
2717                 // (see 2test-22.cs for an example).
2718                 //
2719                 // The AnonymousMethod is created while parsing - at a time when we don't
2720                 // know yet that we're inside an iterator, so it's `Container' is initially
2721                 // null.  Later on, when resolving the iterator, we need to move the
2722                 // anonymous method into that iterator.
2723                 //
2724                 public void ReParent (ToplevelBlock new_parent)
2725                 {
2726                         if ((flags & Flags.VariablesInitialized) != 0)
2727                                 throw new InternalErrorException ("block has already been resolved");
2728
2729                         Parent = new_parent;
2730                 }
2731
2732                 //
2733                 // Returns a `ParameterReference' for the given name, or null if there
2734                 // is no such parameter
2735                 //
2736                 public ParameterReference GetParameterReference (string name, Location loc)
2737                 {
2738                         ToplevelParameterInfo p = GetParameterInfo (name);
2739                         return p == null ? null : new ParameterReference (this, p, loc);
2740                 }
2741
2742                 public ToplevelParameterInfo GetParameterInfo (string name)
2743                 {
2744                         int idx;
2745                         for (ToplevelBlock t = this; t != null; t = t.Container) {
2746                                 Parameter par = t.Parameters.GetParameterByName (name, out idx);
2747                                 if (par != null)
2748                                         return t.parameter_info [idx];
2749                         }
2750                         return null;
2751                 }
2752
2753                 LocalInfo this_variable = null;
2754
2755                 // <summary>
2756                 //   Returns the "this" instance variable of this block.
2757                 //   See AddThisVariable() for more information.
2758                 // </summary>
2759                 public LocalInfo ThisVariable {
2760                         get { return this_variable; }
2761                 }
2762
2763
2764                 // <summary>
2765                 //   This is used by non-static `struct' constructors which do not have an
2766                 //   initializer - in this case, the constructor must initialize all of the
2767                 //   struct's fields.  To do this, we add a "this" variable and use the flow
2768                 //   analysis code to ensure that it's been fully initialized before control
2769                 //   leaves the constructor.
2770                 // </summary>
2771                 public LocalInfo AddThisVariable (DeclSpace ds, Location l)
2772                 {
2773                         if (this_variable == null) {
2774                                 this_variable = new LocalInfo (ds, this, l);
2775                                 this_variable.Used = true;
2776                                 this_variable.IsThis = true;
2777
2778                                 Variables.Add ("this", this_variable);
2779                         }
2780
2781                         return this_variable;
2782                 }
2783
2784                 public bool IsThisAssigned (EmitContext ec)
2785                 {
2786                         return this_variable == null || this_variable.IsThisAssigned (ec);
2787                 }
2788
2789                 public bool ResolveMeta (EmitContext ec, Parameters ip)
2790                 {
2791                         int errors = Report.Errors;
2792                         int orig_count = parameters.Count;
2793
2794                         if (top_level_branching != null)
2795                                 return true;
2796
2797                         if (ip != null)
2798                                 parameters = ip;
2799
2800                         // Assert: orig_count != parameter.Count => orig_count == 0
2801                         if (orig_count != 0 && orig_count != parameters.Count)
2802                                 throw new InternalErrorException ("parameter information mismatch");
2803
2804                         int offset = Parent == null ? 0 : Parent.AssignableSlots;
2805
2806                         for (int i = 0; i < orig_count; ++i) {
2807                                 Parameter.Modifier mod = parameters.ParameterModifier (i);
2808
2809                                 if ((mod & Parameter.Modifier.OUT) != Parameter.Modifier.OUT)
2810                                         continue;
2811
2812                                 VariableInfo vi = new VariableInfo (ip, i, offset);
2813                                 parameter_info [i].VariableInfo = vi;
2814                                 offset += vi.Length;
2815                         }
2816
2817                         ResolveMeta (ec, offset);
2818
2819                         top_level_branching = ec.StartFlowBranching (this);
2820
2821                         return Report.Errors == errors;
2822                 }
2823
2824                 // <summary>
2825                 //   Check whether all `out' parameters have been assigned.
2826                 // </summary>
2827                 public void CheckOutParameters (FlowBranching.UsageVector vector, Location loc)
2828                 {
2829                         if (vector.IsUnreachable)
2830                                 return;
2831
2832                         int n = parameter_info == null ? 0 : parameter_info.Length;
2833
2834                         for (int i = 0; i < n; i++) {
2835                                 VariableInfo var = parameter_info [i].VariableInfo;
2836
2837                                 if (var == null)
2838                                         continue;
2839
2840                                 if (vector.IsAssigned (var, false))
2841                                         continue;
2842
2843                                 Report.Error (177, loc, "The out parameter `{0}' must be assigned to before control leaves the current method",
2844                                         var.Name);
2845                         }
2846                 }
2847
2848                 public override void EmitMeta (EmitContext ec)
2849                 {
2850                         // Avoid declaring an IL variable for this_variable since it is not accessed
2851                         // from the generated IL
2852                         if (this_variable != null)
2853                                 Variables.Remove ("this");
2854                         base.EmitMeta (ec);
2855                         parameters.ResolveVariable (this);
2856                 }
2857
2858                 protected override void EmitSymbolInfo (EmitContext ec)
2859                 {
2860                         if ((AnonymousContainer != null) && (AnonymousContainer.Scope != null))
2861                                 SymbolWriter.DefineScopeVariable (AnonymousContainer.Scope.ID);
2862
2863                         base.EmitSymbolInfo (ec);
2864                 }
2865
2866                 public void MakeIterator (Iterator iterator)
2867                 {
2868                         flags |= Flags.IsIterator;
2869
2870                         Block block = new ExplicitBlock (this, flags, StartLocation, EndLocation);
2871                         foreach (Statement stmt in statements)
2872                                 block.AddStatement (stmt);
2873                         statements.Clear ();
2874                         statements.Add (new MoveNextStatement (iterator, block));
2875                 }
2876
2877                 protected class MoveNextStatement : Statement {
2878                         Iterator iterator;
2879                         Block block;
2880
2881                         public MoveNextStatement (Iterator iterator, Block block)
2882                         {
2883                                 this.iterator = iterator;
2884                                 this.block = block;
2885                                 this.loc = iterator.Location;
2886                         }
2887
2888                         public override bool Resolve (EmitContext ec)
2889                         {
2890                                 return block.Resolve (ec);
2891                         }
2892
2893                         protected override void DoEmit (EmitContext ec)
2894                         {
2895                                 iterator.EmitMoveNext (ec, block);
2896                         }
2897                 }
2898
2899                 public override string ToString ()
2900                 {
2901                         return String.Format ("{0} ({1}:{2}{3}:{4})", GetType (), ID, StartLocation,
2902                                               root_scope, anonymous_container != null ?
2903                                               anonymous_container.Scope : null);
2904                 }
2905         }
2906         
2907         public class SwitchLabel {
2908                 Expression label;
2909                 object converted;
2910                 Location loc;
2911
2912                 Label il_label;
2913                 bool  il_label_set;
2914                 Label il_label_code;
2915                 bool  il_label_code_set;
2916
2917                 public static readonly object NullStringCase = new object ();
2918
2919                 //
2920                 // if expr == null, then it is the default case.
2921                 //
2922                 public SwitchLabel (Expression expr, Location l)
2923                 {
2924                         label = expr;
2925                         loc = l;
2926                 }
2927
2928                 public Expression Label {
2929                         get {
2930                                 return label;
2931                         }
2932                 }
2933
2934                 public object Converted {
2935                         get {
2936                                 return converted;
2937                         }
2938                 }
2939
2940                 public Label GetILLabel (EmitContext ec)
2941                 {
2942                         if (!il_label_set){
2943                                 il_label = ec.ig.DefineLabel ();
2944                                 il_label_set = true;
2945                         }
2946                         return il_label;
2947                 }
2948
2949                 public Label GetILLabelCode (EmitContext ec)
2950                 {
2951                         if (!il_label_code_set){
2952                                 il_label_code = ec.ig.DefineLabel ();
2953                                 il_label_code_set = true;
2954                         }
2955                         return il_label_code;
2956                 }                               
2957                 
2958                 //
2959                 // Resolves the expression, reduces it to a literal if possible
2960                 // and then converts it to the requested type.
2961                 //
2962                 public bool ResolveAndReduce (EmitContext ec, Type required_type, bool allow_nullable)
2963                 {       
2964                         Expression e = label.Resolve (ec);
2965
2966                         if (e == null)
2967                                 return false;
2968
2969                         Constant c = e as Constant;
2970                         if (c == null){
2971                                 Report.Error (150, loc, "A constant value is expected");
2972                                 return false;
2973                         }
2974
2975                         if (required_type == TypeManager.string_type && c.GetValue () == null) {
2976                                 converted = NullStringCase;
2977                                 return true;
2978                         }
2979
2980                         if (allow_nullable && c.GetValue () == null) {
2981                                 converted = NullStringCase;
2982                                 return true;
2983                         }
2984                         
2985                         c = c.ImplicitConversionRequired (required_type, loc);
2986                         if (c == null)
2987                                 return false;
2988
2989                         converted = c.GetValue ();
2990                         return true;
2991                 }
2992
2993                 public void Error_AlreadyOccurs (Type switch_type, SwitchLabel collision_with)
2994                 {
2995                         string label;
2996                         if (converted == null)
2997                                 label = "default";
2998                         else if (converted == NullStringCase)
2999                                 label = "null";
3000                         else if (TypeManager.IsEnumType (switch_type)) 
3001                                 label = TypeManager.CSharpEnumValue (switch_type, converted);
3002                         else
3003                                 label = converted.ToString ();
3004                         
3005                         Report.SymbolRelatedToPreviousError (collision_with.loc, null);
3006                         Report.Error (152, loc, "The label `case {0}:' already occurs in this switch statement", label);
3007                 }
3008
3009                 public SwitchLabel Clone (CloneContext clonectx)
3010                 {
3011                         return new SwitchLabel (label.Clone (clonectx), loc);
3012                 }
3013         }
3014
3015         public class SwitchSection {
3016                 // An array of SwitchLabels.
3017                 public readonly ArrayList Labels;
3018                 public readonly Block Block;
3019                 
3020                 public SwitchSection (ArrayList labels, Block block)
3021                 {
3022                         Labels = labels;
3023                         Block = block;
3024                 }
3025
3026                 public SwitchSection Clone (CloneContext clonectx)
3027                 {
3028                         ArrayList cloned_labels = new ArrayList ();
3029
3030                         foreach (SwitchLabel sl in cloned_labels)
3031                                 cloned_labels.Add (sl.Clone (clonectx));
3032                         
3033                         return new SwitchSection (cloned_labels, clonectx.LookupBlock (Block));
3034                 }
3035         }
3036         
3037         public class Switch : Statement {
3038                 public ArrayList Sections;
3039                 public Expression Expr;
3040
3041                 /// <summary>
3042                 ///   Maps constants whose type type SwitchType to their  SwitchLabels.
3043                 /// </summary>
3044                 public IDictionary Elements;
3045
3046                 /// <summary>
3047                 ///   The governing switch type
3048                 /// </summary>
3049                 public Type SwitchType;
3050
3051                 //
3052                 // Computed
3053                 //
3054                 Label default_target;
3055                 Label null_target;
3056                 Expression new_expr;
3057                 bool is_constant;
3058                 SwitchSection constant_section;
3059                 SwitchSection default_section;
3060
3061 #if GMCS_SOURCE
3062                 //
3063                 // Nullable Types support for GMCS.
3064                 //
3065                 Nullable.Unwrap unwrap;
3066
3067                 protected bool HaveUnwrap {
3068                         get { return unwrap != null; }
3069                 }
3070 #else
3071                 protected bool HaveUnwrap {
3072                         get { return false; }
3073                 }
3074 #endif
3075
3076                 //
3077                 // The types allowed to be implicitly cast from
3078                 // on the governing type
3079                 //
3080                 static Type [] allowed_types;
3081                 
3082                 public Switch (Expression e, ArrayList sects, Location l)
3083                 {
3084                         Expr = e;
3085                         Sections = sects;
3086                         loc = l;
3087                 }
3088
3089                 public bool GotDefault {
3090                         get {
3091                                 return default_section != null;
3092                         }
3093                 }
3094
3095                 public Label DefaultTarget {
3096                         get {
3097                                 return default_target;
3098                         }
3099                 }
3100
3101                 //
3102                 // Determines the governing type for a switch.  The returned
3103                 // expression might be the expression from the switch, or an
3104                 // expression that includes any potential conversions to the
3105                 // integral types or to string.
3106                 //
3107                 Expression SwitchGoverningType (EmitContext ec, Expression expr)
3108                 {
3109                         Type t = expr.Type;
3110
3111                         if (t == TypeManager.byte_type ||
3112                             t == TypeManager.sbyte_type ||
3113                             t == TypeManager.ushort_type ||
3114                             t == TypeManager.short_type ||
3115                             t == TypeManager.uint32_type ||
3116                             t == TypeManager.int32_type ||
3117                             t == TypeManager.uint64_type ||
3118                             t == TypeManager.int64_type ||
3119                             t == TypeManager.char_type ||
3120                             t == TypeManager.string_type ||
3121                             t == TypeManager.bool_type ||
3122                             TypeManager.IsEnumType (t))
3123                                 return expr;
3124
3125                         if (allowed_types == null){
3126                                 allowed_types = new Type [] {
3127                                         TypeManager.sbyte_type,
3128                                         TypeManager.byte_type,
3129                                         TypeManager.short_type,
3130                                         TypeManager.ushort_type,
3131                                         TypeManager.int32_type,
3132                                         TypeManager.uint32_type,
3133                                         TypeManager.int64_type,
3134                                         TypeManager.uint64_type,
3135                                         TypeManager.char_type,
3136                                         TypeManager.string_type,
3137                                         TypeManager.bool_type
3138                                 };
3139                         }
3140
3141                         //
3142                         // Try to find a *user* defined implicit conversion.
3143                         //
3144                         // If there is no implicit conversion, or if there are multiple
3145                         // conversions, we have to report an error
3146                         //
3147                         Expression converted = null;
3148                         foreach (Type tt in allowed_types){
3149                                 Expression e;
3150                                 
3151                                 e = Convert.ImplicitUserConversion (ec, expr, tt, loc);
3152                                 if (e == null)
3153                                         continue;
3154
3155                                 //
3156                                 // Ignore over-worked ImplicitUserConversions that do
3157                                 // an implicit conversion in addition to the user conversion.
3158                                 // 
3159                                 if (!(e is UserCast))
3160                                         continue;
3161
3162                                 if (converted != null){
3163                                         Report.ExtraInformation (
3164                                                 loc,
3165                                                 String.Format ("reason: more than one conversion to an integral type exist for type {0}",
3166                                                                TypeManager.CSharpName (expr.Type)));
3167                                         return null;
3168                                 }
3169
3170                                 converted = e;
3171                         }
3172                         return converted;
3173                 }
3174
3175                 //
3176                 // Performs the basic sanity checks on the switch statement
3177                 // (looks for duplicate keys and non-constant expressions).
3178                 //
3179                 // It also returns a hashtable with the keys that we will later
3180                 // use to compute the switch tables
3181                 //
3182                 bool CheckSwitch (EmitContext ec)
3183                 {
3184                         bool error = false;
3185                         Elements = Sections.Count > 10 ? 
3186                                 (IDictionary)new Hashtable () : 
3187                                 (IDictionary)new ListDictionary ();
3188                                 
3189                         foreach (SwitchSection ss in Sections){
3190                                 foreach (SwitchLabel sl in ss.Labels){
3191                                         if (sl.Label == null){
3192                                                 if (default_section != null){
3193                                                         sl.Error_AlreadyOccurs (SwitchType, (SwitchLabel)default_section.Labels [0]);
3194                                                         error = true;
3195                                                 }
3196                                                 default_section = ss;
3197                                                 continue;
3198                                         }
3199
3200                                         if (!sl.ResolveAndReduce (ec, SwitchType, HaveUnwrap)) {
3201                                                 error = true;
3202                                                 continue;
3203                                         }
3204                                         
3205                                         object key = sl.Converted;
3206                                         try {
3207                                                 Elements.Add (key, sl);
3208                                         } catch (ArgumentException) {
3209                                                 sl.Error_AlreadyOccurs (SwitchType, (SwitchLabel)Elements [key]);
3210                                                 error = true;
3211                                         }
3212                                 }
3213                         }
3214                         return !error;
3215                 }
3216
3217                 void EmitObjectInteger (ILGenerator ig, object k)
3218                 {
3219                         if (k is int)
3220                                 IntConstant.EmitInt (ig, (int) k);
3221                         else if (k is Constant) {
3222                                 EmitObjectInteger (ig, ((Constant) k).GetValue ());
3223                         } 
3224                         else if (k is uint)
3225                                 IntConstant.EmitInt (ig, unchecked ((int) (uint) k));
3226                         else if (k is long)
3227                         {
3228                                 if ((long) k >= int.MinValue && (long) k <= int.MaxValue)
3229                                 {
3230                                         IntConstant.EmitInt (ig, (int) (long) k);
3231                                         ig.Emit (OpCodes.Conv_I8);
3232                                 }
3233                                 else
3234                                         LongConstant.EmitLong (ig, (long) k);
3235                         }
3236                         else if (k is ulong)
3237                         {
3238                                 ulong ul = (ulong) k;
3239                                 if (ul < (1L<<32))
3240                                 {
3241                                         IntConstant.EmitInt (ig, unchecked ((int) ul));
3242                                         ig.Emit (OpCodes.Conv_U8);
3243                                 }
3244                                 else
3245                                 {
3246                                         LongConstant.EmitLong (ig, unchecked ((long) ul));
3247                                 }
3248                         }
3249                         else if (k is char)
3250                                 IntConstant.EmitInt (ig, (int) ((char) k));
3251                         else if (k is sbyte)
3252                                 IntConstant.EmitInt (ig, (int) ((sbyte) k));
3253                         else if (k is byte)
3254                                 IntConstant.EmitInt (ig, (int) ((byte) k));
3255                         else if (k is short)
3256                                 IntConstant.EmitInt (ig, (int) ((short) k));
3257                         else if (k is ushort)
3258                                 IntConstant.EmitInt (ig, (int) ((ushort) k));
3259                         else if (k is bool)
3260                                 IntConstant.EmitInt (ig, ((bool) k) ? 1 : 0);
3261                         else
3262                                 throw new Exception ("Unhandled case");
3263                 }
3264                 
3265                 // structure used to hold blocks of keys while calculating table switch
3266                 class KeyBlock : IComparable
3267                 {
3268                         public KeyBlock (long _first)
3269                         {
3270                                 first = last = _first;
3271                         }
3272                         public long first;
3273                         public long last;
3274                         public ArrayList element_keys = null;
3275                         // how many items are in the bucket
3276                         public int Size = 1;
3277                         public int Length
3278                         {
3279                                 get { return (int) (last - first + 1); }
3280                         }
3281                         public static long TotalLength (KeyBlock kb_first, KeyBlock kb_last)
3282                         {
3283                                 return kb_last.last - kb_first.first + 1;
3284                         }
3285                         public int CompareTo (object obj)
3286                         {
3287                                 KeyBlock kb = (KeyBlock) obj;
3288                                 int nLength = Length;
3289                                 int nLengthOther = kb.Length;
3290                                 if (nLengthOther == nLength)
3291                                         return (int) (kb.first - first);
3292                                 return nLength - nLengthOther;
3293                         }
3294                 }
3295
3296                 /// <summary>
3297                 /// This method emits code for a lookup-based switch statement (non-string)
3298                 /// Basically it groups the cases into blocks that are at least half full,
3299                 /// and then spits out individual lookup opcodes for each block.
3300                 /// It emits the longest blocks first, and short blocks are just
3301                 /// handled with direct compares.
3302                 /// </summary>
3303                 /// <param name="ec"></param>
3304                 /// <param name="val"></param>
3305                 /// <returns></returns>
3306                 void TableSwitchEmit (EmitContext ec, LocalBuilder val)
3307                 {
3308                         int element_count = Elements.Count;
3309                         object [] element_keys = new object [element_count];
3310                         Elements.Keys.CopyTo (element_keys, 0);
3311                         Array.Sort (element_keys);
3312
3313                         // initialize the block list with one element per key
3314                         ArrayList key_blocks = new ArrayList ();
3315                         foreach (object key in element_keys)
3316                                 key_blocks.Add (new KeyBlock (System.Convert.ToInt64 (key)));
3317
3318                         KeyBlock current_kb;
3319                         // iteratively merge the blocks while they are at least half full
3320                         // there's probably a really cool way to do this with a tree...
3321                         while (key_blocks.Count > 1)
3322                         {
3323                                 ArrayList key_blocks_new = new ArrayList ();
3324                                 current_kb = (KeyBlock) key_blocks [0];
3325                                 for (int ikb = 1; ikb < key_blocks.Count; ikb++)
3326                                 {
3327                                         KeyBlock kb = (KeyBlock) key_blocks [ikb];
3328                                         if ((current_kb.Size + kb.Size) * 2 >=  KeyBlock.TotalLength (current_kb, kb))
3329                                         {
3330                                                 // merge blocks
3331                                                 current_kb.last = kb.last;
3332                                                 current_kb.Size += kb.Size;
3333                                         }
3334                                         else
3335                                         {
3336                                                 // start a new block
3337                                                 key_blocks_new.Add (current_kb);
3338                                                 current_kb = kb;
3339                                         }
3340                                 }
3341                                 key_blocks_new.Add (current_kb);
3342                                 if (key_blocks.Count == key_blocks_new.Count)
3343                                         break;
3344                                 key_blocks = key_blocks_new;
3345                         }
3346
3347                         // initialize the key lists
3348                         foreach (KeyBlock kb in key_blocks)
3349                                 kb.element_keys = new ArrayList ();
3350
3351                         // fill the key lists
3352                         int iBlockCurr = 0;
3353                         if (key_blocks.Count > 0) {
3354                                 current_kb = (KeyBlock) key_blocks [0];
3355                                 foreach (object key in element_keys)
3356                                 {
3357                                         bool next_block = (key is UInt64) ? (ulong) key > (ulong) current_kb.last :
3358                                                 System.Convert.ToInt64 (key) > current_kb.last;
3359                                         if (next_block)
3360                                                 current_kb = (KeyBlock) key_blocks [++iBlockCurr];
3361                                         current_kb.element_keys.Add (key);
3362                                 }
3363                         }
3364
3365                         // sort the blocks so we can tackle the largest ones first
3366                         key_blocks.Sort ();
3367
3368                         // okay now we can start...
3369                         ILGenerator ig = ec.ig;
3370                         Label lbl_end = ig.DefineLabel ();      // at the end ;-)
3371                         Label lbl_default = ig.DefineLabel ();
3372
3373                         Type type_keys = null;
3374                         if (element_keys.Length > 0)
3375                                 type_keys = element_keys [0].GetType ();        // used for conversions
3376
3377                         Type compare_type;
3378                         
3379                         if (TypeManager.IsEnumType (SwitchType))
3380                                 compare_type = TypeManager.GetEnumUnderlyingType (SwitchType);
3381                         else
3382                                 compare_type = SwitchType;
3383                         
3384                         for (int iBlock = key_blocks.Count - 1; iBlock >= 0; --iBlock)
3385                         {
3386                                 KeyBlock kb = ((KeyBlock) key_blocks [iBlock]);
3387                                 lbl_default = (iBlock == 0) ? DefaultTarget : ig.DefineLabel ();
3388                                 if (kb.Length <= 2)
3389                                 {
3390                                         foreach (object key in kb.element_keys)
3391                                         {
3392                                                 ig.Emit (OpCodes.Ldloc, val);
3393                                                 EmitObjectInteger (ig, key);
3394                                                 SwitchLabel sl = (SwitchLabel) Elements [key];
3395                                                 ig.Emit (OpCodes.Beq, sl.GetILLabel (ec));
3396                                         }
3397                                 }
3398                                 else
3399                                 {
3400                                         // TODO: if all the keys in the block are the same and there are
3401                                         //       no gaps/defaults then just use a range-check.
3402                                         if (compare_type == TypeManager.int64_type ||
3403                                                 compare_type == TypeManager.uint64_type)
3404                                         {
3405                                                 // TODO: optimize constant/I4 cases
3406
3407                                                 // check block range (could be > 2^31)
3408                                                 ig.Emit (OpCodes.Ldloc, val);
3409                                                 EmitObjectInteger (ig, System.Convert.ChangeType (kb.first, type_keys));
3410                                                 ig.Emit (OpCodes.Blt, lbl_default);
3411                                                 ig.Emit (OpCodes.Ldloc, val);
3412                                                 EmitObjectInteger (ig, System.Convert.ChangeType (kb.last, type_keys));
3413                                                 ig.Emit (OpCodes.Bgt, lbl_default);
3414
3415                                                 // normalize range
3416                                                 ig.Emit (OpCodes.Ldloc, val);
3417                                                 if (kb.first != 0)
3418                                                 {
3419                                                         EmitObjectInteger (ig, System.Convert.ChangeType (kb.first, type_keys));
3420                                                         ig.Emit (OpCodes.Sub);
3421                                                 }
3422                                                 ig.Emit (OpCodes.Conv_I4);      // assumes < 2^31 labels!
3423                                         }
3424                                         else
3425                                         {
3426                                                 // normalize range
3427                                                 ig.Emit (OpCodes.Ldloc, val);
3428                                                 int first = (int) kb.first;
3429                                                 if (first > 0)
3430                                                 {
3431                                                         IntConstant.EmitInt (ig, first);
3432                                                         ig.Emit (OpCodes.Sub);
3433                                                 }
3434                                                 else if (first < 0)
3435                                                 {
3436                                                         IntConstant.EmitInt (ig, -first);
3437                                                         ig.Emit (OpCodes.Add);
3438                                                 }
3439                                         }
3440
3441                                         // first, build the list of labels for the switch
3442                                         int iKey = 0;
3443                                         int cJumps = kb.Length;
3444                                         Label [] switch_labels = new Label [cJumps];
3445                                         for (int iJump = 0; iJump < cJumps; iJump++)
3446                                         {
3447                                                 object key = kb.element_keys [iKey];
3448                                                 if (System.Convert.ToInt64 (key) == kb.first + iJump)
3449                                                 {
3450                                                         SwitchLabel sl = (SwitchLabel) Elements [key];
3451                                                         switch_labels [iJump] = sl.GetILLabel (ec);
3452                                                         iKey++;
3453                                                 }
3454                                                 else
3455                                                         switch_labels [iJump] = lbl_default;
3456                                         }
3457                                         // emit the switch opcode
3458                                         ig.Emit (OpCodes.Switch, switch_labels);
3459                                 }
3460
3461                                 // mark the default for this block
3462                                 if (iBlock != 0)
3463                                         ig.MarkLabel (lbl_default);
3464                         }
3465
3466                         // TODO: find the default case and emit it here,
3467                         //       to prevent having to do the following jump.
3468                         //       make sure to mark other labels in the default section
3469
3470                         // the last default just goes to the end
3471                         ig.Emit (OpCodes.Br, lbl_default);
3472
3473                         // now emit the code for the sections
3474                         bool found_default = false;
3475                         bool found_null = false;
3476                         foreach (SwitchSection ss in Sections)
3477                         {
3478                                 foreach (SwitchLabel sl in ss.Labels)
3479                                         if (sl.Converted == SwitchLabel.NullStringCase)
3480                                                 found_null = true;
3481                         }
3482
3483                         foreach (SwitchSection ss in Sections)
3484                         {
3485                                 foreach (SwitchLabel sl in ss.Labels)
3486                                 {
3487                                         ig.MarkLabel (sl.GetILLabel (ec));
3488                                         ig.MarkLabel (sl.GetILLabelCode (ec));
3489                                         if (sl.Converted == SwitchLabel.NullStringCase)
3490                                                 ig.MarkLabel (null_target);
3491                                         else if (sl.Label == null) {
3492                                                 ig.MarkLabel (lbl_default);
3493                                                 found_default = true;
3494                                                 if (!found_null)
3495                                                         ig.MarkLabel (null_target);
3496                                         }
3497                                 }
3498                                 ss.Block.Emit (ec);
3499                         }
3500                         
3501                         if (!found_default) {
3502                                 ig.MarkLabel (lbl_default);
3503                                 if (HaveUnwrap && !found_null) {
3504                                         ig.MarkLabel (null_target);
3505                                 }
3506                         }
3507                         
3508                         ig.MarkLabel (lbl_end);
3509                 }
3510                 //
3511                 // This simple emit switch works, but does not take advantage of the
3512                 // `switch' opcode. 
3513                 // TODO: remove non-string logic from here
3514                 // TODO: binary search strings?
3515                 //
3516                 void SimpleSwitchEmit (EmitContext ec, LocalBuilder val)
3517                 {
3518                         ILGenerator ig = ec.ig;
3519                         Label end_of_switch = ig.DefineLabel ();
3520                         Label next_test = ig.DefineLabel ();
3521                         bool first_test = true;
3522                         bool pending_goto_end = false;
3523                         bool null_marked = false;
3524                         bool null_found;
3525                         int section_count = Sections.Count;
3526
3527                         // TODO: implement switch optimization for string by using Hashtable
3528                         //if (SwitchType == TypeManager.string_type && section_count > 7)
3529                         //      Console.WriteLine ("Switch optimization possible " + loc);
3530
3531                         ig.Emit (OpCodes.Ldloc, val);
3532                         
3533                         if (Elements.Contains (SwitchLabel.NullStringCase)){
3534                                 ig.Emit (OpCodes.Brfalse, null_target);
3535                         } else
3536                                 ig.Emit (OpCodes.Brfalse, default_target);
3537                         
3538                         ig.Emit (OpCodes.Ldloc, val);
3539                         ig.Emit (OpCodes.Call, TypeManager.string_isinterned_string);
3540                         ig.Emit (OpCodes.Stloc, val);
3541
3542                         for (int section = 0; section < section_count; section++){
3543                                 SwitchSection ss = (SwitchSection) Sections [section];
3544
3545                                 if (ss == default_section)
3546                                         continue;
3547
3548                                 Label sec_begin = ig.DefineLabel ();
3549
3550                                 ig.Emit (OpCodes.Nop);
3551
3552                                 if (pending_goto_end)
3553                                         ig.Emit (OpCodes.Br, end_of_switch);
3554
3555                                 int label_count = ss.Labels.Count;
3556                                 null_found = false;
3557                                 for (int label = 0; label < label_count; label++){
3558                                         SwitchLabel sl = (SwitchLabel) ss.Labels [label];
3559                                         ig.MarkLabel (sl.GetILLabel (ec));
3560                                         
3561                                         if (!first_test){
3562                                                 ig.MarkLabel (next_test);
3563                                                 next_test = ig.DefineLabel ();
3564                                         }
3565                                         //
3566                                         // If we are the default target
3567                                         //
3568                                         if (sl.Label != null){
3569                                                 object lit = sl.Converted;
3570
3571                                                 if (lit == SwitchLabel.NullStringCase){
3572                                                         null_found = true;
3573                                                         if (label + 1 == label_count)
3574                                                                 ig.Emit (OpCodes.Br, next_test);
3575                                                         continue;
3576                                                 }
3577                                                 
3578                                                 ig.Emit (OpCodes.Ldloc, val);
3579                                                 ig.Emit (OpCodes.Ldstr, (string)lit);
3580                                                 if (label_count == 1)
3581                                                         ig.Emit (OpCodes.Bne_Un, next_test);
3582                                                 else {
3583                                                         if (label+1 == label_count)
3584                                                                 ig.Emit (OpCodes.Bne_Un, next_test);
3585                                                         else
3586                                                                 ig.Emit (OpCodes.Beq, sec_begin);
3587                                                 }
3588                                         }
3589                                 }
3590                                 if (null_found) {
3591                                         ig.MarkLabel (null_target);
3592                                         null_marked = true;
3593                                 }
3594                                 ig.MarkLabel (sec_begin);
3595                                 foreach (SwitchLabel sl in ss.Labels)
3596                                         ig.MarkLabel (sl.GetILLabelCode (ec));
3597
3598                                 ss.Block.Emit (ec);
3599                                 pending_goto_end = !ss.Block.HasRet;
3600                                 first_test = false;
3601                         }
3602                         ig.MarkLabel (next_test);
3603                         ig.MarkLabel (default_target);
3604                         if (!null_marked)
3605                                 ig.MarkLabel (null_target);
3606                         if (default_section != null)
3607                                 default_section.Block.Emit (ec);
3608                         ig.MarkLabel (end_of_switch);
3609                 }
3610
3611                 SwitchSection FindSection (SwitchLabel label)
3612                 {
3613                         foreach (SwitchSection ss in Sections){
3614                                 foreach (SwitchLabel sl in ss.Labels){
3615                                         if (label == sl)
3616                                                 return ss;
3617                                 }
3618                         }
3619
3620                         return null;
3621                 }
3622
3623                 public override bool Resolve (EmitContext ec)
3624                 {
3625                         Expr = Expr.Resolve (ec);
3626                         if (Expr == null)
3627                                 return false;
3628
3629                         new_expr = SwitchGoverningType (ec, Expr);
3630
3631 #if GMCS_SOURCE
3632                         if ((new_expr == null) && TypeManager.IsNullableType (Expr.Type)) {
3633                                 unwrap = Nullable.Unwrap.Create (Expr, ec);
3634                                 if (unwrap == null)
3635                                         return false;
3636
3637                                 new_expr = SwitchGoverningType (ec, unwrap);
3638                         }
3639 #endif
3640
3641                         if (new_expr == null){
3642                                 Report.Error (151, loc, "A value of an integral type or string expected for switch");
3643                                 return false;
3644                         }
3645
3646                         // Validate switch.
3647                         SwitchType = new_expr.Type;
3648
3649                         if (RootContext.Version == LanguageVersion.ISO_1 && SwitchType == TypeManager.bool_type) {
3650                                 Report.FeatureIsNotAvailable (loc, "switch expression of boolean type");
3651                                 return false;
3652                         }
3653
3654                         if (!CheckSwitch (ec))
3655                                 return false;
3656
3657                         if (HaveUnwrap)
3658                                 Elements.Remove (SwitchLabel.NullStringCase);
3659
3660                         Switch old_switch = ec.Switch;
3661                         ec.Switch = this;
3662                         ec.Switch.SwitchType = SwitchType;
3663
3664                         Report.Debug (1, "START OF SWITCH BLOCK", loc, ec.CurrentBranching);
3665                         ec.StartFlowBranching (FlowBranching.BranchingType.Switch, loc);
3666
3667                         is_constant = new_expr is Constant;
3668                         if (is_constant) {
3669                                 object key = ((Constant) new_expr).GetValue ();
3670                                 SwitchLabel label = (SwitchLabel) Elements [key];
3671
3672                                 constant_section = FindSection (label);
3673                                 if (constant_section == null)
3674                                         constant_section = default_section;
3675                         }
3676
3677                         bool first = true;
3678                         bool ok = true;
3679                         foreach (SwitchSection ss in Sections){
3680                                 if (!first)
3681                                         ec.CurrentBranching.CreateSibling (
3682                                                 null, FlowBranching.SiblingType.SwitchSection);
3683                                 else
3684                                         first = false;
3685
3686                                 if (is_constant && (ss != constant_section)) {
3687                                         // If we're a constant switch, we're only emitting
3688                                         // one single section - mark all the others as
3689                                         // unreachable.
3690                                         ec.CurrentBranching.CurrentUsageVector.Goto ();
3691                                         if (!ss.Block.ResolveUnreachable (ec, true)) {
3692                                                 ok = false;
3693                                         }
3694                                 } else {
3695                                         if (!ss.Block.Resolve (ec))
3696                                                 ok = false;
3697                                 }
3698                         }
3699
3700                         if (default_section == null)
3701                                 ec.CurrentBranching.CreateSibling (
3702                                         null, FlowBranching.SiblingType.SwitchSection);
3703
3704                         ec.EndFlowBranching ();
3705                         ec.Switch = old_switch;
3706
3707                         Report.Debug (1, "END OF SWITCH BLOCK", loc, ec.CurrentBranching);
3708
3709                         if (TypeManager.string_isinterned_string == null) {
3710                                 TypeManager.string_isinterned_string = TypeManager.GetPredefinedMethod (TypeManager.string_type,
3711                                         "IsInterned", loc, TypeManager.string_type);
3712                         }
3713
3714                         return ok;
3715                 }
3716                 
3717                 protected override void DoEmit (EmitContext ec)
3718                 {
3719                         ILGenerator ig = ec.ig;
3720
3721                         default_target = ig.DefineLabel ();
3722                         null_target = ig.DefineLabel ();
3723
3724                         // Store variable for comparission purposes
3725                         LocalBuilder value;
3726                         if (HaveUnwrap) {
3727                                 value = ig.DeclareLocal (SwitchType);
3728 #if GMCS_SOURCE
3729                                 unwrap.EmitCheck (ec);
3730                                 ig.Emit (OpCodes.Brfalse, null_target);
3731                                 new_expr.Emit (ec);
3732                                 ig.Emit (OpCodes.Stloc, value);
3733 #endif
3734                         } else if (!is_constant) {
3735                                 value = ig.DeclareLocal (SwitchType);
3736                                 new_expr.Emit (ec);
3737                                 ig.Emit (OpCodes.Stloc, value);
3738                         } else
3739                                 value = null;
3740
3741                         //
3742                         // Setup the codegen context
3743                         //
3744                         Label old_end = ec.LoopEnd;
3745                         Switch old_switch = ec.Switch;
3746                         
3747                         ec.LoopEnd = ig.DefineLabel ();
3748                         ec.Switch = this;
3749
3750                         // Emit Code.
3751                         if (is_constant) {
3752                                 if (constant_section != null)
3753                                         constant_section.Block.Emit (ec);
3754                         } else if (SwitchType == TypeManager.string_type)
3755                                 SimpleSwitchEmit (ec, value);
3756                         else
3757                                 TableSwitchEmit (ec, value);
3758
3759                         // Restore context state. 
3760                         ig.MarkLabel (ec.LoopEnd);
3761
3762                         //
3763                         // Restore the previous context
3764                         //
3765                         ec.LoopEnd = old_end;
3766                         ec.Switch = old_switch;
3767                 }
3768
3769                 protected override void CloneTo (CloneContext clonectx, Statement t)
3770                 {
3771                         Switch target = (Switch) t;
3772
3773                         target.Expr = Expr.Clone (clonectx);
3774                         target.Sections = new ArrayList ();
3775                         foreach (SwitchSection ss in Sections){
3776                                 target.Sections.Add (ss.Clone (clonectx));
3777                         }
3778                 }
3779         }
3780
3781         public abstract class ExceptionStatement : Statement
3782         {
3783                 public abstract void EmitFinally (EmitContext ec);
3784
3785                 protected bool emit_finally = true;
3786                 ArrayList parent_vectors;
3787
3788                 protected void DoEmitFinally (EmitContext ec)
3789                 {
3790                         if (emit_finally)
3791                                 ec.ig.BeginFinallyBlock ();
3792                         else if (ec.InIterator)
3793                                 ec.CurrentIterator.MarkFinally (ec, parent_vectors);
3794                         EmitFinally (ec);
3795                 }
3796
3797                 protected void ResolveFinally (FlowBranchingException branching)
3798                 {
3799                         emit_finally = branching.EmitFinally;
3800                         if (!emit_finally)
3801                                 branching.Parent.StealFinallyClauses (ref parent_vectors);
3802                 }
3803         }
3804
3805         public class Lock : ExceptionStatement {
3806                 Expression expr;
3807                 public Statement Statement;
3808                 TemporaryVariable temp;
3809                         
3810                 public Lock (Expression expr, Statement stmt, Location l)
3811                 {
3812                         this.expr = expr;
3813                         Statement = stmt;
3814                         loc = l;
3815                 }
3816
3817                 public override bool Resolve (EmitContext ec)
3818                 {
3819                         expr = expr.Resolve (ec);
3820                         if (expr == null)
3821                                 return false;
3822
3823                         if (expr.Type.IsValueType){
3824                                 Report.Error (185, loc,
3825                                               "`{0}' is not a reference type as required by the lock statement",
3826                                               TypeManager.CSharpName (expr.Type));
3827                                 return false;
3828                         }
3829
3830                         FlowBranchingException branching = ec.StartFlowBranching (this);
3831                         bool ok = Statement.Resolve (ec);
3832
3833                         ResolveFinally (branching);
3834
3835                         ec.EndFlowBranching ();
3836
3837                         // System.Reflection.Emit automatically emits a 'leave' to the end of the finally block.
3838                         // So, ensure there's some IL code after the finally block.
3839                         ec.NeedReturnLabel ();
3840
3841                         // Avoid creating libraries that reference the internal
3842                         // mcs NullType:
3843                         Type t = expr.Type;
3844                         if (t == TypeManager.null_type)
3845                                 t = TypeManager.object_type;
3846                         
3847                         temp = new TemporaryVariable (t, loc);
3848                         temp.Resolve (ec);
3849
3850                         if (TypeManager.void_monitor_enter_object == null || TypeManager.void_monitor_exit_object == null) {
3851                                 Type monitor_type = TypeManager.CoreLookupType ("System.Threading", "Monitor", Kind.Class, true);
3852                                 TypeManager.void_monitor_enter_object = TypeManager.GetPredefinedMethod (
3853                                         monitor_type, "Enter", loc, TypeManager.object_type);
3854                                 TypeManager.void_monitor_exit_object = TypeManager.GetPredefinedMethod (
3855                                         monitor_type, "Exit", loc, TypeManager.object_type);
3856                         }
3857                         
3858                         return ok;
3859                 }
3860                 
3861                 protected override void DoEmit (EmitContext ec)
3862                 {
3863                         ILGenerator ig = ec.ig;
3864
3865                         temp.Store (ec, expr);
3866                         temp.Emit (ec);
3867                         ig.Emit (OpCodes.Call, TypeManager.void_monitor_enter_object);
3868
3869                         // try
3870                         if (emit_finally)
3871                                 ig.BeginExceptionBlock ();
3872                         Statement.Emit (ec);
3873                         
3874                         // finally
3875                         DoEmitFinally (ec);
3876                         if (emit_finally)
3877                                 ig.EndExceptionBlock ();
3878                 }
3879
3880                 public override void EmitFinally (EmitContext ec)
3881                 {
3882                         temp.Emit (ec);
3883                         ec.ig.Emit (OpCodes.Call, TypeManager.void_monitor_exit_object);
3884                 }
3885                 
3886                 protected override void CloneTo (CloneContext clonectx, Statement t)
3887                 {
3888                         Lock target = (Lock) t;
3889
3890                         target.expr = expr.Clone (clonectx);
3891                         target.Statement = Statement.Clone (clonectx);
3892                 }
3893         }
3894
3895         public class Unchecked : Statement {
3896                 public Block Block;
3897                 
3898                 public Unchecked (Block b)
3899                 {
3900                         Block = b;
3901                         b.Unchecked = true;
3902                 }
3903
3904                 public override bool Resolve (EmitContext ec)
3905                 {
3906                         using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
3907                                 return Block.Resolve (ec);
3908                 }
3909                 
3910                 protected override void DoEmit (EmitContext ec)
3911                 {
3912                         using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
3913                                 Block.Emit (ec);
3914                 }
3915
3916                 protected override void CloneTo (CloneContext clonectx, Statement t)
3917                 {
3918                         Unchecked target = (Unchecked) t;
3919
3920                         target.Block = clonectx.LookupBlock (Block);
3921                 }
3922         }
3923
3924         public class Checked : Statement {
3925                 public Block Block;
3926                 
3927                 public Checked (Block b)
3928                 {
3929                         Block = b;
3930                         b.Unchecked = false;
3931                 }
3932
3933                 public override bool Resolve (EmitContext ec)
3934                 {
3935                         using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
3936                                 return Block.Resolve (ec);
3937                 }
3938
3939                 protected override void DoEmit (EmitContext ec)
3940                 {
3941                         using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
3942                                 Block.Emit (ec);
3943                 }
3944
3945                 protected override void CloneTo (CloneContext clonectx, Statement t)
3946                 {
3947                         Checked target = (Checked) t;
3948
3949                         target.Block = clonectx.LookupBlock (Block);
3950                 }
3951         }
3952
3953         public class Unsafe : Statement {
3954                 public Block Block;
3955
3956                 public Unsafe (Block b)
3957                 {
3958                         Block = b;
3959                         Block.Unsafe = true;
3960                 }
3961
3962                 public override bool Resolve (EmitContext ec)
3963                 {
3964                         using (ec.With (EmitContext.Flags.InUnsafe, true))
3965                                 return Block.Resolve (ec);
3966                 }
3967                 
3968                 protected override void DoEmit (EmitContext ec)
3969                 {
3970                         using (ec.With (EmitContext.Flags.InUnsafe, true))
3971                                 Block.Emit (ec);
3972                 }
3973                 protected override void CloneTo (CloneContext clonectx, Statement t)
3974                 {
3975                         Unsafe target = (Unsafe) t;
3976
3977                         target.Block = clonectx.LookupBlock (Block);
3978                 }
3979         }
3980
3981         // 
3982         // Fixed statement
3983         //
3984         public class Fixed : Statement {
3985                 Expression type;
3986                 ArrayList declarators;
3987                 Statement statement;
3988                 Type expr_type;
3989                 Emitter[] data;
3990                 bool has_ret;
3991
3992                 abstract class Emitter
3993                 {
3994                         protected LocalInfo vi;
3995                         protected Expression converted;
3996
3997                         protected Emitter (Expression expr, LocalInfo li)
3998                         {
3999                                 converted = expr;
4000                                 vi = li;
4001                         }
4002
4003                         public abstract void Emit (EmitContext ec);
4004                         public abstract void EmitExit (EmitContext ec);
4005                 }
4006
4007                 class ExpressionEmitter : Emitter {
4008                         public ExpressionEmitter (Expression converted, LocalInfo li) :
4009                                 base (converted, li)
4010                         {
4011                         }
4012
4013                         public override void Emit (EmitContext ec) {
4014                                 //
4015                                 // Store pointer in pinned location
4016                                 //
4017                                 converted.Emit (ec);
4018                                 vi.Variable.EmitAssign (ec);
4019                         }
4020
4021                         public override void EmitExit (EmitContext ec)
4022                         {
4023                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
4024                                 ec.ig.Emit (OpCodes.Conv_U);
4025                                 vi.Variable.EmitAssign (ec);
4026                         }
4027                 }
4028
4029                 class StringEmitter : Emitter {
4030                         LocalBuilder pinned_string;
4031                         Location loc;
4032
4033                         public StringEmitter (Expression expr, LocalInfo li, Location loc):
4034                                 base (expr, li)
4035                         {
4036                                 this.loc = loc;
4037                         }
4038
4039                         public override void Emit (EmitContext ec)
4040                         {
4041                                 ILGenerator ig = ec.ig;
4042                                 pinned_string = TypeManager.DeclareLocalPinned (ig, TypeManager.string_type);
4043                                         
4044                                 converted.Emit (ec);
4045                                 ig.Emit (OpCodes.Stloc, pinned_string);
4046
4047                                 Expression sptr = new StringPtr (pinned_string, loc);
4048                                 converted = Convert.ImplicitConversionRequired (
4049                                         ec, sptr, vi.VariableType, loc);
4050                                         
4051                                 if (converted == null)
4052                                         return;
4053
4054                                 converted.Emit (ec);
4055                                 vi.Variable.EmitAssign (ec);
4056                         }
4057
4058                         public override void EmitExit (EmitContext ec)
4059                         {
4060                                 ec.ig.Emit (OpCodes.Ldnull);
4061                                 ec.ig.Emit (OpCodes.Stloc, pinned_string);
4062                         }
4063                 }
4064
4065                 public Fixed (Expression type, ArrayList decls, Statement stmt, Location l)
4066                 {
4067                         this.type = type;
4068                         declarators = decls;
4069                         statement = stmt;
4070                         loc = l;
4071                 }
4072
4073                 public Statement Statement {
4074                         get { return statement; }
4075                 }
4076
4077                 public override bool Resolve (EmitContext ec)
4078                 {
4079                         if (!ec.InUnsafe){
4080                                 Expression.UnsafeError (loc);
4081                                 return false;
4082                         }
4083                         
4084                         TypeExpr texpr = type.ResolveAsContextualType (ec, false);
4085                         if (texpr == null) {
4086                                 if (type is VarExpr)
4087                                         Report.Error (821, type.Location, "A fixed statement cannot use an implicitly typed local variable");
4088
4089                                 return false;
4090                         }
4091
4092                         expr_type = texpr.Type;
4093
4094                         data = new Emitter [declarators.Count];
4095
4096                         if (!expr_type.IsPointer){
4097                                 Report.Error (209, loc, "The type of locals declared in a fixed statement must be a pointer type");
4098                                 return false;
4099                         }
4100                         
4101                         int i = 0;
4102                         foreach (Pair p in declarators){
4103                                 LocalInfo vi = (LocalInfo) p.First;
4104                                 Expression e = (Expression) p.Second;
4105                                 
4106                                 vi.VariableInfo.SetAssigned (ec);
4107                                 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4108
4109                                 //
4110                                 // The rules for the possible declarators are pretty wise,
4111                                 // but the production on the grammar is more concise.
4112                                 //
4113                                 // So we have to enforce these rules here.
4114                                 //
4115                                 // We do not resolve before doing the case 1 test,
4116                                 // because the grammar is explicit in that the token &
4117                                 // is present, so we need to test for this particular case.
4118                                 //
4119
4120                                 if (e is Cast){
4121                                         Report.Error (254, loc, "The right hand side of a fixed statement assignment may not be a cast expression");
4122                                         return false;
4123                                 }
4124                                 
4125                                 //
4126                                 // Case 1: & object.
4127                                 //
4128                                 if (e is Unary && ((Unary) e).Oper == Unary.Operator.AddressOf){
4129                                         Expression child = ((Unary) e).Expr;
4130
4131                                         if (child is ParameterReference || child is LocalVariableReference){
4132                                                 Report.Error (
4133                                                         213, loc, 
4134                                                         "No need to use fixed statement for parameters or " +
4135                                                         "local variable declarations (address is already " +
4136                                                         "fixed)");
4137                                                 return false;
4138                                         }
4139
4140                                         ec.InFixedInitializer = true;
4141                                         e = e.Resolve (ec);
4142                                         ec.InFixedInitializer = false;
4143                                         if (e == null)
4144                                                 return false;
4145
4146                                         child = ((Unary) e).Expr;
4147                                         
4148                                         if (!TypeManager.VerifyUnManaged (child.Type, loc))
4149                                                 return false;
4150
4151                                         if (!Convert.ImplicitConversionExists (ec, e, expr_type)) {
4152                                                 e.Error_ValueCannotBeConverted (ec, e.Location, expr_type, false);
4153                                                 return false;
4154                                         }
4155
4156                                         data [i] = new ExpressionEmitter (e, vi);
4157                                         i++;
4158
4159                                         continue;
4160                                 }
4161
4162                                 ec.InFixedInitializer = true;
4163                                 e = e.Resolve (ec);
4164                                 ec.InFixedInitializer = false;
4165                                 if (e == null)
4166                                         return false;
4167
4168                                 //
4169                                 // Case 2: Array
4170                                 //
4171                                 if (e.Type.IsArray){
4172                                         Type array_type = TypeManager.GetElementType (e.Type);
4173                                         
4174                                         //
4175                                         // Provided that array_type is unmanaged,
4176                                         //
4177                                         if (!TypeManager.VerifyUnManaged (array_type, loc))
4178                                                 return false;
4179
4180                                         //
4181                                         // and T* is implicitly convertible to the
4182                                         // pointer type given in the fixed statement.
4183                                         //
4184                                         ArrayPtr array_ptr = new ArrayPtr (e, array_type, loc);
4185                                         
4186                                         Expression converted = Convert.ImplicitConversionRequired (
4187                                                 ec, array_ptr, vi.VariableType, loc);
4188                                         if (converted == null)
4189                                                 return false;
4190                                         
4191                                         //
4192                                         // fixed (T* e_ptr = (e == null || e.Length == 0) ? null : converted [0])
4193                                         //
4194                                         converted = new Conditional (new Binary (Binary.Operator.LogicalOr,
4195                                                 new Binary (Binary.Operator.Equality, e, new NullLiteral (loc)),
4196                                                 new Binary (Binary.Operator.Equality, new MemberAccess (e, "Length"), new IntConstant (0, loc))),
4197                                                         NullPointer.Null,
4198                                                         converted);
4199
4200                                         converted = converted.Resolve (ec);                                     
4201
4202                                         data [i] = new ExpressionEmitter (converted, vi);
4203                                         i++;
4204
4205                                         continue;
4206                                 }
4207
4208                                 //
4209                                 // Case 3: string
4210                                 //
4211                                 if (e.Type == TypeManager.string_type){
4212                                         data [i] = new StringEmitter (e, vi, loc);
4213                                         i++;
4214                                         continue;
4215                                 }
4216
4217                                 // Case 4: fixed buffer
4218                                 FixedBufferPtr fixed_buffer_ptr = e as FixedBufferPtr;
4219                                 if (fixed_buffer_ptr != null) {
4220                                         data [i++] = new ExpressionEmitter (fixed_buffer_ptr, vi);
4221                                         continue;
4222                                 }
4223
4224                                 //
4225                                 // For other cases, flag a `this is already fixed expression'
4226                                 //
4227                                 if (e is LocalVariableReference || e is ParameterReference ||
4228                                     Convert.ImplicitConversionExists (ec, e, vi.VariableType)){
4229                                     
4230                                         Report.Error (245, loc, "right hand expression is already fixed, no need to use fixed statement ");
4231                                         return false;
4232                                 }
4233
4234                                 Report.Error (245, loc, "Fixed statement only allowed on strings, arrays or address-of expressions");
4235                                 return false;
4236                         }
4237
4238                         ec.StartFlowBranching (FlowBranching.BranchingType.Conditional, loc);
4239                         bool ok = statement.Resolve (ec);
4240                         bool flow_unreachable = ec.EndFlowBranching ();
4241                         has_ret = flow_unreachable;
4242
4243                         return ok;
4244                 }
4245                 
4246                 protected override void DoEmit (EmitContext ec)
4247                 {
4248                         for (int i = 0; i < data.Length; i++) {
4249                                 data [i].Emit (ec);
4250                         }
4251
4252                         statement.Emit (ec);
4253
4254                         if (has_ret)
4255                                 return;
4256
4257                         //
4258                         // Clear the pinned variable
4259                         //
4260                         for (int i = 0; i < data.Length; i++) {
4261                                 data [i].EmitExit (ec);
4262                         }
4263                 }
4264
4265                 protected override void CloneTo (CloneContext clonectx, Statement t)
4266                 {
4267                         Fixed target = (Fixed) t;
4268
4269                         target.type = type.Clone (clonectx);
4270                         target.declarators = new ArrayList (declarators.Count);
4271                         foreach (Pair p in declarators) {
4272                                 LocalInfo vi = (LocalInfo) p.First;
4273                                 Expression e = (Expression) p.Second;
4274
4275                                 target.declarators.Add (
4276                                         new Pair (clonectx.LookupVariable (vi), e.Clone (clonectx)));                           
4277                         }
4278                         
4279                         target.statement = statement.Clone (clonectx);
4280                 }
4281         }
4282         
4283         public class Catch : Statement {
4284                 public readonly string Name;
4285                 public Block  Block;
4286                 public Block  VarBlock;
4287
4288                 Expression type_expr;
4289                 Type type;
4290                 
4291                 public Catch (Expression type, string name, Block block, Block var_block, Location l)
4292                 {
4293                         type_expr = type;
4294                         Name = name;
4295                         Block = block;
4296                         VarBlock = var_block;
4297                         loc = l;
4298                 }
4299
4300                 public Type CatchType {
4301                         get {
4302                                 return type;
4303                         }
4304                 }
4305
4306                 public bool IsGeneral {
4307                         get {
4308                                 return type_expr == null;
4309                         }
4310                 }
4311
4312                 protected override void DoEmit(EmitContext ec)
4313                 {
4314                         ILGenerator ig = ec.ig;
4315
4316                         if (CatchType != null)
4317                                 ig.BeginCatchBlock (CatchType);
4318                         else
4319                                 ig.BeginCatchBlock (TypeManager.object_type);
4320
4321                         if (VarBlock != null)
4322                                 VarBlock.Emit (ec);
4323
4324                         if (Name != null) {
4325                                 LocalInfo vi = Block.GetLocalInfo (Name);
4326                                 if (vi == null)
4327                                         throw new Exception ("Variable does not exist in this block");
4328
4329                                 if (vi.Variable.NeedsTemporary) {
4330                                         LocalBuilder e = ig.DeclareLocal (vi.VariableType);
4331                                         ig.Emit (OpCodes.Stloc, e);
4332
4333                                         vi.Variable.EmitInstance (ec);
4334                                         ig.Emit (OpCodes.Ldloc, e);
4335                                         vi.Variable.EmitAssign (ec);
4336                                 } else
4337                                         vi.Variable.EmitAssign (ec);
4338                         } else
4339                                 ig.Emit (OpCodes.Pop);
4340
4341                         Block.Emit (ec);
4342                 }
4343
4344                 public override bool Resolve (EmitContext ec)
4345                 {
4346                         using (ec.With (EmitContext.Flags.InCatch, true)) {
4347                                 if (type_expr != null) {
4348                                         TypeExpr te = type_expr.ResolveAsTypeTerminal (ec, false);
4349                                         if (te == null)
4350                                                 return false;
4351
4352                                         type = te.Type;
4353
4354                                         if (type != TypeManager.exception_type && !TypeManager.IsSubclassOf (type, TypeManager.exception_type)){
4355                                                 Error (155, "The type caught or thrown must be derived from System.Exception");
4356                                                 return false;
4357                                         }
4358                                 } else
4359                                         type = null;
4360
4361                                 if (!Block.Resolve (ec))
4362                                         return false;
4363
4364                                 // Even though VarBlock surrounds 'Block' we resolve it later, so that we can correctly
4365                                 // emit the "unused variable" warnings.
4366                                 if (VarBlock != null)
4367                                         return VarBlock.Resolve (ec);
4368
4369                                 return true;
4370                         }
4371                 }
4372
4373                 protected override void CloneTo (CloneContext clonectx, Statement t)
4374                 {
4375                         Catch target = (Catch) t;
4376
4377                         if (type_expr != null)
4378                                 target.type_expr = type_expr.Clone (clonectx);
4379                         if (VarBlock != null)
4380                                 target.VarBlock = clonectx.LookupBlock (VarBlock);                      
4381                         target.Block = clonectx.LookupBlock (Block);
4382                 }
4383         }
4384
4385         public class Try : ExceptionStatement {
4386                 public Block Fini, Block;
4387                 public ArrayList Specific;
4388                 public Catch General;
4389
4390                 bool need_exc_block;
4391                 
4392                 //
4393                 // specific, general and fini might all be null.
4394                 //
4395                 public Try (Block block, ArrayList specific, Catch general, Block fini, Location l)
4396                 {
4397                         if (specific == null && general == null){
4398                                 Console.WriteLine ("CIR.Try: Either specific or general have to be non-null");
4399                         }
4400                         
4401                         this.Block = block;
4402                         this.Specific = specific;
4403                         this.General = general;
4404                         this.Fini = fini;
4405                         loc = l;
4406                 }
4407
4408                 public override bool Resolve (EmitContext ec)
4409                 {
4410                         bool ok = true;
4411                         
4412                         FlowBranchingException branching = ec.StartFlowBranching (this);
4413
4414                         Report.Debug (1, "START OF TRY BLOCK", Block.StartLocation);
4415
4416                         if (!Block.Resolve (ec))
4417                                 ok = false;
4418
4419                         FlowBranching.UsageVector vector = ec.CurrentBranching.CurrentUsageVector;
4420
4421                         Report.Debug (1, "START OF CATCH BLOCKS", vector);
4422
4423                         Type[] prev_catches = new Type [Specific.Count];
4424                         int last_index = 0;
4425                         foreach (Catch c in Specific){
4426                                 ec.CurrentBranching.CreateSibling (
4427                                         c.Block, FlowBranching.SiblingType.Catch);
4428
4429                                 Report.Debug (1, "STARTED SIBLING FOR CATCH", ec.CurrentBranching);
4430
4431                                 if (c.Name != null) {
4432                                         LocalInfo vi = c.Block.GetLocalInfo (c.Name);
4433                                         if (vi == null)
4434                                                 throw new Exception ();
4435
4436                                         vi.VariableInfo = null;
4437                                 }
4438
4439                                 if (!c.Resolve (ec))
4440                                         return false;
4441
4442                                 Type resolved_type = c.CatchType;
4443                                 for (int ii = 0; ii < last_index; ++ii) {
4444                                         if (resolved_type == prev_catches [ii] || TypeManager.IsSubclassOf (resolved_type, prev_catches [ii])) {
4445                                                 Report.Error (160, c.loc, "A previous catch clause already catches all exceptions of this or a super type `{0}'", prev_catches [ii].FullName);
4446                                                 return false;
4447                                         }
4448                                 }
4449
4450                                 prev_catches [last_index++] = resolved_type;
4451                                 need_exc_block = true;
4452                         }
4453
4454                         Report.Debug (1, "END OF CATCH BLOCKS", ec.CurrentBranching);
4455
4456                         if (General != null){
4457                                 if (CodeGen.Assembly.WrapNonExceptionThrows) {
4458                                         foreach (Catch c in Specific){
4459                                                 if (c.CatchType == TypeManager.exception_type) {
4460                                                         Report.Warning (1058, 1, c.loc, "A previous catch clause already catches all exceptions. All non-exceptions thrown will be wrapped in a `System.Runtime.CompilerServices.RuntimeWrappedException'");
4461                                                 }
4462                                         }
4463                                 }
4464
4465                                 ec.CurrentBranching.CreateSibling (
4466                                         General.Block, FlowBranching.SiblingType.Catch);
4467
4468                                 Report.Debug (1, "STARTED SIBLING FOR GENERAL", ec.CurrentBranching);
4469
4470                                 if (!General.Resolve (ec))
4471                                         ok = false;
4472
4473                                 need_exc_block = true;
4474                         }
4475
4476                         Report.Debug (1, "END OF GENERAL CATCH BLOCKS", ec.CurrentBranching);
4477
4478                         if (Fini != null) {
4479                                 if (ok)
4480                                         ec.CurrentBranching.CreateSibling (Fini, FlowBranching.SiblingType.Finally);
4481
4482                                 Report.Debug (1, "STARTED SIBLING FOR FINALLY", ec.CurrentBranching, vector);
4483                                 using (ec.With (EmitContext.Flags.InFinally, true)) {
4484                                         if (!Fini.Resolve (ec))
4485                                                 ok = false;
4486                                 }
4487
4488                                 if (!ec.InIterator)
4489                                         need_exc_block = true;
4490                         }
4491
4492                         if (ec.InIterator) {
4493                                 ResolveFinally (branching);
4494                                 need_exc_block |= emit_finally;
4495                         } else
4496                                 emit_finally = Fini != null;
4497
4498                         ec.EndFlowBranching ();
4499
4500                         // System.Reflection.Emit automatically emits a 'leave' to the end of the finally block.
4501                         // So, ensure there's some IL code after the finally block.
4502                         ec.NeedReturnLabel ();
4503
4504                         FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
4505
4506                         Report.Debug (1, "END OF TRY", ec.CurrentBranching, vector, f_vector);
4507
4508                         return ok;
4509                 }
4510                 
4511                 protected override void DoEmit (EmitContext ec)
4512                 {
4513                         ILGenerator ig = ec.ig;
4514
4515                         if (need_exc_block)
4516                                 ig.BeginExceptionBlock ();
4517                         Block.Emit (ec);
4518
4519                         foreach (Catch c in Specific)
4520                                 c.Emit (ec);
4521
4522                         if (General != null)
4523                                 General.Emit (ec);
4524
4525                         DoEmitFinally (ec);
4526                         if (need_exc_block)
4527                                 ig.EndExceptionBlock ();
4528                 }
4529
4530                 public override void EmitFinally (EmitContext ec)
4531                 {
4532                         if (Fini != null)
4533                                 Fini.Emit (ec);
4534                 }
4535
4536                 public bool HasCatch
4537                 {
4538                         get {
4539                                 return General != null || Specific.Count > 0;
4540                         }
4541                 }
4542
4543                 protected override void CloneTo (CloneContext clonectx, Statement t)
4544                 {
4545                         Try target = (Try) t;
4546
4547                         target.Block = clonectx.LookupBlock (Block);
4548                         if (Fini != null)
4549                                 target.Fini = clonectx.LookupBlock (Fini);
4550                         if (General != null)
4551                                 target.General = (Catch) General.Clone (clonectx);
4552                         if (Specific != null){
4553                                 target.Specific = new ArrayList ();
4554                                 foreach (Catch c in Specific)
4555                                         target.Specific.Add (c.Clone (clonectx));
4556                         }
4557                 }
4558         }
4559
4560         public class Using : ExceptionStatement {
4561                 object expression_or_block;
4562                 public Statement Statement;
4563                 ArrayList var_list;
4564                 Expression expr;
4565                 Type expr_type;
4566                 Expression [] resolved_vars;
4567                 Expression [] converted_vars;
4568                 Expression [] assign;
4569                 TemporaryVariable local_copy;
4570                 
4571                 public Using (object expression_or_block, Statement stmt, Location l)
4572                 {
4573                         this.expression_or_block = expression_or_block;
4574                         Statement = stmt;
4575                         loc = l;
4576                 }
4577
4578                 //
4579                 // Resolves for the case of using using a local variable declaration.
4580                 //
4581                 bool ResolveLocalVariableDecls (EmitContext ec)
4582                 {
4583                         resolved_vars = new Expression[var_list.Count];
4584                         assign = new Expression [var_list.Count];
4585                         converted_vars = new Expression[var_list.Count];
4586
4587                         for (int i = 0; i < assign.Length; ++i) {
4588                                 DictionaryEntry e = (DictionaryEntry) var_list [i];
4589                                 Expression var = (Expression) e.Key;
4590                                 Expression new_expr = (Expression) e.Value;
4591
4592                                 Expression a = new Assign (var, new_expr, loc);
4593                                 a = a.Resolve (ec);
4594                                 if (a == null)
4595                                         return false;
4596
4597                                 resolved_vars [i] = var;
4598                                 assign [i] = a;
4599
4600                                 if (TypeManager.ImplementsInterface (a.Type, TypeManager.idisposable_type)) {
4601                                         converted_vars [i] = var;
4602                                         continue;
4603                                 }
4604
4605                                 a = Convert.ImplicitConversionStandard (ec, a, TypeManager.idisposable_type, var.Location);
4606                                 if (a == null) {
4607                                         Error_IsNotConvertibleToIDisposable (var);
4608                                         return false;
4609                                 }
4610
4611                                 converted_vars [i] = a;
4612                         }
4613
4614                         return true;
4615                 }
4616
4617                 static void Error_IsNotConvertibleToIDisposable (Expression expr)
4618                 {
4619                         Report.SymbolRelatedToPreviousError (expr.Type);
4620                         Report.Error (1674, expr.Location, "`{0}': type used in a using statement must be implicitly convertible to `System.IDisposable'",
4621                                 expr.GetSignatureForError ());
4622                 }
4623
4624                 bool ResolveExpression (EmitContext ec)
4625                 {
4626                         if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
4627                                 if (Convert.ImplicitConversion (ec, expr, TypeManager.idisposable_type, loc) == null) {
4628                                         Error_IsNotConvertibleToIDisposable (expr);
4629                                         return false;
4630                                 }
4631                         }
4632
4633                         local_copy = new TemporaryVariable (expr_type, loc);
4634                         local_copy.Resolve (ec);
4635
4636                         return true;
4637                 }
4638                 
4639                 //
4640                 // Emits the code for the case of using using a local variable declaration.
4641                 //
4642                 void EmitLocalVariableDecls (EmitContext ec)
4643                 {
4644                         ILGenerator ig = ec.ig;
4645                         int i = 0;
4646
4647                         for (i = 0; i < assign.Length; i++) {
4648                                 ExpressionStatement es = assign [i] as ExpressionStatement;
4649
4650                                 if (es != null)
4651                                         es.EmitStatement (ec);
4652                                 else {
4653                                         assign [i].Emit (ec);
4654                                         ig.Emit (OpCodes.Pop);
4655                                 }
4656
4657                                 if (emit_finally)
4658                                         ig.BeginExceptionBlock ();
4659                         }
4660                         Statement.Emit (ec);
4661
4662                         var_list.Reverse ();
4663
4664                         DoEmitFinally (ec);
4665                 }
4666
4667                 void EmitLocalVariableDeclFinally (EmitContext ec)
4668                 {
4669                         ILGenerator ig = ec.ig;
4670
4671                         int i = assign.Length;
4672                         for (int ii = 0; ii < var_list.Count; ++ii){
4673                                 Expression var = resolved_vars [--i];
4674                                 Label skip = ig.DefineLabel ();
4675
4676                                 if (emit_finally)
4677                                         ig.BeginFinallyBlock ();
4678                                 
4679                                 if (!var.Type.IsValueType) {
4680                                         var.Emit (ec);
4681                                         ig.Emit (OpCodes.Brfalse, skip);
4682                                         converted_vars [i].Emit (ec);
4683                                         ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4684                                 } else {
4685                                         Expression ml = Expression.MemberLookup(ec.ContainerType, TypeManager.idisposable_type, var.Type, "Dispose", Mono.CSharp.Location.Null);
4686
4687                                         if (!(ml is MethodGroupExpr)) {
4688                                                 var.Emit (ec);
4689                                                 ig.Emit (OpCodes.Box, var.Type);
4690                                                 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4691                                         } else {
4692                                                 MethodInfo mi = null;
4693
4694                                                 foreach (MethodInfo mk in ((MethodGroupExpr) ml).Methods) {
4695                                                         if (TypeManager.GetParameterData (mk).Count == 0) {
4696                                                                 mi = mk;
4697                                                                 break;
4698                                                         }
4699                                                 }
4700
4701                                                 if (mi == null) {
4702                                                         Report.Error(-100, Mono.CSharp.Location.Null, "Internal error: No Dispose method which takes 0 parameters.");
4703                                                         return;
4704                                                 }
4705
4706                                                 IMemoryLocation mloc = (IMemoryLocation) var;
4707
4708                                                 mloc.AddressOf (ec, AddressOp.Load);
4709                                                 ig.Emit (OpCodes.Call, mi);
4710                                         }
4711                                 }
4712
4713                                 ig.MarkLabel (skip);
4714
4715                                 if (emit_finally) {
4716                                         ig.EndExceptionBlock ();
4717                                         if (i > 0)
4718                                                 ig.BeginFinallyBlock ();
4719                                 }
4720                         }
4721                 }
4722
4723                 void EmitExpression (EmitContext ec)
4724                 {
4725                         //
4726                         // Make a copy of the expression and operate on that.
4727                         //
4728                         ILGenerator ig = ec.ig;
4729
4730                         local_copy.Store (ec, expr);
4731
4732                         if (emit_finally)
4733                                 ig.BeginExceptionBlock ();
4734
4735                         Statement.Emit (ec);
4736                         
4737                         DoEmitFinally (ec);
4738                         if (emit_finally)
4739                                 ig.EndExceptionBlock ();
4740                 }
4741
4742                 void EmitExpressionFinally (EmitContext ec)
4743                 {
4744                         ILGenerator ig = ec.ig;
4745                         if (!expr_type.IsValueType) {
4746                                 Label skip = ig.DefineLabel ();
4747                                 local_copy.Emit (ec);
4748                                 ig.Emit (OpCodes.Brfalse, skip);
4749                                 local_copy.Emit (ec);
4750                                 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4751                                 ig.MarkLabel (skip);
4752                         } else {
4753                                 Expression ml = Expression.MemberLookup (
4754                                         ec.ContainerType, TypeManager.idisposable_type, expr_type,
4755                                         "Dispose", Location.Null);
4756
4757                                 if (!(ml is MethodGroupExpr)) {
4758                                         local_copy.Emit (ec);
4759                                         ig.Emit (OpCodes.Box, expr_type);
4760                                         ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
4761                                 } else {
4762                                         MethodInfo mi = null;
4763
4764                                         foreach (MethodInfo mk in ((MethodGroupExpr) ml).Methods) {
4765                                                 if (TypeManager.GetParameterData (mk).Count == 0) {
4766                                                         mi = mk;
4767                                                         break;
4768                                                 }
4769                                         }
4770
4771                                         if (mi == null) {
4772                                                 Report.Error(-100, Mono.CSharp.Location.Null, "Internal error: No Dispose method which takes 0 parameters.");
4773                                                 return;
4774                                         }
4775
4776                                         local_copy.AddressOf (ec, AddressOp.Load);
4777                                         ig.Emit (OpCodes.Call, mi);
4778                                 }
4779                         }
4780                 }
4781                 
4782                 public override bool Resolve (EmitContext ec)
4783                 {
4784                         if (expression_or_block is DictionaryEntry){
4785                                 expr = (Expression) ((DictionaryEntry) expression_or_block).Key;
4786                                 var_list = (ArrayList)((DictionaryEntry)expression_or_block).Value;
4787
4788                                 if (!ResolveLocalVariableDecls (ec))
4789                                         return false;
4790
4791                         } else if (expression_or_block is Expression){
4792                                 expr = (Expression) expression_or_block;
4793
4794                                 expr = expr.Resolve (ec);
4795                                 if (expr == null)
4796                                         return false;
4797
4798                                 expr_type = expr.Type;
4799
4800                                 if (!ResolveExpression (ec))
4801                                         return false;
4802                         }
4803
4804                         FlowBranchingException branching = ec.StartFlowBranching (this);
4805
4806                         bool ok = Statement.Resolve (ec);
4807
4808                         ResolveFinally (branching);
4809
4810                         ec.EndFlowBranching ();
4811
4812                         // System.Reflection.Emit automatically emits a 'leave' to the end of the finally block.
4813                         // So, ensure there's some IL code after the finally block.
4814                         ec.NeedReturnLabel ();
4815
4816                         if (TypeManager.void_dispose_void == null) {
4817                                 TypeManager.void_dispose_void = TypeManager.GetPredefinedMethod (
4818                                         TypeManager.idisposable_type, "Dispose", loc, Type.EmptyTypes);
4819                         }
4820
4821                         return ok;
4822                 }
4823                 
4824                 protected override void DoEmit (EmitContext ec)
4825                 {
4826                         if (expression_or_block is DictionaryEntry)
4827                                 EmitLocalVariableDecls (ec);
4828                         else if (expression_or_block is Expression)
4829                                 EmitExpression (ec);
4830                 }
4831
4832                 public override void EmitFinally (EmitContext ec)
4833                 {
4834                         if (expression_or_block is DictionaryEntry)
4835                                 EmitLocalVariableDeclFinally (ec);
4836                         else if (expression_or_block is Expression)
4837                                 EmitExpressionFinally (ec);
4838                 }
4839
4840                 protected override void CloneTo (CloneContext clonectx, Statement t)
4841                 {
4842                         Using target = (Using) t;
4843
4844                         if (expression_or_block is Expression) {
4845                                 target.expression_or_block = ((Expression) expression_or_block).Clone (clonectx);
4846                         } else {
4847                                 DictionaryEntry de = (DictionaryEntry) expression_or_block;
4848                                 ArrayList var_list = (ArrayList) de.Value;
4849                                 ArrayList target_var_list = new ArrayList (var_list.Count);
4850
4851                                 foreach (DictionaryEntry de_variable in var_list)
4852                                         target_var_list.Add (new DictionaryEntry (
4853                                                 ((Expression) de_variable.Key).Clone (clonectx),
4854                                                 ((Expression) de_variable.Value).Clone (clonectx)));
4855
4856                                 target.expression_or_block = new DictionaryEntry (
4857                                         ((Expression) de.Key).Clone (clonectx),
4858                                         target_var_list);
4859                         }
4860                         
4861                         target.Statement = Statement.Clone (clonectx);
4862                 }
4863         }
4864
4865         /// <summary>
4866         ///   Implementation of the foreach C# statement
4867         /// </summary>
4868         public class Foreach : Statement {
4869                 Expression type;
4870                 Expression variable;
4871                 Expression expr;
4872                 Statement statement;
4873                 ArrayForeach array;
4874                 CollectionForeach collection;
4875                 
4876                 public Foreach (Expression type, LocalVariableReference var, Expression expr,
4877                                 Statement stmt, Location l)
4878                 {
4879                         this.type = type;
4880                         this.variable = var;
4881                         this.expr = expr;
4882                         statement = stmt;
4883                         loc = l;
4884                 }
4885
4886                 public Statement Statement {
4887                         get { return statement; }
4888                 }
4889
4890                 public override bool Resolve (EmitContext ec)
4891                 {
4892                         expr = expr.Resolve (ec);
4893                         if (expr == null)
4894                                 return false;
4895
4896                         if (expr.IsNull) {
4897                                 Report.Error (186, loc, "Use of null is not valid in this context");
4898                                 return false;
4899                         }
4900
4901                         if (expr.eclass == ExprClass.MethodGroup || expr is AnonymousMethodExpression) {
4902                                 Report.Error (446, expr.Location, "Foreach statement cannot operate on a `{0}'",
4903                                         expr.ExprClassName);
4904                                 return false;
4905                         }
4906
4907                         //
4908                         // We need an instance variable.  Not sure this is the best
4909                         // way of doing this.
4910                         //
4911                         // FIXME: When we implement propertyaccess, will those turn
4912                         // out to return values in ExprClass?  I think they should.
4913                         //
4914                         if (!(expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.Value ||
4915                               expr.eclass == ExprClass.PropertyAccess || expr.eclass == ExprClass.IndexerAccess)){
4916                                 collection.Error_Enumerator ();
4917                                 return false;
4918                         }
4919
4920                         if (expr.Type.IsArray) {
4921                                 array = new ArrayForeach (type, variable, expr, statement, loc);
4922                                 return array.Resolve (ec);
4923                         }
4924                         
4925                         collection = new CollectionForeach (type, variable, expr, statement, loc);
4926                         return collection.Resolve (ec);
4927                 }
4928
4929                 protected override void DoEmit (EmitContext ec)
4930                 {
4931                         ILGenerator ig = ec.ig;
4932                         
4933                         Label old_begin = ec.LoopBegin, old_end = ec.LoopEnd;
4934                         ec.LoopBegin = ig.DefineLabel ();
4935                         ec.LoopEnd = ig.DefineLabel ();
4936
4937                         if (collection != null)
4938                                 collection.Emit (ec);
4939                         else
4940                                 array.Emit (ec);
4941                         
4942                         ec.LoopBegin = old_begin;
4943                         ec.LoopEnd = old_end;
4944                 }
4945
4946                 protected class ArrayCounter : TemporaryVariable
4947                 {
4948                         public ArrayCounter (Location loc)
4949                                 : base (TypeManager.int32_type, loc)
4950                         { }
4951
4952                         public void Initialize (EmitContext ec)
4953                         {
4954                                 EmitThis (ec);
4955                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
4956                                 EmitStore (ec);
4957                         }
4958
4959                         public void Increment (EmitContext ec)
4960                         {
4961                                 EmitThis (ec);
4962                                 Emit (ec);
4963                                 ec.ig.Emit (OpCodes.Ldc_I4_1);
4964                                 ec.ig.Emit (OpCodes.Add);
4965                                 EmitStore (ec);
4966                         }
4967                 }
4968
4969                 protected class ArrayForeach : Statement
4970                 {
4971                         Expression variable, expr, conv;
4972                         Statement statement;
4973                         Type array_type;
4974                         Expression var_type;
4975                         TemporaryVariable[] lengths;
4976                         ArrayCounter[] counter;
4977                         int rank;
4978
4979                         TemporaryVariable copy;
4980                         Expression access;
4981
4982                         public ArrayForeach (Expression var_type, Expression var,
4983                                              Expression expr, Statement stmt, Location l)
4984                         {
4985                                 this.var_type = var_type;
4986                                 this.variable = var;
4987                                 this.expr = expr;
4988                                 statement = stmt;
4989                                 loc = l;
4990                         }
4991
4992                         public override bool Resolve (EmitContext ec)
4993                         {
4994                                 array_type = expr.Type;
4995                                 rank = array_type.GetArrayRank ();
4996
4997                                 copy = new TemporaryVariable (array_type, loc);
4998                                 copy.Resolve (ec);
4999
5000                                 counter = new ArrayCounter [rank];
5001                                 lengths = new TemporaryVariable [rank];
5002
5003                                 ArrayList list = new ArrayList ();
5004                                 for (int i = 0; i < rank; i++) {
5005                                         counter [i] = new ArrayCounter (loc);
5006                                         counter [i].Resolve (ec);
5007
5008                                         lengths [i] = new TemporaryVariable (TypeManager.int32_type, loc);
5009                                         lengths [i].Resolve (ec);
5010
5011                                         list.Add (counter [i]);
5012                                 }
5013
5014                                 access = new ElementAccess (copy, list).Resolve (ec);
5015                                 if (access == null)
5016                                         return false;
5017
5018                                 VarExpr ve = var_type as VarExpr;
5019                                 if (ve != null) {
5020                                         // Infer implicitly typed local variable from foreach array type
5021                                         var_type = new TypeExpression (access.Type, ve.Location);
5022                                 }
5023
5024                                 var_type = var_type.ResolveAsTypeTerminal (ec, false);
5025                                 if (var_type == null)
5026                                         return false;
5027
5028                                 conv = Convert.ExplicitConversion (ec, access, var_type.Type, loc);
5029                                 if (conv == null)
5030                                         return false;
5031
5032                                 bool ok = true;
5033
5034                                 ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
5035                                 ec.CurrentBranching.CreateSibling ();
5036
5037                                 variable = variable.ResolveLValue (ec, conv, loc);
5038                                 if (variable == null)
5039                                         ok = false;
5040
5041                                 ec.StartFlowBranching (FlowBranching.BranchingType.Embedded, loc);
5042                                 if (!statement.Resolve (ec))
5043                                         ok = false;
5044                                 ec.EndFlowBranching ();
5045
5046                                 // There's no direct control flow from the end of the embedded statement to the end of the loop
5047                                 ec.CurrentBranching.CurrentUsageVector.Goto ();
5048
5049                                 ec.EndFlowBranching ();
5050
5051                                 return ok;
5052                         }
5053
5054                         protected override void DoEmit (EmitContext ec)
5055                         {
5056                                 ILGenerator ig = ec.ig;
5057
5058                                 copy.Store (ec, expr);
5059
5060                                 Label[] test = new Label [rank];
5061                                 Label[] loop = new Label [rank];
5062
5063                                 for (int i = 0; i < rank; i++) {
5064                                         test [i] = ig.DefineLabel ();
5065                                         loop [i] = ig.DefineLabel ();
5066
5067                                         lengths [i].EmitThis (ec);
5068                                         ((ArrayAccess) access).EmitGetLength (ec, i);
5069                                         lengths [i].EmitStore (ec);
5070                                 }
5071
5072                                 for (int i = 0; i < rank; i++) {
5073                                         counter [i].Initialize (ec);
5074
5075                                         ig.Emit (OpCodes.Br, test [i]);
5076                                         ig.MarkLabel (loop [i]);
5077                                 }
5078
5079                                 ((IAssignMethod) variable).EmitAssign (ec, conv, false, false);
5080
5081                                 statement.Emit (ec);
5082
5083                                 ig.MarkLabel (ec.LoopBegin);
5084
5085                                 for (int i = rank - 1; i >= 0; i--){
5086                                         counter [i].Increment (ec);
5087
5088                                         ig.MarkLabel (test [i]);
5089                                         counter [i].Emit (ec);
5090                                         lengths [i].Emit (ec);
5091                                         ig.Emit (OpCodes.Blt, loop [i]);
5092                                 }
5093
5094                                 ig.MarkLabel (ec.LoopEnd);
5095                         }
5096                 }
5097
5098                 protected class CollectionForeach : ExceptionStatement
5099                 {
5100                         Expression variable, expr;
5101                         Statement statement;
5102
5103                         TemporaryVariable enumerator;
5104                         Expression init;
5105                         Statement loop;
5106
5107                         MethodGroupExpr get_enumerator;
5108                         PropertyExpr get_current;
5109                         MethodInfo move_next;
5110                         Expression var_type;
5111                         Type enumerator_type;
5112                         bool is_disposable;
5113                         bool enumerator_found;
5114
5115                         public CollectionForeach (Expression var_type, Expression var,
5116                                                   Expression expr, Statement stmt, Location l)
5117                         {
5118                                 this.var_type = var_type;
5119                                 this.variable = var;
5120                                 this.expr = expr;
5121                                 statement = stmt;
5122                                 loc = l;
5123                         }
5124
5125                         bool GetEnumeratorFilter (EmitContext ec, MethodInfo mi)
5126                         {
5127                                 Type return_type = mi.ReturnType;
5128
5129                                 if ((return_type == TypeManager.ienumerator_type) && (mi.DeclaringType == TypeManager.string_type))
5130                                         //
5131                                         // Apply the same optimization as MS: skip the GetEnumerator
5132                                         // returning an IEnumerator, and use the one returning a 
5133                                         // CharEnumerator instead. This allows us to avoid the 
5134                                         // try-finally block and the boxing.
5135                                         //
5136                                         return false;
5137
5138                                 //
5139                                 // Ok, we can access it, now make sure that we can do something
5140                                 // with this `GetEnumerator'
5141                                 //
5142
5143                                 if (return_type == TypeManager.ienumerator_type ||
5144                                     TypeManager.ienumerator_type.IsAssignableFrom (return_type) ||
5145                                     (!RootContext.StdLib && TypeManager.ImplementsInterface (return_type, TypeManager.ienumerator_type))) {
5146                                         //
5147                                         // If it is not an interface, lets try to find the methods ourselves.
5148                                         // For example, if we have:
5149                                         // public class Foo : IEnumerator { public bool MoveNext () {} public int Current { get {}}}
5150                                         // We can avoid the iface call. This is a runtime perf boost.
5151                                         // even bigger if we have a ValueType, because we avoid the cost
5152                                         // of boxing.
5153                                         //
5154                                         // We have to make sure that both methods exist for us to take
5155                                         // this path. If one of the methods does not exist, we will just
5156                                         // use the interface. Sadly, this complex if statement is the only
5157                                         // way I could do this without a goto
5158                                         //
5159
5160                                         if (TypeManager.bool_movenext_void == null) {
5161                                                 TypeManager.bool_movenext_void = TypeManager.GetPredefinedMethod (
5162                                                         TypeManager.ienumerator_type, "MoveNext", loc, Type.EmptyTypes);
5163                                         }
5164
5165                                         if (TypeManager.ienumerator_getcurrent == null) {
5166                                                 TypeManager.ienumerator_getcurrent = TypeManager.GetPredefinedProperty (
5167                                                         TypeManager.ienumerator_type, "Current", loc, TypeManager.object_type);
5168                                         }
5169
5170 #if GMCS_SOURCE
5171                                         //
5172                                         // Prefer a generic enumerator over a non-generic one.
5173                                         //
5174                                         if (return_type.IsInterface && return_type.IsGenericType) {
5175                                                 enumerator_type = return_type;
5176                                                 if (!FetchGetCurrent (ec, return_type))
5177                                                         get_current = new PropertyExpr (
5178                                                                 ec.ContainerType, TypeManager.ienumerator_getcurrent, loc);
5179                                                 if (!FetchMoveNext (return_type))
5180                                                         move_next = TypeManager.bool_movenext_void;
5181                                                 return true;
5182                                         }
5183 #endif
5184
5185                                         if (return_type.IsInterface ||
5186                                             !FetchMoveNext (return_type) ||
5187                                             !FetchGetCurrent (ec, return_type)) {
5188                                                 enumerator_type = return_type;
5189                                                 move_next = TypeManager.bool_movenext_void;
5190                                                 get_current = new PropertyExpr (
5191                                                         ec.ContainerType, TypeManager.ienumerator_getcurrent, loc);
5192                                                 return true;
5193                                         }
5194                                 } else {
5195                                         //
5196                                         // Ok, so they dont return an IEnumerable, we will have to
5197                                         // find if they support the GetEnumerator pattern.
5198                                         //
5199
5200                                         if (TypeManager.HasElementType (return_type) || !FetchMoveNext (return_type) || !FetchGetCurrent (ec, return_type)) {
5201                                                 Report.Error (202, loc, "foreach statement requires that the return type `{0}' of `{1}' must have a suitable public MoveNext method and public Current property",
5202                                                         TypeManager.CSharpName (return_type), TypeManager.CSharpSignature (mi));
5203                                                 return false;
5204                                         }
5205                                 }
5206
5207                                 enumerator_type = return_type;
5208                                 is_disposable = !enumerator_type.IsSealed ||
5209                                         TypeManager.ImplementsInterface (
5210                                                 enumerator_type, TypeManager.idisposable_type);
5211
5212                                 return true;
5213                         }
5214
5215                         //
5216                         // Retrieves a `public bool MoveNext ()' method from the Type `t'
5217                         //
5218                         bool FetchMoveNext (Type t)
5219                         {
5220                                 MemberList move_next_list;
5221
5222                                 move_next_list = TypeContainer.FindMembers (
5223                                         t, MemberTypes.Method,
5224                                         BindingFlags.Public | BindingFlags.Instance,
5225                                         Type.FilterName, "MoveNext");
5226                                 if (move_next_list.Count == 0)
5227                                         return false;
5228
5229                                 foreach (MemberInfo m in move_next_list){
5230                                         MethodInfo mi = (MethodInfo) m;
5231                                 
5232                                         if ((TypeManager.GetParameterData (mi).Count == 0) &&
5233                                             TypeManager.TypeToCoreType (mi.ReturnType) == TypeManager.bool_type) {
5234                                                 move_next = mi;
5235                                                 return true;
5236                                         }
5237                                 }
5238
5239                                 return false;
5240                         }
5241                 
5242                         //
5243                         // Retrieves a `public T get_Current ()' method from the Type `t'
5244                         //
5245                         bool FetchGetCurrent (EmitContext ec, Type t)
5246                         {
5247                                 PropertyExpr pe = Expression.MemberLookup (
5248                                         ec.ContainerType, t, "Current", MemberTypes.Property,
5249                                         Expression.AllBindingFlags, loc) as PropertyExpr;
5250                                 if (pe == null)
5251                                         return false;
5252
5253                                 get_current = pe;
5254                                 return true;
5255                         }
5256
5257                         // 
5258                         // Retrieves a `public void Dispose ()' method from the Type `t'
5259                         //
5260                         static MethodInfo FetchMethodDispose (Type t)
5261                         {
5262                                 MemberList dispose_list;
5263
5264                                 dispose_list = TypeContainer.FindMembers (
5265                                         t, MemberTypes.Method,
5266                                         BindingFlags.Public | BindingFlags.Instance,
5267                                         Type.FilterName, "Dispose");
5268                                 if (dispose_list.Count == 0)
5269                                         return null;
5270
5271                                 foreach (MemberInfo m in dispose_list){
5272                                         MethodInfo mi = (MethodInfo) m;
5273
5274                                         if (TypeManager.GetParameterData (mi).Count == 0){
5275                                                 if (mi.ReturnType == TypeManager.void_type)
5276                                                         return mi;
5277                                         }
5278                                 }
5279                                 return null;
5280                         }
5281
5282                         public void Error_Enumerator ()
5283                         {
5284                                 if (enumerator_found) {
5285                                         return;
5286                                 }
5287
5288                             Report.Error (1579, loc,
5289                                         "foreach statement cannot operate on variables of type `{0}' because it does not contain a definition for `GetEnumerator' or is not accessible",
5290                                         TypeManager.CSharpName (expr.Type));
5291                         }
5292
5293                         bool IsOverride (MethodInfo m)
5294                         {
5295                                 m = (MethodInfo) TypeManager.DropGenericMethodArguments (m);
5296
5297                                 if (!m.IsVirtual || ((m.Attributes & MethodAttributes.NewSlot) != 0))
5298                                         return false;
5299                                 if (m is MethodBuilder)
5300                                         return true;
5301
5302                                 MethodInfo base_method = m.GetBaseDefinition ();
5303                                 return base_method != m;
5304                         }
5305
5306                         bool TryType (EmitContext ec, Type t)
5307                         {
5308                                 MethodGroupExpr mg = Expression.MemberLookup (
5309                                         ec.ContainerType, t, "GetEnumerator", MemberTypes.Method,
5310                                         Expression.AllBindingFlags, loc) as MethodGroupExpr;
5311                                 if (mg == null)
5312                                         return false;
5313
5314                                 MethodInfo result = null;
5315                                 MethodInfo tmp_move_next = null;
5316                                 PropertyExpr tmp_get_cur = null;
5317                                 Type tmp_enumerator_type = enumerator_type;
5318                                 foreach (MethodInfo mi in mg.Methods) {
5319                                         if (TypeManager.GetParameterData (mi).Count != 0)
5320                                                 continue;
5321                         
5322                                         // Check whether GetEnumerator is public
5323                                         if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
5324                                                 continue;
5325
5326                                         if (IsOverride (mi))
5327                                                 continue;
5328
5329                                         enumerator_found = true;
5330
5331                                         if (!GetEnumeratorFilter (ec, mi))
5332                                                 continue;
5333
5334                                         if (result != null) {
5335                                                 if (TypeManager.IsGenericType (result.ReturnType)) {
5336                                                         if (!TypeManager.IsGenericType (mi.ReturnType))
5337                                                                 continue;
5338
5339                                                         MethodBase mb = TypeManager.DropGenericMethodArguments (mi);
5340                                                         Report.SymbolRelatedToPreviousError (t);
5341                                                         Report.Error(1640, loc, "foreach statement cannot operate on variables of type `{0}' " +
5342                                                                      "because it contains multiple implementation of `{1}'. Try casting to a specific implementation",
5343                                                                      TypeManager.CSharpName (t), TypeManager.CSharpSignature (mb));
5344                                                         return false;
5345                                                 }
5346
5347                                                 // Always prefer generics enumerators
5348                                                 if (!TypeManager.IsGenericType (mi.ReturnType)) {
5349                                                         if (TypeManager.ImplementsInterface (mi.DeclaringType, result.DeclaringType) ||
5350                                                             TypeManager.ImplementsInterface (result.DeclaringType, mi.DeclaringType))
5351                                                                 continue;
5352
5353                                                         Report.SymbolRelatedToPreviousError (result);
5354                                                         Report.SymbolRelatedToPreviousError (mi);
5355                                                         Report.Warning (278, 2, loc, "`{0}' contains ambiguous implementation of `{1}' pattern. Method `{2}' is ambiguous with method `{3}'",
5356                                                                         TypeManager.CSharpName (t), "enumerable", TypeManager.CSharpSignature (result), TypeManager.CSharpSignature (mi));
5357                                                         return false;
5358                                                 }
5359                                         }
5360                                         result = mi;
5361                                         tmp_move_next = move_next;
5362                                         tmp_get_cur = get_current;
5363                                         tmp_enumerator_type = enumerator_type;
5364                                         if (mi.DeclaringType == t)
5365                                                 break;
5366                                 }
5367
5368                                 if (result != null) {
5369                                         move_next = tmp_move_next;
5370                                         get_current = tmp_get_cur;
5371                                         enumerator_type = tmp_enumerator_type;
5372                                         MethodInfo[] mi = new MethodInfo[] { (MethodInfo) result };
5373                                         get_enumerator = new MethodGroupExpr (mi, enumerator_type, loc);
5374
5375                                         if (t != expr.Type) {
5376                                                 expr = Convert.ExplicitConversion (
5377                                                         ec, expr, t, loc);
5378                                                 if (expr == null)
5379                                                         throw new InternalErrorException ();
5380                                         }
5381
5382                                         get_enumerator.InstanceExpression = expr;
5383                                         get_enumerator.IsBase = t != expr.Type;
5384
5385                                         return true;
5386                                 }
5387
5388                                 return false;
5389                         }               
5390
5391                         bool ProbeCollectionType (EmitContext ec, Type t)
5392                         {
5393                                 int errors = Report.Errors;
5394                                 for (Type tt = t; tt != null && tt != TypeManager.object_type;){
5395                                         if (TryType (ec, tt))
5396                                                 return true;
5397                                         tt = tt.BaseType;
5398                                 }
5399
5400                                 if (Report.Errors > errors)
5401                                         return false;
5402
5403                                 //
5404                                 // Now try to find the method in the interfaces
5405                                 //
5406                                 Type [] ifaces = TypeManager.GetInterfaces (t);
5407                                 foreach (Type i in ifaces){
5408                                         if (TryType (ec, i))
5409                                                 return true;
5410                                 }
5411
5412                                 return false;
5413                         }
5414
5415                         public override bool Resolve (EmitContext ec)
5416                         {
5417                                 enumerator_type = TypeManager.ienumerator_type;
5418                                 is_disposable = true;
5419
5420                                 if (!ProbeCollectionType (ec, expr.Type)) {
5421                                         Error_Enumerator ();
5422                                         return false;
5423                                 }
5424
5425                                 VarExpr ve = var_type as VarExpr;
5426                                 if (ve != null) {
5427                                         // Infer implicitly typed local variable from foreach enumerable type
5428                                         var_type = new TypeExpression (get_current.PropertyInfo.PropertyType, var_type.Location);
5429                                 }
5430
5431                                 var_type = var_type.ResolveAsTypeTerminal (ec, false);
5432                                 if (var_type == null)
5433                                         return false;
5434                                                                 
5435                                 enumerator = new TemporaryVariable (enumerator_type, loc);
5436                                 enumerator.Resolve (ec);
5437
5438                                 init = new Invocation (get_enumerator, null);
5439                                 init = init.Resolve (ec);
5440                                 if (init == null)
5441                                         return false;
5442
5443                                 Expression move_next_expr;
5444                                 {
5445                                         MemberInfo[] mi = new MemberInfo[] { move_next };
5446                                         MethodGroupExpr mg = new MethodGroupExpr (mi, var_type.Type, loc);
5447                                         mg.InstanceExpression = enumerator;
5448
5449                                         move_next_expr = new Invocation (mg, null);
5450                                 }
5451
5452                                 get_current.InstanceExpression = enumerator;
5453
5454                                 Statement block = new CollectionForeachStatement (
5455                                         var_type.Type, variable, get_current, statement, loc);
5456
5457                                 loop = new While (move_next_expr, block, loc);
5458
5459                                 bool ok = true;
5460
5461                                 FlowBranchingException branching = null;
5462                                 if (is_disposable)
5463                                         branching = ec.StartFlowBranching (this);
5464
5465                                 if (!loop.Resolve (ec))
5466                                         ok = false;
5467
5468                                 if (is_disposable) {
5469                                         ResolveFinally (branching);
5470                                         ec.EndFlowBranching ();
5471
5472                                         if (TypeManager.void_dispose_void == null) {
5473                                                 TypeManager.void_dispose_void = TypeManager.GetPredefinedMethod (
5474                                                         TypeManager.idisposable_type, "Dispose", loc, Type.EmptyTypes);
5475                                         }
5476                                 } else
5477                                         emit_finally = true;
5478
5479                                 return ok;
5480                         }
5481
5482                         protected override void DoEmit (EmitContext ec)
5483                         {
5484                                 ILGenerator ig = ec.ig;
5485
5486                                 enumerator.Store (ec, init);
5487
5488                                 //
5489                                 // Protect the code in a try/finalize block, so that
5490                                 // if the beast implement IDisposable, we get rid of it
5491                                 //
5492                                 if (is_disposable && emit_finally)
5493                                         ig.BeginExceptionBlock ();
5494                         
5495                                 loop.Emit (ec);
5496
5497                                 //
5498                                 // Now the finally block
5499                                 //
5500                                 if (is_disposable) {
5501                                         DoEmitFinally (ec);
5502                                         if (emit_finally)
5503                                                 ig.EndExceptionBlock ();
5504                                 }
5505                         }
5506
5507
5508                         public override void EmitFinally (EmitContext ec)
5509                         {
5510                                 ILGenerator ig = ec.ig;
5511
5512                                 if (enumerator_type.IsValueType) {
5513                                         MethodInfo mi = FetchMethodDispose (enumerator_type);
5514                                         if (mi != null) {
5515                                                 enumerator.EmitLoadAddress (ec);
5516                                                 ig.Emit (OpCodes.Call, mi);
5517                                         } else {
5518                                                 enumerator.Emit (ec);
5519                                                 ig.Emit (OpCodes.Box, enumerator_type);
5520                                                 ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
5521                                         }
5522                                 } else {
5523                                         Label call_dispose = ig.DefineLabel ();
5524
5525                                         enumerator.Emit (ec);
5526                                         ig.Emit (OpCodes.Isinst, TypeManager.idisposable_type);
5527                                         ig.Emit (OpCodes.Dup);
5528                                         ig.Emit (OpCodes.Brtrue_S, call_dispose);
5529                                         ig.Emit (OpCodes.Pop);
5530
5531                                         Label end_finally = ig.DefineLabel ();
5532                                         ig.Emit (OpCodes.Br, end_finally);
5533
5534                                         ig.MarkLabel (call_dispose);
5535                                         ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
5536                                         ig.MarkLabel (end_finally);
5537                                 }
5538                         }
5539                 }
5540
5541                 protected class CollectionForeachStatement : Statement
5542                 {
5543                         Type type;
5544                         Expression variable, current, conv;
5545                         Statement statement;
5546                         Assign assign;
5547
5548                         public CollectionForeachStatement (Type type, Expression variable,
5549                                                            Expression current, Statement statement,
5550                                                            Location loc)
5551                         {
5552                                 this.type = type;
5553                                 this.variable = variable;
5554                                 this.current = current;
5555                                 this.statement = statement;
5556                                 this.loc = loc;
5557                         }
5558
5559                         public override bool Resolve (EmitContext ec)
5560                         {
5561                                 current = current.Resolve (ec);
5562                                 if (current == null)
5563                                         return false;
5564
5565                                 conv = Convert.ExplicitConversion (ec, current, type, loc);
5566                                 if (conv == null)
5567                                         return false;
5568
5569                                 assign = new Assign (variable, conv, loc);
5570                                 if (assign.Resolve (ec) == null)
5571                                         return false;
5572
5573                                 if (!statement.Resolve (ec))
5574                                         return false;
5575
5576                                 return true;
5577                         }
5578
5579                         protected override void DoEmit (EmitContext ec)
5580                         {
5581                                 assign.EmitStatement (ec);
5582                                 statement.Emit (ec);
5583                         }
5584                 }
5585
5586                 protected override void CloneTo (CloneContext clonectx, Statement t)
5587                 {
5588                         Foreach target = (Foreach) t;
5589
5590                         target.type = type.Clone (clonectx);
5591                         target.variable = variable.Clone (clonectx);
5592                         target.expr = expr.Clone (clonectx);
5593                         target.statement = statement.Clone (clonectx);
5594                 }
5595         }
5596 }