[mcs] Implements C#7 expression bodied members enhancement
authorMarek Safar <marek.safar@gmail.com>
Mon, 31 Jul 2017 14:58:14 +0000 (16:58 +0200)
committerMarek Safar <marek.safar@gmail.com>
Mon, 31 Jul 2017 15:01:12 +0000 (17:01 +0200)
mcs/errors/cs1525-23.cs
mcs/mcs/cs-parser.jay
mcs/tests/test-expression-bodied-04.cs [new file with mode: 0644]
mcs/tests/ver-il-net_4_x.xml

index 8a97e05efb17d661c307723d41b8d7a9424a36ce..aa5ffc64e486a002b8d36427f830c9e9a4e05120 100644 (file)
@@ -1,4 +1,4 @@
-// CS1525: Unexpected symbol `public', expecting `:', `;', or `{'
+// CS1525: Unexpected symbol `public', expecting `:', `;', `{', or `=>'
 // Line: 5
 
 class TorrentEditor {
index 514113a6bf7734da8fd148ae1ddfb3fcbf89ca1c..5f885a77763b8ccebc07be47fb643c310be926f5 100644 (file)
@@ -1337,7 +1337,7 @@ method_declaration
 
                // Was added earlier in the case of body being eof for full ast
          }
-         method_body_expression_block
+         method_body
          {
                Method method = (Method) $1;
                method.Block = (ToplevelBlock) $3;
@@ -1492,16 +1492,38 @@ method_header
          }
        ;
 
-method_body_expression_block
-       : method_body
-       | expression_block
-       ;
-
 method_body
        : block
+       | expression_block
        | SEMICOLON             { $$ = null; }
        ;
 
+destructor_body
+       : method_body
+       ;
+
+constructor_body
+       : block_prepared
+       | SEMICOLON             { current_block = null; $$ = null; }
+       | ARROW
+        {
+               if (lang_version < LanguageVersion.V_7) {
+                       FeatureIsNotAvailable (GetLocation ($1), "expression body constructor");
+               }
+
+               ++lexer.parsing_block;
+        }
+        expression SEMICOLON
+        {
+               lexer.parsing_block = 0;
+               current_block.AddStatement (new ContextualReturn ((Expression) $3));
+               var b = end_block (GetLocation ($4));
+               b.IsCompilerGenerated = true;
+               $$ = b;
+               current_block = null;
+        }
+       ;
+
 expression_block
        : ARROW
         {
@@ -2197,7 +2219,7 @@ operator_declaration
        : opt_attributes opt_modifiers operator_declarator 
          {
          }
-         method_body_expression_block
+         method_body
          {
                OperatorDeclaration decl = (OperatorDeclaration) $3;
                if (decl != null) {
@@ -2464,11 +2486,6 @@ constructor_declarator
          }
        ;
 
-constructor_body
-       : block_prepared
-       | SEMICOLON             { current_block = null; $$ = null; }
-       ;
-
 opt_constructor_initializer
        : /* Empty */
        | constructor_initializer
@@ -2518,7 +2535,7 @@ destructor_declaration
                
                current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
          }
-         IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
+         IDENTIFIER OPEN_PARENS CLOSE_PARENS destructor_body
          {
                var lt = (LocatedToken) $5;
                if (lt.Value != current_container.MemberName.Name){
diff --git a/mcs/tests/test-expression-bodied-04.cs b/mcs/tests/test-expression-bodied-04.cs
new file mode 100644 (file)
index 0000000..815eabc
--- /dev/null
@@ -0,0 +1,16 @@
+// Introduced in C#7
+
+class VV
+{
+    VV () => Test ();
+
+    ~VV () => Test ();
+
+    void Test ()
+    {
+    }
+
+    public static void Main ()
+    {
+    }
+}
\ No newline at end of file
index 5eac11969e1220f675083b69ce0a2e414135e6e0..221283df2e4e6f62662a6dcc4d17c30a2e5428bc 100644 (file)
       </method>
     </type>
   </test>
+  <test name="test-expression-bodied-04.cs">
+    <type name="VV">
+      <method name="Void Finalize()" attrs="196">
+        <size>19</size>
+      </method>
+      <method name="Void Test()" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void Main()" attrs="150">
+        <size>2</size>
+      </method>
+      <method name="Void .ctor()" attrs="6273">
+        <size>13</size>
+      </method>
+    </type>
+  </test>
   <test name="test-externalias-01.cs">
     <type name="Test">
       <method name="Int32 Main()" attrs="150">