Author: raja
Date: 2006-07-21 09:45:55 -0400 (Fri, 21 Jul 2006)
New Revision: 62859

Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/codegen.cs
   trunk/mcs/gmcs/ecore.cs
   trunk/mcs/gmcs/expression.cs
   trunk/mcs/gmcs/statement.cs
   trunk/mcs/mcs/ChangeLog
   trunk/mcs/mcs/codegen.cs
   trunk/mcs/mcs/ecore.cs
   trunk/mcs/mcs/expression.cs
   trunk/mcs/mcs/statement.cs
Log:
Make comparisons use the same IL irrespective of whether they're
in a 'checked' or 'unchecked' context: one of the issues in #78899
* codegen.cs (EmitContext.CheckState): Make read-only property.
(EmitContext.ConstantCheckState): Likewise.
(EmitContext.CheckStateHandle, EmitContext.WithCheckState): New
helper that implement a save/restore stack for CheckState
values.  This is the only way to change check-state.
* ecore.cs (Expression.ExpressionToArrayArgument): Use WithCheckState.
* expression.cs (CheckedExpr.DoResolve, CheckedExpr.Emit): Likewise.
(CheckedExpr.EmitBranchable): New forwarding method.
(UnCheckedExpr): Likewise.
* statement.cs (Block.ResolveMeta): Use WithCheckState.
(Unchecked.Resolve, Unchecked.DoEmit): Likewise.
(Checked.Resolve, checked.DoEmit): Likewise.


Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/gmcs/ChangeLog    2006-07-21 13:45:55 UTC (rev 62859)
@@ -1,3 +1,20 @@
+2006-07-21  Raja R Harinath  <[EMAIL PROTECTED]>
+
+       Make comparisons use the same IL irrespective of whether they're
+       in a 'checked' or 'unchecked' context: one of the issues in #78899
+       * codegen.cs (EmitContext.CheckState): Make read-only property.
+       (EmitContext.ConstantCheckState): Likewise.
+       (EmitContext.CheckStateHandle, EmitContext.WithCheckState): New
+       helper that implement a save/restore stack for CheckState
+       values.  This is the only way to change check-state.
+       * ecore.cs (Expression.ExpressionToArrayArgument): Use WithCheckState.
+       * expression.cs (CheckedExpr.DoResolve, CheckedExpr.Emit): Likewise.
+       (CheckedExpr.EmitBranchable): New forwarding method.
+       (UnCheckedExpr): Likewise.
+       * statement.cs (Block.ResolveMeta): Use WithCheckState.
+       (Unchecked.Resolve, Unchecked.DoEmit): Likewise.
+       (Checked.Resolve, checked.DoEmit): Likewise.
+
 2006-07-21  Martin Baulig  <[EMAIL PROTECTED]>
 
        * generic.cs (TypeManager.InferType): When inferring an array

Modified: trunk/mcs/gmcs/codegen.cs
===================================================================
--- trunk/mcs/gmcs/codegen.cs   2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/gmcs/codegen.cs   2006-07-21 13:45:55 UTC (rev 62859)
@@ -217,17 +217,16 @@
                ///   The default setting comes from the command line option to 
generate
                ///   checked or unchecked code plus any source code changes 
using the
                ///   checked/unchecked statements or expressions.   Contrast 
this with
-               ///   the ConstantCheckState flag.
+               ///   the constant_check_state flag.
                /// </summary>
-               
-               public bool CheckState;
+               bool check_state;
 
                /// <summary>
                ///   The constant check state is always set to `true' and cant 
be changed
                ///   from the command line.  The source code can change this 
setting with
                ///   the `checked' and `unchecked' statements and expressions. 
                /// </summary>
-               public bool ConstantCheckState;
+               bool constant_check_state;
 
                /// <summary>
                ///   Whether we are emitting code inside a static or instance 
method
@@ -395,8 +394,8 @@
 
                        TypeContainer = parent;
                        this.declSpace = ds;
-                       CheckState = RootContext.Checked;
-                       ConstantCheckState = true;
+                       check_state = RootContext.Checked;
+                       constant_check_state = true;
 
                        if ((return_type is TypeBuilder) && 
return_type.IsGenericTypeDefinition)
                                throw new InternalErrorException ();
@@ -438,6 +437,42 @@
                        set { declSpace = value; }
                }
 
+               public bool CheckState {
+                       get { return check_state; }
+               }
+
+               public bool ConstantCheckState {
+                       get { return constant_check_state; }
+               }
+
+               // utility helper for CheckExpr, UnCheckExpr, Checked and 
Unchecked statements
+               // it's public so that we can use a struct at the callsite
+               public struct CheckStateHandle : IDisposable
+               {
+                       EmitContext ec;
+                       bool check, const_check;
+                       public CheckStateHandle (EmitContext ec, bool 
new_check_state, bool new_constant_check_state)
+                       {
+                               this.ec = ec;
+                               check = ec.check_state;
+                               const_check = ec.constant_check_state;
+                               ec.check_state = new_check_state;
+                               ec.constant_check_state = 
new_constant_check_state;
+                       }
+                       void IDisposable.Dispose ()
+                       {
+                               ec.check_state = check;
+                               ec.constant_check_state = const_check;
+                       }
+               }
+
+               // Temporarily set 'CheckState' and 'ConstantCheckState' to the 
given values.
+               // Should be used in an 'using' statement
+               public CheckStateHandle WithCheckState (bool check_state, bool 
constant_check_state)
+               {
+                       return new CheckStateHandle (this, check_state, 
constant_check_state);
+               }
+               
                public bool IsInObsoleteScope {
                        get { return ResolveContext.IsInObsoleteScope; }
                }
@@ -447,12 +482,8 @@
                }
 
                public bool IsAnonymousMethodAllowed {
-                       get {
-                               return isAnonymousMethodAllowed;
-                       }
-                       set {
-                               isAnonymousMethodAllowed = value;
-                       }
+                       get { return isAnonymousMethodAllowed; }
+                       set { isAnonymousMethodAllowed = value; }
                }
 
                public FlowBranching CurrentBranching {

Modified: trunk/mcs/gmcs/ecore.cs
===================================================================
--- trunk/mcs/gmcs/ecore.cs     2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/gmcs/ecore.cs     2006-07-21 13:45:55 UTC (rev 62859)
@@ -1098,22 +1098,20 @@
                {
                        Expression target;
                        
-                       bool old_checked = ec.CheckState;
-                       ec.CheckState = true;
-                       
-                       target = Convert.ImplicitConversion (ec, source, 
TypeManager.int32_type, loc);
-                       if (target == null){
-                               target = Convert.ImplicitConversion (ec, 
source, TypeManager.uint32_type, loc);
-                               if (target == null){
+                       using (ec.WithCheckState (true, ec.ConstantCheckState)) 
{
+                               target = Convert.ImplicitConversion (ec, 
source, TypeManager.int32_type, loc);
+                               if (target == null)
+                                       target = Convert.ImplicitConversion 
(ec, source, TypeManager.uint32_type, loc);
+                               if (target == null)
                                        target = Convert.ImplicitConversion 
(ec, source, TypeManager.int64_type, loc);
-                                       if (target == null){
-                                               target = 
Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
-                                               if (target == null)
-                                                       
source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
-                                       }
+                               if (target == null)
+                                       target = Convert.ImplicitConversion 
(ec, source, TypeManager.uint64_type, loc);
+
+                               if (target == null) {
+                                       source.Error_ValueCannotBeConverted 
(loc, TypeManager.int32_type, false);
+                                       return null;
                                }
-                       } 
-                       ec.CheckState = old_checked;
+                       }
 
                        //
                        // Only positive constants are allowed at compile time

Modified: trunk/mcs/gmcs/expression.cs
===================================================================
--- trunk/mcs/gmcs/expression.cs        2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/gmcs/expression.cs        2006-07-21 13:45:55 UTC (rev 62859)
@@ -7352,14 +7352,8 @@
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Expr = Expr.Resolve (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.WithCheckState (true, true))
+                               Expr = Expr.Resolve (ec);
                        
                        if (Expr == null)
                                return null;
@@ -7374,16 +7368,15 @@
 
                public override void Emit (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Expr.Emit (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.WithCheckState (true, true))
+                               Expr.Emit (ec);
                }
-               
+
+               public override void EmitBranchable (EmitContext ec, Label 
target, bool onTrue)
+               {
+                       using (ec.WithCheckState (true, true))
+                               Expr.EmitBranchable (ec, target, onTrue);
+               }
        }
 
        /// <summary>
@@ -7401,15 +7394,9 @@
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
+                       using (ec.WithCheckState (false, false))
+                               Expr = Expr.Resolve (ec);
 
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Expr = Expr.Resolve (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
-
                        if (Expr == null)
                                return null;
 
@@ -7423,16 +7410,15 @@
 
                public override void Emit (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-                       
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Expr.Emit (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.WithCheckState (false, false))
+                               Expr.Emit (ec);
                }
                
+               public override void EmitBranchable (EmitContext ec, Label 
target, bool onTrue)
+               {
+                       using (ec.WithCheckState (false, false))
+                               Expr.EmitBranchable (ec, target, onTrue);
+               }
        }
 
        /// <summary>

Modified: trunk/mcs/gmcs/statement.cs
===================================================================
--- trunk/mcs/gmcs/statement.cs 2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/gmcs/statement.cs 2006-07-21 13:45:55 UTC (rev 62859)
@@ -1719,70 +1719,68 @@
                        param_map = new VariableMap (ip);
                        flags |= Flags.VariablesInitialized;
 
-                       bool old_check_state = ec.ConstantCheckState;
-                       ec.ConstantCheckState = (flags & Flags.Unchecked) == 0;
-                               
                        //
                        // Process this block variables
                        //
                        if (variables != null){
-                               foreach (DictionaryEntry de in variables){
-                                       string name = (string) de.Key;
-                                       LocalInfo vi = (LocalInfo) de.Value;
-                                       
-                                       if (vi.VariableType == null)
-                                               continue;
+                               using (ec.WithCheckState (ec.CheckState, (flags 
& Flags.Unchecked) == 0)) {
+                                       foreach (DictionaryEntry de in 
variables) {
+                                               string name = (string) de.Key;
+                                               LocalInfo vi = (LocalInfo) 
de.Value;
 
-                                       Type variable_type = vi.VariableType;
+                                               if (vi.VariableType == null)
+                                                       continue;
 
-                                       if (variable_type.IsPointer){
-                                               //
-                                               // Am not really convinced that 
this test is required (Microsoft does it)
-                                               // but the fact is that you 
would not be able to use the pointer variable
-                                               // *anyways*
-                                               //
-                                               if 
(!TypeManager.VerifyUnManaged (TypeManager.GetElementType (variable_type),
-                                                                               
   vi.Location))
+                                               Type variable_type = 
vi.VariableType;
+
+                                               if (variable_type.IsPointer) {
+                                                       //
+                                                       // Am not really 
convinced that this test is required (Microsoft does it)
+                                                       // but the fact is that 
you would not be able to use the pointer variable
+                                                       // *anyways*
+                                                       //
+                                                       if 
(!TypeManager.VerifyUnManaged (TypeManager.GetElementType (variable_type),
+                                                                               
          vi.Location))
+                                                               continue;
+                                               }
+
+                                               if (constants == null)
                                                        continue;
-                                       }
 
-                                       if (constants == null)
-                                               continue;
+                                               Expression cv = (Expression) 
constants [name];
+                                               if (cv == null)
+                                                       continue;
 
-                                       Expression cv = (Expression) constants 
[name];
-                                       if (cv == null)
-                                               continue;
+                                               // Don't let 'const int Foo = 
Foo;' succeed.
+                                               // Removing the name from 
'constants' ensures that we get a LocalVariableReference below,
+                                               // which in turn causes the 
'must be constant' error to be triggered.
+                                               constants.Remove (name);
 
-                                       // Don't let 'const int Foo = Foo;' 
succeed.
-                                       // Removing the name from 'constants' 
ensures that we get a LocalVariableReference below,
-                                       // which in turn causes the 'must be 
constant' error to be triggered.
-                                       constants.Remove (name);
+                                               ec.CurrentBlock = this;
+                                               Expression e = cv.Resolve (ec);
+                                               if (e == null)
+                                                       continue;
 
-                                       ec.CurrentBlock = this;
-                                       Expression e = cv.Resolve (ec);
-                                       if (e == null)
-                                               continue;
+                                               Constant ce = e as Constant;
+                                               if (ce == null) {
+                                                       
Const.Error_ExpressionMustBeConstant (variable_type, vi.Location, name);
+                                                       continue;
+                                               }
 
-                                       Constant ce = e as Constant;
-                                       if (ce == null){
-                                               
Const.Error_ExpressionMustBeConstant (variable_type, vi.Location, name);
-                                               continue;
-                                       }
+                                               e = ce.ToType (variable_type, 
vi.Location);
+                                               if (e == null)
+                                                       continue;
 
-                                       e = ce.ToType (variable_type, 
vi.Location);
-                                       if (e == null)
-                                               continue;
+                                               if (!variable_type.IsValueType 
&& variable_type != TypeManager.string_type && !ce.IsDefaultValue) {
+                                                       
Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
+                                                       continue;
+                                               }
 
-                                       if (!variable_type.IsValueType && 
variable_type != TypeManager.string_type && !ce.IsDefaultValue) {
-                                               
Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
-                                               continue;
+                                               constants.Add (name, e);
+                                               vi.IsConstant = true;
                                        }
-
-                                       constants.Add (name, e);
-                                       vi.IsConstant = true;
                                }
                        }
-                       ec.ConstantCheckState = old_check_state;
 
                        //
                        // Now, handle the children
@@ -3227,28 +3225,14 @@
 
                public override bool Resolve (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       bool ret = Block.Resolve (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
-
-                       return ret;
+                       using (ec.WithCheckState (false, false))
+                               return Block.Resolve (ec);
                }
                
                protected override void DoEmit (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-                       
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Block.Emit (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
+                       using (ec.WithCheckState (false, false))
+                               Block.Emit (ec);
                }
        }
 
@@ -3263,28 +3247,14 @@
 
                public override bool Resolve (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       bool ret = Block.Resolve (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
-
-                       return ret;
+                       using (ec.WithCheckState (true, true))
+                               return Block.Resolve (ec);
                }
 
                protected override void DoEmit (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Block.Emit (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
+                       using (ec.WithCheckState (true, true))
+                               Block.Emit (ec);
                }
        }
 

Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog     2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/mcs/ChangeLog     2006-07-21 13:45:55 UTC (rev 62859)
@@ -1,3 +1,20 @@
+2006-07-21  Raja R Harinath  <[EMAIL PROTECTED]>
+
+       Make comparisons use the same IL irrespective of whether they're
+       in a 'checked' or 'unchecked' context: one of the issues in #78899
+       * codegen.cs (EmitContext.CheckState): Make read-only property.
+       (EmitContext.ConstantCheckState): Likewise.
+       (EmitContext.CheckStateHandle, EmitContext.WithCheckState): New
+       helper that implement a save/restore stack for CheckState
+       values.  This is the only way to change check-state.
+       * ecore.cs (Expression.ExpressionToArrayArgument): Use WithCheckState.
+       * expression.cs (CheckedExpr.DoResolve, CheckedExpr.Emit): Likewise.
+       (CheckedExpr.EmitBranchable): New forwarding method.
+       (UnCheckedExpr): Likewise.
+       * statement.cs (Block.ResolveMeta): Use WithCheckState.
+       (Unchecked.Resolve, Unchecked.DoEmit): Likewise.
+       (Checked.Resolve, checked.DoEmit): Likewise.
+
 2006-07-20  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * anonymous.cs: Cache the resolved anonymous delegate, and return

Modified: trunk/mcs/mcs/codegen.cs
===================================================================
--- trunk/mcs/mcs/codegen.cs    2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/mcs/codegen.cs    2006-07-21 13:45:55 UTC (rev 62859)
@@ -213,17 +213,16 @@
                ///   The default setting comes from the command line option to 
generate
                ///   checked or unchecked code plus any source code changes 
using the
                ///   checked/unchecked statements or expressions.   Contrast 
this with
-               ///   the ConstantCheckState flag.
+               ///   the constant_check_state flag.
                /// </summary>
-               
-               public bool CheckState;
+               bool check_state;
 
                /// <summary>
                ///   The constant check state is always set to `true' and cant 
be changed
                ///   from the command line.  The source code can change this 
setting with
                ///   the `checked' and `unchecked' statements and expressions. 
                /// </summary>
-               public bool ConstantCheckState;
+               bool constant_check_state;
 
                /// <summary>
                ///   Whether we are emitting code inside a static or instance 
method
@@ -384,8 +383,8 @@
 
                        TypeContainer = parent;
                        this.declSpace = ds;
-                       CheckState = RootContext.Checked;
-                       ConstantCheckState = true;
+                       check_state = RootContext.Checked;
+                       constant_check_state = true;
 
                        IsStatic = (code_flags & Modifiers.STATIC) != 0;
                        MethodIsStatic = IsStatic;
@@ -424,6 +423,42 @@
                        set { declSpace = value; }
                }
 
+               public bool CheckState {
+                       get { return check_state; }
+               }
+
+               public bool ConstantCheckState {
+                       get { return constant_check_state; }
+               }
+
+               // utility helper for CheckExpr, UnCheckExpr, Checked and 
Unchecked statements
+               // it's public so that we can use a struct at the callsite
+               public struct CheckStateHandle : IDisposable
+               {
+                       EmitContext ec;
+                       bool check, const_check;
+                       public CheckStateHandle (EmitContext ec, bool 
new_check_state, bool new_constant_check_state)
+                       {
+                               this.ec = ec;
+                               check = ec.check_state;
+                               const_check = ec.constant_check_state;
+                               ec.check_state = new_check_state;
+                               ec.constant_check_state = 
new_constant_check_state;
+                       }
+                       void IDisposable.Dispose ()
+                       {
+                               ec.check_state = check;
+                               ec.constant_check_state = const_check;
+                       }
+               }
+
+               // Temporarily set 'CheckState' and 'ConstantCheckState' to the 
given values.
+               // Should be used in an 'using' statement
+               public CheckStateHandle WithCheckState (bool check_state, bool 
constant_check_state)
+               {
+                       return new CheckStateHandle (this, check_state, 
constant_check_state);
+               }
+               
                public bool IsInObsoleteScope {
                        get { return ResolveContext.IsInObsoleteScope; }
                }
@@ -433,12 +468,8 @@
                }
 
                public bool IsAnonymousMethodAllowed {
-                       get {
-                               return isAnonymousMethodAllowed;
-                       }
-                       set {
-                               isAnonymousMethodAllowed = value;
-                       }
+                       get { return isAnonymousMethodAllowed; }
+                       set { isAnonymousMethodAllowed = value; }
                }
 
                public FlowBranching CurrentBranching {

Modified: trunk/mcs/mcs/ecore.cs
===================================================================
--- trunk/mcs/mcs/ecore.cs      2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/mcs/ecore.cs      2006-07-21 13:45:55 UTC (rev 62859)
@@ -1070,22 +1070,20 @@
                {
                        Expression target;
                        
-                       bool old_checked = ec.CheckState;
-                       ec.CheckState = true;
-                       
-                       target = Convert.ImplicitConversion (ec, source, 
TypeManager.int32_type, loc);
-                       if (target == null){
-                               target = Convert.ImplicitConversion (ec, 
source, TypeManager.uint32_type, loc);
-                               if (target == null){
+                       using (ec.WithCheckState (true, ec.ConstantCheckState)) 
{
+                               target = Convert.ImplicitConversion (ec, 
source, TypeManager.int32_type, loc);
+                               if (target == null)
+                                       target = Convert.ImplicitConversion 
(ec, source, TypeManager.uint32_type, loc);
+                               if (target == null)
                                        target = Convert.ImplicitConversion 
(ec, source, TypeManager.int64_type, loc);
-                                       if (target == null){
-                                               target = 
Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
-                                               if (target == null)
-                                                       
source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
-                                       }
+                               if (target == null)
+                                       target = Convert.ImplicitConversion 
(ec, source, TypeManager.uint64_type, loc);
+
+                               if (target == null) {
+                                       source.Error_ValueCannotBeConverted 
(loc, TypeManager.int32_type, false);
+                                       return null;
                                }
-                       } 
-                       ec.CheckState = old_checked;
+                       }
 
                        //
                        // Only positive constants are allowed at compile time

Modified: trunk/mcs/mcs/expression.cs
===================================================================
--- trunk/mcs/mcs/expression.cs 2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/mcs/expression.cs 2006-07-21 13:45:55 UTC (rev 62859)
@@ -6953,14 +6953,8 @@
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Expr = Expr.Resolve (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.WithCheckState (true, true))
+                               Expr = Expr.Resolve (ec);
                        
                        if (Expr == null)
                                return null;
@@ -6975,16 +6969,15 @@
 
                public override void Emit (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Expr.Emit (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.WithCheckState (true, true))
+                               Expr.Emit (ec);
                }
-               
+
+               public override void EmitBranchable (EmitContext ec, Label 
target, bool onTrue)
+               {
+                       using (ec.WithCheckState (true, true))
+                               Expr.EmitBranchable (ec, target, onTrue);
+               }
        }
 
        /// <summary>
@@ -7002,15 +6995,9 @@
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
+                       using (ec.WithCheckState (false, false))
+                               Expr = Expr.Resolve (ec);
 
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Expr = Expr.Resolve (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
-
                        if (Expr == null)
                                return null;
 
@@ -7024,16 +7011,15 @@
 
                public override void Emit (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-                       
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Expr.Emit (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.WithCheckState (false, false))
+                               Expr.Emit (ec);
                }
                
+               public override void EmitBranchable (EmitContext ec, Label 
target, bool onTrue)
+               {
+                       using (ec.WithCheckState (false, false))
+                               Expr.EmitBranchable (ec, target, onTrue);
+               }
        }
 
        /// <summary>

Modified: trunk/mcs/mcs/statement.cs
===================================================================
--- trunk/mcs/mcs/statement.cs  2006-07-21 13:34:49 UTC (rev 62858)
+++ trunk/mcs/mcs/statement.cs  2006-07-21 13:45:55 UTC (rev 62859)
@@ -1719,75 +1719,73 @@
                        param_map = new VariableMap (ip);
                        flags |= Flags.VariablesInitialized;
 
-                       bool old_check_state = ec.ConstantCheckState;
-                       ec.ConstantCheckState = (flags & Flags.Unchecked) == 0;
-                       
                        //
                        // Process this block variables
                        //
-                       if (variables != null){
-                               foreach (DictionaryEntry de in variables){
-                                       string name = (string) de.Key;
-                                       LocalInfo vi = (LocalInfo) de.Value;
-                                       
-                                       if (vi.VariableType == null)
-                                               continue;
+                       if (variables != null) {
+                               using (ec.WithCheckState (ec.CheckState, (flags 
& Flags.Unchecked) == 0)) {
+                                       foreach (DictionaryEntry de in 
variables) {
+                                               string name = (string) de.Key;
+                                               LocalInfo vi = (LocalInfo) 
de.Value;
+                                               
+                                               if (vi.VariableType == null)
+                                                       continue;
 
-                                       Type variable_type = vi.VariableType;
+                                               Type variable_type = 
vi.VariableType;
 
-                                       if (variable_type.IsPointer){
-                                               //
-                                               // Am not really convinced that 
this test is required (Microsoft does it)
-                                               // but the fact is that you 
would not be able to use the pointer variable
-                                               // *anyways*
-                                               //
-                                               if 
(!TypeManager.VerifyUnManaged (TypeManager.GetElementType (variable_type),
-                                                                               
   vi.Location))
+                                               if (variable_type.IsPointer) {
+                                                       //
+                                                       // Am not really 
convinced that this test is required (Microsoft does it)
+                                                       // but the fact is that 
you would not be able to use the pointer variable
+                                                       // *anyways*
+                                                       //
+                                                       if 
(!TypeManager.VerifyUnManaged (TypeManager.GetElementType (variable_type),
+                                                                               
          vi.Location))
+                                                               continue;
+                                               }
+
+                                               if (constants == null)
                                                        continue;
-                                       }
 
-                                       if (constants == null)
-                                               continue;
+                                               Expression cv = (Expression) 
constants [name];
+                                               if (cv == null)
+                                                       continue;
 
-                                       Expression cv = (Expression) constants 
[name];
-                                       if (cv == null)
-                                               continue;
+                                               // Don't let 'const int Foo = 
Foo;' succeed.
+                                               // Removing the name from 
'constants' ensures that we get a LocalVariableReference below,
+                                               // which in turn causes the 
'must be constant' error to be triggered.
+                                               constants.Remove (name);
 
-                                       // Don't let 'const int Foo = Foo;' 
succeed.
-                                       // Removing the name from 'constants' 
ensures that we get a LocalVariableReference below,
-                                       // which in turn causes the 'must be 
constant' error to be triggered.
-                                       constants.Remove (name);
+                                               ec.CurrentBlock = this;
+                                               Expression e = cv.Resolve (ec);
+                                               if (e == null)
+                                                       continue;
 
-                                       ec.CurrentBlock = this;
-                                       Expression e = cv.Resolve (ec);
-                                       if (e == null)
-                                               continue;
+                                               Constant ce = e as Constant;
+                                               if (ce == null) {
+                                                       
Const.Error_ExpressionMustBeConstant (variable_type, vi.Location, name);
+                                                       continue;
+                                               }
 
-                                       Constant ce = e as Constant;
-                                       if (ce == null){
-                                               
Const.Error_ExpressionMustBeConstant (variable_type, vi.Location, name);
-                                               continue;
-                                       }
+                                               e = ce.ToType (variable_type, 
vi.Location);
+                                               if (e == null)
+                                                       continue;
 
-                                       e = ce.ToType (variable_type, 
vi.Location);
-                                       if (e == null)
-                                               continue;
+                                               if (!variable_type.IsValueType 
&& variable_type != TypeManager.string_type && !ce.IsDefaultValue) {
+                                                       
Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
+                                                       continue;
+                                               }
 
-                                       if (!variable_type.IsValueType && 
variable_type != TypeManager.string_type && !ce.IsDefaultValue) {
-                                               
Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
-                                               continue;
+                                               constants.Add (name, e);
+                                               vi.IsConstant = true;
                                        }
-
-                                       constants.Add (name, e);
-                                       vi.IsConstant = true;
                                }
                        }
-                       ec.ConstantCheckState = old_check_state;
 
                        //
                        // Now, handle the children
                        //
-                       if (children != null){
+                       if (children != null) {
                                foreach (Block b in children)
                                        b.ResolveMeta (toplevel, ec, ip);
                        }
@@ -3190,28 +3188,14 @@
 
                public override bool Resolve (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       bool ret = Block.Resolve (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
-
-                       return ret;
+                       using (ec.WithCheckState (false, false))
+                               return Block.Resolve (ec);
                }
                
                protected override void DoEmit (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-                       
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Block.Emit (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
+                       using (ec.WithCheckState (false, false))
+                               Block.Emit (ec);
                }
        }
 
@@ -3226,28 +3210,14 @@
 
                public override bool Resolve (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       bool ret = Block.Resolve (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
-
-                       return ret;
+                       using (ec.WithCheckState (true, true))
+                               return Block.Resolve (ec);
                }
 
                protected override void DoEmit (EmitContext ec)
                {
-                       bool previous_state = ec.CheckState;
-                       bool previous_state_const = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Block.Emit (ec);
-                       ec.CheckState = previous_state;
-                       ec.ConstantCheckState = previous_state_const;
+                       using (ec.WithCheckState (true, true))
+                               Block.Emit (ec);
                }
        }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to