2005-04-11 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / statement.cs
index e050e8f00d2c735232f52470dce16789f9a9e040..0ce9b01df8d59a1a2cc86440d32be57701bc7f83 100644 (file)
@@ -139,11 +139,14 @@ namespace Mono.CSharp {
 
                public override bool Resolve (EmitContext ec)
                {
+                       bool ok = true;
+
                        Report.Debug (1, "START IF BLOCK", loc);
 
                        expr = Expression.ResolveBoolean (ec, expr, loc);
                        if (expr == null){
-                               return false;
+                               ok = false;
+                               goto skip;
                        }
 
                        Assign ass = expr as Assign;
@@ -177,17 +180,17 @@ namespace Mono.CSharp {
 
                                return true;
                        }
-                       
+               skip:
                        ec.StartFlowBranching (FlowBranching.BranchingType.Conditional, loc);
                        
-                       bool ok = TrueStatement.Resolve (ec);
+                       ok &= TrueStatement.Resolve (ec);
 
                        is_true_ret = ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable;
 
                        ec.CurrentBranching.CreateSibling ();
 
-                       if ((FalseStatement != null) && !FalseStatement.Resolve (ec))
-                               ok = false;
+                       if (FalseStatement != null)
+                               ok &= FalseStatement.Resolve (ec);
                                        
                        ec.EndFlowBranching ();
 
@@ -530,7 +533,8 @@ namespace Mono.CSharp {
 
                public override bool Resolve (EmitContext ec)
                {
-                       expr = expr.ResolveStatement (ec);
+                       if (expr != null)
+                               expr = expr.ResolveStatement (ec);
                        return expr != null;
                }
                
@@ -579,6 +583,12 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
+                               if (ec.InIterator) {
+                                       Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
+                                               "statement to return a value, or yield break to end the iteration");
+                                       return false;
+                               }
+
                                Expr = Expr.Resolve (ec);
                                if (Expr == null)
                                        return false;
@@ -601,7 +611,7 @@ namespace Mono.CSharp {
                        if (ec.CurrentBranching.InTryOrCatch (true)) {
                                ec.CurrentBranching.AddFinallyVector (vector);
                                in_exc = true;
-                       } else if (ec.CurrentBranching.InFinally (true)) {
+                       } else if (ec.InFinally) {
                                Error (157, "Control can not leave the body of the finally block");
                                return false;
                        } else
@@ -632,7 +642,6 @@ namespace Mono.CSharp {
 
        public class Goto : Statement {
                string target;
-               Block block;
                LabeledStatement label;
                
                public override bool Resolve (EmitContext ec)
@@ -646,13 +655,13 @@ namespace Mono.CSharp {
                                label.AddUsageVector (ec.CurrentBranching.CurrentUsageVector);
 
                        ec.CurrentBranching.CurrentUsageVector.Goto ();
+                       label.AddReference ();
 
                        return true;
                }
                
-               public Goto (Block parent_block, string label, Location l)
+               public Goto (string label, Location l)
                {
-                       block = parent_block;
                        loc = l;
                        target = label;
                }
@@ -718,8 +727,6 @@ namespace Mono.CSharp {
                {
                        ec.CurrentBranching.Label (vectors);
 
-                       referenced = true;
-
                        return true;
                }
 
@@ -732,6 +739,11 @@ namespace Mono.CSharp {
                        LabelTarget (ec);
                        ec.ig.MarkLabel (label);
                }
+
+               public void AddReference ()
+               {
+                       referenced = true;
+               }
        }
        
 
@@ -859,13 +871,13 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-                       if (ec.CurrentBranching.InFinally (true)) {
-                               Error (724, "A throw statement with no argument is only allowed in a catch clause nested inside of the innermost catch clause");
+                       if (!ec.InCatch) {
+                               Error (156, "A throw statement with no arguments is not allowed outside of a catch clause");
                                return false;
                        }
 
-                       if (!ec.CurrentBranching.InCatch ()) {
-                               Error (156, "A throw statement with no argument is only allowed in a catch clause");
+                       if (ec.InFinally) {
+                               Error (724, "A throw statement with no argument is only allowed in a catch clause nested inside of the innermost catch clause");
                                return false;
                        }
                        return true;
@@ -897,7 +909,7 @@ namespace Mono.CSharp {
                        if (!ec.CurrentBranching.InLoop () && !ec.CurrentBranching.InSwitch ()){
                                Error (139, "No enclosing loop or switch to continue to");
                                return false;
-                       } else if (ec.CurrentBranching.InFinally (false)) {
+                       } else if (ec.InFinally) {
                                Error (157, "Control can not leave the body of the finally block");
                                return false;
                        } else if (ec.CurrentBranching.InTryOrCatch (false))
@@ -942,7 +954,7 @@ namespace Mono.CSharp {
                        if (!ec.CurrentBranching.InLoop () && !ec.CurrentBranching.InSwitch ()){
                                Error (139, "No enclosing loop to continue to");
                                return false;
-                       } else if (ec.CurrentBranching.InFinally (false)) {
+                       } else if (ec.InFinally) {
                                Error (157, "Control can not leave the body of the finally block");
                                return false;
                        } else if (ec.CurrentBranching.InTryOrCatch (false))
@@ -1159,6 +1171,8 @@ namespace Mono.CSharp {
                public readonly Location  StartLocation;
                public Location EndLocation = Location.Null;
 
+               public readonly ToplevelBlock Toplevel;
+
                [Flags]
                public enum Flags {
                        Implicit  = 1,
@@ -1237,11 +1251,6 @@ namespace Mono.CSharp {
                //
                // Keeps track of constants
                Hashtable constants;
-
-               //
-               // The parameters for the block, this is only needed on the toplevel block really
-               // TODO: move `parameters' into ToplevelBlock
-               Parameters parameters;
                
                //
                // If this is a switch section, the enclosing switch block.
@@ -1260,42 +1269,33 @@ namespace Mono.CSharp {
                        : this (parent, flags, Location.Null, Location.Null)
                { }
 
-               public Block (Block parent, Flags flags, Parameters parameters)
-                       : this (parent, flags, parameters, Location.Null, Location.Null)
-               { }
-
                public Block (Block parent, Location start, Location end)
                        : this (parent, (Flags) 0, start, end)
                { }
 
-               public Block (Block parent, Parameters parameters, Location start, Location end)
-                       : this (parent, (Flags) 0, parameters, start, end)
-               { }
-
                public Block (Block parent, Flags flags, Location start, Location end)
-                       : this (parent, flags, Parameters.EmptyReadOnlyParameters, start, end)
-               { }
-
-               public Block (Block parent, Flags flags, Parameters parameters,
-                             Location start, Location end)
                {
                        if (parent != null)
                                parent.AddChild (this);
                        
                        this.Parent = parent;
                        this.flags = flags;
-                       this.parameters = parameters;
                        this.StartLocation = start;
                        this.EndLocation = end;
                        this.loc = start;
                        this_id = id++;
                        statements = new ArrayList ();
 
+                       if ((flags & Flags.IsToplevel) != 0)
+                               Toplevel = (ToplevelBlock) this;
+                       else
+                               Toplevel = parent.Toplevel;
+
                        if (parent != null && Implicit) {
-                               if (parent.child_variable_names == null)
-                                       parent.child_variable_names = new Hashtable();
+                               if (parent.known_variables == null)
+                                       parent.known_variables = new Hashtable ();
                                // share with parent
-                               child_variable_names = parent.child_variable_names;
+                               known_variables = parent.known_variables;
                        }
                                
                }
@@ -1442,30 +1442,54 @@ namespace Mono.CSharp {
                        }
                }
 
-               Hashtable child_variable_names;
+               Hashtable known_variables;
 
                // <summary>
-               //   Marks a variable with name @name as being used in a child block.
+               //   Marks a variable with name @name as being used in this or a child block.
                //   If a variable name has been used in a child block, it's illegal to
                //   declare a variable with the same name in the current block.
                // </summary>
-               public void AddChildVariableName (string name)
+               void AddKnownVariable (string name, LocalInfo info)
                {
-                       if (child_variable_names == null)
-                               child_variable_names = new Hashtable ();
+                       if (known_variables == null)
+                               known_variables = new Hashtable ();
 
-                       child_variable_names [name] = null;
+                       known_variables [name] = info;
                }
 
-               // <summary>
-               //   Checks whether a variable name has already been used in a child block.
-               // </summary>
-               public bool IsVariableNameUsedInChildBlock (string name)
+               LocalInfo GetKnownVariableInfo (string name)
+               {
+                       if (known_variables == null)
+                               return null;
+                       return (LocalInfo) known_variables [name];
+               }
+
+               public bool CheckInvariantMeaningInBlock (string name, Expression e, Location loc)
                {
-                       if (child_variable_names == null)
+                       LocalInfo kvi = GetKnownVariableInfo (name);
+                       if (kvi == null || kvi.Block == this)
+                               return true;
+
+                       if (known_variables != kvi.Block.known_variables) {
+                               Report.SymbolRelatedToPreviousError (kvi.Location, name);
+                               Report.Error (135, loc, "'{0}' has a different meaning in a child block", name);
                                return false;
+                       }
+
+                       //
+                       // this block and kvi.Block are the same textual block.
+                       // However, different variables are extant.
+                       //
+                       // Check if the variable is in scope in both blocks.  We use
+                       // an indirect check that depends on AddVariable doing its
+                       // part in maintaining the invariant-meaning-in-block property.
+                       //
+                       if (e is LocalVariableReference || (e is Constant && GetLocalInfo (name) != null))
+                               return true;
 
-                       return child_variable_names.Contains (name);
+                       Report.SymbolRelatedToPreviousError (kvi.Location, name);
+                       Report.Error (136, loc, "'{0}' has a different meaning later in the block", name);
+                       return false;
                }
 
                // <summary>
@@ -1492,58 +1516,47 @@ namespace Mono.CSharp {
                        return this_variable;
                }
 
-               public LocalInfo AddVariable (Expression type, string name, Parameters pars, Location l)
+               public LocalInfo AddVariable (Expression type, string name, Location l)
                {
                        if (variables == null)
                                variables = new Hashtable ();
 
                        LocalInfo vi = GetLocalInfo (name);
                        if (vi != null) {
-                               if (vi.Block != this)
-                                       Report.Error (136, l, "A local variable named `" + name + "' " +
-                                                     "cannot be declared in this scope since it would " +
-                                                     "give a different meaning to `" + name + "', which " +
-                                                     "is already used in a `parent or current' scope to " +
-                                                     "denote something else");
+                               Report.SymbolRelatedToPreviousError (vi.Location, name);
+                               if (known_variables == vi.Block.known_variables)
+                                       Report.Error (128, l,
+                                               "A local variable '{0}' is already declared in this scope", name);
                                else
-                                       Report.Error (128, l, "A local variable `" + name + "' is already " +
-                                                     "defined in this scope");
+                                       Report.Error (136, l,
+                                               "'{0}' hides the declaration of local variable '{0}' in a parent scope", name);
                                return null;
                        }
 
-                       if (IsVariableNameUsedInChildBlock (name)) {
-                               Report.Error (136, l, "A local variable named `" + name + "' " +
-                                             "cannot be declared in this scope since it would " +
-                                             "give a different meaning to `" + name + "', which " +
-                                             "is already used in a `child' scope to denote something " +
-                                             "else");
+                       vi = GetKnownVariableInfo (name);
+                       if (vi != null) {
+                               Report.SymbolRelatedToPreviousError (vi.Location, name);
+                               Report.Error (136, l,
+                                       "A child block already has a declaration of local variable '{0}':" +
+                                       " allowing this declaration would violate 'invariant meaning in a block'", 
+                                       name);
                                return null;
                        }
 
-                       if (pars != null) {
-                               int idx;
-                               Parameter p = pars.GetParameterByName (name, out idx);
-                               if (p != null) {
-                                       Report.Error (136, l, "A local variable named `" + name + "' " +
-                                                     "cannot be declared in this scope since it would " +
-                                                     "give a different meaning to `" + name + "', which " +
-                                                     "is already used in a `parent or current' scope to " +
-                                                     "denote something else");
-                                       return null;
-                               }
+                       int idx;
+                       Parameter p = Toplevel.Parameters.GetParameterByName (name, out idx);
+                       if (p != null) {
+                               Report.SymbolRelatedToPreviousError (Toplevel.Parameters.Location, name);
+                               Report.Error (136, l, "'{0}' hides a method parameter", name);
+                               return null;
                        }
 
                        vi = new LocalInfo (type, name, this, l);
 
                        variables.Add (name, vi);
 
-                       // Mark 'name' as "used by a child block" in every surrounding block
-                       Block cur = this;
-                       while (cur != null && cur.Implicit) 
-                               cur = cur.Parent;
-                       if (cur != null)
-                               for (Block par = cur.Parent; par != null; par = par.Parent)
-                                       par.AddChildVariableName (name);
+                       for (Block b = this; b != null; b = b.Parent)
+                               b.AddKnownVariable (name, vi);
 
                        if ((flags & Flags.VariablesInitialized) != 0)
                                throw new Exception ();
@@ -1552,9 +1565,9 @@ namespace Mono.CSharp {
                        return vi;
                }
 
-               public bool AddConstant (Expression type, string name, Expression value, Parameters pars, Location l)
+               public bool AddConstant (Expression type, string name, Expression value, Location l)
                {
-                       if (AddVariable (type, name, pars, l) == null)
+                       if (AddVariable (type, name, l) == null)
                                return false;
                        
                        if (constants == null)
@@ -1622,25 +1635,15 @@ namespace Mono.CSharp {
                //
                public ParameterReference GetParameterReference (string name, Location loc)
                {
-                       Block b = this;
-
-                       do {
-                               Parameters pars = b.parameters;
-                               
-                               if (pars != null){
-                                       Parameter par;
-                                       int idx;
-                                       
-                                       par = pars.GetParameterByName (name, out idx);
-                                       if (par != null){
-                                               ParameterReference pr;
+                       Parameter par;
+                       int idx;
 
-                                               pr = new ParameterReference (pars, this, idx, name, loc);
-                                               return pr;
-                                       }
-                               }
-                               b = b.Parent;
-                       } while (b != null);
+                       for (Block b = this; b != null; b = b.Toplevel.Parent) {
+                               Parameters pars = b.Toplevel.Parameters;
+                               par = pars.GetParameterByName (name, out idx);
+                               if (par != null)
+                                       return new ParameterReference (pars, this, idx, name, loc);
+                       }
                        return null;
                }
 
@@ -1650,24 +1653,7 @@ namespace Mono.CSharp {
                //
                public bool IsLocalParameter (string name)
                {
-                       Block b = this;
-                       int toplevel_count = 0;
-
-                       do {
-                               if (this is ToplevelBlock)
-                                       toplevel_count++;
-
-                               Parameters pars = b.parameters;
-                               if (pars != null){
-                                       if (pars.GetParameterByName (name) != null)
-                                               return true;
-                                       return false;
-                               }
-                               if (toplevel_count > 0)
-                                       return false;
-                               b = b.Parent;
-                       } while (b != null);
-                       return false;
+                       return Toplevel.Parameters.GetParameterByName (name) != null;
                }
                
                //
@@ -1675,16 +1661,14 @@ namespace Mono.CSharp {
                //
                public bool IsParameterReference (string name)
                {
-                       Block b = this;
+                       Parameter par;
+                       int idx;
 
-                       do {
-                               Parameters pars = b.parameters;
-                               
-                               if (pars != null)
-                                       if (pars.GetParameterByName (name) != null)
-                                               return true;
-                               b = b.Parent;
-                       } while (b != null);
+                       for (Block b = this; b != null; b = b.Toplevel.Parent) {
+                               par = b.Toplevel.Parameters.GetParameterByName (name, out idx);
+                               if (par != null)
+                                       return true;
+                       }
                        return false;
                }
                
@@ -1947,6 +1931,12 @@ namespace Mono.CSharp {
 
                        Report.Debug (4, "RESOLVE BLOCK", StartLocation, ec.CurrentBranching);
 
+                       //
+                       // This flag is used to notate nested statements as unreachable from the beginning of this block.
+                       // For the purposes of this resolution, it doesn't matter that the whole block is unreachable 
+                       // from the beginning of the function.  The outer Resolve() that detected the unreachability is
+                       // responsible for handling the situation.
+                       //
                        bool unreachable = false;
 
                        int statement_count = statements.Count;
@@ -1965,8 +1955,10 @@ namespace Mono.CSharp {
                                        else
                                                s.loc = Location.Null;
 
-                                       statements [ix] = EmptyStatement.Value;
-                                       continue;
+                                       if (ok && !(s is Block)) {
+                                               statements [ix] = EmptyStatement.Value;
+                                               continue;
+                                       }
                                }
 
                                if (s.Resolve (ec) == false) {
@@ -2023,7 +2015,18 @@ namespace Mono.CSharp {
                public override bool ResolveUnreachable (EmitContext ec, bool warn)
                {
                        unreachable_shown = true;
-                       return base.ResolveUnreachable (ec, warn);
+
+                       if (warn && (RootContext.WarningLevel >= 2))
+                               Report.Warning (162, loc, "Unreachable code detected");
+
+                       if (Implicit)
+                               return Resolve (ec);
+
+                       ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
+                       bool ok = Resolve (ec);
+                       ec.KillFlowBranching ();
+
+                       return ok;
                }
                
                protected override void DoEmit (EmitContext ec)
@@ -2034,7 +2037,7 @@ namespace Mono.CSharp {
                                // Check whether we are the last statement in a
                                // top-level block.
 
-                               if ((Parent == null) && (ix+1 == num_statements))
+                               if (((Parent == null) || Implicit) && (ix+1 == num_statements) && !(s is Block))
                                        ec.IsLastStatement = true;
                                else
                                        ec.IsLastStatement = false;
@@ -2054,7 +2057,7 @@ namespace Mono.CSharp {
 
                        if (emit_debug_info) {
                                if (is_lexical_block)
-                                       ec.ig.BeginScope ();
+                                       ec.BeginScope ();
 
                                if (variables != null) {
                                        foreach (DictionaryEntry de in variables) {
@@ -2074,24 +2077,11 @@ namespace Mono.CSharp {
                        ec.Mark (EndLocation, true); 
 
                        if (emit_debug_info && is_lexical_block)
-                               ec.ig.EndScope ();
+                               ec.EndScope ();
 
                        ec.CurrentBlock = prev_block;
                }
 
-               public ToplevelBlock Toplevel {
-                       get {
-                               Block b = this;
-                               while (b.Parent != null){
-                                       if ((b.flags & Flags.IsToplevel) != 0)
-                                               break;
-                                       b = b.Parent;
-                               }
-
-                               return (ToplevelBlock) b;
-                       }
-               }
-
                //
                // Returns true if we ar ea child of `b'.
                //
@@ -2126,8 +2116,10 @@ namespace Mono.CSharp {
 
                Hashtable capture_contexts;
 
-               static int did = 0;
-               
+               //
+               // The parameters for the block.
+               //
+               public readonly Parameters Parameters;
                        
                public void RegisterCaptureContext (CaptureContext cc)
                {
@@ -2157,22 +2149,28 @@ namespace Mono.CSharp {
                // parents
                //
                public ToplevelBlock (ToplevelBlock container, Parameters parameters, Location start) :
-                       base (null, Flags.IsToplevel, parameters, start, Location.Null)
+                       this (container, (Flags) 0, parameters, start)
                {
-                       Container = container;
                }
                
                public ToplevelBlock (Parameters parameters, Location start) :
-                       base (null, Flags.IsToplevel, parameters, start, Location.Null)
+                       this (null, (Flags) 0, parameters, start)
                {
                }
 
                public ToplevelBlock (Flags flags, Parameters parameters, Location start) :
-                       base (null, flags | Flags.IsToplevel, parameters, start, Location.Null)
+                       this (null, flags, parameters, start)
                {
                }
 
-               public ToplevelBlock (Location loc) : base (null, Flags.IsToplevel, loc, loc)
+               public ToplevelBlock (ToplevelBlock container, Flags flags, Parameters parameters, Location start) :
+                       base (null, flags | Flags.IsToplevel, start, Location.Null)
+               {
+                       Parameters = parameters == null ? Parameters.EmptyReadOnlyParameters : parameters;
+                       Container = container;
+               }
+
+               public ToplevelBlock (Location loc) : this (null, (Flags) 0, null, loc)
                {
                }
 
@@ -2953,24 +2951,6 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               bool ResolveConstantSwitch (EmitContext ec)
-               {
-                       object key = ((Constant) new_expr).GetValue ();
-                       SwitchLabel label = (SwitchLabel) Elements [key];
-
-                       if (label == null)
-                               return true;
-
-                       constant_section = FindSection (label);
-                       if (constant_section == null)
-                               return true;
-
-                       if (constant_section.Block.Resolve (ec) != true)
-                               return false;
-
-                       return true;
-               }
-
                public override bool Resolve (EmitContext ec)
                {
                        Expr = Expr.Resolve (ec);
@@ -3295,15 +3275,81 @@ namespace Mono.CSharp {
                ArrayList declarators;
                Statement statement;
                Type expr_type;
-               FixedData[] data;
+               Emitter[] data;
                bool has_ret;
 
-               struct FixedData {
-                       public bool is_object;
-                       public LocalInfo vi;
-                       public Expression expr;
-                       public Expression converted;
-               }                       
+               abstract class Emitter
+               {
+                       protected LocalInfo vi;
+                       protected Expression converted;
+
+                       protected Emitter (Expression expr, LocalInfo li)
+                       {
+                               converted = expr;
+                               vi = li;
+                       }
+
+                       public abstract void Emit (EmitContext ec);
+                       public abstract void EmitExit (ILGenerator ig);
+               }
+
+               class ExpressionEmitter: Emitter {
+                       public ExpressionEmitter (Expression converted, LocalInfo li) :
+                               base (converted, li)
+                       {
+                       }
+
+                       public override void Emit (EmitContext ec) {
+                               //
+                               // Store pointer in pinned location
+                               //
+                               converted.Emit (ec);
+                               ec.ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+                       }
+
+                       public override void EmitExit (ILGenerator ig)
+                       {
+                               ig.Emit (OpCodes.Ldc_I4_0);
+                               ig.Emit (OpCodes.Conv_U);
+                               ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+                       }
+               }
+
+               class StringEmitter: Emitter {
+                       LocalBuilder pinned_string;
+                       Location loc;
+
+                       public StringEmitter (Expression expr, LocalInfo li, Location loc):
+                               base (expr, li)
+                       {
+                               this.loc = loc;
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               ILGenerator ig = ec.ig;
+                               pinned_string = TypeManager.DeclareLocalPinned (ig, TypeManager.string_type);
+                                       
+                               converted.Emit (ec);
+                               ig.Emit (OpCodes.Stloc, pinned_string);
+
+                               Expression sptr = new StringPtr (pinned_string, loc);
+                               converted = Convert.ImplicitConversionRequired (
+                                       ec, sptr, vi.VariableType, loc);
+                                       
+                               if (converted == null)
+                                       return;
+
+                               converted.Emit (ec);
+                               ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+                       }
+
+                       public override void EmitExit(ILGenerator ig)
+                       {
+                               ig.Emit (OpCodes.Ldnull);
+                               ig.Emit (OpCodes.Stloc, pinned_string);
+                       }
+               }
 
                public Fixed (Expression type, ArrayList decls, Statement stmt, Location l)
                {
@@ -3333,7 +3379,7 @@ namespace Mono.CSharp {
                                return false;
                        }
                        
-                       data = new FixedData [declarators.Count];
+                       data = new Emitter [declarators.Count];
 
                        if (!expr_type.IsPointer){
                                Report.Error (209, loc, "Variables in a fixed statement must be pointers");
@@ -3390,10 +3436,7 @@ namespace Mono.CSharp {
                                        if (!TypeManager.VerifyUnManaged (child.Type, loc))
                                                return false;
 
-                                       data [i].is_object = true;
-                                       data [i].expr = e;
-                                       data [i].converted = null;
-                                       data [i].vi = vi;
+                                       data [i] = new ExpressionEmitter (e, vi);
                                        i++;
 
                                        continue;
@@ -3421,17 +3464,14 @@ namespace Mono.CSharp {
                                        // and T* is implicitly convertible to the
                                        // pointer type given in the fixed statement.
                                        //
-                                       ArrayPtr array_ptr = new ArrayPtr (e, loc);
+                                       ArrayPtr array_ptr = new ArrayPtr (e, array_type, loc);
                                        
                                        Expression converted = Convert.ImplicitConversionRequired (
                                                ec, array_ptr, vi.VariableType, loc);
                                        if (converted == null)
                                                return false;
 
-                                       data [i].is_object = false;
-                                       data [i].expr = e;
-                                       data [i].converted = converted;
-                                       data [i].vi = vi;
+                                       data [i] = new ExpressionEmitter (converted, vi);
                                        i++;
 
                                        continue;
@@ -3441,14 +3481,30 @@ namespace Mono.CSharp {
                                // Case 3: string
                                //
                                if (e.Type == TypeManager.string_type){
-                                       data [i].is_object = false;
-                                       data [i].expr = e;
-                                       data [i].converted = null;
-                                       data [i].vi = vi;
+                                       data [i] = new StringEmitter (e, vi, loc);
                                        i++;
                                        continue;
                                }
 
+                               // Case 4: fixed buffer
+                               FieldExpr fe = e as FieldExpr;
+                               if (fe != null) {
+                                       IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
+                                       if (ff != null) {
+                                               Expression fixed_buffer_ptr = new FixedBufferPtr (fe, ff.ElementType, loc);
+                                       
+                                               Expression converted = Convert.ImplicitConversionRequired (
+                                                       ec, fixed_buffer_ptr, vi.VariableType, loc);
+                                               if (converted == null)
+                                                       return false;
+
+                                               data [i] = new ExpressionEmitter (converted, vi);
+                                               i++;
+
+                                               continue;
+                                       }
+                               }
+
                                //
                                // For other cases, flag a `this is already fixed expression'
                                //
@@ -3478,60 +3534,8 @@ namespace Mono.CSharp {
                
                protected override void DoEmit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       LocalBuilder [] clear_list = new LocalBuilder [data.Length];
-                       
                        for (int i = 0; i < data.Length; i++) {
-                               LocalInfo vi = data [i].vi;
-
-                               //
-                               // Case 1: & object.
-                               //
-                               if (data [i].is_object) {
-                                       //
-                                       // Store pointer in pinned location
-                                       //
-                                       data [i].expr.Emit (ec);
-                                       ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
-                                       clear_list [i] = vi.LocalBuilder;
-                                       continue;
-                               }
-
-                               //
-                               // Case 2: Array
-                               //
-                               if (data [i].expr.Type.IsArray){
-                                       //
-                                       // Store pointer in pinned location
-                                       //
-                                       data [i].converted.Emit (ec);
-                                       
-                                       ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
-                                       clear_list [i] = vi.LocalBuilder;
-                                       continue;
-                               }
-
-                               //
-                               // Case 3: string
-                               //
-                               if (data [i].expr.Type == TypeManager.string_type){
-                                       LocalBuilder pinned_string = TypeManager.DeclareLocalPinned (ig, TypeManager.string_type);
-                                       clear_list [i] = pinned_string;
-                                       
-                                       data [i].expr.Emit (ec);
-                                       ig.Emit (OpCodes.Stloc, pinned_string);
-
-                                       Expression sptr = new StringPtr (pinned_string, loc);
-                                       Expression converted = Convert.ImplicitConversionRequired (
-                                               ec, sptr, vi.VariableType, loc);
-                                       
-                                       if (converted == null)
-                                               continue;
-
-                                       converted.Emit (ec);
-                                       ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
-                               }
+                               data [i].Emit (ec);
                        }
 
                        statement.Emit (ec);
@@ -3539,18 +3543,13 @@ namespace Mono.CSharp {
                        if (has_ret)
                                return;
 
+                       ILGenerator ig = ec.ig;
+
                        //
                        // Clear the pinned variable
                        //
                        for (int i = 0; i < data.Length; i++) {
-                               if (data [i].is_object || data [i].expr.Type.IsArray) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                       ig.Emit (OpCodes.Conv_U);
-                                       ig.Emit (OpCodes.Stloc, clear_list [i]);
-                               } else if (data [i].expr.Type == TypeManager.string_type){
-                                       ig.Emit (OpCodes.Ldnull);
-                                       ig.Emit (OpCodes.Stloc, clear_list [i]);
-                               }
+                               data [i].EmitExit (ig);
                        }
                }
        }
@@ -3588,23 +3587,30 @@ namespace Mono.CSharp {
 
                public override bool Resolve (EmitContext ec)
                {
-                       if (type_expr != null) {
-                               TypeExpr te = type_expr.ResolveAsTypeTerminal (ec, false);
-                               if (te == null)
-                                       return false;
+                       bool was_catch = ec.InCatch;
+                       ec.InCatch = true;
+                       try {
+                               if (type_expr != null) {
+                                       TypeExpr te = type_expr.ResolveAsTypeTerminal (ec, false);
+                                       if (te == null)
+                                               return false;
 
-                               type = te.ResolveType (ec);
+                                       type = te.ResolveType (ec);
 
-                               CheckObsolete (type);
+                                       CheckObsolete (type);
 
-                               if (type != TypeManager.exception_type && !type.IsSubclassOf (TypeManager.exception_type)){
-                                       Error (155, "The type caught or thrown must be derived from System.Exception");
-                                       return false;
-                               }
-                       } else
-                               type = null;
+                                       if (type != TypeManager.exception_type && !type.IsSubclassOf (TypeManager.exception_type)){
+                                               Error (155, "The type caught or thrown must be derived from System.Exception");
+                                               return false;
+                                       }
+                               } else
+                                       type = null;
 
-                       return Block.Resolve (ec);
+                               return Block.Resolve (ec);
+                       }
+                       finally {
+                               ec.InCatch = was_catch;
+                       }
                }
        }
 
@@ -3699,9 +3705,11 @@ namespace Mono.CSharp {
                                                Fini, FlowBranching.SiblingType.Finally);
 
                                Report.Debug (1, "STARTED SIBLING FOR FINALLY", ec.CurrentBranching, vector);
-
+                               bool was_finally = ec.InFinally;
+                               ec.InFinally = true;
                                if (!Fini.Resolve (ec))
                                        ok = false;
+                               ec.InFinally = was_finally;
                        }
 
                        ResolveFinally (branching);
@@ -3767,6 +3775,13 @@ namespace Mono.CSharp {
                                Fini.Emit (ec);
                        }
                }
+
+               public bool HasCatch
+               {
+                       get {
+                               return General != null || Specific.Count > 0;
+                       }
+               }
        }
 
        public class Using : ExceptionStatement {
@@ -3858,11 +3873,11 @@ namespace Mono.CSharp {
                bool ResolveExpression (EmitContext ec)
                {
                        if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
-                               conv = Convert.ImplicitConversionRequired (
-                                       ec, expr, TypeManager.idisposable_type, loc);
-
-                               if (conv == null)
+                               if (Convert.ImplicitConversion (ec, expr, TypeManager.idisposable_type, loc) == null) {
+                                       Report.Error (1674, loc, "'{0}': type used in a using statement must be implicitly convertible to 'System.IDisposable'",
+                                               TypeManager.CSharpName (expr_type));
                                        return false;
+                               }
                        }
 
                        return true;