* statement.cs (ToplevelBlock.ResolveMeta): Move CS0136 checks ...
[mono.git] / mcs / mcs / cs-parser.jay
index e641649b84ae7b308c87daec47e5ecf957720682..fbc6e2a7c93b1efe7810bfc9662de7bf8a9f0bc0 100644 (file)
@@ -4,6 +4,7 @@
 //
 // Authors: Miguel de Icaza (miguel@gnu.org)
 //          Ravi Pratap     (ravi@ximian.com)
+//          Marek Safar                (marek.safar@gmail.com)
 //
 // Licensed under the terms of the GNU GPL
 //
@@ -41,7 +42,7 @@ namespace Mono.CSharp
                ///   Current block is used to add statements as we find
                ///   them.  
                /// </summary>
-               Block      current_block, top_current_block;
+               Block      current_block;
 
                Delegate   current_delegate;
 
@@ -74,11 +75,11 @@ namespace Mono.CSharp
                bool parsing_indexer;
 
                bool parsing_anonymous_method;
-
+               
                ///
                /// An out-of-band stack.
                ///
-               Stack oob_stack;
+               static Stack oob_stack;
 
                ///
                /// Switch stack.
@@ -545,55 +546,63 @@ opt_attributes
 attribute_sections
        : attribute_section
          {
-               ArrayList sect = (ArrayList) $1;
-
-               if (global_attrs_enabled) {
-                       if (current_attr_target == "module") {
-                               CodeGen.Module.AddAttributes (sect);
-                               $$ = null;
-                       } else if (current_attr_target != null && current_attr_target.Length > 0) {
-                               CodeGen.Assembly.AddAttributes (sect);
-                               $$ = null;
+               if (current_attr_target != String.Empty) {
+                       ArrayList sect = (ArrayList) $1;
+
+                       if (global_attrs_enabled) {
+                               if (current_attr_target == "module") {
+                                       CodeGen.Module.AddAttributes (sect);
+                                       $$ = null;
+                               } else if (current_attr_target != null && current_attr_target.Length > 0) {
+                                       CodeGen.Assembly.AddAttributes (sect);
+                                       $$ = null;
+                               } else {
+                                       $$ = new Attributes (sect);
+                               }
+                               if ($$ == null) {
+                                       if (RootContext.Documentation != null) {
+                                               Lexer.check_incorrect_doc_comment ();
+                                               Lexer.doc_state =
+                                                       XmlCommentState.Allowed;
+                                       }
+                               }
                        } else {
                                $$ = new Attributes (sect);
-                       }
-                       if ($$ == null) {
-                               if (RootContext.Documentation != null) {
-                                       Lexer.check_incorrect_doc_comment ();
-                                       Lexer.doc_state =
-                                               XmlCommentState.Allowed;
-                               }
-                       }
-               } else {
-                       $$ = new Attributes (sect);
-               }               
+                       }               
+               }
+               else
+                       $$ = null;
                current_attr_target = null;
          }
        | attribute_sections attribute_section
          {
-               Attributes attrs = $1 as Attributes;
-               ArrayList sect = (ArrayList) $2;
-
-               if (global_attrs_enabled) {
-                       if (current_attr_target == "module") {
-                               CodeGen.Module.AddAttributes (sect);
-                               $$ = null;
-                       } else if (current_attr_target == "assembly") {
-                               CodeGen.Assembly.AddAttributes (sect);
-                               $$ = null;
+               if (current_attr_target != String.Empty) {
+                       Attributes attrs = $1 as Attributes;
+                       ArrayList sect = (ArrayList) $2;
+
+                       if (global_attrs_enabled) {
+                               if (current_attr_target == "module") {
+                                       CodeGen.Module.AddAttributes (sect);
+                                       $$ = null;
+                               } else if (current_attr_target == "assembly") {
+                                       CodeGen.Assembly.AddAttributes (sect);
+                                       $$ = null;
+                               } else {
+                                       if (attrs == null)
+                                               attrs = new Attributes (sect);
+                                       else
+                                               attrs.AddAttributes (sect);                     
+                               }
                        } else {
                                if (attrs == null)
                                        attrs = new Attributes (sect);
                                else
-                                       attrs.AddAttributes (sect);                     
-                       }
-               } else {
-                       if (attrs == null)
-                               attrs = new Attributes (sect);
-                       else
-                               attrs.AddAttributes (sect);
-               }               
-               $$ = attrs;
+                                       attrs.AddAttributes (sect);
+                       }               
+                       $$ = attrs;
+               }
+               else
+                       $$ = null;
                current_attr_target = null;
          }
        ;
@@ -621,11 +630,15 @@ attribute_target
        : IDENTIFIER
          {
                LocatedToken lt = (LocatedToken) $1;
-               CheckAttributeTarget (lt.Value, lt.Location);
-               $$ = lt.Value; // Location won't be required anymore.
+               $$ = CheckAttributeTarget (lt.Value, lt.Location);
+         }
+      | EVENT  { $$ = "event"; }         
+      | RETURN { $$ = "return"; }
+      | error
+         {
+               string name = yyNames [yyToken].ToLower ();
+               $$ = CheckAttributeTarget (name, GetLocation ($1));
          }
-        | EVENT  { $$ = "event"; }       
-        | RETURN { $$ = "return"; }
        ;
 
 attribute_list
@@ -656,7 +669,9 @@ attribute
 
                Expression left_expr = left == null ? null : left.GetTypeExpression ();
 
-               if (current_attr_target == "assembly" || current_attr_target == "module")
+               if (current_attr_target == String.Empty)
+                       $$ = null;
+               else if (current_attr_target == "assembly" || current_attr_target == "module")
                        // FIXME: supply "nameEscaped" parameter here.
                        $$ = new GlobalAttribute (current_namespace, current_attr_target,
                                                  left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
@@ -790,7 +805,7 @@ struct_declaration
                MemberName name = MakeName ((MemberName) $5);
                push_current_class (new Struct (
                        current_namespace, current_class, name, (int) $2,
-                       (Attributes) $1), false, $3);
+                       (Attributes) $1), $3);
 
          }
          opt_class_base
@@ -1103,11 +1118,22 @@ opt_error_modifier
          }
        ;
 
+       // 
+       // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
+       // is returned (type followed by an identifier), these are the
+       // declarations (methods, overloads, constructors, etc) and a handful
+       // of expressions ("using", "fixed") or "catch".
+       //
+open_parens
+       : OPEN_PARENS
+       | OPEN_PARENS_LAMBDA
+       ;
+
 method_header
        : opt_attributes
          opt_modifiers
          type namespace_or_type_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
+         open_parens opt_formal_parameter_list CLOSE_PARENS 
          {
                MemberName name = (MemberName) $4;
 
@@ -1125,7 +1151,7 @@ method_header
        | opt_attributes
          opt_modifiers
          VOID namespace_or_type_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
+         open_parens opt_formal_parameter_list CLOSE_PARENS 
          {
                MemberName name = (MemberName) $4;
 
@@ -1144,7 +1170,7 @@ method_header
        | opt_attributes
          opt_modifiers
          type 
-         modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
+         modifiers namespace_or_type_name open_parens opt_formal_parameter_list CLOSE_PARENS
          {
                MemberName name = (MemberName) $5;
                Report.Error (1585, name.Location, 
@@ -1322,6 +1348,7 @@ parameter_modifiers
 parameter_modifier
        : REF                   { $$ = Parameter.Modifier.REF; }
        | OUT                   { $$ = Parameter.Modifier.OUT; }
+       | THIS                  { $$ = Parameter.Modifier.This; }
        ;
 
 parameter_array
@@ -1337,7 +1364,12 @@ parameter_array
          }
        | opt_attributes PARAMS parameter_modifier type IDENTIFIER 
          {
+               Parameter.Modifier mod = (Parameter.Modifier)$3;
+               if ((mod & Parameter.Modifier.This) != 0) {
+                       Report.Error (1104, lexer.Location, "The parameter modifiers `this' and `params' cannot be used altogether");
+               } else {
                Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
+               }
                 $$ = null;
          }
        | opt_attributes PARAMS type error {
@@ -1522,7 +1554,7 @@ interface_declaration
 
                push_current_class (new Interface (
                        current_namespace, current_class, name, (int) $2,
-                       (Attributes) $1), true, $3);
+                       (Attributes) $1), $3);
          }
          opt_class_base
          {
@@ -1886,13 +1918,13 @@ operator_body
        ; 
 operator_declarator
        : type OPERATOR overloadable_operator 
-         OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
+         open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
          {
                // TODO: wrong location
-               if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
+               if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
                        Error_ParameterModifierNotValid ((Location) $2);
          
-               LocatedToken lt = (LocatedToken) $7;
+               LocatedToken lt = (LocatedToken) $8;
                Operator.OpType op = (Operator.OpType) $3;
                CheckUnaryOperator (op, lt.Location);
 
@@ -1903,9 +1935,9 @@ operator_declarator
                        op = Operator.OpType.UnaryNegation;
 
                Parameter [] pars = new Parameter [1];
-               Expression type = (Expression) $6;
+               Expression type = (Expression) $7;
 
-               pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
+               pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, (Attributes) $5, lt.Location);
 
                current_local_parameters = new Parameters (pars);
 
@@ -1918,26 +1950,26 @@ operator_declarator
                                              null, null, (Location) $2);
          }
        | type OPERATOR overloadable_operator
-         OPEN_PARENS 
-               opt_parameter_modifier type IDENTIFIER COMMA
-               opt_parameter_modifier type IDENTIFIER 
+         open_parens 
+               opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
+               opt_attributes opt_parameter_modifier type IDENTIFIER 
          CLOSE_PARENS
           {
                // TODO: wrong location
-               if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE || (Parameter.Modifier)$9 != Parameter.Modifier.NONE)
+               if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
                        Error_ParameterModifierNotValid ((Location) $2);
 
-               LocatedToken ltParam1 = (LocatedToken) $7;
-               LocatedToken ltParam2 = (LocatedToken) $11;
+               LocatedToken ltParam1 = (LocatedToken) $8;
+               LocatedToken ltParam2 = (LocatedToken) $13;
                CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
 
                Parameter [] pars = new Parameter [2];
 
-               Expression typeL = (Expression) $6;
-               Expression typeR = (Expression) $10;
+               Expression typeL = (Expression) $7;
+               Expression typeR = (Expression) $12;
 
-              pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
-              pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
+              pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, (Attributes) $5, ltParam1.Location);
+              pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, (Attributes) $10, ltParam2.Location);
 
               current_local_parameters = new Parameters (pars);
 
@@ -1952,18 +1984,16 @@ operator_declarator
           }
        | conversion_operator_declarator
        | type OPERATOR overloadable_operator
-         OPEN_PARENS 
-               opt_parameter_modifier type IDENTIFIER COMMA
-               opt_parameter_modifier type IDENTIFIER COMMA
-               opt_parameter_modifier type IDENTIFIER
-         CLOSE_PARENS
+         open_parens 
+               opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
+               opt_attributes opt_parameter_modifier type IDENTIFIER COMMA error
          {
                Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
                        Operator.GetName ((Operator.OpType) $3));
                $$ = null;
          }
        | type OPERATOR overloadable_operator 
-         OPEN_PARENS CLOSE_PARENS
+         open_parens CLOSE_PARENS
          {
                Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
                        Operator.GetName ((Operator.OpType) $3));
@@ -2000,7 +2030,7 @@ overloadable_operator
        ;
 
 conversion_operator_declarator
-       : IMPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
+       : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
          {
                // TODO: wrong location
                if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
@@ -2021,7 +2051,7 @@ conversion_operator_declarator
                $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
                                              null, null, (Location) $2);
          }
-       | EXPLICIT OPERATOR type OPEN_PARENS opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
+       | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
          {
                // TODO: wrong location
                if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
@@ -2098,6 +2128,18 @@ constructor_declaration
        ;
 
 constructor_declarator
+       : constructor_header
+         {
+               $$ = $1;
+         }
+       | constructor_header constructor_initializer
+         {
+               ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
+               $$ = $1;
+         }
+       ;
+
+constructor_header
        : IDENTIFIER
          {
                if (RootContext.Documentation != null) {
@@ -2105,36 +2147,30 @@ constructor_declarator
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
-         {
-               current_local_parameters = (Parameters) $4;
-         }
-         opt_constructor_initializer
+         open_parens opt_formal_parameter_list CLOSE_PARENS
          {
                LocatedToken lt = (LocatedToken) $1;
-               $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
-                                     (ConstructorInitializer) $7, lt.Location);
+               current_local_parameters = (Parameters) $4;
+               current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
 
+               $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
+                                     null, lt.Location);
+                                     
                anonymous_host = (IAnonymousHost) $$;
          }
        ;
 
 constructor_body
-       : block
-       | SEMICOLON             { $$ = null; }
-       ;
-
-opt_constructor_initializer
-       : /* empty */                   { $$ = null; }
-       | constructor_initializer
+       : block_prepared
+       | SEMICOLON             { current_block = null; $$ = null; }
        ;
 
 constructor_initializer
-       : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
+       : COLON BASE open_parens opt_argument_list CLOSE_PARENS
          {
                $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
          }
-       | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
+       | COLON THIS open_parens opt_argument_list CLOSE_PARENS
          {
                $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
          }
@@ -2296,11 +2332,19 @@ add_accessor_declaration
                
                current_local_parameters = new Parameters (args);  
                lexer.EventParsing = false;
+               
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
-          block
+      block
          {
-               $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
+               Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
                lexer.EventParsing = true;
+               
+               current_local_parameters = null;
+               SimpleAnonymousHost.Simple.Propagate (accessor);
+               anonymous_host = null;
+               
+               $$ = accessor;
          }
        | opt_attributes ADD error {
                Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
@@ -2457,8 +2501,9 @@ enum_declaration
 
                EnumMember em = null;
                foreach (VariableDeclaration ev in (ArrayList) $7) {
-                       em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
-                               new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
+                       em = new EnumMember (
+                               e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
+                               ev.OptAttributes, ev.Location);
 
 //                     if (RootContext.Documentation != null)
                                em.DocComment = ev.DocComment;
@@ -2466,7 +2511,7 @@ enum_declaration
                        e.AddEnumMember (em);
                }
 
-               current_container.AddEnum (e);
+               current_container.AddTypeContainer (e);
                $$ = e;
 
          }
@@ -2725,7 +2770,6 @@ primary_expression
          {
                // 7.5.1: Literals
          }
        | member_name
          {
                MemberName mn = (MemberName) $1;
@@ -2922,6 +2966,10 @@ non_simple_argument
                Expression expr = new Arglist (args, (Location) $1);
                $$ = new Argument (expr, Argument.AType.Expression);
          }
+       | ARGLIST OPEN_PARENS CLOSE_PARENS
+         {
+               $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
+         }       
        | ARGLIST
          {
                $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
@@ -3187,48 +3235,12 @@ pointer_member_access
 anonymous_method_expression
        : DELEGATE opt_anonymous_method_signature
          {
-               if (oob_stack == null)
-                       oob_stack = new Stack (6);
-
-               oob_stack.Push (current_anonymous_method);
-               oob_stack.Push (current_local_parameters);
-               current_local_parameters = (Parameters)$2;
-
-               // Force the next block to be created as a ToplevelBlock
-               oob_stack.Push (current_block);
-               oob_stack.Push (top_current_block);
-
-               Location loc = (Location) $1;
-               current_anonymous_method = new AnonymousMethodExpression (
-                       current_anonymous_method, null, current_container,
-                       (Parameters) $2, (ToplevelBlock) top_current_block, loc);
-
-               parsing_anonymous_method = true;
+               start_anonymous (false, (Parameters) $2, (Location) $1);
          }
          block
          {
-               Location loc = (Location) $1;
-               top_current_block = (Block) oob_stack.Pop ();
-               current_block = (Block) oob_stack.Pop ();
-
-               if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotISO1 (loc, "anonymous methods");
-                       $$ = null;
-               } else  {
-                       ToplevelBlock anon_block = (ToplevelBlock) $4;
-
-                       anon_block.Parent = current_block;
-
-                       current_anonymous_method.Block = anon_block;
-                       if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
-                               anonymous_host.AddAnonymousMethod (current_anonymous_method);
-
-                       $$ = current_anonymous_method;
-               }
-
-               current_local_parameters = (Parameters) oob_stack.Pop ();
-               current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
-       }
+               $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
+         }
        ;
 
 opt_anonymous_method_signature
@@ -3237,7 +3249,7 @@ opt_anonymous_method_signature
        ;
 
 anonymous_method_signature
-       : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
+       : open_parens opt_anonymous_method_parameter_list CLOSE_PARENS 
          {
                if ($2 == null)
                        $$ = Parameters.EmptyReadOnlyParameters;
@@ -3567,9 +3579,114 @@ assignment_expression
          }
        ;
 
+implicitly_typed_lambda_parameter_list
+       : IDENTIFIER { 
+               LocatedToken lt = (LocatedToken) $1;
+               ArrayList a = new ArrayList (4); 
+
+               a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
+               $$ = a;
+         } 
+       | implicitly_typed_lambda_parameter_list COMMA IDENTIFIER {
+               LocatedToken lt = (LocatedToken) $3;
+               ArrayList a = (ArrayList) $1;
+               a.Add (new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location));
+               $$ = a;
+         }
+       ;
+
+explicitly_typed_lambda_parameter_list
+       : explicitly_typed_lambda_parameter
+         {
+               ArrayList pars = new ArrayList (4);
+               pars.Add ($1);
+
+               $$ = pars;
+         }
+       | explicitly_typed_lambda_parameter_list COMMA explicitly_typed_lambda_parameter
+         {
+               ArrayList pars = (ArrayList) $1;
+               pars.Add ($3);
+
+               $$ = pars;
+         }
+       ;
+
+explicitly_typed_lambda_parameter
+       : parameter_modifier type IDENTIFIER
+         {
+               LocatedToken lt = (LocatedToken) $3;
+
+               $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
+         }
+       | type IDENTIFIER
+         {
+               LocatedToken lt = (LocatedToken) $2;
+
+               $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
+         }
+       ;
+
+lambda_parameter_list
+       : implicitly_typed_lambda_parameter_list { $$ = $1; }
+       | explicitly_typed_lambda_parameter_list { $$ = $1; }
+       ;
+
+opt_lambda_parameter_list
+       : /* empty */                   { $$ = null; }
+       | lambda_parameter_list         { 
+               ArrayList pars_list = (ArrayList) $1;
+
+               Parameter [] pars = new Parameter [pars_list.Count];
+               pars_list.CopyTo (pars);
+               $$ = new Parameters (pars);
+         }
+       ;
+
+lambda_expression_body
+       : {
+               start_block (lexer.Location);
+         }
+         expression 
+         {
+               Block b = end_block (lexer.Location);
+               b.AddStatement (new ContextualReturn ((Expression) $2));
+               $$ = b;
+         } 
+       | block { 
+               $$ = $1; 
+         } 
+       ;
+
+lambda_expression
+       : IDENTIFIER ARROW 
+         {
+               Parameter [] pars = new Parameter [1];
+               LocatedToken lt = (LocatedToken) $1;
+               pars [0] = new Parameter ((Expression)null, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
+
+               Parameters parameters = new Parameters (pars);
+
+               start_anonymous (true, parameters, (Location) $2);
+         }
+         lambda_expression_body
+         {
+               $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
+         }
+       | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW 
+         {
+               start_anonymous (true, (Parameters) $2, (Location) $4);
+         }
+         lambda_expression_body 
+         {
+               $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
+         }
+       ;
+
 expression
        : conditional_expression
        | assignment_expression
+       | lambda_expression
        ;
 
 constant_expression
@@ -3594,7 +3711,7 @@ class_declaration
 
                push_current_class (new Class (
                        current_namespace, current_class, name,
-                       mod_flags, (Attributes) $1), false, $3);
+                       mod_flags, (Attributes) $1), $3);
          }
          opt_class_base
          {
@@ -3692,29 +3809,19 @@ class_base
 block
        : OPEN_BRACE 
          {
-               if (parsing_anonymous_method) {
-                       top_current_block = new ToplevelBlock (
-                               current_block, current_local_parameters, null, (Location) $1);
-                       if (current_block != null)
-                               current_block.AddAnonymousChild ((ToplevelBlock) top_current_block);
-                       current_block = top_current_block;
-                       parsing_anonymous_method = false;
-               } else if (current_block == null){
-                       current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
-                       top_current_block = current_block;
-               } else {
-                       current_block = new Block (current_block, (Location) $1, Location.Null);
-               }
+               start_block ((Location) $1);
          } 
          opt_statement_list CLOSE_BRACE 
          { 
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block.SetEndLocation ((Location) $4);
-               current_block = current_block.Parent;
-               if (current_block == null)
-                       top_current_block = null;
+               $$ = end_block ((Location) $4);
+         }
+       ;
+
+block_prepared
+       : OPEN_BRACE 
+         opt_statement_list CLOSE_BRACE 
+         { 
+               $$ = end_block ((Location) $3);
          }
        ;
 
@@ -3938,7 +4045,7 @@ statement_expression
                Expression expr = (Expression) $1;
                ExpressionStatement s = expr as ExpressionStatement;
                if (s == null) {
-                       Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
+                       expr.Error_InvalidExpressionStatement ();
                        $$ = null;
                }
                $$ = new StatementExpression (s);
@@ -4045,11 +4152,7 @@ switch_section
          }
          statement_list 
          {
-               Block topmost = current_block;
-
-               while (topmost.Implicit)
-                       topmost = topmost.Parent;
-               $$ = new SwitchSection ((ArrayList) $1, topmost);
+               $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
          }
        ;
 
@@ -4106,11 +4209,12 @@ do_statement
        ;
 
 for_statement
-       : FOR OPEN_PARENS 
+       : FOR open_parens 
          opt_for_initializer SEMICOLON
          {
-               Block assign_block = new Block (current_block);
-               current_block = assign_block;
+               Location l = lexer.Location;
+               start_block (l);  
+               Block assign_block = current_block;
 
                if ($3 is DictionaryEntry){
                        DictionaryEntry de = (DictionaryEntry) $3;
@@ -4126,7 +4230,6 @@ for_statement
                                if (vi == null)
                                        continue;
 
-                               Location l = lexer.Location;
                                Expression expr = decl.expression_or_array_initializer;
                                        
                                LocalVariableReference var;
@@ -4155,10 +4258,8 @@ for_statement
                For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
 
                current_block.AddStatement (f);
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+
+               $$ = end_block (lexer.Location);
          }
        ;
 
@@ -4191,7 +4292,7 @@ statement_expression_list
          {
                // CHANGE: was `null'
                Statement s = (Statement) $1;
-               Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);   
+               Block b = new Block (current_block, s.loc, lexer.Location);   
 
                b.AddStatement (s);
                $$ = b;
@@ -4206,16 +4307,16 @@ statement_expression_list
        ;
 
 foreach_statement
-       : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
+       : FOREACH open_parens type IN expression CLOSE_PARENS
          {
                Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
                $$ = null;
          }
-       | FOREACH OPEN_PARENS type IDENTIFIER IN
+       | FOREACH open_parens type IDENTIFIER IN
          expression CLOSE_PARENS 
          {
-               Block foreach_block = new Block (current_block);
-               current_block = foreach_block;
+               start_block (lexer.Location);
+               Block foreach_block = current_block;
 
                LocatedToken lt = (LocatedToken) $4;
                Location l = lt.Location;
@@ -4244,10 +4345,7 @@ foreach_statement
                        current_block.AddStatement (f);
                }
 
-               while (current_block.Implicit)
-                         current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+               $$ = end_block (lexer.Location);
          }
        ;
 
@@ -4445,9 +4543,8 @@ catch_clause
 
                                one.Add (new VariableDeclaration (lt, null));
 
-                               current_block = new Block (current_block);
-                               Block b = declare_local_variables (type, one, lt.Location);
-                               current_block = b;
+                               start_block (lexer.Location);
+                               current_block = declare_local_variables (type, one, lt.Location);
                        }
                }
          } block {
@@ -4462,10 +4559,7 @@ catch_clause
 
                        if (lt != null){
                                id = lt.Value;
-                               while (current_block.Implicit)
-                                       current_block = current_block.Parent;
-                               var_block = current_block;
-                               current_block = current_block.Parent;
+                               var_block = end_block (lexer.Location);
                        }
                }
 
@@ -4479,7 +4573,7 @@ opt_catch_args
        ;         
 
 catch_args 
-        : OPEN_PARENS type opt_identifier CLOSE_PARENS 
+        : open_parens type opt_identifier CLOSE_PARENS 
           {
                $$ = new DictionaryEntry ($2, $3);
          }
@@ -4510,7 +4604,7 @@ unsafe_statement
        ;
 
 fixed_statement
-       : FIXED OPEN_PARENS 
+       : FIXED open_parens 
          type fixed_pointer_declarators 
          CLOSE_PARENS
          {
@@ -4519,8 +4613,8 @@ fixed_statement
                Location l = (Location) $1;
                int top = list.Count;
 
-               Block assign_block = new Block (current_block);
-               current_block = assign_block;
+               start_block (lexer.Location);
+               Block assign_block = current_block;
 
                for (int i = 0; i < top; i++){
                        Pair p = (Pair) list [i];
@@ -4543,10 +4637,8 @@ fixed_statement
                Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
 
                current_block.AddStatement (f);
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+
+               $$ = end_block (lexer.Location);
          }
        ;
 
@@ -4592,10 +4684,10 @@ lock_statement
        ;
 
 using_statement
-       : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
+       : USING open_parens resource_acquisition CLOSE_PARENS
          {
-               Block assign_block = new Block (current_block);
-               current_block = assign_block;
+               start_block (lexer.Location);
+               Block assign_block = current_block;
 
                if ($3 is DictionaryEntry){
                        DictionaryEntry de = (DictionaryEntry) $3;
@@ -4643,10 +4735,7 @@ using_statement
          {
                Using u = new Using ($5, (Statement) $6, (Location) $1);
                current_block.AddStatement (u);
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+               $$ = end_block (lexer.Location);
          }
        ; 
 
@@ -4773,7 +4862,7 @@ void Error_ExpectingTypeName (Expression expr)
        if (expr is Invocation){
                Report.Error (1002, expr.Location, "Expecting `;'");
        } else {
-               Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
+               expr.Error_InvalidExpressionStatement ();
        }
 }
 
@@ -4788,12 +4877,12 @@ static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier m
                Parameter.GetModifierSignature (mod));
 }
 
-void push_current_class (TypeContainer tc, bool is_interface, object partial_token)
+void push_current_class (TypeContainer tc, object partial_token)
 {
        if (partial_token != null)
-               current_container = current_container.AddPartial (tc, is_interface);
+               current_container = current_container.AddPartial (tc);
        else
-               current_container = current_container.AddTypeContainer (tc, is_interface);
+               current_container = current_container.AddTypeContainer (tc);
        current_class = tc;
 }
 
@@ -4847,7 +4936,7 @@ Block declare_local_variables (Expression type, ArrayList variable_declarators,
        // int j = 1;  int k = j + 1;
        //
        if (current_block.Used)
-               implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
+               implicit_block = new Block (current_block, loc, Location.Null);
        else
                implicit_block = current_block;
 
@@ -4885,7 +4974,7 @@ Block declare_local_constants (Expression type, ArrayList declarators)
        Block implicit_block;
 
        if (current_block.Used)
-               implicit_block = new Block (current_block, Block.Flags.Implicit);
+               implicit_block = new Block (current_block);
        else
                implicit_block = current_block;
 
@@ -4896,18 +4985,16 @@ Block declare_local_constants (Expression type, ArrayList declarators)
        return implicit_block;
 }
 
-void CheckAttributeTarget (string a, Location l)
+string CheckAttributeTarget (string a, Location l)
 {
        switch (a) {
-
-       case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
-               return;
-               
-       default :
-               Report.Error (658, l, "`" + a + "' is an invalid attribute target");
-               break;
+               case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
+                       return a;
        }
-
+       
+       Report.Warning (658, 1, l,
+                "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
+       return string.Empty;
 }
 
 void CheckUnaryOperator (Operator.OpType op, Location l)
@@ -4979,6 +5066,11 @@ public Tokenizer Lexer {
        }
 }                 
 
+static CSharpParser ()
+{
+       oob_stack = new Stack ();
+}
+
 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
 {
        this.name = file.Name;
@@ -4986,7 +5078,7 @@ public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList def
        current_namespace = new NamespaceEntry (null, file, null);
        current_class = current_namespace.SlaveDeclSpace;
        current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
-
+       oob_stack.Clear ();
        lexer = new Tokenizer (reader, file, defines);
 }
 
@@ -5049,5 +5141,74 @@ Location GetLocation (object obj)
        return lexer.Location;
 }
 
+static GenericMethod current_generic_method = null;
+
+void start_block (Location loc)
+{
+       if (current_block == null || parsing_anonymous_method) {
+               current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
+               parsing_anonymous_method = false;
+       } else {
+               current_block = new ExplicitBlock (current_block, loc, Location.Null);
+       }
+}
+
+Block
+end_block (Location loc)
+{
+       Block retval = current_block.Explicit;
+       retval.SetEndLocation (loc);
+       current_block = retval.Parent;
+       return retval;
+}
+
+void
+start_anonymous (bool lambda, Parameters parameters, Location loc)
+{
+       oob_stack.Push (current_anonymous_method);
+       oob_stack.Push (current_local_parameters);
+
+       current_local_parameters = parameters;
+
+       ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
+
+       current_anonymous_method = lambda 
+               ? new LambdaExpression (
+                       current_anonymous_method, current_generic_method, current_container,
+                       parameters, top_current_block, loc) 
+               : new AnonymousMethodExpression (
+                       current_anonymous_method, current_generic_method, current_container,
+                       parameters, top_current_block, loc);
+
+       // Force the next block to be created as a ToplevelBlock
+       parsing_anonymous_method = true;
+}
+
+/*
+ * Completes the anonymous method processing, if lambda_expr is null, this
+ * means that we have a Statement instead of an Expression embedded 
+ */
+AnonymousMethodExpression 
+end_anonymous (ToplevelBlock anon_block, Location loc)
+{
+       AnonymousMethodExpression retval;
+
+       if (RootContext.Version == LanguageVersion.ISO_1){
+               Report.FeatureIsNotISO1 (loc, "anonymous methods");
+               retval = null;
+       } else  {
+               current_anonymous_method.Block = anon_block;
+               if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
+                       anonymous_host.AddAnonymousMethod (current_anonymous_method);
+
+               retval = current_anonymous_method;
+       }
+
+       current_local_parameters = (Parameters) oob_stack.Pop ();
+       current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
+
+       return retval;
+}
+
 /* end end end */
 }