Author: sudha
Date: 2005-05-05 06:12:30 -0400 (Thu, 05 May 2005)
New Revision: 44068
Modified:
trunk/mcs/mbas/ChangeLog
trunk/mcs/mbas/ecore.cs
trunk/mcs/mbas/expression.cs
trunk/mcs/mbas/mb-parser.jay
trunk/mcs/mbas/statement.cs
Log:
* statement.cs
* mb-parser.jay:
Changed the 'For' class to :
- Handle negative step values
- Supporting all types (numeric/object) of loop control variables
* expression.cs : Removed some unused methods
* ecore.cs : Added float/double -> decimal conversion to
NarrowingConversion
Modified: trunk/mcs/mbas/ChangeLog
===================================================================
--- trunk/mcs/mbas/ChangeLog 2005-05-05 09:46:42 UTC (rev 44067)
+++ trunk/mcs/mbas/ChangeLog 2005-05-05 10:12:30 UTC (rev 44068)
@@ -1,3 +1,13 @@
+2005-05-05 Satya Sudha K <[EMAIL PROTECTED]>
+ * statement.cs
+ * mb-parser.jay:
+ Changed the 'For' class to :
+ - Handle negative step values
+ - Supporting all types (numeric/object) of loop control
variables
+
+ * expression.cs : Removed some unused methods
+ * ecore.cs : Added float/double -> decimal conversion to
NarrowingConversion
+
2005-05-02 Manjula GHM <[EMAIL PROTECTED]>
* expression.cs : Support Reference Parameter - when argument and
parameter are different.
Modified: trunk/mcs/mbas/ecore.cs
===================================================================
--- trunk/mcs/mbas/ecore.cs 2005-05-05 09:46:42 UTC (rev 44067)
+++ trunk/mcs/mbas/ecore.cs 2005-05-05 10:12:30 UTC (rev 44068)
@@ -1914,6 +1914,10 @@
return true;
+ } else if (target_type == TypeManager.decimal_type){
+ if (expr_type == TypeManager.float_type ||
+ expr_type == TypeManager.double_type)
+ return true;
} else if (target_type == TypeManager.float_type){
//
// To float from double
@@ -2726,7 +2730,7 @@
if (real_target_type == TypeManager.char_type)
return new ConvCast (ec, rounded_expr,
target_type, ConvCast.Mode.R8_CH);
if (real_target_type == TypeManager.float_type)
- return new ConvCast (ec, rounded_expr,
target_type, ConvCast.Mode.R8_R4);
+ return new ConvCast (ec, expr,
target_type, ConvCast.Mode.R8_R4);
}
// decimal is taken care of by the op_Explicit methods.
Modified: trunk/mcs/mbas/expression.cs
===================================================================
--- trunk/mcs/mbas/expression.cs 2005-05-05 09:46:42 UTC (rev 44067)
+++ trunk/mcs/mbas/expression.cs 2005-05-05 10:12:30 UTC (rev 44068)
@@ -724,14 +724,6 @@
"++" : "--";
}
- void Error23 (Type t)
- {
- Error (
- 30311, "Operator " + OperName (mode) +
- " cannot be applied to operand of type '" +
- TypeManager.MonoBASIC_Name (t) + "'");
- }
-
/// <summary>
/// Returns whether an object of type 't' can be incremented
/// or decremented with add/sub (ie, basically whether we can
@@ -2038,17 +2030,6 @@
oper == Operator.ExclusiveOr);
}
- bool IsNumericType (Type type) {
- return (type == TypeManager.byte_type ||
- type == TypeManager.sbyte_type ||
- type == TypeManager.short_type ||
- type == TypeManager.int32_type ||
- type == TypeManager.int64_type ||
- type == TypeManager.decimal_type ||
- type == TypeManager.double_type ||
- type == TypeManager.float_type);
- }
-
Expression ResolveOperator (EmitContext ec)
{
Type l = left.Type;
Modified: trunk/mcs/mbas/mb-parser.jay
===================================================================
--- trunk/mcs/mbas/mb-parser.jay 2005-05-05 09:46:42 UTC (rev 44067)
+++ trunk/mcs/mbas/mb-parser.jay 2005-05-05 10:12:30 UTC (rev 44068)
@@ -3328,37 +3328,27 @@
}
_mark_ opt_identifier
{
- string s = $3.ToString();
- string s1 = "";
- if ($16 != null)
- s1 = $16.ToString();
- if (s1 != "" && s != s1)
- {
- Report.Error(30070, (Location)$15,
"Next Control Variable '"+s1+"' does not match with For Loop control variable
'"+s+"'");
- }
+ string s = $3.ToString();
+ string s1 = "";
+ if ($16 != null)
+ s1 = $16.ToString();
+ if (s1 != "" && s != s1) {
+ Report.Error(30070, (Location)$15, "Next Control
Variable '"+s1+"' does not match with For Loop control variable '"+s+"'");
+ }
- Block inner_statement = end_block();
- Location l = (Location)$2;
- Expression for_var = (Expression) DecomposeQI
((string)$3, l);
+ Block inner_statement = end_block();
+ Location l = (Location)$2;
+ Expression for_var = (Expression) DecomposeQI ((string)$3, l);
- Expression assign_expr = new Assign (for_var, (Expression) $6, l);
- Expression test_expr = new Binary
(Binary.Operator.LessThanOrEqual,
- for_var,
(Expression) $8, l);
- Expression step_expr = new Assign (for_var, (Expression) new
Binary (Binary.Operator.Addition,
- for_var, (Expression) $9, l), l);
-
- Statement assign_stmt = new StatementExpression
((ExpressionStatement) assign_expr, l);
- Statement step_stmt = new StatementExpression
((ExpressionStatement) step_expr, l);
-
- For f = new For (assign_stmt, test_expr, step_stmt,
inner_statement, l);
- if ($4 != null)
- {
- current_block.AddStatement (f);
- $$ = end_block();
- }
- else
- $$ = f;
- }
+ For f = new For (for_var, (Expression) $6, (Expression) $8,
(Expression) $9, inner_statement, l);
+ if ($4 != null)
+ {
+ current_block.AddStatement (f);
+ $$ = end_block();
+ }
+ else
+ $$ = f;
+ }
;
opt_step
Modified: trunk/mcs/mbas/statement.cs
===================================================================
--- trunk/mcs/mbas/statement.cs 2005-05-05 09:46:42 UTC (rev 44067)
+++ trunk/mcs/mbas/statement.cs 2005-05-05 10:12:30 UTC (rev 44068)
@@ -462,35 +462,183 @@
}
public class For : Statement {
- Expression Test;
- readonly Statement InitStatement;
- readonly Statement Increment;
- readonly Statement Statement;
+ Expression LoopControlVar;
+ Expression Start;
+ Expression Limit;
+ Expression StepValue;
+ Statement statement, Increment;
bool may_return, infinite, empty;
+ private Statement InitStatement;
+ // required when loop control var is of type 'Object'
+ Expression Test, AddnTest;
+ LocalTemporary ltmp;
+ bool is_lcv_object;
- public For (Statement initStatement,
- Expression test,
- Statement increment,
+ public For (Expression loopVar,
+ Expression start,
+ Expression limit,
+ Expression stepVal,
Statement statement,
Location l)
{
- InitStatement = initStatement;
- Test = test;
- Increment = increment;
- Statement = statement;
+ LoopControlVar = loopVar;
+ Start = start;
+ Limit = limit;
+ StepValue = stepVal;
+ this.statement = statement;
loc = l;
+ ltmp = null;
+
+ InitStatement = new StatementExpression
((ExpressionStatement) (new Assign (LoopControlVar, Start, loc)), loc);
+ Increment = new StatementExpression (
+ (ExpressionStatement) (new
CompoundAssign (Binary.Operator.Addition,
+
LoopControlVar, StepValue, loc)), loc);
+ AddnTest = null;
+ is_lcv_object = false;
}
-
public override bool Resolve (EmitContext ec)
{
bool ok = true;
+ LoopControlVar = LoopControlVar.Resolve (ec);
+ if (LoopControlVar == null)
+ return false;
+
+ Start = Start.Resolve (ec);
+ Limit = Limit.Resolve (ec);
+ StepValue = StepValue.Resolve (ec);
+ if (StepValue == null || Start == null || Limit == null)
+ return false;
+
+ double value = 0;
+ if (StepValue is Constant) {
+
+ value = GetValue (StepValue);
+ if (value > 0) // Positive Step value
+ Test = new Binary
(Binary.Operator.LessThanOrEqual, LoopControlVar, Limit, loc);
+ else if (value < 0)
+ Test = new Binary
(Binary.Operator.GreaterThanOrEqual, LoopControlVar, Limit, loc);
+ }
+
+ if (Start is Constant && Limit is Constant) {
+ if (value > 0)
+ AddnTest = ConstantFold.BinaryFold (ec,
Binary.Operator.LessThanOrEqual,
+
(Constant) Start, (Constant) Limit, loc);
+ else if (value < 0)
+ AddnTest = ConstantFold.BinaryFold (ec,
Binary.Operator.GreaterThanOrEqual,
+
(Constant) Start, (Constant) Limit, loc);
+ }
+
+
+ string method_to_call = null;
+ Binary left, right;
+ left = right = null;
+
+ switch (Type.GetTypeCode (LoopControlVar.Type)) {
+ case TypeCode.Boolean :
+ case TypeCode.Char :
+ case TypeCode.DateTime :
+ case TypeCode.String :
+ Report.Error (30337,loc,"'For' loop control
variable cannot be of type '" + LoopControlVar.Type + "'");
+ return false;
+ case TypeCode.Byte :
+ if (Test == null)
+ Test = new Binary
(Binary.Operator.LessThanOrEqual, LoopControlVar, Limit, loc);
+ break;
+ case TypeCode.Int16 :
+ if (Test == null) {
+ left = new Binary
(Binary.Operator.ExclusiveOr,
+ new Binary
(Binary.Operator.RightShift, StepValue, new IntLiteral (15), loc),
+ LoopControlVar,
+ loc);
+ right = new Binary
(Binary.Operator.ExclusiveOr,
+ new Binary
(Binary.Operator.RightShift, StepValue, new IntLiteral (15), loc),
+ Limit,
+ loc);
+ Test = new Binary
(Binary.Operator.LessThanOrEqual, left, right, loc);
+ }
+ break;
+ case TypeCode.Int32 :
+ if (Test == null) {
+ left = new Binary
(Binary.Operator.ExclusiveOr,
+ new Binary
(Binary.Operator.RightShift, StepValue, new IntLiteral (31), loc),
+ LoopControlVar,
+ loc);
+ right = new Binary
(Binary.Operator.ExclusiveOr,
+ new Binary
(Binary.Operator.RightShift, StepValue, new IntLiteral (31), loc),
+ Limit,
+ loc);
+ Test = new Binary
(Binary.Operator.LessThanOrEqual, left, right, loc);
+ }
+ break;
+ case TypeCode.Int64 :
+ if (Test == null) {
+ left = new Binary
(Binary.Operator.ExclusiveOr,
+ new Binary
(Binary.Operator.RightShift, StepValue, new IntLiteral (63), loc),
+ LoopControlVar,
+ loc);
+ right = new Binary
(Binary.Operator.ExclusiveOr,
+ new Binary
(Binary.Operator.RightShift, StepValue, new IntLiteral (63), loc),
+ Limit,
+ loc);
+ Test = new Binary
(Binary.Operator.LessThanOrEqual, left, right, loc);
+ }
+ break;
+ case TypeCode.Decimal :
+ method_to_call =
"Microsoft.VisualBasic.CompilerServices.FlowControl.ForNextCheckDec";
+ break;
+ case TypeCode.Single :
+ method_to_call =
"Microsoft.VisualBasic.CompilerServices.FlowControl.ForNextCheckR4";
+ break;
+ case TypeCode.Double :
+ method_to_call =
"Microsoft.VisualBasic.CompilerServices.FlowControl.ForNextCheckR8";
+ break;
+ case TypeCode.Object :
+ is_lcv_object = true;
+ ArrayList initArgs = new ArrayList ();
+ initArgs.Add (new Argument (LoopControlVar,
Argument.AType.Expression));
+ initArgs.Add (new Argument (Start,
Argument.AType.Expression));
+ initArgs.Add (new Argument (Limit,
Argument.AType.Expression));
+ initArgs.Add (new Argument (StepValue,
Argument.AType.Expression));
+ ltmp = new LocalTemporary (ec,
TypeManager.object_type);
+ initArgs.Add (new Argument (ltmp,
Argument.AType.Ref));
+ initArgs.Add (new Argument (LoopControlVar,
Argument.AType.Ref));
+ Expression sname = Parser.DecomposeQI
("Microsoft.VisualBasic.CompilerServices.FlowControl.ForLoopInitObj", loc);
+ AddnTest = new Invocation (sname, initArgs,
loc);
+ //AddnTest = new Binary
(Binary.Operator.Inequality, inv, new BoolLiteral (false), loc);
+ ArrayList args = new ArrayList ();
+ args.Add (new Argument (LoopControlVar,
Argument.AType.Expression));
+ args.Add (new Argument (ltmp,
Argument.AType.Expression));
+ args.Add (new Argument (LoopControlVar,
Argument.AType.Ref));
+ sname = Parser.DecomposeQI
("Microsoft.VisualBasic.CompilerServices.FlowControl.ForNextCheckObj", loc);
+ Test = new Invocation (sname, args, loc);
+ //Test = new Binary
(Binary.Operator.Inequality, inv, new BoolLiteral (false), loc);
+ break;
+ }
+
+ if (method_to_call != null && !method_to_call.Equals
("")) {
+ ArrayList args = null;
+ args = new ArrayList ();
+ args.Add (new Argument (LoopControlVar,
Argument.AType.Expression));
+ args.Add (new Argument (Limit,
Argument.AType.Expression));
+ args.Add (new Argument (StepValue,
Argument.AType.Expression));
+ Expression sname = Parser.DecomposeQI
(method_to_call, loc);
+ Test = new Invocation (sname, args, loc);
+ //Test = new Binary
(Binary.Operator.Inequality, invocation, new BoolLiteral (false), loc);
+ }
+
if (InitStatement != null){
if (!InitStatement.Resolve (ec))
ok = false;
}
+ if (AddnTest != null) {
+ AddnTest = ResolveBoolean (ec, AddnTest, loc);
+ if (AddnTest == null)
+ ok = false;
+ }
+
if (Test != null){
Test = ResolveBoolean (ec, Test, loc);
if (Test == null)
@@ -499,7 +647,7 @@
BoolConstant bc = (BoolConstant) Test;
if (bc.Value == false){
- Warning_DeadCodeFound
(Statement.loc);
+ Warning_DeadCodeFound
(statement.loc);
empty = true;
} else
infinite = true;
@@ -507,7 +655,7 @@
} else
infinite = true;
- if (Increment != null){
+ if (Increment != null) {
if (!Increment.Resolve (ec))
ok = false;
}
@@ -516,7 +664,7 @@
if (!infinite)
ec.CurrentBranching.CreateSibling ();
- if (!Statement.Resolve (ec))
+ if (!statement.Resolve (ec))
ok = false;
if (empty)
@@ -543,7 +691,7 @@
Label loop = ig.DefineLabel ();
Label test = ig.DefineLabel ();
- if (InitStatement != null)
+ if (!is_lcv_object && InitStatement != null)
if (! (InitStatement is EmptyStatement))
InitStatement.Emit (ec);
@@ -552,12 +700,23 @@
ec.InLoop = true;
ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
- ig.Emit (OpCodes.Br, test);
+ if (AddnTest != null) {
+ if (AddnTest is BoolConstant) {
+ if (!((BoolConstant) AddnTest).Value)
+ // We can actually branch to
the end of the loop,
+ // but vbc does it this way
+ ig.Emit (OpCodes.Br, test);
+ } else if (is_lcv_object)
+ EmitBoolExpression (ec, AddnTest,
ec.LoopEnd, false);
+ else
+ EmitBoolExpression (ec, AddnTest, test,
false);
+ } else
+ ig.Emit (OpCodes.Br, test);
ig.MarkLabel (loop);
- Statement.Emit (ec);
+ statement.Emit (ec);
ig.MarkLabel (ec.LoopBegin);
- if (!(Increment is EmptyStatement))
+ if (!is_lcv_object && !(Increment is EmptyStatement))
Increment.Emit (ec);
ig.MarkLabel (test);
@@ -579,6 +738,9 @@
//
// Inform whether we are infinite or not
//
+
+ if (ltmp != null)
+ ltmp.Release (ec);
if (Test != null){
if (Test is BoolConstant){
BoolConstant bc = (BoolConstant) Test;
@@ -590,6 +752,20 @@
} else
return may_return == false;
}
+
+ private double GetValue (Expression e) {
+ if (e is DoubleConstant)
+ return ((DoubleConstant) e).Value;
+ if (e is FloatConstant)
+ return (double)((FloatConstant) e).Value;
+ if (e is IntConstant)
+ return (double)((IntConstant) e).Value;
+ if (e is LongConstant)
+ return (double)((LongConstant) e).Value;
+ if (e is DecimalConstant)
+ return (double)((DecimalConstant) e).Value;
+ return 0;
+ }
}
public class StatementExpression : Statement {
@@ -2710,7 +2886,7 @@
return isLateBindingRequired;
}
set {
- isLateBindingRequired = true;
+ isLateBindingRequired = value;
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches