2001-10-12 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / assign.cs
index a32516a8fbd5fdf6a77b26106bd29f98b8687ad3..36081efe19fa5bf07bf9e464354075ccedf5a1a4 100755 (executable)
@@ -11,7 +11,7 @@ using System.Reflection;
 using System.Reflection.Emit;
 
 namespace CIR {
-       public class Assign : Expression {
+       public class Assign : ExpressionStatement {
                Expression target, source;
                Location l;
                
@@ -42,32 +42,36 @@ namespace CIR {
                        }
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       target = target.Resolve (tc);
-                       source = source.Resolve (tc);
+                       target = target.Resolve (ec);
+                       source = source.Resolve (ec);
 
                        if (target == null || source == null)
                                return null;
 
                        Type target_type = target.Type;
-                       Type source_type = source.Type;
                        
+                       Type source_type = source.Type;
+
                        if (target_type != source_type){
-                               source = ConvertImplicitRequired (tc, source, target_type, l);
+                               source = ConvertImplicitRequired (ec, source, target_type, l);
                                if (source == null)
                                        return null;
                        }
                        
                        if (!(target is LValue)){
-                               tc.RootContext.Report.Error (131, "Left hand of an assignment must be a variable, a property or an indexer");
+                               Report.Error (131, "Left hand of an assignment must be a variable, a property or an indexer");
                        }
                        type = target_type;
+                       eclass = ExprClass.Value;
                        return this;
                }
 
-               public override bool Emit (EmitContext ec)
+               void Emit (EmitContext ec, bool is_statement)
                {
+                       ILGenerator ig = ec.ig;
+                       
                        if (target.ExprClass == ExprClass.Variable){
 
                                //
@@ -77,12 +81,15 @@ namespace CIR {
                                        FieldExpr fe = (FieldExpr) target;
                                        
                                        if (!fe.FieldInfo.IsStatic)
-                                               ec.ig.Emit (OpCodes.Ldarg_0);
+                                               ig.Emit (OpCodes.Ldarg_0);
                                }
-                                                   
+
                                source.Emit (ec);
 
-                               ((LValue) target).Store (ec.ig);
+                               if (!is_statement)
+                                       ig.Emit (OpCodes.Dup);
+
+                               ((LValue) target).Store (ec);
                        } else if (target.ExprClass == ExprClass.PropertyAccess){
                                // FIXME
                                throw new Exception ("Can not assign to properties yet");
@@ -90,11 +97,20 @@ namespace CIR {
                                // FIXME
                                throw new Exception ("Can not assign to indexers yet");
                        }
+               }
+               
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
+               }
 
-                       return false;
+               public override void EmitStatement (EmitContext ec)
+               {
+                       Emit (ec, true);
                }
        }
 }
 
 
 
+