r77877 - in trunk/mcs: errors gmcs mcs tests

3 views
Skip to first unread message

Scott Peterson (lunchtimemama@gmail.com)

unread,
May 23, 2007, 7:07:50 PM5/23/07
to mono-p...@lists.ximian.com, ximian....@gmail.com, mono-svn-patche...@googlegroups.com
Author: speterson
Date: 2007-05-23 19:07:49 -0400 (Wed, 23 May 2007)
New Revision: 77877

Added:
trunk/mcs/errors/gcs0271.cs
trunk/mcs/errors/gcs0272.cs
trunk/mcs/tests/gtest-autoproperty-01.cs
trunk/mcs/tests/gtest-autoproperty-02.cs
trunk/mcs/tests/gtest-autoproperty-03.cs
Modified:
trunk/mcs/errors/ChangeLog
trunk/mcs/gmcs/ChangeLog
trunk/mcs/gmcs/cs-parser.jay
trunk/mcs/mcs/ChangeLog
trunk/mcs/mcs/class.cs
trunk/mcs/tests/ChangeLog
Log:
mcs/class.cs: gmcs/cs-parser.jay: Implemented automatic properties (C#
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.


Modified: trunk/mcs/errors/ChangeLog
===================================================================
--- trunk/mcs/errors/ChangeLog 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/errors/ChangeLog 2007-05-23 23:07:49 UTC (rev 77877)
@@ -1,3 +1,7 @@
+2007-05-22 Scott Peterson <luncht...@gmail.com>
+ * gcs0271.cs, gcs0272.cs: Test automatic properties
+ for proper access control.
+
2007-05-17 Raja R Harinath <rhar...@novell.com>

* cs0159-5.cs, cs0159-6.cs, cs0159-7.cs: New tests as

Added: trunk/mcs/errors/gcs0271.cs
===================================================================
--- trunk/mcs/errors/gcs0271.cs 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/errors/gcs0271.cs 2007-05-23 23:07:49 UTC (rev 77877)
@@ -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;
+ }
+}

Added: trunk/mcs/errors/gcs0272.cs
===================================================================
--- trunk/mcs/errors/gcs0272.cs 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/errors/gcs0272.cs 2007-05-23 23:07:49 UTC (rev 77877)
@@ -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";
+ }
+}

Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/gmcs/ChangeLog 2007-05-23 23:07:49 UTC (rev 77877)
@@ -1,3 +1,7 @@
+2007-05-22 Scott Peterson <luncht...@gmail.com>
+
+ * cs-parser.jay: Implemented automatic properties (C# 3.0)
+
2007-05-15 Scott Peterson <luncht...@gmail.com>

* cs-parser.jay: Improved grammar for object and collection

Modified: trunk/mcs/gmcs/cs-parser.jay
===================================================================
--- trunk/mcs/gmcs/cs-parser.jay 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/gmcs/cs-parser.jay 2007-05-23 23:07:49 UTC (rev 77877)
@@ -1515,7 +1515,7 @@
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;

Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/mcs/ChangeLog 2007-05-23 23:07:49 UTC (rev 77877)
@@ -1,3 +1,8 @@
+2007-05-23 Scott Peterson <luncht...@gmail.com>
+
+ * class.cs: Implemented automatic properties (C# 3.0)
+ Thanks to Marek for the help.
+
2007-05-23 Raja R Harinath <rhar...@novell.com>

* flowanalysis.cs (VariableInfo.SetAssigned): When noting a

Modified: trunk/mcs/mcs/class.cs
===================================================================
--- trunk/mcs/mcs/class.cs 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/mcs/class.cs 2007-05-23 23:07:49 UTC (rev 77877)
@@ -5784,9 +5784,10 @@

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 @@
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 @@
//
// 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 @@

void CheckModifiers (int modflags)
{
+ modflags &= Modifiers.Accessibility;
int flags = 0;
int mflags = method.ModFlags & Modifiers.Accessibility;

@@ -6747,7 +6749,8 @@
//
// 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 @@
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 @@
}
}
}
+

Modified: trunk/mcs/tests/ChangeLog
===================================================================
--- trunk/mcs/tests/ChangeLog 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/tests/ChangeLog 2007-05-23 23:07:49 UTC (rev 77877)
@@ -1,3 +1,12 @@
+2007-05-22 Scott Peterson <luncht...@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 <luncht...@gmail.com>

* gtest-initialize-02.cs: Uber-test for object and

Added: trunk/mcs/tests/gtest-autoproperty-01.cs
===================================================================
--- trunk/mcs/tests/gtest-autoproperty-01.cs 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/tests/gtest-autoproperty-01.cs 2007-05-23 23:07:49 UTC (rev 77877)
@@ -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;
+ }
+}

Added: trunk/mcs/tests/gtest-autoproperty-02.cs
===================================================================
--- trunk/mcs/tests/gtest-autoproperty-02.cs 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/tests/gtest-autoproperty-02.cs 2007-05-23 23:07:49 UTC (rev 77877)
@@ -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;
+ }
+}

Added: trunk/mcs/tests/gtest-autoproperty-03.cs
===================================================================
--- trunk/mcs/tests/gtest-autoproperty-03.cs 2007-05-23 22:47:58 UTC (rev 77876)
+++ trunk/mcs/tests/gtest-autoproperty-03.cs 2007-05-23 23:07:49 UTC (rev 77877)
@@ -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;
+ }
+}

Reply all
Reply to author
Forward
0 new messages