Author: alexandre
Date: 2005-10-11 09:53:35 -0400 (Tue, 11 Oct 2005)
New Revision: 51573

Modified:
   trunk/mcs/mbas/ChangeLog
   trunk/mcs/mbas/block.cs
   trunk/mcs/mbas/mb-parser.jay
   trunk/mcs/mbas/statement.cs
Log:
2005-10-11 Maverson Eduardo Schulze Rosa <[EMAIL PROTECTED]>
           
        * block.cs, statements.cs, mb-parser.jay: Support for On Error 
Goto 0, On Error Goto -1 and On Error Goto <Label>



Modified: trunk/mcs/mbas/ChangeLog
===================================================================
--- trunk/mcs/mbas/ChangeLog    2005-10-11 13:52:53 UTC (rev 51572)
+++ trunk/mcs/mbas/ChangeLog    2005-10-11 13:53:35 UTC (rev 51573)
@@ -1,3 +1,8 @@
+2005-10-11 Maverson Eduardo Schulze Rosa <[EMAIL PROTECTED]>
+           
+       * block.cs, statements.cs, mb-parser.jay: Support for On Error Goto 0,
+       On Error Goto -1 and On Error Goto <Label>
+       
 2005-10-07 Maverson Eduardo Schulze Rosa <[EMAIL PROTECTED]>
            Alexandre Rocha Lima e Marcondes <[EMAIL PROTECTED]>       
            Rafael Teixeira <[EMAIL PROTECTED]>  

Modified: trunk/mcs/mbas/block.cs
===================================================================
--- trunk/mcs/mbas/block.cs     2005-10-11 13:52:53 UTC (rev 51572)
+++ trunk/mcs/mbas/block.cs     2005-10-11 13:53:35 UTC (rev 51573)
@@ -34,7 +34,7 @@
        ///   declarations.
        /// </remarks>
        public class Block : Statement {
-               public readonly Block     Parent;
+               public Block     Parent;
                public readonly bool      Implicit;
                public readonly Location  StartLocation;
                public Location    EndLocation;
@@ -156,7 +156,7 @@
                        }
                }
 
-               void AddChild (Block b)
+               public void AddChild (Block b)
                {
                        if (children == null)
                                children = new ArrayList ();
@@ -886,6 +886,8 @@
        /// </remarks>
        public class MethodBlock : Block {
                public readonly string MethodName;
+               public Block onerror;
+               public ArrayList Pending_Assigns;
                
                public MethodBlock (Block parent, string MethodName)
                        : base (parent, false, Location.Null, Location.Null)

Modified: trunk/mcs/mbas/mb-parser.jay
===================================================================
--- trunk/mcs/mbas/mb-parser.jay        2005-10-11 13:52:53 UTC (rev 51572)
+++ trunk/mcs/mbas/mb-parser.jay        2005-10-11 13:53:35 UTC (rev 51573)
@@ -1593,6 +1593,10 @@
                                            (ArrayList) $6, (Location)$3);
        
                method.Block = (Block) end_block();
+               
+               if ( ((MethodBlock) method.Block).onerror != null ) {
+                       method.Block = ((MethodBlock) method.Block).onerror;
+               }
                $$ = method;
 
                if ($5 != null) { 
@@ -2807,6 +2811,7 @@
        | selection_statement
        | iteration_statement
        | try_statement
+       | on_error_statement
        | synclock_statement
        | jump_statement
        | array_handling_statement 
@@ -3333,7 +3338,195 @@
                $$ = null;
        }
         ;
-        
+
+on_error_statement
+       : ON ERROR GOTO MINUS _mark_ LITERAL_INTEGER //ON ERROR GOTO -1
+         {// This should disables the exception in the current procedure.
+               
+               Location l = (Location) $5;
+       
+               if ( $6.ToString() == "1" )
+               {
+                       Block method_block = current_block;
+                       
+                       while ( !(method_block is MethodBlock) && 
(method_block.Parent != null) )
+                                       method_block = method_block.Parent;
+
+                       // call 
Microsoft.VisualBasic.CompilerServices.ProjectData.ClearProjectError()
+                       // This call will be created in all kinds of on error 
goto ( 0, -1, label )
+                       Expression clearerrors = DecomposeQI 
("Microsoft.VisualBasic.CompilerServices.ProjectData.ClearProjectError", l);
+                       StatementExpression stex = new StatementExpression (
+                                                       (ExpressionStatement) 
new Invocation (clearerrors, null, l),
+                                                       l);
+                       current_block.AddStatement ((Statement) stex );
+                       
+                       String var2 = "$ONERROR$2";//Loc_1 -Int-
+                       
+                       // This Assignment must be emited only if the method 
have a statement like ON ERROR GOTO label.
+                       // $ONERROR$2 = 0  (this Variable is created in a on 
error goto label stmt)
+                       // This variable will be used in a filter block to 
disable de exception, if the method have a statement like ON ERROR GOTO label.
+
+                       Expression var = DecomposeQI (var2, l);
+                       Expression value = new IntLiteral (0);
+                               
+                       StatementExpression assign = new StatementExpression( 
((ExpressionStatement) new Assign (var, value, l)), l);
+
+                       if( ((MethodBlock) method_block).onerror  != null ) 
//There is an On_Error Statement created.
+                               current_block.AddStatement ( (Statement) assign 
);
+                       else // Mark this assignment as pending, if exists an 
ON ERROR GOTO LABEL statement after this.
+                       {
+                               if ( ((MethodBlock) 
method_block).Pending_Assigns == null )
+                                       ((MethodBlock) 
method_block).Pending_Assigns = new ArrayList();
+
+                               Pending_Assign pa = new 
Pending_Assign(current_block, (Statement) assign, 
current_block.statements.Count );
+                               ((MethodBlock) 
method_block).Pending_Assigns.Add(pa) ;
+                       }       
+               }
+               else
+               {
+                       Report.Error (30203, l, "Identifier expected.");
+               }
+
+               $$ = null;
+         }
+       | ON ERROR GOTO _mark_ label_name
+         {
+               Location l = (Location) $4;
+               Block old_current = current_block;
+               Block method_block = current_block;
+                       
+               while ( !(method_block is MethodBlock) && (method_block.Parent 
!= null) )
+                               method_block = method_block.Parent;
+
+               // call 
Microsoft.VisualBasic.CompilerServices.ProjectData.ClearProjectError()
+               // This call will be created in all kinds of on error goto ( 0, 
-1, label )
+               Expression clearerrors = DecomposeQI 
("Microsoft.VisualBasic.CompilerServices.ProjectData.ClearProjectError", l);
+               StatementExpression stex = new StatementExpression (
+                                               (ExpressionStatement) new 
Invocation (clearerrors, null, l),
+                                               l);
+               current_block.AddStatement ((Statement) stex );
+
+               if ( $5.ToString() == "0") // ON ERROR GOTO 0
+               {// This should disables error handling in the current 
procedure.
+                       
+                       String var1 = "$ONERROR$1";//Loc_0 -Int- Number of 
Label.
+                       
+                       // This Assignment must be emited only if the method 
have a statement like ON ERROR GOTO label.
+                       // $ONERROR$1 = 0  (this Variable is created in a on 
error goto label stmt)
+                       // This variable will be used in a filter block to 
disable error handling, if the method have a statement like ON ERROR GOTO label.
+
+                       Expression var = DecomposeQI (var1, l);
+                       Expression value = new IntLiteral (0);
+                               
+                       StatementExpression assign = new StatementExpression( 
((ExpressionStatement) new Assign (var, value, l)), l);
+
+                       if( ((MethodBlock) method_block).onerror  != null ) 
//There is an On_Error Statement created.
+                               current_block.AddStatement ( (Statement) assign 
);
+                       else // Mark this assignment as pending, if exists an 
ON ERROR GOTO LABEL statement after this.
+                       {
+                               if ( ((MethodBlock) 
method_block).Pending_Assigns == null )
+                                       ((MethodBlock) 
method_block).Pending_Assigns = new ArrayList();
+
+                               Pending_Assign pa = new 
Pending_Assign(current_block, (Statement) assign, 
current_block.statements.Count );
+                               ((MethodBlock) 
method_block).Pending_Assigns.Add(pa) ;
+                       }       
+               }
+               else // ON ERROR GOTO LABEL
+               {// This should Enables the error-handling routine that starts 
at the line specified in the required line argument.
+       
+                       String var1 = "$ONERROR$1";//Loc_0 -Int-Number of Label.
+                       String var2 = "$ONERROR$2";//Loc_1 -Int-
+                       String var3 = "$ONERROR$3";//Loc_2 -System.Exception- 
Exception Object.
+                       
+                       VariableInfo vi = current_block.GetVariableInfo (var1);
+                       
+                       if( vi == null )
+                       { // First On Error Statement
+                         // 1) Declare local variables to handle goto.
+                         // 1.1) Dim $ONERROR$01  (Variable Generated because 
on error stmt)
+                         // 1.2) Dim $ONERROR$02  (Variable Generated because 
on error stmt)
+                         // 1.3) Dim $ONERROR$03  (Variable Generated because 
on error stmt)
+                         // 2) Declare a Parent Block of Method_Block to 
handle the method_block stmts within a try stmt.
+                         // 3) Declere OnError stmt and mark its block as 
Parent of method_block.
+                         // 4) Assign the pending assignments if they exist.
+                       
+                               Block block = new Block( method_block.Parent, 
null,
+                                  lexer.Location, Location.Null);
+
+                               //Cleaning the variables created in a top 
block(these variables already were created in method_block)
+                               block.Variables.Clear();
+
+                               // Dim $ONERROR$01 As Integer (Variable 
Genearted because on error stmt)
+                               // Dim $ONERROR$02 As Integer (Variable 
Genearted because on error stmt)
+                               // Dim $ONERROR$03 As System.Exception 
(Variable Genearted because on error stmt)
+                               ArrayList VarDeclarations = new ArrayList ();
+                               VarDeclarations.Add (new VariableDeclaration 
(var1, TypeManager.system_int32_expr, null, l, null));
+                               VarDeclarations.Add (new VariableDeclaration 
(var2, TypeManager.system_int32_expr, null, l, null));
+                               VarDeclarations.Add (new VariableDeclaration 
(var3, DecomposeQI("System.Exception", l), null, l, null));
+                               ArrayList decl = new ArrayList ();
+                               decl.AddRange (VarDeclarations);
+                       
+                               DictionaryEntry de = new DictionaryEntry 
(DecomposeQI("_local_vars_", l), decl);
+                               current_block = block;
+                               declare_local_variables ((Expression) de.Key, 
(ArrayList) de.Value, l);
+
+                               //Mark these variables as Used
+                               vi = current_block.GetVariableInfo (var1);
+                               vi.Used = true;
+                               vi = current_block.GetVariableInfo (var2);
+                               vi.Used = true;
+                               vi = current_block.GetVariableInfo (var3);
+                               vi.Used = true;
+
+                               current_block = old_current;
+                               
+                               Statement stmt = new On_Error( method_block, l 
);
+                               block.AddStatement(stmt);
+                               block.AddChild(method_block);
+                               // This will make on_error block as Parent of 
method_block
+                               // This is needed because an implicit insertion 
of a try outside of method_block statements.
+                               ((MethodBlock) method_block).onerror = block;
+                               method_block.Parent = block;
+
+                               //This adds the pendinding assignments of any 
on error goto 0 or -1 before the first ON ERROR GOTO LABEL statement.
+                               if ( ((MethodBlock) 
method_block).Pending_Assigns != null )
+                               {
+                                       foreach ( Pending_Assign pa in  
((MethodBlock) method_block).Pending_Assigns )
+                                               pa.AddAssign();
+                                       ((MethodBlock) 
method_block).Pending_Assigns.Clear();
+                               }
+                       }
+               
+                       int targetnumber = 0;
+
+                       foreach ( Statement s in ((MethodBlock) 
method_block).onerror.statements )
+                       {
+                               if ( s is On_Error )
+                               {
+                                       targetnumber = ((On_Error) 
s).AddTarget( (string) $5 );
+                                       break;  
+                               }
+                       }
+
+                       // $ONERROR$1 = targetnumber + 1 (this Variable already 
was created in a on error stmt)
+                       // This variable will be used in a switch to indicate 
de used label
+                       // and to indicate the exit in a comparison with false.
+
+                       Expression var = DecomposeQI (var1, l);
+                       Expression value = new IntLiteral (targetnumber + 1);
+                               
+                       StatementExpression assign = new StatementExpression( 
((ExpressionStatement) new Assign (var, value, l)), l);
+                       current_block.AddStatement ( (Statement) assign );
+                       
+               }
+               $$ = null;
+         } 
+       | ON ERROR _mark_ RESUME NEXT
+         { //Control goes to the statement immediately following the statement 
+           //where the error occurred and execution continues from that point.
+               
+         }
+       ;
        
 do_statement
        : DO _mark_ opt_do_construct end_of_stmt

Modified: trunk/mcs/mbas/statement.cs
===================================================================
--- trunk/mcs/mbas/statement.cs 2005-10-11 13:52:53 UTC (rev 51572)
+++ trunk/mcs/mbas/statement.cs 2005-10-11 13:53:35 UTC (rev 51573)
@@ -4560,7 +4560,213 @@
                        return true;
                }
        }
+       
+       public  class Pending_Assign {
+               Statement assign;
+               Block block_to_be_inserted;
+               int statement_index;
+               
+               public Pending_Assign (Block block, Statement assign, int index)
+               {
+                       this.assign = assign;
+                       this.block_to_be_inserted = block;
+                       this.statement_index = index;
+               }
 
+               public  void AddAssign()
+               {
+                       block_to_be_inserted.statements.Insert( 
statement_index, assign );
+               }       
+       }
+       
+       public class On_Error : Statement {
+               public readonly Block method_block;
+               ArrayList targets;
+               LabeledStatement[] labeledstatements;
+               
+               public On_Error (Block method_block, Location l)
+               {       
+                       //Goto
+                       this.method_block = method_block;
+                       base.loc = l;
+                       targets = new ArrayList();
+               }
+
+               public int AddTarget ( string target )
+               {                               
+                       int i = targets.IndexOf( target ) ;
+                       
+                       if ( i == -1 ) 
+                               return targets.Add ( target );
+                       else
+                               return i;
+               }
+
+               public ArrayList Targets {
+                       get {
+                               return targets;
+                       }
+               }
+
+               public override bool Resolve (EmitContext ec)
+               {
+                       this.labeledstatements = new 
LabeledStatement[targets.Count];
+                       
+                       bool ok = true;
+                       
+                       ec.StartFlowBranching (FlowBranchingType.EXCEPTION, 
method_block.StartLocation);
+
+                       Report.Debug (1, "START OF TRY BLOCK", 
method_block.StartLocation);
+
+                       bool old_in_try = ec.InTry;
+                       ec.InTry = true;
+
+                       if (!method_block.Resolve (ec))
+                               ok = false;
+
+                       ec.InTry = old_in_try;
+
+                       FlowBranching.UsageVector vector = 
ec.CurrentBranching.CurrentUsageVector;
+
+                       ec.EndFlowBranching ();
+
+                       ec.CurrentBranching.CurrentUsageVector.Or (vector);
+
+                       Report.Debug (1, "END OF TRY", ec.CurrentBranching);
+
+                       //This statement has more than one label in cases of 
more than one 'on error goto' specification.
+                       for ( int i = 0 ; i < targets.Count ; i++ )
+                       {
+                               labeledstatements[i] = 
method_block.Parent.LookupLabel ((string) targets[i]);
+
+                               // If this is a forward goto.
+                               if (!labeledstatements[i].IsDefined)
+                                       labeledstatements[i].AddUsageVector 
(ec.CurrentBranching.CurrentUsageVector);
+
+                               labeledstatements[i].AddReference ();
+                       }
+                       ec.CurrentBranching.CurrentUsageVector.Breaks = 
FlowReturns.ALWAYS;
+                       
+                       return ok;
+               }
+               protected override bool DoEmit (EmitContext ec)
+               {       
+                       ILGenerator ig = ec.ig;
+                               
+                       //Try
+
+                       bool returns;
+
+                       ec.TryCatchLevel++;
+                       
+                       Label finish = ig.BeginExceptionBlock ();
+                               ec.HasExitLabel = true;
+                               ec.ExitLabel = finish;
+                               
+                               bool old_in_try = ec.InTry;
+                               ec.InTry = true;
+                               returns = method_block.Emit (ec);
+                               ec.InTry = old_in_try;
+                               bool old_in_catch = ec.InCatch;
+                               ec.InCatch = true;
+
+                       Label error = ig.DefineLabel();
+                       Label endfilter = ig.DefineLabel();
+                       Label verification = ig.DefineLabel();
+                       
+                       ig.BeginExceptFilterBlock();
+                               ig.Emit (OpCodes.Isinst, 
Type.GetType("System.Exception") );
+                               ig.Emit (OpCodes.Brtrue_S, verification );
+
+                               ig.Emit (OpCodes.Br_S, error);
+                       
+                               ig.MarkLabel ( verification );  
+                               ig.Emit (OpCodes.Ldloc_0);
+                               ig.Emit (OpCodes.Brfalse_S, error);
+                               
+                               ig.Emit (OpCodes.Ldloc_1);
+                               ig.Emit (OpCodes.Brtrue_S, error);
+
+                               ig.Emit (OpCodes.Ldc_I4_1);
+                               ig.Emit (OpCodes.Br_S, endfilter);
+                               
+                               ig.MarkLabel (error);   
+                               ig.Emit (OpCodes.Ldc_I4_0);
+
+                               ig.MarkLabel ( endfilter );     
+                       
+                       
+                       Label endcatch = ig.DefineLabel();              
+                       Label verification2 = ig.DefineLabel();
+                       
+                       ig.BeginCatchBlock (null);
+                               ig.Emit (OpCodes.Castclass, 
Type.GetType("System.Exception"));
+                               ig.Emit (OpCodes.Dup);
+
+                               Type t = 
typeof(Microsoft.VisualBasic.CompilerServices.ProjectData);
+                               MethodInfo mi = t.GetMethod("SetProjectError", 
new Type[] { typeof(System.Exception) } );
+                               ig.Emit (OpCodes.Call, mi);
+                               ig.Emit (OpCodes.Stloc_3);
+                               ig.Emit (OpCodes.Ldloc_1);
+                               ig.Emit (OpCodes.Brfalse_S, verification2 );
+                               ig.Emit (OpCodes.Leave_S, finish );
+                               ig.MarkLabel ( verification2 );
+                               ig.Emit (OpCodes.Ldc_I4_M1);
+                               ig.Emit (OpCodes.Stloc_1);
+                               ig.Emit (OpCodes.Ldloc_0);
+
+                               Label[] lbswitch = new Label[targets.Count + 1];
+                               for ( int i = 0; i <= targets.Count; i++ )
+                                       lbswitch[i] = ig.DefineLabel();
+                               
+                               ig.Emit (OpCodes.Switch, lbswitch);     
+                               ig.MarkLabel ( lbswitch[0] ) ;
+                               ig.Emit (OpCodes.Leave_S, endcatch);    
+                                                               
+                               Label[] labels = new Label[targets.Count];
+                               for ( int i = 0; i < targets.Count; i++ )
+                               {
+                                       labels[i] = 
labeledstatements[i].LabelTarget(ec);
+
+                                       ig.MarkLabel ( lbswitch[i+1] ) ;
+                                       ig.Emit ( OpCodes.Leave, labels[i] );
+                               }
+
+                               ig.MarkLabel ( endcatch );
+                               ig.Emit ( OpCodes.Rethrow );    
+
+                       ec.InCatch = old_in_catch;
+
+                       ig.EndExceptionBlock ();
+                       ec.TryCatchLevel--;
+
+                       Label end = ig.DefineLabel();
+                       //ig.MarkLabel ( finish ) ;
+                       ig.Emit (OpCodes.Ldloc_1);
+                       ig.Emit (OpCodes.Brfalse_S, end );
+                               
+                       mi = t.GetMethod("ClearProjectError");
+                       ig.Emit (OpCodes.Call, mi);
+
+                       ig.MarkLabel(end) ;
+                       
+                       if (!returns)
+                               return returns;
+
+                       // Unfortunately, System.Reflection.Emit automatically 
emits a leave
+                       // to the end of the finally block.  This is a problem 
if 'returns'
+                       // is true since we may jump to a point after the end 
of the method.
+                       // As a workaround, emit an explicit ret here.
+
+                       if (ec.ReturnType != null)
+                               ec.ig.Emit (OpCodes.Ldloc, ec.TemporaryReturn 
());
+                       ec.ig.Emit (OpCodes.Ret);
+
+                       return true;
+               }
+               
+       }
+
        public class Using : Statement {
                object expression_or_block;
                Statement Statement;

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

Reply via email to