Remove debugging comment
[mono.git] / mcs / mcs / assign.cs
old mode 100755 (executable)
new mode 100644 (file)
index 94467b2..4d25f9f
@@ -3,9 +3,10 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
-//   Martin Baulig (martin@gnome.org)
+//   Martin Baulig (martin@ximian.com)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
+// (C) 2004 Novell, Inc
 //
 using System;
 using System.Reflection;
@@ -29,19 +30,125 @@ namespace Mono.CSharp {
        /// </remarks>
        public interface IAssignMethod {
                //
-               // This method will emit the code for the actual assignment
+               // This is an extra version of Emit. If leave_copy is `true'
+               // A copy of the expression will be left on the stack at the
+               // end of the code generated for EmitAssign
                //
-               void EmitAssign (EmitContext ec, Expression source);
-
-               //
-               // This method is invoked before any code generation takes
-               // place, and it is a mechanism to inform that the expression
-               // will be invoked more than once, and that the method should
-               // use temporary values to avoid having side effects
+               void Emit (EmitContext ec, bool leave_copy);
+               
                //
-               // Example: a [ g () ] ++
+               // This method does the assignment
+               // `source' will be stored into the location specified by `this'
+               // if `leave_copy' is true, a copy of `source' will be left on the stack
+               // if `prepare_for_load' is true, when `source' is emitted, there will
+               // be data on the stack that it can use to compuatate its value. This is
+               // for expressions like a [f ()] ++, where you can't call `f ()' twice.
                //
-               void CacheTemporaries (EmitContext ec);
+               void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load);
+               
+               /*
+               For simple assignments, this interface is very simple, EmitAssign is called with source
+               as the source expression and leave_copy and prepare_for_load false.
+               
+               For compound assignments it gets complicated.
+               
+               EmitAssign will be called as before, however, prepare_for_load will be
+               true. The @source expression will contain an expression
+               which calls Emit. So, the calls look like:
+               
+               this.EmitAssign (ec, source, false, true) ->
+                       source.Emit (ec); ->
+                               [...] ->
+                                       this.Emit (ec, false); ->
+                                       end this.Emit (ec, false); ->
+                               end [...]
+                       end source.Emit (ec);
+               end this.EmitAssign (ec, source, false, true)
+               
+               
+               When prepare_for_load is true, EmitAssign emits a `token' on the stack that
+               Emit will use for its state.
+               
+               Let's take FieldExpr as an example. assume we are emitting f ().y += 1;
+               
+               Here is the call tree again. This time, each call is annotated with the IL
+               it produces:
+               
+               this.EmitAssign (ec, source, false, true)
+                       call f
+                       dup
+                       
+                       Binary.Emit ()
+                               this.Emit (ec, false);
+                               ldfld y
+                               end this.Emit (ec, false);
+                               
+                               IntConstant.Emit ()
+                               ldc.i4.1
+                               end IntConstant.Emit
+                               
+                               add
+                       end Binary.Emit ()
+                       
+                       stfld
+               end this.EmitAssign (ec, source, false, true)
+               
+               Observe two things:
+                       1) EmitAssign left a token on the stack. It was the result of f ().
+                       2) This token was used by Emit
+               
+               leave_copy (in both EmitAssign and Emit) tells the compiler to leave a copy
+               of the expression at that point in evaluation. This is used for pre/post inc/dec
+               and for a = x += y. Let's do the above example with leave_copy true in EmitAssign
+               
+               this.EmitAssign (ec, source, true, true)
+                       call f
+                       dup
+                       
+                       Binary.Emit ()
+                               this.Emit (ec, false);
+                               ldfld y
+                               end this.Emit (ec, false);
+                               
+                               IntConstant.Emit ()
+                               ldc.i4.1
+                               end IntConstant.Emit
+                               
+                               add
+                       end Binary.Emit ()
+                       
+                       dup
+                       stloc temp
+                       stfld
+                       ldloc temp
+               end this.EmitAssign (ec, source, true, true)
+               
+               And with it true in Emit
+               
+               this.EmitAssign (ec, source, false, true)
+                       call f
+                       dup
+                       
+                       Binary.Emit ()
+                               this.Emit (ec, true);
+                               ldfld y
+                               dup
+                               stloc temp
+                               end this.Emit (ec, true);
+                               
+                               IntConstant.Emit ()
+                               ldc.i4.1
+                               end IntConstant.Emit
+                               
+                               add
+                       end Binary.Emit ()
+                       
+                       stfld
+                       ldloc temp
+               end this.EmitAssign (ec, source, false, true)
+               
+               Note that these two examples are what happens for ++x and x++, respectively.
+               */
        }
 
        /// <summary>
@@ -254,6 +361,7 @@ namespace Mono.CSharp {
                                type = target_type;
                        eclass = ExprClass.Value;
 
+
                        if (target is EventExpr) {
                                EventInfo ei = ((EventExpr) target).EventInfo;
 
@@ -280,14 +388,19 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       if (source is New && target_type.IsValueType &&
-                           (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
-                               New n = (New) source;
-
-                               if (n.SetValueTypeVariable (target))
-                                       return n;
-                               else
+                       FieldExpr field_exp = target as FieldExpr;
+                       if (field_exp != null && field_exp.DeclaringType.IsValueType && !ec.IsConstructor && !ec.IsFieldInitializer) {
+                               field_exp = field_exp.InstanceExpression as FieldExpr;
+                               if (field_exp != null && field_exp.FieldInfo.IsInitOnly) {
+                                       if (field_exp.IsStatic) {
+                                               Report.Error (1650, loc, "Members of static readonly field '{0}' cannot be assigned to " +
+                                                       "(except in a static constructor or a variable initializer)", TypeManager.GetFullNameSignature (field_exp.FieldInfo));
+                                       } else {
+                                               Report.Error (1648, loc, "Members of readonly field '{0}' cannot be assigned to " +
+                                                       "(except in a constructor or a variable initializer)", TypeManager.GetFullNameSignature (field_exp.FieldInfo));
+                                       }
                                        return null;
+                               }
                        }
 
                        if (!(target is IAssignMethod) && (target.eclass != ExprClass.EventAccess)) {
@@ -298,16 +411,28 @@ namespace Mono.CSharp {
                        }
 
                        if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
-                               source.Error_UnexpectedKind ("variable or value");
+                               source.Error_UnexpectedKind ("variable or value", loc);
                                return null;
-                       } else if (!RootContext.V2 && (source is MethodGroupExpr)){
+                       } else if ((RootContext.Version == LanguageVersion.ISO_1) &&
+                                  (source is MethodGroupExpr)){
                                ((MethodGroupExpr) source).ReportUsageError ();
                                return null;
 
                        }
-                       
-                       if (target_type == source_type)
+
+                       if (target_type == source_type){
+                               if (source is New && target_type.IsValueType &&
+                                   (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
+                                       New n = (New) source;
+                                       
+                                       if (n.SetValueTypeVariable (target))
+                                               return n;
+                                       else
+                                               return null;
+                               }
+                               
                                return this;
+                       }
                        
                        //
                        // If this assignemnt/operator was part of a compound binary
@@ -335,7 +460,7 @@ namespace Mono.CSharp {
                                        // 2. and the original right side is implicitly convertible to
                                        // the type of target
                                        //
-                                       if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
+                                       if (Convert.ImplicitStandardConversionExists (ec, a.original_source, target_type))
                                                return this;
 
                                        //
@@ -394,7 +519,7 @@ namespace Mono.CSharp {
                        }
 
                        Expression temp_source = (temp != null) ? temp : source;
-                       ((IAssignMethod) target).EmitAssign (ec, temp_source);
+                       ((IAssignMethod) target).EmitAssign (ec, temp_source, false, false);
                        return temp_source;
                }
 
@@ -416,21 +541,9 @@ namespace Mono.CSharp {
                                ((EventExpr) target).EmitAddOrRemove (ec, source);
                                return;
                        }
-
-                       bool use_temporaries = false;
                        
-                       //
-                       // FIXME! We need a way to "probe" if the process can
-                       // just use `dup' to propagate the result
-                       // 
                        IAssignMethod am = (IAssignMethod) target;
 
-                       if (this is CompoundAssign)
-                               am.CacheTemporaries (ec);
-
-                       if (!is_statement)
-                               use_temporaries = true;
-
                        Expression temp_source;
                        if (embedded != null) {
                                temp_source = embedded.EmitEmbedded (ec);
@@ -442,27 +555,8 @@ namespace Mono.CSharp {
                                }
                        } else
                                temp_source = source;
-
-                       if (use_temporaries){
-                               //
-                               // Doing this for every path is too expensive
-                               // I wonder if we can work around this and have a less
-                               // expensive path
-                               //
-                               LocalTemporary tempo;
-                               
-                               tempo = new LocalTemporary (ec, source.Type);
-                               
-                               temp_source.Emit (ec);
-                               tempo.Store (ec);
-                               am.EmitAssign (ec, tempo);
-                               if (!is_statement)
-                                       tempo.Emit (ec);
-                               
-                               tempo.Release (ec);
-                       } else {
-                               am.EmitAssign (ec, temp_source);
-                       }
+               
+                       am.EmitAssign (ec, temp_source, !is_statement, this is CompoundAssign);
                                
                        if (embedded != null) {
                                if (temp != null)