* statement.cs (ToplevelBlock.ResolveMeta): Move CS0136 checks ...
[mono.git] / mcs / mcs / cs-parser.jay
index 5d67c3a60c887b955f9a6db41f859f84a4a6ee5c..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
 //
@@ -33,15 +34,19 @@ namespace Mono.CSharp
        public class CSharpParser {
                NamespaceEntry  current_namespace;
                TypeContainer   current_container;
-               TypeContainer   current_class;
+               DeclSpace       current_class;
        
-               IIteratorContainer iterator_container;
+               IAnonymousHost anonymous_host;
 
                /// <summary>
                ///   Current block is used to add statements as we find
                ///   them.  
                /// </summary>
-               Block      current_block, top_current_block;
+               Block      current_block;
+
+               Delegate   current_delegate;
+
+               AnonymousMethodExpression current_anonymous_method;
 
                /// <summary>
                ///   This is used by the unary_expression code to resolve
@@ -57,16 +62,24 @@ namespace Mono.CSharp
                Expression implicit_value_parameter_type;
                Parameters indexer_parameters;
 
+               /// <summary>
+               ///   Hack to help create non-typed array initializer
+               /// </summary>
+               public static Expression current_array_type;
+               Expression pushed_current_array_type;
+
                /// <summary>
                ///   Used to determine if we are parsing the get/set pair
                ///   of an indexer or a property
                /// </summmary>
-               bool  parsing_indexer;
+               bool parsing_indexer;
 
+               bool parsing_anonymous_method;
+               
                ///
                /// An out-of-band stack.
                ///
-               Stack oob_stack;
+               static Stack oob_stack;
 
                ///
                /// Switch stack.
@@ -95,7 +108,6 @@ namespace Mono.CSharp
                
                /// assembly and module attribute definitions are enabled
                bool global_attrs_enabled = true;
-
                bool has_get, has_set;
 
 %}
@@ -191,6 +203,7 @@ namespace Mono.CSharp
 %token WHILE   
 %token ARGLIST
 %token PARTIAL
+%token ARROW
 
 /* C# keywords which are not really keywords */
 %token GET           "get"
@@ -216,7 +229,9 @@ namespace Mono.CSharp
 %token BANG           "!"
 %token ASSIGN         "="
 %token OP_LT          "<"
+%token OP_GENERICS_LT "<"
 %token OP_GT          ">"
+%token OP_GENERICS_GT ">"
 %token BITWISE_AND    "&"
 %token BITWISE_OR     "|"
 %token STAR           "*"
@@ -258,6 +273,7 @@ namespace Mono.CSharp
 %token LITERAL_STRING            "string literal"
 
 %token IDENTIFIER
+%token OPEN_PARENS_LAMBDA
 %token CLOSE_PARENS_CAST
 %token CLOSE_PARENS_NO_CAST
 %token CLOSE_PARENS_OPEN_PARENS
@@ -326,7 +342,7 @@ extern_alias_directive
                if (s != "alias"){
                        Report.Error (1003, lt.Location, "'alias' expected");
                } else if (RootContext.Version == LanguageVersion.ISO_1) {
-                       Report.FeatureIsNotStandardized (lt.Location, "external alias");
+                       Report.FeatureIsNotISO1 (lt.Location, "external alias");
                } else {
                        lt = (LocatedToken) $3; 
                        current_namespace.UsingExternalAlias (lt.Value, lt.Location);
@@ -358,7 +374,6 @@ using_alias_directive
          {
                LocatedToken lt = (LocatedToken) $2;
                current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
-               current_namespace.UsingFound = true;
          }
        | USING error {
                CheckIdentifierToken (yyToken, GetLocation ($2));
@@ -369,7 +384,6 @@ using_namespace_directive
        : USING namespace_name SEMICOLON 
          {
                current_namespace.Using ((MemberName) $2, (Location) $1);
-               current_namespace.UsingFound = true;
           }
        ;
 
@@ -388,11 +402,15 @@ namespace_declaration
                }
 
                current_namespace = new NamespaceEntry (
-                       current_namespace, file, name.GetName (), name.Location);
+                       current_namespace, file, name.GetName ());
+               current_class = current_namespace.SlaveDeclSpace;
+               current_container = current_class.PartialContainer;
          } 
          namespace_body opt_semicolon
          { 
                current_namespace = current_namespace.Parent;
+               current_class = current_namespace.SlaveDeclSpace;
+               current_container = current_class.PartialContainer;
          }
        ;
 
@@ -416,10 +434,26 @@ namespace_body
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
-         opt_extern_alias_directives
+         namespace_body_body
+       ;
+       
+namespace_body_body
+       : opt_extern_alias_directives
          opt_using_directives
          opt_namespace_member_declarations
          CLOSE_BRACE
+       | error
+         {
+               Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
+         }
+         CLOSE_BRACE
+       | opt_extern_alias_directives
+         opt_using_directives
+         opt_namespace_member_declarations
+         EOF
+         {
+               Report.Error (1513, lexer.Location, "} expected");
+         }
        ;
 
 opt_using_directives
@@ -512,55 +546,63 @@ opt_attributes
 attribute_sections
        : attribute_section
          {
-               ArrayList sect = (ArrayList) $1;
+               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;
+                       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;
          }
        ;
@@ -588,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
@@ -617,15 +663,17 @@ attribute
        : attribute_name opt_attribute_arguments
          {
                MemberName mname = (MemberName) $1;
-               ArrayList arguments = (ArrayList) $2;
+               object[] arguments = (object[]) $2;
                MemberName left = mname.Left;
                string identifier = mname.Name;
 
                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_class, current_attr_target,
+                       $$ = new GlobalAttribute (current_namespace, current_attr_target,
                                                  left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
                else
                        $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
@@ -651,29 +699,18 @@ attribute_arguments
                if ($1 == null)
                        $$ = null;
                else {
-                       ArrayList args = new ArrayList (4);
-                       args.Add ($1);
-               
-                       $$ = args;
+                       $$ = new object [] { $1, null };
                }
          }
-        | positional_argument_list COMMA named_argument_list
+    | positional_argument_list COMMA named_argument_list
          {
-               ArrayList args = new ArrayList (4);
-               args.Add ($1);
-               args.Add ($3);
-
-               $$ = args;
+               $$ = new object[] { $1, $3 };
          }
-        | named_argument_list
+    | named_argument_list
          {
-               ArrayList args = new ArrayList (4);
-               args.Add (null);
-               args.Add ($1);
-               
-               $$ = args;
+               $$ = new object [] { null, $1 };
          }
-        ;
+    ;
 
 
 opt_positional_argument_list
@@ -766,30 +803,18 @@ struct_declaration
          STRUCT member_name
          { 
                MemberName name = MakeName ((MemberName) $5);
-               if ($3 != null) {
-                       ClassPart part = PartialContainer.CreatePart (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1, Kind.Struct, (Location) $3);
+               push_current_class (new Struct (
+                       current_namespace, current_class, name, (int) $2,
+                       (Attributes) $1), $3);
 
-                       current_container = part.PartialContainer;
-                       current_class = part;
-               } else {
-                       current_class = new Struct (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1);
-
-                       current_container.AddClassOrStruct (current_class);
-                       current_container = current_class;
-                       RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
-               }
          }
          opt_class_base
          {
                if ($7 != null)
-                       current_class.Bases = (ArrayList) $7;
+                       current_container.AddBasesForPart (current_class, (ArrayList) $7);
 
                if (RootContext.Documentation != null)
-                       current_class.DocComment = Lexer.consume_doc_comment ();
+                       current_container.DocComment = Lexer.consume_doc_comment ();
          }
          struct_body
          {
@@ -912,11 +937,14 @@ field_declaration
                Expression type = (Expression) $3;
                int mod = (int) $2;
 
+               current_array_type = null;
+
                foreach (VariableDeclaration var in (ArrayList) $4){
                        Field field = new Field (current_class, type, mod, var.identifier, 
-                                                var.expression_or_array_initializer, 
                                                 (Attributes) $1, var.Location);
 
+                       field.Initializer = var.expression_or_array_initializer;
+
                        if (RootContext.Documentation != null) {
                                field.DocComment = Lexer.consume_doc_comment ();
                                Lexer.doc_state = XmlCommentState.Allowed;
@@ -935,6 +963,8 @@ field_declaration
                        Expression type = (Expression) $4;
                        int mod = (int) $2;
 
+                       current_array_type = null;
+
                        foreach (VariableDeclaration var in (ArrayList) $5) {
                                FixedField field = new FixedField (current_class, type, mod, var.identifier,
                                        (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
@@ -947,11 +977,20 @@ field_declaration
                                $$ = field; // FIXME: might be better if it points to the top item
                        }
          }
+       | opt_attributes
+         opt_modifiers
+         FIXED
+         type 
+         error
+         {
+               Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
+         }
        | opt_attributes
          opt_modifiers
          VOID  
          variable_declarators
          SEMICOLON {
+               current_array_type = null;
                Report.Error (670, (Location) $3, "Fields cannot have void type");
          }
        ;
@@ -1029,6 +1068,10 @@ variable_initializer
          {
                $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
          }
+       | ARGLIST
+         {
+               $$ = new ArglistAccess ((Location) $1);
+         }
        | STACKALLOC type
          {
                Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
@@ -1038,7 +1081,7 @@ variable_initializer
 
 method_declaration
        : method_header {
-               iterator_container = (IIteratorContainer) $1;
+               anonymous_host = (IAnonymousHost) $1;
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.NotAllowed;
          }
@@ -1048,8 +1091,8 @@ method_declaration
                method.Block = (ToplevelBlock) $3;
                current_container.AddMethod (method);
 
+               anonymous_host = null;
                current_local_parameters = null;
-               iterator_container = null;
 
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
@@ -1075,17 +1118,29 @@ 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;
 
-               Method method = new Method (current_class, (Expression) $3, (int) $2,
+               Method method = new Method (current_class, null, (Expression) $3, (int) $2,
                                            false, name,  (Parameters) $6, (Attributes) $1);
 
+               anonymous_host = method;
                current_local_parameters = (Parameters) $6;
 
                if (RootContext.Documentation != null)
@@ -1096,14 +1151,15 @@ 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;
 
-               Method method = new Method (current_class, TypeManager.system_void_expr,
+               Method method = new Method (current_class, null, TypeManager.system_void_expr,
                                            (int) $2, false, name, (Parameters) $6,
                                            (Attributes) $1);
 
+               anonymous_host = method;
                current_local_parameters = (Parameters) $6;
 
                if (RootContext.Documentation != null)
@@ -1114,13 +1170,13 @@ 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, 
                        "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
 
-               Method method = new Method (current_class, TypeManager.system_void_expr,
+               Method method = new Method (current_class, null, TypeManager.system_void_expr,
                                            0, false, name, (Parameters) $7, (Attributes) $1);
 
                current_local_parameters = (Parameters) $7;
@@ -1172,17 +1228,28 @@ formal_parameter_list
 
                $$ = new Parameters (pars, true);
          }
-       | parameter_array COMMA fixed_parameters
+       | parameter_array COMMA error
          {
                if ($1 != null)
                        Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
                $$ = null;
          }
-       | ARGLIST COMMA fixed_parameters
+       | fixed_parameters COMMA parameter_array COMMA error
+         {
+               if ($3 != null)
+                       Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
+               $$ = null;
+         }
+       | ARGLIST COMMA error
          {
                Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
                $$ = null;
          }
+       | fixed_parameters COMMA ARGLIST COMMA error 
+         {
+               Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
+               $$ = null;
+         }
        | parameter_array 
          {
                $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
@@ -1257,12 +1324,31 @@ fixed_parameter
 
 opt_parameter_modifier
        : /* empty */           { $$ = Parameter.Modifier.NONE; }
-       | parameter_modifier
+       | parameter_modifiers
+       ;
+
+parameter_modifiers
+       : parameter_modifier
+         {
+               $$ = $1;
+         }
+       | parameter_modifiers parameter_modifier
+         {
+               Parameter.Modifier p2 = (Parameter.Modifier)$2;
+               Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
+               if (((Parameter.Modifier)$1 & p2) == p2) {
+                       Error_DuplicateParameterModifier (lexer.Location, p2);
+               } else {
+                       Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
+               }
+               $$ = mod;
+         }
        ;
 
 parameter_modifier
        : REF                   { $$ = Parameter.Modifier.REF; }
        | OUT                   { $$ = Parameter.Modifier.OUT; }
+       | THIS                  { $$ = Parameter.Modifier.This; }
        ;
 
 parameter_array
@@ -1270,11 +1356,20 @@ parameter_array
          { 
                LocatedToken lt = (LocatedToken) $4;
                $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
-               note ("type must be a single-dimension array type"); 
+         }
+       | opt_attributes PARAMS PARAMS type IDENTIFIER 
+         {
+               Error_DuplicateParameterModifier (lexer.Location, Parameter.Modifier.PARAMS);
+               $$ = null;
          }
        | 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 {
@@ -1309,14 +1404,14 @@ property_declaration
                        break;
 
                Property prop;
-               Pair pair = (Pair) $8;
-               Accessor get_block = (Accessor) pair.First;
-               Accessor set_block = (Accessor) pair.Second;
+               Accessors accessors = (Accessors) $8;
+               Accessor get_block = accessors.get_or_add;
+               Accessor set_block = accessors.set_or_remove;
 
                MemberName name = (MemberName) $4;
 
                prop = new Property (current_class, (Expression) $3, (int) $2,
-                       false, name, (Attributes) $1, get_block, set_block);
+                       false, name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse);
                
                current_container.AddProperty (prop);
                implicit_value_parameter_type = null;
@@ -1330,23 +1425,24 @@ property_declaration
 accessor_declarations
        : get_accessor_declaration
         {
-               $$ = new Pair ($1, null);
+               $$ = new Accessors ((Accessor) $1, null);
         }
        | get_accessor_declaration accessor_declarations
         { 
-               Pair pair = (Pair) $2;
-               pair.First = $1;
-               $$ = pair;
+               Accessors accessors = (Accessors) $2;
+               accessors.get_or_add = (Accessor) $1;
+               $$ = accessors;
         }
        | set_accessor_declaration
         {
-               $$ = new Pair (null, $1);
+               $$ = new Accessors (null, (Accessor) $1);
         }
        | set_accessor_declaration accessor_declarations
         { 
-               Pair pair = (Pair) $2;
-               pair.Second = $1;
-               $$ = pair;
+               Accessors accessors = (Accessors) $2;
+               accessors.set_or_remove = (Accessor) $1;
+               accessors.declared_in_reverse = true;
+               $$ = accessors;
         }
        | error
          {
@@ -1366,7 +1462,7 @@ get_accessor_declaration
                        current_local_parameters = indexer_parameters;
                lexer.PropertyParsing = false;
 
-               iterator_container = SimpleIteratorContainer.GetSimple ();
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
           accessor_body
          {
@@ -1379,10 +1475,8 @@ get_accessor_declaration
                current_local_parameters = null;
                lexer.PropertyParsing = true;
 
-               if (SimpleIteratorContainer.Simple.Yields)
-                       accessor.SetYields ();
-
-               iterator_container = null;
+               SimpleAnonymousHost.Simple.Propagate (accessor);
+               anonymous_host = null;
 
                if (RootContext.Documentation != null)
                        if (Lexer.doc_state == XmlCommentState.Error)
@@ -1421,7 +1515,7 @@ set_accessor_declaration
                
                lexer.PropertyParsing = false;
 
-               iterator_container = SimpleIteratorContainer.GetSimple ();
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
          accessor_body
          {
@@ -1434,10 +1528,8 @@ set_accessor_declaration
                current_local_parameters = null;
                lexer.PropertyParsing = true;
 
-               if (SimpleIteratorContainer.Simple.Yields)
-                       accessor.SetYields ();
-
-               iterator_container = null;
+               SimpleAnonymousHost.Simple.Propagate (accessor);
+               anonymous_host = null;
 
                if (RootContext.Documentation != null
                        && Lexer.doc_state == XmlCommentState.Error)
@@ -1460,29 +1552,16 @@ interface_declaration
          {
                MemberName name = MakeName ((MemberName) $5);
 
-               if ($3 != null) {
-                       ClassPart part = PartialContainer.CreatePart (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1, Kind.Interface, (Location) $3);
-
-                       current_container = part.PartialContainer;
-                       current_class = part;
-               } else {
-                       current_class = new Interface (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1);
-
-                       current_container.AddInterface (current_class);
-                       current_container = current_class;
-                       RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
-               }
+               push_current_class (new Interface (
+                       current_namespace, current_class, name, (int) $2,
+                       (Attributes) $1), $3);
          }
          opt_class_base
          {
-               current_class.Bases = (ArrayList) $7;
+               current_container.AddBasesForPart (current_class, (ArrayList) $7);
 
                if (RootContext.Documentation != null) {
-                       current_class.DocComment = Lexer.consume_doc_comment ();
+                       current_container.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
@@ -1635,7 +1714,7 @@ interface_method_declaration
          OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
          SEMICOLON
          {
-               $$ = new Method (current_class, (Expression) $3, (int) $2, true,
+               $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
                                 (MemberName) $4, (Parameters) $6, (Attributes) $1);
                if (RootContext.Documentation != null)
                        ((Method) $$).DocComment = Lexer.consume_doc_comment ();
@@ -1644,7 +1723,7 @@ interface_method_declaration
          OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
          OPEN_BRACE opt_statement_list CLOSE_BRACE
          {
-               $$ = new Method (current_class, (Expression) $3, (int) $2, true,
+               $$ = new Method (current_class, null, (Expression) $3, (int) $2, true,
                                 (MemberName) $4, (Parameters) $6, (Attributes) $1);
 
                Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
@@ -1677,24 +1756,24 @@ interface_property_declaration
                if ($7 == null) {
                        p = new Property (current_class, (Expression) $3, (int) $2, true,
                                   name, (Attributes) $1,
-                                  null, null);
+                                  null, null, false);
 
                        Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
                        break;
                }
 
-               Pair pair = (Pair) $7;
+               Accessors accessor = (Accessors) $7;
                p = new Property (current_class, (Expression) $3, (int) $2, true,
                                   name, (Attributes) $1,
-                                  (Accessor)pair.First, (Accessor)pair.Second);
+                                  accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
 
-               if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
+               if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
                        Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
                        $$ = null;
                        break;
                }
 
-               if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
+               if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
                        Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
                        $$ = null;
                        break;
@@ -1719,7 +1798,7 @@ interface_event_declaration
          {
                LocatedToken lt = (LocatedToken) $5;
                $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
-                                    new MemberName (lt.Value, lt.Location), null,
+                                    new MemberName (lt.Value, lt.Location),
                                     (Attributes) $1);
                if (RootContext.Documentation != null)
                        ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
@@ -1764,25 +1843,25 @@ interface_indexer_declaration
                        i = new Indexer (current_class, (Expression) $3,
                                  new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
                                  (int) $2, true, (Parameters) $6, (Attributes) $1,
-                                 null, null);
+                                 null, null, false);
 
                        Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
                        break;
                }
 
-               Pair pair = (Pair) $10;
+               Accessors accessors = (Accessors) $10;
                i = new Indexer (current_class, (Expression) $3,
                                  new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
                                  (int) $2, true, (Parameters) $6, (Attributes) $1,
-                                  (Accessor)pair.First, (Accessor)pair.Second);
+                                  accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
 
-               if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
+               if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
                        Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
                        $$ = null;
                        break;
                }
 
-               if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
+               if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
                        Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
                        $$ = null;
                        break;
@@ -1798,7 +1877,7 @@ interface_indexer_declaration
 operator_declaration
        : opt_attributes opt_modifiers operator_declarator 
          {
-               iterator_container = SimpleIteratorContainer.GetSimple ();
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
          operator_body
          {
@@ -1823,14 +1902,13 @@ operator_declaration
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
 
-               if (SimpleIteratorContainer.Simple.Yields)
-                       op.SetYields ();
+               SimpleAnonymousHost.Simple.Propagate (op);
+               anonymous_host = null;
 
                // Note again, checking is done in semantic analysis
                current_container.AddOperator (op);
 
                current_local_parameters = null;
-               iterator_container = null;
          }
        ;
 
@@ -1840,9 +1918,13 @@ operator_body
        ; 
 operator_declarator
        : type OPERATOR overloadable_operator 
-         OPEN_PARENS type IDENTIFIER CLOSE_PARENS
+         open_parens opt_attributes opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
          {
-               LocatedToken lt = (LocatedToken) $6;
+               // TODO: wrong location
+               if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE)
+                       Error_ParameterModifierNotValid ((Location) $2);
+         
+               LocatedToken lt = (LocatedToken) $8;
                Operator.OpType op = (Operator.OpType) $3;
                CheckUnaryOperator (op, lt.Location);
 
@@ -1853,9 +1935,9 @@ operator_declarator
                        op = Operator.OpType.UnaryNegation;
 
                Parameter [] pars = new Parameter [1];
-               Expression type = (Expression) $5;
+               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);
 
@@ -1868,22 +1950,26 @@ operator_declarator
                                              null, null, (Location) $2);
          }
        | type OPERATOR overloadable_operator
-         OPEN_PARENS 
-               type IDENTIFIER COMMA
-               type IDENTIFIER 
+         open_parens 
+               opt_attributes opt_parameter_modifier type IDENTIFIER COMMA
+               opt_attributes opt_parameter_modifier type IDENTIFIER 
          CLOSE_PARENS
           {
-               LocatedToken ltParam1 = (LocatedToken) $6;
-               LocatedToken ltParam2 = (LocatedToken) $9;
+               // TODO: wrong location
+               if ((Parameter.Modifier)$6 != Parameter.Modifier.NONE || (Parameter.Modifier)$11 != Parameter.Modifier.NONE)
+                       Error_ParameterModifierNotValid ((Location) $2);
+
+               LocatedToken ltParam1 = (LocatedToken) $8;
+               LocatedToken ltParam2 = (LocatedToken) $13;
                CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
 
                Parameter [] pars = new Parameter [2];
 
-               Expression typeL = (Expression) $5;
-               Expression typeR = (Expression) $8;
+               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);
 
@@ -1898,18 +1984,16 @@ operator_declarator
           }
        | conversion_operator_declarator
        | type OPERATOR overloadable_operator
-         OPEN_PARENS 
-               type IDENTIFIER COMMA
-               type IDENTIFIER COMMA
-               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));
@@ -1946,12 +2030,16 @@ overloadable_operator
        ;
 
 conversion_operator_declarator
-       : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
+       : IMPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
          {
-               LocatedToken lt = (LocatedToken) $6;
+               // TODO: wrong location
+               if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
+                       Error_ParameterModifierNotValid (GetLocation ($4));
+
+               LocatedToken lt = (LocatedToken) $7;
                Parameter [] pars = new Parameter [1];
 
-               pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
+               pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
 
                current_local_parameters = new Parameters (pars);  
                  
@@ -1960,15 +2048,19 @@ conversion_operator_declarator
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
-               $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
+               $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $6, lt.Value,
                                              null, null, (Location) $2);
          }
-       | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
+       | EXPLICIT OPERATOR type open_parens opt_parameter_modifier type IDENTIFIER CLOSE_PARENS
          {
-               LocatedToken lt = (LocatedToken) $6;
+               // TODO: wrong location
+               if ((Parameter.Modifier)$5 != Parameter.Modifier.NONE)
+                       Error_ParameterModifierNotValid (GetLocation ($4));
+         
+               LocatedToken lt = (LocatedToken) $7;
                Parameter [] pars = new Parameter [1];
 
-               pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
+               pars [0] = new Parameter ((Expression) $6, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
 
                current_local_parameters = new Parameters (pars);  
                  
@@ -1977,7 +2069,7 @@ conversion_operator_declarator
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
-               $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
+               $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $6, lt.Value,
                                              null, null, (Location) $2);
          }
        | IMPLICIT error 
@@ -2019,11 +2111,6 @@ constructor_declaration
                                                "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
                                                c.GetSignatureForError ());
                                }
-       
-                               if (!c.Parameters.Empty){
-                                       Report.Error (132, c.Location,
-                                               "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
-                               }
                        } else {
                                c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
                        }
@@ -2041,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) {
@@ -2048,34 +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);
          }
@@ -2132,14 +2227,17 @@ event_declaration
          opt_modifiers
          EVENT type variable_declarators SEMICOLON
          {
+               current_array_type = null;
                foreach (VariableDeclaration var in (ArrayList) $5) {
 
                        MemberName name = new MemberName (var.identifier,
                                var.Location);
 
-                       Event e = new EventField (
+                       EventField e = new EventField (
                                current_class, (Expression) $4, (int) $2, false, name,
-                               var.expression_or_array_initializer, (Attributes) $1);
+                               (Attributes) $1);
+
+                       e.Initializer = var.expression_or_array_initializer;
 
                        current_container.AddEvent (e);
 
@@ -2170,14 +2268,14 @@ event_declaration
                                current_container.Name, name.ToString ());
                        $$ = null;
                } else {
-                       Pair pair = (Pair) $8;
-                       if (pair.First == null || pair.Second == null)
+                       Accessors accessors = (Accessors) $8;
+                       if (accessors.get_or_add == null || accessors.set_or_remove == null)
                                // CS0073 is already reported, so no CS0065 here.
                                $$ = null;
                        else {
                                Event e = new EventProperty (
-                                       current_class, (Expression) $4, (int) $2, false, name, null,
-                                       (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
+                                       current_class, (Expression) $4, (int) $2, false, name,
+                                       (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
                                if (RootContext.Documentation != null) {
                                        e.DocComment = Lexer.consume_doc_comment ();
                                        Lexer.doc_state = XmlCommentState.Allowed;
@@ -2204,11 +2302,13 @@ event_declaration
 event_accessor_declarations
        : add_accessor_declaration remove_accessor_declaration
          {
-               $$ = new Pair ($1, $2);
+               $$ = new Accessors ((Accessor) $1, (Accessor) $2);
          }
        | remove_accessor_declaration add_accessor_declaration
          {
-               $$ = new Pair ($2, $1);
+               Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
+               accessors.declared_in_reverse = true;
+               $$ = accessors;
          }     
        | add_accessor_declaration  { $$ = null; } 
        | remove_accessor_declaration { $$ = null; } 
@@ -2232,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");
@@ -2288,6 +2396,7 @@ indexer_declaration
                parsing_indexer  = true;
                
                indexer_parameters = decl.param_list;
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
           accessor_declarations 
          {
@@ -2305,9 +2414,9 @@ indexer_declaration
                Indexer indexer;
                IndexerDeclaration decl = (IndexerDeclaration) $3;
                Location loc = decl.location;
-               Pair pair = (Pair) $6;
-               Accessor get_block = (Accessor) pair.First;
-               Accessor set_block = (Accessor) pair.Second;
+               Accessors accessors = (Accessors) $6;
+               Accessor get_block = accessors.get_or_add;
+               Accessor set_block = accessors.set_or_remove;
 
                MemberName name;
                if (decl.interface_type != null)
@@ -2317,7 +2426,7 @@ indexer_declaration
 
                indexer = new Indexer (current_class, decl.type, name,
                                       (int) $2, false, decl.param_list, (Attributes) $1,
-                                      get_block, set_block);
+                                      get_block, set_block, accessors.declared_in_reverse);
 
                if (RootContext.Documentation != null)
                        indexer.DocComment = ConsumeStoredComment ();
@@ -2371,7 +2480,6 @@ indexer_declarator
 enum_declaration
        : opt_attributes
          opt_modifiers
-         opt_partial
          ENUM IDENTIFIER 
          opt_enum_base {
                if (RootContext.Documentation != null)
@@ -2380,16 +2488,11 @@ enum_declaration
          enum_body
          opt_semicolon
          {
-               LocatedToken lt = (LocatedToken) $5;
+               LocatedToken lt = (LocatedToken) $4;
                Location enum_location = lt.Location;
 
-               if ($3 != null) {
-                       Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
-                       break;  // assumes that the parser put us in a switch
-               }
-
                MemberName name = MakeName (new MemberName (lt.Value, enum_location));
-               Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
+               Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
                                   name, (Attributes) $1);
                
                if (RootContext.Documentation != null)
@@ -2397,9 +2500,10 @@ enum_declaration
 
 
                EnumMember em = null;
-               foreach (VariableDeclaration ev in (ArrayList) $8) {
-                       em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
-                               new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
+               foreach (VariableDeclaration ev in (ArrayList) $7) {
+                       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;
@@ -2407,8 +2511,7 @@ enum_declaration
                        e.AddEnumMember (em);
                }
 
-               current_container.AddEnum (e);
-               RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
+               current_container.AddTypeContainer (e);
                $$ = e;
 
          }
@@ -2500,8 +2603,14 @@ delegate_declaration
          SEMICOLON
          {
                MemberName name = MakeName ((MemberName) $5);
+               Parameters p = (Parameters) $7;
+               if (p.HasArglist) {
+                       // TODO: wrong location
+                       Report.Error (1669, name.Location, "__arglist is not valid in this context");
+               }
+
                Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
-                                            (int) $2, name, (Parameters) $7, (Attributes) $1);
+                                            (int) $2, name, p, (Attributes) $1);
 
                if (RootContext.Documentation != null) {
                        del.DocComment = Lexer.consume_doc_comment ();
@@ -2509,7 +2618,6 @@ delegate_declaration
                }
 
                current_container.AddDelegate (del);
-               RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
                $$ = del;
          }     
        ;
@@ -2650,7 +2758,7 @@ integral_type
 array_type
        : type rank_specifiers
          {
-               $$ = new ComposedCast ((Expression) $1, (string) $2);
+               $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
          }
        ;
 
@@ -2662,7 +2770,6 @@ primary_expression
          {
                // 7.5.1: Literals
          }
        | member_name
          {
                MemberName mn = (MemberName) $1;
@@ -2732,18 +2839,23 @@ parenthesized_expression_0
        : OPEN_PARENS expression CLOSE_PARENS
          {
                $$ = $2;
-               lexer.Deambiguate_CloseParens ();
+               lexer.Deambiguate_CloseParens ($$);
                // After this, the next token returned is one of
-               // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
+               // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
                // or CLOSE_PARENS_MINUS.
          }
+       | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
        ;
 
 parenthesized_expression
        : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
          {
                $$ = $1;
-         }
+         }  
+       | parenthesized_expression_0 CLOSE_PARENS
+         {
+               $$ = $1;
+         }       
        | parenthesized_expression_0 CLOSE_PARENS_MINUS
          {
                // If a parenthesized expression is followed by a minus, we need to wrap
@@ -2751,17 +2863,18 @@ parenthesized_expression
                // in Binary.DoResolve().
                $$ = new ParenthesizedExpression ((Expression) $1);
          }
-       ;;
+       ;
 
 member_access
        : primary_expression DOT IDENTIFIER
          {
                LocatedToken lt = (LocatedToken) $3;
-               $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
+               $$ = new MemberAccess ((Expression) $1, lt.Value);
          }
        | predefined_type DOT IDENTIFIER
          {
                LocatedToken lt = (LocatedToken) $3;
+               // TODO: Location is wrong as some predefined types doesn't hold a location
                $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
          }
        ;
@@ -2771,15 +2884,12 @@ predefined_type
        ;
 
 invocation_expression
-       : primary_expression {
-               $$ = lexer.Location;
-         } OPEN_PARENS opt_argument_list CLOSE_PARENS
+       : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
          {
-               if ($1 == null) {
-                       Location l = (Location) $3;
-                       Report.Error (1, l, "Parse error");
-               }
-               $$ = new Invocation ((Expression) $1, (ArrayList) $4);
+               if ($1 == null)
+                       Report.Error (1, (Location) $2, "Parse error");
+               else
+                       $$ = new Invocation ((Expression) $1, (ArrayList) $3);
          }
        | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
          {
@@ -2856,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);
@@ -2893,6 +3007,7 @@ element_access
                        // 
                        $$ = new ComposedCast (expr, (string) $2);
                }
+               current_array_type = (Expression)$$;
          }
        ;
 
@@ -3071,13 +3186,18 @@ variable_initializer_list
        ;
 
 typeof_expression
-       : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
-         {
-               $$ = new TypeOfVoid ((Location) $1);
+       : TYPEOF
+      {
+               pushed_current_array_type = current_array_type;
          }
-       | TYPEOF OPEN_PARENS type CLOSE_PARENS
+         OPEN_PARENS type CLOSE_PARENS
          {
-               $$ = new TypeOf ((Expression) $3, (Location) $1);
+               Expression type = (Expression)$4;
+               if (type == TypeManager.system_void_expr)
+                       $$ = new TypeOfVoid ((Location) $1);
+               else
+                       $$ = new TypeOf (type, (Location) $1);
+               current_array_type = pushed_current_array_type;
          }
        ;
 
@@ -3108,37 +3228,18 @@ pointer_member_access
                LocatedToken lt = (LocatedToken) $3;
 
                deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
-               $$ = new MemberAccess (deref, lt.Value, lt.Location);
+               $$ = new MemberAccess (deref, lt.Value);
          }
        ;
 
 anonymous_method_expression
        : DELEGATE opt_anonymous_method_signature
          {
-               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);
-               current_block = null;
-         } 
+               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.FeatureIsNotStandardized (loc, "anonymous methods");
-                       $$ = null;
-               } else  {
-                       ToplevelBlock anon_block = (ToplevelBlock) $4;
-
-                       anon_block.Parent = current_block;
-                       $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
-                               anon_block, loc);
-               }
-               current_local_parameters = (Parameters) oob_stack.Pop ();
+               $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
          }
        ;
 
@@ -3148,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;
@@ -3478,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
@@ -3503,28 +3709,9 @@ class_declaration
                MemberName name = MakeName ((MemberName) $5);
                int mod_flags = (int) $2;
 
-               if ($3 != null) {
-                       ClassPart part = PartialContainer.CreatePart (
-                               current_namespace, current_class, name, mod_flags,
-                               (Attributes) $1, Kind.Class, (Location) $3);
-
-                       current_container = part.PartialContainer;
-                       current_class = part;
-               } else {
-                       if ((mod_flags & Modifiers.STATIC) != 0) {
-                               current_class = new StaticClass (
-                                       current_namespace, current_class, name,
-                                       mod_flags, (Attributes) $1);
-                       } else {
-                               current_class = new Class (
-                                       current_namespace, current_class, name,
-                                       mod_flags, (Attributes) $1);
-                       }
-
-                       current_container.AddClassOrStruct (current_class);
-                       current_container = current_class;
-                       RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
-               }
+               push_current_class (new Class (
+                       current_namespace, current_class, name,
+                       mod_flags, (Attributes) $1), $3);
          }
          opt_class_base
          {
@@ -3534,11 +3721,11 @@ class_declaration
                                              "The class System.Object cannot have a base " +
                                              "class or implement an interface.");
                        }
-                       current_class.Bases = (ArrayList) $7;
+                       current_container.AddBasesForPart (current_class, (ArrayList) $7);
                }
 
                if (RootContext.Documentation != null) {
-                       current_class.DocComment = Lexer.consume_doc_comment ();
+                       current_container.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
@@ -3564,7 +3751,7 @@ opt_modifiers
        : /* empty */           { $$ = (int) 0; }
        | modifiers
        ;
-
+       
 modifiers
        : modifier
        | modifiers modifier
@@ -3578,7 +3765,7 @@ modifiers
                }
                $$ = (int) (m1 | m2);
          }
-        ;
+      ;
 
 modifier
        : NEW                   { $$ = Modifiers.NEW; }
@@ -3622,22 +3809,19 @@ class_base
 block
        : OPEN_BRACE 
          {
-               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);
          }
        ;
 
@@ -3709,7 +3893,7 @@ labeled_statement
                LocatedToken lt = (LocatedToken) $1;
                LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
 
-               if (current_block.AddLabel (lt.Value, labeled, lt.Location))
+               if (current_block.AddLabel (labeled))
                        current_block.AddStatement (labeled);
          }
          statement
@@ -3718,6 +3902,7 @@ labeled_statement
 declaration_statement
        : local_variable_declaration SEMICOLON
          {
+               current_array_type = null;
                if ($1 != null){
                        DictionaryEntry de = (DictionaryEntry) $1;
                        Expression e = (Expression) de.Key;
@@ -3728,6 +3913,7 @@ declaration_statement
 
        | local_constant_declaration SEMICOLON
          {
+               current_array_type = null;
                if ($1 != null){
                        DictionaryEntry de = (DictionaryEntry) $1;
 
@@ -3782,7 +3968,7 @@ local_variable_type
                if ((string) $2 == "")
                        $$ = $1;
                else
-                       $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+                       $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
          }
         ;
 
@@ -3846,10 +4032,7 @@ local_constant_declaration
        ;
 
 expression_statement
-       : statement_expression SEMICOLON
-         {
-               $$ = $1;
-         }
+       : statement_expression SEMICOLON { $$ = $1; }
        ;
 
        //
@@ -3857,14 +4040,18 @@ expression_statement
        // because statement_expression is used for example in for_statement
        //
 statement_expression
-       : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | error {
+       : expression
+         {
+               Expression expr = (Expression) $1;
+               ExpressionStatement s = expr as ExpressionStatement;
+               if (s == null) {
+                       expr.Error_InvalidExpressionStatement ();
+                       $$ = null;
+               }
+               $$ = new StatementExpression (s);
+         }
+       | error
+         {
                Report.Error (1002, GetLocation ($1), "Expecting `;'");
                $$ = null;
          }
@@ -3911,6 +4098,8 @@ if_statement
 switch_statement
        : SWITCH OPEN_PARENS
          { 
+               if (switch_stack == null)
+                       switch_stack = new Stack (2);
                switch_stack.Push (current_block);
          }
          expression CLOSE_PARENS 
@@ -3933,7 +4122,8 @@ switch_block
 opt_switch_sections
        : /* empty */           
           {
-               Report.Error (1522, lexer.Location, "Empty switch block"); 
+               Report.Warning (1522, 1, lexer.Location, "Empty switch block"); 
+               $$ = new ArrayList ();
          }
        | switch_sections
        ;
@@ -3962,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);
          }
        ;
 
@@ -4023,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;
@@ -4043,16 +4230,7 @@ for_statement
                                if (vi == null)
                                        continue;
 
-                               Location l = lexer.Location;
-                               Expression expr;
-                               if (decl.expression_or_array_initializer is Expression){
-                                       expr = (Expression) decl.expression_or_array_initializer;
-                               } else if (decl.expression_or_array_initializer == null) {
-                                       expr = null;
-                               } else {
-                                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                                       expr = new ArrayCreation (type, "", init, decl.Location);
-                               }
+                               Expression expr = decl.expression_or_array_initializer;
                                        
                                LocalVariableReference var;
                                var = new LocalVariableReference (assign_block, decl.identifier, l);
@@ -4080,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);
          }
        ;
 
@@ -4116,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;
@@ -4131,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;
@@ -4169,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);
          }
        ;
 
@@ -4239,14 +4412,14 @@ yield_statement
                        $$ = null;
                }
                if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotStandardized (lt.Location, "yield statement");
+                       Report.FeatureIsNotISO1 (lt.Location, "yield statement");
                        $$ = null;
                }
-               if (iterator_container == null){
+               if (anonymous_host == null){
                        Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
                        $$ = null;
                } else {
-                       iterator_container.SetYields ();
+                       anonymous_host.SetYields ();
                        $$ = new Yield ((Expression) $3, lt.Location); 
                }
          }
@@ -4264,14 +4437,14 @@ yield_statement
                        $$ = null;
                }
                if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotStandardized (lt.Location, "yield statement");
+                       Report.FeatureIsNotISO1 (lt.Location, "yield statement");
                        $$ = null;
                }
-               if (iterator_container == null){
+               if (anonymous_host == null){
                        Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
                        $$ = null;
                } else {
-                       iterator_container.SetYields ();
+                       anonymous_host.SetYields ();
                        $$ = new YieldBreak (lt.Location);
                }
          }
@@ -4370,14 +4543,14 @@ 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 {
                Expression type = null;
                string id = null;
+               Block var_block = null;
 
                if ($2 != null){
                        DictionaryEntry cc = (DictionaryEntry) $2;
@@ -4386,13 +4559,11 @@ catch_clause
 
                        if (lt != null){
                                id = lt.Value;
-                               while (current_block.Implicit)
-                                       current_block = current_block.Parent;
-                               current_block = current_block.Parent;
+                               var_block = end_block (lexer.Location);
                        }
                }
 
-               $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
+               $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
          }
         ;
 
@@ -4402,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);
          }
@@ -4433,7 +4604,7 @@ unsafe_statement
        ;
 
 fixed_statement
-       : FIXED OPEN_PARENS 
+       : FIXED open_parens 
          type fixed_pointer_declarators 
          CLOSE_PARENS
          {
@@ -4442,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];
@@ -4466,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);
          }
        ;
 
@@ -4515,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;
@@ -4536,16 +4705,9 @@ using_statement
                                        continue;
                                vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
 
-                               Expression expr;
-                               if (decl.expression_or_array_initializer is Expression){
-                                       expr = (Expression) decl.expression_or_array_initializer;
-                               } else {
-                                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                                       if (init == null) {
-                                               Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
-                                       }
-                                       
-                                       expr = new ArrayCreation (type, "", init, decl.Location);
+                               Expression expr = decl.expression_or_array_initializer;
+                               if (expr == null) {
+                                       Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
                                }
 
                                LocalVariableReference var;
@@ -4573,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);
          }
        ; 
 
@@ -4592,7 +4751,7 @@ resource_acquisition
 // </summary>
 public class VariableDeclaration {
        public string identifier;
-       public object expression_or_array_initializer;
+       public Expression expression_or_array_initializer;
        public Location Location;
        public Attributes OptAttributes;
        public string DocComment;
@@ -4600,7 +4759,14 @@ public class VariableDeclaration {
        public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
        {
                this.identifier = lt.Value;
-               this.expression_or_array_initializer = eoai;
+               if (eoai is ArrayList) {
+                       if (CSharpParser.current_array_type == null)
+                               Report.Error (622, lt.Location,
+                                       "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
+                       this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
+               } else {
+                       this.expression_or_array_initializer = (Expression)eoai;
+               }
                this.Location = lt.Location;
                this.OptAttributes = opt_attrs;
        }
@@ -4630,22 +4796,41 @@ public class IndexerDeclaration {
 }
 
 //
-// We use this when we do not have an object in advance that is an IIteratorContainer
+// We use this when we do not have an object in advance that is an IAnonymousHost
 //
-public class SimpleIteratorContainer : IIteratorContainer {
-       public bool Yields;
+public class SimpleAnonymousHost : IAnonymousHost {
+       public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
 
-       public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
+       bool yields;
+       ArrayList anonymous_methods;
 
-       //
-       // Reset and return
-       //
-       public static SimpleIteratorContainer GetSimple () { 
-               Simple.Yields = false;
+       public static SimpleAnonymousHost GetSimple () {
+               Simple.yields = false;
+               Simple.anonymous_methods = null;
                return Simple;
        }
 
-       public void SetYields () { Yields = true; } 
+       public void SetYields ()
+       {
+               yields = true;
+       }
+
+       public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
+       {
+               if (anonymous_methods == null)
+                       anonymous_methods = new ArrayList ();
+               anonymous_methods.Add (anonymous);
+       }
+
+       public void Propagate (IAnonymousHost real_host)
+       {
+               if (yields)
+                       real_host.SetYields ();
+               if (anonymous_methods != null) {
+                       foreach (AnonymousMethodExpression ame in anonymous_methods)
+                               real_host.AddAnonymousMethod (ame);
+               }
+       }
 }
 
 // <summary>
@@ -4677,23 +4862,36 @@ 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 ();
        }
 }
 
-TypeContainer pop_current_class ()
+public static void Error_ParameterModifierNotValid (Location loc)
 {
-       TypeContainer retval = current_class;
+       Report.Error (631, loc, "The modifiers `ref' and `out' are not valid in this context");
+}
+
+static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
+{
+       Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
+               Parameter.GetModifierSignature (mod));
+}
+
+void push_current_class (TypeContainer tc, object partial_token)
+{
+       if (partial_token != null)
+               current_container = current_container.AddPartial (tc);
+       else
+               current_container = current_container.AddTypeContainer (tc);
+       current_class = tc;
+}
+
+DeclSpace pop_current_class ()
+{
+       DeclSpace retval = current_class;
 
        current_class = current_class.Parent;
-       current_container = current_container.Parent;
-       
-       if (current_class != current_container) {
-               if (!(current_class is ClassPart) ||
-                   ((ClassPart) current_class).PartialContainer != current_container)
-                       throw new InternalErrorException ();
-       } else if (current_container is ClassPart)
-               current_container = ((ClassPart) current_class).PartialContainer;
+       current_container = current_class.PartialContainer;
 
        return retval;
 }
@@ -4707,8 +4905,8 @@ MakeName (MemberName class_name)
 {
        Namespace ns = current_namespace.NS;
 
-       if (current_container.Name == ""){
-               if (ns.Name != "")
+       if (current_container.Name.Length == 0){
+               if (ns.Name.Length != 0)
                        return new MemberName (ns.MemberName, class_name);
                else
                        return class_name;
@@ -4738,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;
 
@@ -4758,17 +4956,8 @@ Block declare_local_variables (Expression type, ArrayList variable_declarators,
 
        foreach (VariableDeclaration decl in inits){
                Assign assign;
-               Expression expr;
+               Expression expr = decl.expression_or_array_initializer;
                
-               if (decl.expression_or_array_initializer is Expression){
-                       expr = (Expression) decl.expression_or_array_initializer;
-
-               } else {
-                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                       
-                       expr = new ArrayCreation (type, "", init, decl.Location);
-               }
-
                LocalVariableReference var;
                var = new LocalVariableReference (implicit_block, decl.identifier, loc);
 
@@ -4785,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;
 
@@ -4796,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)
@@ -4879,23 +5066,25 @@ public Tokenizer Lexer {
        }
 }                 
 
+static CSharpParser ()
+{
+       oob_stack = new Stack ();
+}
+
 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
 {
-       current_namespace = new NamespaceEntry (null, file, null, Location.Null);
        this.name = file.Name;
        this.file = file;
-       current_container = RootContext.Tree.Types;
-       // TODO: Make RootContext.Tree.Types a PartialContainer.
-       current_class = current_container;
-       current_container.NamespaceEntry = current_namespace;
-       oob_stack = new Stack ();
-       switch_stack = new Stack ();
-
+       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);
 }
 
 public void parse ()
 {
+       int errors = Report.Errors;
        try {
                if (yacc_verbose_flag > 1)
                        yyparse (lexer, new yydebug.yyDebugSimple ());
@@ -4908,21 +5097,22 @@ public void parse ()
                // Removed for production use, use parser verbose to get the output.
                //
                // Console.WriteLine (e);
-               Report.Error (-25, lexer.Location, "Parsing error");
+               if (Report.Errors == errors)
+                       Report.Error (-25, lexer.Location, "Parsing error");
                if (yacc_verbose_flag > 0)
                        Console.WriteLine (e);
        }
 
-       RootContext.Tree.Types.NamespaceEntry = null;
+       if (RootContext.ToplevelTypes.NamespaceEntry != null)
+               throw new InternalErrorException ("who set it?");
 }
 
-void CheckToken (int error, int yyToken, string msg, Location loc)
+static void CheckToken (int error, int yyToken, string msg, Location loc)
 {
-       if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
-               Report.Error (error, loc, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
-               return;
-       }               
-       Report.Error (error, loc, msg);
+       if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
+               Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
+       else
+               Report.Error (error, loc, msg);
 }
 
 void CheckIdentifierToken (int yyToken, Location loc)
@@ -4951,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 */
 }