2004-01-19 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / gmcs / assign.cs
old mode 100755 (executable)
new mode 100644 (file)
index 9ee1108..6c5c87d
@@ -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;
@@ -386,6 +387,21 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       FieldExpr field_exp = target as FieldExpr;
+                       if (field_exp != null && !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)) {
                                Report.Error (131, loc,
                                              "Left hand of an assignment must be a variable, " +
@@ -404,16 +420,16 @@ namespace Mono.CSharp {
                        }
 
                        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;
-                               }
-                               
+                       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;
                        }