mcs/class.cs: gmcs/cs-parser.jay: Implemented automatic properties (C#
authorScott Thomas <lunchtimemama@gmail.com>
Wed, 23 May 2007 23:07:49 +0000 (23:07 -0000)
committerScott Thomas <lunchtimemama@gmail.com>
Wed, 23 May 2007 23:07:49 +0000 (23:07 -0000)
3.0)

errors/gcs0271.cs: errors/gcs0272.cs: Test automatic properties for
proper access control.

tests/gtest-autoproperty-01.cs: tests/gtest-autoproperty-02.cs:
tests/gtest-autoproperty-03.cs: Tests for automatic properties.

svn path=/trunk/mcs/; revision=77877

mcs/errors/ChangeLog
mcs/errors/gcs0271.cs [new file with mode: 0644]
mcs/errors/gcs0272.cs [new file with mode: 0644]
mcs/gmcs/ChangeLog
mcs/gmcs/cs-parser.jay
mcs/mcs/ChangeLog
mcs/mcs/class.cs
mcs/tests/ChangeLog
mcs/tests/gtest-autoproperty-01.cs [new file with mode: 0644]
mcs/tests/gtest-autoproperty-02.cs [new file with mode: 0644]
mcs/tests/gtest-autoproperty-03.cs [new file with mode: 0644]

index 71e9787b44c1375fbfb470a6214ee4ec824fd27c..040699955d77c16baf88377e8806c125c2c12bf1 100644 (file)
@@ -1,3 +1,7 @@
+2007-05-22  Scott Peterson  <lunchtimemama@gmail.com>
+       * gcs0271.cs, gcs0272.cs: Test automatic properties
+         for proper access control.
+
 2007-05-17  Raja R Harinath  <rharinath@novell.com>
 
        * cs0159-5.cs, cs0159-6.cs, cs0159-7.cs: New tests as
diff --git a/mcs/errors/gcs0271.cs b/mcs/errors/gcs0271.cs
new file mode 100644 (file)
index 0000000..ab5d617
--- /dev/null
@@ -0,0 +1,19 @@
+// CS0271: The property or indexer `Test.A.B' cannot be used in this context because the get accessor is inaccessible
+// Line: 17
+// Compiler options: -langversion:linq
+using System;
+
+public class Test
+{
+       private class A
+       {
+               public string B { protected get; set; }
+       }
+       
+       static void Main ()
+       {
+               A a = new A ();
+               a.B = "foo";
+               string b = a.B;
+       }
+}
diff --git a/mcs/errors/gcs0272.cs b/mcs/errors/gcs0272.cs
new file mode 100644 (file)
index 0000000..545d5c3
--- /dev/null
@@ -0,0 +1,18 @@
+// CS0272: The property or indexer `Test.A.B' cannot be used in this context because the set accessor is inaccessible
+// Line: 16
+// Compiler options: -langversion:linq
+using System;
+
+public class Test
+{
+       private class A
+       {
+               public string B { get; private set; }
+       }
+       
+       static void Main ()
+       {
+               A a = new A ();
+               a.B = "Foo";
+       }
+}
index 173af34aa2011834c770a5044d80b7594bc7ffef..b76ddb0078df7669664a31e176fbe2fc984fce46 100644 (file)
@@ -1,3 +1,7 @@
+2007-05-22  Scott Peterson  <lunchtimemama@gmail.com>
+       
+       * cs-parser.jay: Implemented automatic properties (C# 3.0)
+
 2007-05-15  Scott Peterson  <lunchtimemama@gmail.com>
        
        * cs-parser.jay: Improved grammar for object and collection
index 1b17287e108e2a3e190d968cd27342bde9c7a0bc..933ff0a7d41d3f0be49bb9bc65537b6f5ba68db1 100644 (file)
@@ -1515,7 +1515,7 @@ property_declaration
                        syntax_error (lexer.Location, "a property can't have type arguments");
 
                prop = new Property (current_class, (Expression) $3, (int) $2, false,
-                                    name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
+                                    name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse, current_block);
 
                current_container.AddProperty (prop);
                implicit_value_parameter_type = null;
index 37720e650efd7012f3cf7e814cbbdd7b183f1ced..d809578f7c4847308b03f5f2db0f3585e12dc586 100644 (file)
@@ -1,3 +1,8 @@
+2007-05-23  Scott Peterson  <lunchtimemama@gmail.com>
+       
+       * class.cs: Implemented automatic properties (C# 3.0)
+         Thanks to Marek for the help.
+
 2007-05-23  Raja R Harinath  <rharinath@novell.com>
 
        * flowanalysis.cs (VariableInfo.SetAssigned): When noting a
index ece473fc748b7cbbf88705fa88bf1d3b432200dd..9402ad7686d48f2a88ec06d1d8126fefe6ffd8ba 100644 (file)
@@ -5784,9 +5784,10 @@ namespace Mono.CSharp {
 
                public override void Emit ()
                {
-#if GMCS_SOURCE                        
-                       if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0)
+#if GMCS_SOURCE
+                       if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0) {
                                FieldBuilder.SetCustomAttribute (TypeManager.compiler_generated_attr);
+                       }
 #endif
 
                        if (OptAttributes != null) {
@@ -6030,8 +6031,8 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE |
                        Modifiers.STATIC |
-                       Modifiers.VOLATILE |
-                       Modifiers.UNSAFE |
+                       Modifiers.VOLATILE |
+                       Modifiers.UNSAFE |
                        Modifiers.READONLY;
 
                public Field (DeclSpace parent, Expression type, int mod, string name,
@@ -6602,8 +6603,8 @@ namespace Mono.CSharp {
                                //
                                // Check for custom access modifier
                                //
-                               if (ModFlags == 0) {
-                                       ModFlags = method.ModFlags;
+                               if ((ModFlags & Modifiers.Accessibility) == 0) {
+                                       ModFlags |= method.ModFlags;
                                        flags = method.flags;
                                } else {
                                        if (container.Kind == Kind.Interface)
@@ -6650,6 +6651,7 @@ namespace Mono.CSharp {
                        
                        void CheckModifiers (int modflags)
                        {
+                               modflags &= Modifiers.Accessibility;
                                int flags = 0;
                                int mflags = method.ModFlags & Modifiers.Accessibility;
 
@@ -6747,7 +6749,8 @@ namespace Mono.CSharp {
                        //
                        // Accessors modifiers check
                        //
-                       if (Get.ModFlags != 0 && Set.ModFlags != 0) {
+                       if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
+                               (Set.ModFlags & Modifiers.Accessibility) != 0) {
                                Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
                                                GetSignatureForError ());
                                return false;
@@ -6930,13 +6933,52 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
+               void CreateAutomaticProperty (Block block, Accessor get_block, Accessor set_block)
+               {
+                       // Make the field
+                       Field field = new Field (
+                               Parent, Type,
+                               Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & Modifiers.STATIC),
+                               CompilerGeneratedClass.MakeName ("CompilerGeneratedField"),
+                               null, Location);
+                       ((TypeContainer)Parent).AddField (field);
+
+                       // Make get block
+                       get_block.Block = new ToplevelBlock (block, null, Location);
+                       Return r = new Return (new SimpleName(field.Name, Location), Location);
+                       get_block.Block.AddStatement (r);
+                       get_block.ModFlags |= Modifiers.COMPILER_GENERATED;
+
+                       // Make set block
+                       Parameters parameters = new Parameters (new Parameter (Type, "value", Parameter.Modifier.NONE, null, Location));
+                       set_block.Block = new ToplevelBlock (block, parameters, Location);
+                       Assign a = new Assign (new SimpleName(field.Name, Location), new SimpleName ("value", Location));
+                       set_block.Block.AddStatement (new StatementExpression(a));
+                       set_block.ModFlags |= Modifiers.COMPILER_GENERATED;
+               }
+
                public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
                                 MemberName name, Attributes attrs, Accessor get_block,
                                 Accessor set_block, bool define_set_first)
+                       : this (parent, type, mod, is_iface, name, attrs, get_block, set_block,
+                               define_set_first, null)
+               {
+               }
+               
+               public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
+                                MemberName name, Attributes attrs, Accessor get_block,
+                                Accessor set_block, bool define_set_first, Block current_block)
                        : base (parent, type, mod,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
                                is_iface, name, attrs, define_set_first)
                {
+                       if (RootContext.Version >= LanguageVersion.LINQ &&
+                               !is_iface &&
+                               (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
+                               get_block != null && get_block.Block == null &&
+                               set_block != null && set_block.Block == null)
+                               CreateAutomaticProperty (current_block, get_block, set_block);
+
                        if (get_block == null)
                                Get = new GetMethod (this);
                        else
@@ -8320,3 +8362,4 @@ namespace Mono.CSharp {
                }
        }
 }
+
index fcf5d1411cee47c7f8b9f34916c9a3d21cb61c96..ba45d7a603e2e1d5a3595b8656208d7253c3acc4 100644 (file)
@@ -1,3 +1,12 @@
+2007-05-22  Scott Peterson  <lunchtimemama@gmail.com>
+       
+       * gtest-autoproperty-01.cs: Test for instance automatic properties (C# 3.0)
+       
+       * gtest-autoproperty-02.cs: Test for static automatic properties (C# 3.0)
+       
+       * gtest-autoproperty-03.cs: Make sure that the field and accessor methods
+         of an automatic property have the CompilerGenerated attribute (C# 3.0)
+
 2007-05-15  Scott Peterson  <lunchtimemama@gmail.com>
 
        * gtest-initialize-02.cs: Uber-test for object and
diff --git a/mcs/tests/gtest-autoproperty-01.cs b/mcs/tests/gtest-autoproperty-01.cs
new file mode 100644 (file)
index 0000000..a215897
--- /dev/null
@@ -0,0 +1,37 @@
+// Compiler options: -langversion:linq
+// Tests automatic properties
+using System;
+
+public class Test
+{
+       private class A
+       {
+               public string B { get; set; }
+       }
+       
+       public string Foo { get; set; }
+       public int Answer { get; private set; }
+       
+       public Test ()
+       {
+               Answer = 42;
+       }
+       
+       static int Main ()
+       {
+               Test t = new Test ();
+               t.Foo = "Bar";
+               if (t.Foo != "Bar")
+                       return 1;
+               
+               if (t.Answer != 42)
+                       return 2;
+               
+               A a = new A ();
+               a.B = "C";
+               if (a.B != "C")
+                       return 3;
+               
+               return 0;
+       }
+}
diff --git a/mcs/tests/gtest-autoproperty-02.cs b/mcs/tests/gtest-autoproperty-02.cs
new file mode 100644 (file)
index 0000000..6eabb60
--- /dev/null
@@ -0,0 +1,40 @@
+// Compiler options: -langversion:linq
+// Tests static automatic properties
+using System;
+
+public class Test
+{
+       private class A
+       {
+               public static string B { get; set; }
+               public static string C { get; private set; }
+               public static void DoThings ()
+               {
+                       C = "C";
+               }
+       }
+       
+       public static string Foo { get; set; }
+       public static int Answer { get; private set; }
+       
+       static int Main ()
+       {
+               Foo = "Bar";
+               if (Foo != "Bar")
+                       return 1;
+               
+               Answer = 42;
+               if (Answer != 42)
+                       return 2;
+               
+               A.B = "B";
+               if (A.B != "B")
+                       return 3;
+               
+               A.DoThings();
+               if (A.C != "C")
+                       return 4;
+               
+               return 0;
+       }
+}
diff --git a/mcs/tests/gtest-autoproperty-03.cs b/mcs/tests/gtest-autoproperty-03.cs
new file mode 100644 (file)
index 0000000..9252cad
--- /dev/null
@@ -0,0 +1,39 @@
+// Compiler options: -langversion:linq
+// Make sure that the field and accessor methods of an automatic property have the CompilerGenerated attribute
+using System;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+public class Test
+{
+       public string Foo { get; set; }
+       
+       static int Main ()
+       {
+               FieldInfo [] fields = typeof (Test).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
+               if (!(fields.Length > 0))
+                       return 1;
+               object [] field_atts = fields[0].GetCustomAttributes (false);
+               if (!(field_atts.Length > 0))
+                       return 2;
+               if (field_atts[0].GetType() != typeof (CompilerGeneratedAttribute))
+                       return 3;
+               
+               PropertyInfo property = typeof (Test).GetProperty ("Foo");
+               MethodInfo get = property.GetGetMethod (false);
+               object [] get_atts = get.GetCustomAttributes (false);
+               if (!(get_atts.Length > 0))
+                       return 4;
+               if (get_atts[0].GetType() != typeof (CompilerGeneratedAttribute))
+                       return 5;
+                       
+               MethodInfo set = property.GetSetMethod (false);
+               object [] set_atts = set.GetCustomAttributes (false);
+               if (!(set_atts.Length > 0))
+                       return 6;
+               if (set_atts[0].GetType() != typeof (CompilerGeneratedAttribute))
+                       return 7;
+
+               return 0;
+       }
+}