[672969] Delay type parameter constrain import until its declaring type is ready
[mono.git] / mcs / mcs / cs-parser.jay
index 65de9589b80b4014aef0ed3f62f7636f7e38b88f..dd7fac0a49d8d3ca06b396a9e1f566984702f2b4 100644 (file)
@@ -49,12 +49,16 @@ namespace Mono.CSharp
                DeclSpace       current_class;
                PropertyBase current_property;
                EventProperty current_event;
+               EventField current_event_field;
+               FieldBase current_field;
        
                /// <summary>
                ///   Current block is used to add statements as we find
                ///   them.  
                /// </summary>
                Block      current_block;
+               
+               BlockVariableDeclaration current_variable;
 
                Delegate   current_delegate;
                
@@ -76,11 +80,6 @@ namespace Mono.CSharp
                ///
                static Stack<object> oob_stack;
 
-               ///
-               /// Switch stack.
-               ///
-               Stack<Block> switch_stack;
-
                ///
                /// Controls the verbosity of the errors produced by the parser
                ///
@@ -116,7 +115,7 @@ namespace Mono.CSharp
 
                /// When using the interactive parser, this holds the
                /// resulting expression
-               public object InteractiveResult;
+               public Class InteractiveResult;
 
                //
                // Keeps track of global data changes to undo on parser error
@@ -125,10 +124,12 @@ namespace Mono.CSharp
                
                Stack<Linq.QueryBlock> linq_clause_blocks;
 
-               // A counter to create new class names in interactive mode
-               static int class_count;
+               ModuleContainer module;
                
-               CompilerContext compiler;
+               readonly CompilerContext compiler;
+               readonly LanguageVersion lang_version;
+               readonly bool doc_support;
+               readonly CompilerSettings settings;
                
                //
                // Instead of allocating carrier array everytime we
@@ -136,7 +137,6 @@ namespace Mono.CSharp
                // be recursive
                //
                static List<Parameter> parameters_bucket = new List<Parameter> (6);
-               static List<object> variables_bucket = new List<object> (6);
                
                //
                // Full AST support members
@@ -252,11 +252,7 @@ namespace Mono.CSharp
 %token INTO
 %token INTERR_NULLABLE
 %token EXTERN_ALIAS
-
-/* Generics <,> tokens */
-%token OP_GENERICS_LT
-%token OP_GENERICS_LT_DECL
-%token OP_GENERICS_GT
+%token ASYNC
 
 /* C# keywords which are not really keywords */
 %token GET
@@ -317,6 +313,11 @@ namespace Mono.CSharp
 %token OP_PTR
 %token OP_COALESCING
 
+/* Generics <,> tokens */
+%token OP_GENERICS_LT
+%token OP_GENERICS_LT_DECL
+%token OP_GENERICS_GT
+
 %token LITERAL
 
 %token IDENTIFIER
@@ -408,8 +409,8 @@ extern_alias_directive
                string s = lt.Value;
                if (s != "alias"){
                        syntax_error (lt.Location, "`alias' expected");
-               } else if (RootContext.Version == LanguageVersion.ISO_1) {
-                       Report.FeatureIsNotAvailable (lt.Location, "external alias");
+               } else if (lang_version == LanguageVersion.ISO_1) {
+                       FeatureIsNotAvailable (lt.Location, "external alias");
                } else {
                        lt = (Tokenizer.LocatedToken) $3; 
                        current_namespace.AddUsingExternalAlias (lt.Value, lt.Location, Report);
@@ -429,12 +430,12 @@ using_directives
 using_directive
        : using_alias_directive
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
        | using_namespace_directive
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
        ;
@@ -443,10 +444,16 @@ using_alias_directive
        : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
          {
                var lt = (Tokenizer.LocatedToken) $2;
+               if (lang_version != LanguageVersion.ISO_1 && lt.Value == "global") {
+                       Report.Warning (440, 2, lt.Location,
+                        "An alias named `global' will not be used when resolving `global::'. The global namespace will be used instead");
+               }
+
                current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, GetLocation ($1));
          }
-       | USING error {
-               CheckIdentifierToken (yyToken, GetLocation ($2));
+       | USING error
+        {
+               Error_SyntaxError (yyToken);
                $$ = null;
          }
        ;
@@ -472,7 +479,7 @@ namespace_declaration
                        Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
                }
 
-               current_namespace = new NamespaceEntry (
+               current_namespace = new NamespaceEntry (module,
                        current_namespace, file, name.GetName ());
                current_class = current_namespace.SlaveDeclSpace;
                current_container = current_class.PartialContainer;
@@ -498,7 +505,7 @@ qualified_identifier
          }
        | error
          {
-               syntax_error (lexer.Location, "`.' expected");
+               Error_SyntaxError (yyToken);
                $$ = new MemberName ("<invalid>", lexer.Location);
          }
        ;
@@ -528,29 +535,13 @@ namespace_name
 namespace_body
        : OPEN_BRACE
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
-         namespace_body_body
-       ;
-       
-namespace_body_body
-       : opt_extern_alias_directives
+         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
@@ -586,16 +577,14 @@ namespace_member_declaration
                }
                current_namespace.DeclarationFound = true;
          }
-       | namespace_declaration {
+       | namespace_declaration
+         {
                current_namespace.DeclarationFound = true;
          }
-
-       | field_declaration {
-               Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
-         }
-       | method_declaration {
-               Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
-         }
+       | error
+        {
+               Error_SyntaxError (yyToken);
+        }
        ;
 
 type_declaration
@@ -623,7 +612,7 @@ global_attributes
                if ($1 != null) {
                        Attributes attrs = (Attributes)$1;
                        if (global_attrs_enabled) {
-                               CodeGen.Assembly.AddAttributes (attrs.Attrs, current_namespace);
+                               module.AddAttributes (attrs.Attrs, current_namespace);
                        } else {
                                foreach (Attribute a in attrs.Attrs) {
                                        Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
@@ -655,17 +644,14 @@ attribute_sections
                        var sect = (List<Attribute>) $1;
 
                        if (global_attrs_enabled) {
-                               if (current_attr_target == "module") {
-                                       current_container.Module.Compiled.AddAttributes (sect);
-                                       $$ = null;
-                               } else if (current_attr_target != null && current_attr_target.Length > 0) {
-                                       CodeGen.Assembly.AddAttributes (sect, current_namespace);
+                               if (!string.IsNullOrEmpty (current_attr_target)) {
+                                       module.AddAttributes (sect, current_namespace);
                                        $$ = null;
                                } else {
                                        $$ = new Attributes (sect);
                                }
                                if ($$ == null) {
-                                       if (RootContext.Documentation != null) {
+                                       if (doc_support) {
                                                Lexer.check_incorrect_doc_comment ();
                                                Lexer.doc_state =
                                                        XmlCommentState.Allowed;
@@ -686,11 +672,8 @@ attribute_sections
                        var sect = (List<Attribute>) $2;
 
                        if (global_attrs_enabled) {
-                               if (current_attr_target == "module") {
-                                       current_container.Module.Compiled.AddAttributes (sect);
-                                       $$ = null;
-                               } else if (current_attr_target == "assembly") {
-                                       CodeGen.Assembly.AddAttributes (sect, current_namespace);
+                               if (!string.IsNullOrEmpty (current_attr_target)) {
+                                       module.AddAttributes (sect);
                                        $$ = null;
                                } else {
                                        if (attrs == null)
@@ -782,9 +765,9 @@ attribute
                else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
                        // FIXME: supply "nameEscaped" parameter here.
                        $$ = new GlobalAttribute (current_namespace, current_attr_target,
-                                                 expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
+                                                 expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname));
                else
-                       $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
+                       $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname));
          }
        ;
 
@@ -864,8 +847,8 @@ named_attribute_argument
 named_argument
        : IDENTIFIER COLON opt_named_modifier expression
          {
-               if (RootContext.Version <= LanguageVersion.V_3)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
+               if (lang_version <= LanguageVersion.V_3)
+                       FeatureIsNotAvailable (GetLocation ($1), "named argument");
                        
                // Avoid boxing in common case (no modifier)
                var arg_mod = $3 == null ? Argument.AType.None : (Argument.AType) $3;
@@ -938,31 +921,38 @@ struct_declaration
 
                current_class.SetParameterInfo ((List<Constraints>) $9);
 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        current_container.DocComment = Lexer.consume_doc_comment ();
+
+               lbag.AddMember (current_class, mod_locations, GetLocation ($4));
          }
          struct_body
          {
                --lexer.parsing_declaration;      
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
          opt_semicolon
          {
+               lbag.AppendToMember (current_class, GetLocation ($13));
                $$ = pop_current_class ();
          }
-       | opt_attributes opt_modifiers opt_partial STRUCT error {
-               CheckIdentifierToken (yyToken, GetLocation ($5));
+       | opt_attributes opt_modifiers opt_partial STRUCT error
+         {
+               Error_SyntaxError (yyToken);
          }
        ;
 
 struct_body
        : OPEN_BRACE
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
          opt_struct_member_declarations CLOSE_BRACE
+         {
+               lbag.AppendToMember (current_class, GetLocation ($1), GetLocation ($4));
+         }
        ;
 
 opt_struct_member_declarations
@@ -992,75 +982,79 @@ struct_member_declaration
         */
        | destructor_declaration 
        ;
-
+       
 constant_declaration
        : opt_attributes 
          opt_modifiers
-         CONST
-         type
-         constant_declarators
-         SEMICOLON
+         CONST type IDENTIFIER
          {
-               var modflags = (Modifiers) $2;
-               foreach (VariableDeclaration constant in (List<object>) $5){
-                       Location l = constant.Location;
-                       if ((modflags & Modifiers.STATIC) != 0) {
-                               Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + "." + (string) constant.identifier);
-                               continue;
-                       }
-
-                       Const c = new Const (
-                               current_class, (FullNamedExpression) $4, (string) constant.identifier, 
-                               constant.GetInitializer ((FullNamedExpression) $4), modflags, 
-                               (Attributes) $1, l);
-
-                       if (RootContext.Documentation != null) {
-                               c.DocComment = Lexer.consume_doc_comment ();
-                               Lexer.doc_state = XmlCommentState.Allowed;
-                       }
-                       
-                       current_container.AddConstant (c);
-                       lbag.AddMember (c, mod_locations, GetLocation ($3), GetLocation ($6));
+               var lt = (Tokenizer.LocatedToken) $5;
+               var mod = (Modifiers) $2;
+               current_field = new Const (current_class, (FullNamedExpression) $4, mod, new MemberName (lt.Value, lt.Location), (Attributes) $1);
+               current_container.AddConstant ((Const) current_field);
+               
+               if ((mod & Modifiers.STATIC) != 0) {
+                       Report.Error (504, current_field.Location, "The constant `{0}' cannot be marked static", current_field.GetSignatureForError ());
+               }
+               
+               $$ = current_field;
+         }
+         constant_initializer opt_constant_declarators SEMICOLON
+         {
+               if (doc_support) {
+                       current_field.DocComment = Lexer.consume_doc_comment ();
+                       Lexer.doc_state = XmlCommentState.Allowed;
                }
+               
+               current_field.Initializer = (ConstInitializer) $7;
+               lbag.AddMember (current_field, mod_locations, GetLocation ($3), GetLocation ($9));
+               current_field = null;
          }
        ;
-
+       
+opt_constant_declarators
+       : /* empty */
+       | constant_declarators
+       ;
+       
 constant_declarators
-       : constant_declarator 
+       : constant_declarator
          {
-               variables_bucket.Clear ();
-               if ($1 != null)
-                       variables_bucket.Add ($1);
-               $$ = variables_bucket;
+               current_field.AddDeclarator ((FieldDeclarator) $1);
          }
-       | constant_declarators COMMA constant_declarator
+       | constant_declarators constant_declarator
          {
-               if ($3 != null) {
-                       var constants = (List<object>) $1;
-                       constants.Add ($3);
-               }
+               current_field.AddDeclarator ((FieldDeclarator) $2);
          }
        ;
-
+       
 constant_declarator
-       : IDENTIFIER ASSIGN
+       : COMMA IDENTIFIER constant_initializer
          {
-               ++lexer.parsing_block;
-         }     
-         constant_initializer
+               var lt = (Tokenizer.LocatedToken) $2;
+               $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (ConstInitializer) $3);
+               lbag.AddLocation ($$, GetLocation ($1));
+         }
+       ;               
+
+constant_initializer
+       : ASSIGN
          {
-               --lexer.parsing_block;
-               $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, (Expression) $4);
+               ++lexer.parsing_block;
          }
-       | IDENTIFIER
+         constant_initializer_expr
          {
-               // A const field requires a value to be provided
-               Report.Error (145, GetLocation ($1), "A const field requires a value to be provided");
-               $$ = null;
+               --lexer.parsing_block;
+               $$ = new ConstInitializer (current_field, (Expression) $3, GetLocation ($1));
          }
+       | error
+         {
+               Report.Error (145, lexer.Location, "A const field requires a value to be provided");
+               $$ = null;
+         }       
        ;
        
-constant_initializer
+constant_initializer_expr
        : constant_expression
        | array_initializer
        ;
@@ -1068,211 +1062,186 @@ constant_initializer
 field_declaration
        : opt_attributes
          opt_modifiers
-         member_type
-         variable_declarators
-         SEMICOLON
-         { 
+         member_type IDENTIFIER
+         {
+               lexer.parsing_generic_declaration = false;
+
                FullNamedExpression type = (FullNamedExpression) $3;
                if (type.Type == TypeManager.void_type)
                        Report.Error (670, GetLocation ($3), "Fields cannot have void type");
-               
-               var mod = (Modifiers) $2;
-
-               foreach (VariableMemberDeclaration var in (List<object>) $4){
-                       Field field = new Field (current_class, type, mod, var.MemberName, (Attributes) $1);
-
-                       field.Initializer = var.GetInitializer (type);
-
-                       if (RootContext.Documentation != null) {
-                               field.DocComment = Lexer.consume_doc_comment ();
-                               Lexer.doc_state = XmlCommentState.Allowed;
-                       }
-                       
-                       current_container.AddField (field);
-                       lbag.AddMember (field, mod_locations, GetLocation ($5));
                        
-                       $$ = field; // FIXME: might be better if it points to the top item
+               var lt = (Tokenizer.LocatedToken) $4;
+               current_field = new Field (current_class, type, (Modifiers) $2, new MemberName (lt.Value, lt.Location), (Attributes) $1);
+               current_container.AddField (current_field);
+               $$ = current_field;
+         }
+         opt_field_initializer
+         opt_field_declarators
+         SEMICOLON
+         { 
+               if (doc_support) {
+                       current_field.DocComment = Lexer.consume_doc_comment ();
+                       Lexer.doc_state = XmlCommentState.Allowed;
                }
+                       
+               lbag.AddMember (current_field, mod_locations, GetLocation ($8));
+               $$ = current_field;
+               current_field = null;
          }
        | opt_attributes
          opt_modifiers
-         FIXED simple_type
-         fixed_variable_declarators
-         SEMICOLON
+         FIXED simple_type IDENTIFIER
          { 
-               FullNamedExpression type = (FullNamedExpression) $4;
-               var mod = (Modifiers) $2;
-                       
-               if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($3), "fixed size buffers");          
-
-               foreach (VariableDeclaration var in (List<VariableDeclaration>) $5) {
-                       FixedField field = new FixedField (current_class, type, mod, var.identifier,
-                               var.GetInitializer (type), (Attributes) $1, var.Location);
-                                       
-                       if (RootContext.Documentation != null) {
-                               field.DocComment = Lexer.consume_doc_comment ();
-                               Lexer.doc_state = XmlCommentState.Allowed;
-                       }
-                       
-                       current_container.AddField (field);
-                       lbag.AddMember (field, mod_locations, GetLocation ($3), GetLocation ($6));
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($3), "fixed size buffers");
+
+               var lt = (Tokenizer.LocatedToken) $5;
+               current_field = new FixedField (current_class, (FullNamedExpression) $4, (Modifiers) $2,
+                       new MemberName (lt.Value, lt.Location), (Attributes) $1);
                        
-                       $$ = field; // FIXME: might be better if it points to the top item
-               }
+               current_container.AddField (current_field);
+         }
+         fixed_field_size opt_fixed_field_declarators SEMICOLON
+         {
+               if (doc_support) {
+                       current_field.DocComment = Lexer.consume_doc_comment ();
+                       Lexer.doc_state = XmlCommentState.Allowed;
+           }
+
+               current_field.Initializer = (ConstInitializer) $7;          
+               lbag.AddMember (current_field, mod_locations, GetLocation ($9));
+               $$ = current_field;
+           current_field = null;
          }
        | opt_attributes
          opt_modifiers
-         FIXED simple_type
-         error
+         FIXED simple_type error
          SEMICOLON
          {
                Report.Error (1641, GetLocation ($5), "A fixed size buffer field must have the array size specifier after the field name");
          }
        ;
-
-fixed_variable_declarators
-       : fixed_variable_declarator
-         {
-               var decl = new List<VariableDeclaration> (2);
-               decl.Add ((VariableDeclaration)$1);
-               $$ = decl;
-         }
-       | fixed_variable_declarators COMMA fixed_variable_declarator
-         {
-               var decls = (List<VariableDeclaration>) $1;
-               decls.Add ((VariableDeclaration)$3);
-               $$ = $1;
-         }
-       ;
-
-fixed_variable_declarator
-       : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
+       
+opt_field_initializer
+       : /* empty */
+       | ASSIGN
          {
-               $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, (Expression) $3);
+               ++lexer.parsing_block;
+               current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
+               start_block (GetLocation ($1));
          }
-       | IDENTIFIER OPEN_BRACKET error
+         variable_initializer
          {
-               Report.Error (443, lexer.Location, "Value or constant expected");
-               $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, null);
+               --lexer.parsing_block;
+               current_field.Initializer = (Expression) $3;
+               end_block (lexer.Location);
+               current_local_parameters = null;
          }
        ;
        
+opt_field_declarators
+       : /* empty */
+       | field_declarators
+       ;
        
-local_variable_declarators     
-       : local_variable_declarator 
+field_declarators
+       : field_declarator
          {
-               variables_bucket.Clear ();
-               if ($1 != null)
-                       variables_bucket.Add ($1);
-               $$ = variables_bucket;
+               current_field.AddDeclarator ((FieldDeclarator) $1);
          }
-       | local_variable_declarators COMMA local_variable_declarator
+       | field_declarators field_declarator
          {
-               var decls = (List<object>) $1;
-               decls.Add ($3);
-               lbag.AppendTo ($3, GetLocation ($2));
-               $$ = $1;
+               current_field.AddDeclarator ((FieldDeclarator) $2);
          }
        ;
        
-local_variable_declarator
-       : IDENTIFIER ASSIGN local_variable_initializer
+field_declarator
+       : COMMA IDENTIFIER
          {
-               $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, (Expression) $3);
-               lbag.AddLocation ($$, GetLocation ($2));
+               var lt = (Tokenizer.LocatedToken) $2;
+               $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null);
+               lbag.AddLocation ($$, GetLocation ($1));
          }
-       | IDENTIFIER
+       | COMMA IDENTIFIER ASSIGN
          {
-               $$ = new VariableDeclaration ((Tokenizer.LocatedToken) $1, null);
+               ++lexer.parsing_block;
          }
-       | IDENTIFIER variable_bad_array
+         variable_initializer
          {
-               $$ = null;
+               --lexer.parsing_block;
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) $5);
+               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
          }
-       ;
+       ;       
 
-local_variable_initializer
-       : expression
-       | array_initializer
-       | STACKALLOC simple_type OPEN_BRACKET_EXPR expression CLOSE_BRACKET
-         {
-               $$ = new StackAlloc ((Expression) $2, (Expression) $4, GetLocation ($1));
-               lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
-         }
-       | ARGLIST
+opt_fixed_field_declarators
+       : /* empty */
+       | fixed_field_declarators
+       ;
+       
+fixed_field_declarators
+       : fixed_field_declarator
          {
-               $$ = new ArglistAccess (GetLocation ($1));
+               current_field.AddDeclarator ((FieldDeclarator) $1);
          }
-       | STACKALLOC simple_type
+       | fixed_field_declarators fixed_field_declarator
          {
-               Report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
-               $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));          
+               current_field.AddDeclarator ((FieldDeclarator) $2);
          }
        ;
-
-variable_declarators
-       : variable_declarator 
-         {
-               variables_bucket.Clear ();
-               if ($1 != null)
-                       variables_bucket.Add ($1);
-               $$ = variables_bucket;
-         }
-       | variable_declarators COMMA variable_declarator
+       
+fixed_field_declarator
+       : COMMA IDENTIFIER fixed_field_size
          {
-               var decls = (List<object>) $1;
-               decls.Add ($3);
-               $$ = $1;
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (ConstInitializer) $3);
+               lbag.AddLocation ($$, GetLocation ($1));
          }
        ;
 
-variable_declarator
-       : member_declaration_name ASSIGN
-         {
-               ++lexer.parsing_block;
-               lexer.parsing_generic_declaration = false;
-         }
-         variable_initializer
+fixed_field_size
+       : OPEN_BRACKET
          {
-               --lexer.parsing_block;
-               $$ = new VariableMemberDeclaration ((MemberName) $1, (Expression) $4);
+               ++lexer.parsing_block;
          }
-       | member_declaration_name
+         expression CLOSE_BRACKET
          {
-               lexer.parsing_generic_declaration = false;
-               $$ = new VariableMemberDeclaration ((MemberName) $1, null);
+               --lexer.parsing_block;
+               $$ = new ConstInitializer (current_field, (Expression) $3, GetLocation ($1));
+               lbag.AddLocation ($$, GetLocation ($4));
          }
-       | member_declaration_name variable_bad_array
+       | OPEN_BRACKET error
          {
-               lexer.parsing_generic_declaration = false;        
+               Report.Error (443, lexer.Location, "Value or constant expected");
                $$ = null;
-         }
-       ;
-       
-variable_bad_array
-       : OPEN_BRACKET_EXPR opt_expression CLOSE_BRACKET
-         {
-               Report.Error (650, GetLocation ($1), "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
-                       "To declare a fixed size buffer field, use the fixed keyword before the field type");
-         }
+         }       
        ;
 
 variable_initializer
        : expression
        | array_initializer
+       | error
+         {
+               // It has to be here for the parent to safely restore artificial block
+               Error_SyntaxError (yyToken);
+               $$ = null;
+         }
        ;
 
 method_declaration
-       : method_header {
-               if (RootContext.Documentation != null)
+       : method_header
+         {
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.NotAllowed;
+
+               // Add it early in the case of body being eof for full aot
+               current_container.AddMethod ((Method) $1);
          }
          method_body
          {
                Method method = (Method) $1;
                method.Block = (ToplevelBlock) $3;
-               current_container.AddMethod (method);
                
                if (current_container.Kind == MemberKind.Interface && method.Block != null) {
                        Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
@@ -1280,7 +1249,7 @@ method_declaration
 
                current_local_parameters = null;
 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
        ;
@@ -1324,7 +1293,7 @@ method_header
                                method.GetSignatureForError ());
                }
 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        method.DocComment = Lexer.consume_doc_comment ();
 
                lbag.AddMember (method, mod_locations, GetLocation ($5), GetLocation ($8));
@@ -1386,7 +1355,7 @@ method_header
                method = new Method (current_class, generic, new TypeExpression (TypeManager.void_type, GetLocation ($4)),
                                     modifiers, name, current_local_parameters, (Attributes) $1);
 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        method.DocComment = Lexer.consume_doc_comment ();
 
                // TODO: lbag, push void
@@ -1408,7 +1377,7 @@ method_header
 
                current_local_parameters = (ParametersCompiled) $7;
 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        method.DocComment = Lexer.consume_doc_comment ();
 
                $$ = method;
@@ -1427,29 +1396,29 @@ opt_formal_parameter_list
        
 formal_parameter_list
        : fixed_parameters
-         { 
+         {
                var pars_list = (List<Parameter>) $1;
-               $$ = new ParametersCompiled (compiler, pars_list.ToArray ());
+               $$ = new ParametersCompiled (pars_list.ToArray ());
          } 
        | fixed_parameters COMMA parameter_array
          {
                var pars_list = (List<Parameter>) $1;
                pars_list.Add ((Parameter) $3);
 
-               $$ = new ParametersCompiled (compiler, pars_list.ToArray ()); 
+               $$ = new ParametersCompiled (pars_list.ToArray ()); 
          }
        | fixed_parameters COMMA arglist_modifier
          {
                var pars_list = (List<Parameter>) $1;
                pars_list.Add (new ArglistParameter (GetLocation ($3)));
-               $$ = new ParametersCompiled (compiler, pars_list.ToArray (), true);
+               $$ = new ParametersCompiled (pars_list.ToArray (), true);
          }
        | 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");
 
-               $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );                    
+               $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );                      
          }
        | fixed_parameters COMMA parameter_array COMMA error
          {
@@ -1459,13 +1428,13 @@ formal_parameter_list
                var pars_list = (List<Parameter>) $1;
                pars_list.Add (new ArglistParameter (GetLocation ($3)));
 
-               $$ = new ParametersCompiled (compiler, pars_list.ToArray (), true);
+               $$ = new ParametersCompiled (pars_list.ToArray (), true);
          }
        | arglist_modifier COMMA error
          {
                Report.Error (257, GetLocation ($1), "An __arglist parameter must be the last parameter in a formal parameter list");
 
-               $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
+               $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
          }
        | fixed_parameters COMMA ARGLIST COMMA error 
          {
@@ -1474,15 +1443,20 @@ formal_parameter_list
                var pars_list = (List<Parameter>) $1;
                pars_list.Add (new ArglistParameter (GetLocation ($3)));
 
-               $$ = new ParametersCompiled (compiler, pars_list.ToArray (), true);
+               $$ = new ParametersCompiled (pars_list.ToArray (), true);
          }
        | parameter_array 
          {
-               $$ = new ParametersCompiled (compiler, new Parameter[] { (Parameter) $1 } );
+               $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );
          }
        | arglist_modifier
          {
-               $$ = new ParametersCompiled (compiler, new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
+               $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
+         }
+       | error
+         {
+               Error_SyntaxError (yyToken);
+               $$ = ParametersCompiled.EmptyReadOnlyParameters;
          }
        ;
 
@@ -1539,8 +1513,8 @@ fixed_parameter
          parameter_type
          error
          {
+               Error_SyntaxError (yyToken);      
                Location l = GetLocation ($4);
-               CheckIdentifierToken (yyToken, l);
                $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, l);
          }
        | opt_attributes
@@ -1554,8 +1528,8 @@ fixed_parameter
          constant_expression
          {
                --lexer.parsing_block;
-               if (RootContext.Version <= LanguageVersion.V_3) {
-                       Report.FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
+               if (lang_version <= LanguageVersion.V_3) {
+                       FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
                }
                
                Parameter.Modifier mod = (Parameter.Modifier) $2;
@@ -1583,10 +1557,10 @@ fixed_parameter
                
                var lt = (Tokenizer.LocatedToken) $4;
                $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
-               lbag.AppendTo ($$, GetLocation ($6));
+               lbag.AddLocation ($$, GetLocation ($5));
                
                if ($7 != null)
-                       ((Parameter) $$).DefaultValue = (Expression) $7;
+                       ((Parameter) $$).DefaultValue = new DefaultParameterValueExpression ((Expression) $7);
          }
        ;
 
@@ -1643,8 +1617,8 @@ parameter_modifier
                if ((valid_param_mod & ParameterModifierType.This) == 0)
                        Error_ParameterModifierNotValid ("this", GetLocation ($1));
 
-               if (RootContext.Version <= LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
+               if (lang_version <= LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "extension methods");
                                
                $$ = Parameter.Modifier.This;
          }
@@ -1663,8 +1637,9 @@ parameter_array
                var lt = (Tokenizer.LocatedToken) $4;
                $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);            
          }
-       | opt_attributes params_modifier type error {
-               CheckIdentifierToken (yyToken, GetLocation ($4));
+       | opt_attributes params_modifier type error
+         {
+               Error_SyntaxError (yyToken);
                $$ = null;
          }
        ;
@@ -1704,7 +1679,7 @@ property_declaration
          member_type
          member_declaration_name
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        tmpComment = Lexer.consume_doc_comment ();
          }
          OPEN_BRACE
@@ -1724,7 +1699,7 @@ property_declaration
          {
                lexer.PropertyParsing = false;
                
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        current_property.DocComment = ConsumeStoredComment ();                          
          }
          CLOSE_BRACE
@@ -1760,7 +1735,7 @@ indexer_declaration
                        Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
                }
 
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
@@ -1773,7 +1748,7 @@ indexer_declaration
          }
          CLOSE_BRACE
          { 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        current_property.DocComment = ConsumeStoredComment ();
                        
                lbag.AppendToMember (current_property, GetLocation ($12));
@@ -1803,8 +1778,8 @@ accessor_declarations
 get_accessor_declaration
        : opt_attributes opt_modifiers GET
          {
-               if ($2 != ModifierNone && RootContext.Version == LanguageVersion.ISO_1) {
-                       Report.FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
+               if ($2 != ModifierNone && lang_version == LanguageVersion.ISO_1) {
+                       FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
                }
          
                if (current_property.Get != null) {
@@ -1837,7 +1812,7 @@ get_accessor_declaration
                current_local_parameters = null;
                lexer.PropertyParsing = true;
 
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        if (Lexer.doc_state == XmlCommentState.Error)
                                Lexer.doc_state = XmlCommentState.NotAllowed;
          }
@@ -1846,8 +1821,8 @@ get_accessor_declaration
 set_accessor_declaration
        : opt_attributes opt_modifiers SET 
          {
-               if ($2 != ModifierNone && RootContext.Version == LanguageVersion.ISO_1) {
-                       Report.FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
+               if ($2 != ModifierNone && lang_version == LanguageVersion.ISO_1) {
+                       FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
                }
                
                if (current_property.Set != null) {
@@ -1885,7 +1860,7 @@ set_accessor_declaration
                current_local_parameters = null;
                lexer.PropertyParsing = true;
 
-               if (RootContext.Documentation != null
+               if (doc_support
                        && Lexer.doc_state == XmlCommentState.Error)
                        Lexer.doc_state = XmlCommentState.NotAllowed;
          }
@@ -1926,7 +1901,7 @@ interface_declaration
 
                current_class.SetParameterInfo ((List<Constraints>) $9);
 
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        current_container.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
@@ -1934,7 +1909,7 @@ interface_declaration
          OPEN_BRACE opt_interface_member_declarations CLOSE_BRACE
          {
                --lexer.parsing_declaration;      
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
          opt_semicolon 
@@ -1942,8 +1917,9 @@ interface_declaration
            lbag.AppendToMember (current_class, GetLocation ($11), GetLocation ($13));
                $$ = pop_current_class ();
          }
-       | opt_attributes opt_modifiers opt_partial INTERFACE error {
-               CheckIdentifierToken (yyToken, GetLocation ($5));
+       | opt_attributes opt_modifiers opt_partial INTERFACE error
+         {
+               Error_SyntaxError (yyToken);      
          }
        ;
 
@@ -1990,23 +1966,24 @@ operator_declaration
          }
          operator_body
          {
-               if ($3 == null)
-                       break;
-
                OperatorDeclaration decl = (OperatorDeclaration) $3;
-               Operator op = new Operator (
-                       current_class, decl.optype, decl.ret_type, (Modifiers) $2, 
-                       current_local_parameters,
-                       (ToplevelBlock) $5, (Attributes) $1, decl.location);
-
-               if (RootContext.Documentation != null) {
-                       op.DocComment = tmpComment;
-                       Lexer.doc_state = XmlCommentState.Allowed;
-               }
+               if (decl != null) {
+                       Operator op = new Operator (
+                               current_class, decl.optype, decl.ret_type, (Modifiers) $2, 
+                               current_local_parameters,
+                               (ToplevelBlock) $5, (Attributes) $1, decl.location);
+
+                       if (doc_support) {
+                               op.DocComment = tmpComment;
+                               Lexer.doc_state = XmlCommentState.Allowed;
+                       }
 
-               // Note again, checking is done in semantic analysis
-               current_container.AddOperator (op);
+                       // Note again, checking is done in semantic analysis
+                       current_container.AddOperator (op);
 
+                       lbag.AddMember (op, mod_locations, lbag.GetLocations (decl));
+               }
+               
                current_local_parameters = null;
          }
        ;
@@ -2062,12 +2039,13 @@ operator_declarator
                        }
                }
                
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
                $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
+               lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3), GetLocation ($4), GetLocation ($7));
          }
        | conversion_operator_declarator
        ;
@@ -2112,12 +2090,13 @@ conversion_operator_declarator
                Location loc = GetLocation ($2);
                current_local_parameters = (ParametersCompiled)$6;  
                  
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
                $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
+               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($4), GetLocation ($7));
          }
        | EXPLICIT OPERATOR type OPEN_PARENS
          {
@@ -2130,12 +2109,13 @@ conversion_operator_declarator
                Location loc = GetLocation ($2);
                current_local_parameters = (ParametersCompiled)$6;  
                  
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
                $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
+               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($4), GetLocation ($7));
          }
        | IMPLICIT error 
          {
@@ -2158,13 +2138,13 @@ constructor_declaration
                Constructor c = (Constructor) $1;
                c.Block = (ToplevelBlock) $2;
                
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        c.DocComment = ConsumeStoredComment ();
 
                current_container.AddConstructor (c);
 
                current_local_parameters = null;
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
        ;
@@ -2174,7 +2154,7 @@ constructor_declarator
          opt_modifiers
          IDENTIFIER
          {
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
@@ -2253,8 +2233,9 @@ constructor_initializer
                $$ = new ConstructorThisInitializer ((Arguments) $5, GetLocation ($2));
                lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3), GetLocation ($6));
          }
-       | COLON error {
-               Report.Error (1018, GetLocation ($1), "Keyword `this' or `base' expected");
+       | error
+         {
+               Error_SyntaxError (yyToken);
                $$ = null;
          }
        ;
@@ -2262,7 +2243,7 @@ constructor_initializer
 destructor_declaration
        : opt_attributes opt_modifiers TILDE 
          {
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
@@ -2280,7 +2261,7 @@ destructor_declaration
                
                Destructor d = new Destructor (current_class, (Modifiers) $2,
                        ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        d.DocComment = ConsumeStoredComment ();
                  
                d.Block = (ToplevelBlock) $8;
@@ -2294,32 +2275,29 @@ destructor_declaration
 event_declaration
        : opt_attributes
          opt_modifiers
-         EVENT type variable_declarators SEMICOLON
+         EVENT type member_declaration_name
          {
-               foreach (VariableMemberDeclaration var in (List<object>) $5) {
-
-                       EventField e = new EventField (
-                               current_class, (FullNamedExpression) $4, (Modifiers) $2, var.MemberName, (Attributes) $1);
-                               
-                       e.Initializer = var.GetInitializer ((FullNamedExpression) $4);
-                       if (current_container.Kind == MemberKind.Interface && e.Initializer != null) {
-                               Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
-                       }
-                       
-                       if (var.MemberName.Left != null) {
-                               Report.Error (71, e.Location,
-                                       "`{0}': An explicit interface implementation of an event must use property syntax",
-                                       e.GetSignatureForError ());
-                       }
-
-                       current_container.AddEvent (e);
-                       lbag.AddMember (e, mod_locations, GetLocation ($3), GetLocation ($6));
-
-                       if (RootContext.Documentation != null) {
-                               e.DocComment = Lexer.consume_doc_comment ();
-                               Lexer.doc_state = XmlCommentState.Allowed;
-                       }
+               current_event_field = new EventField (current_class, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, (Attributes) $1);
+               current_container.AddEvent (current_event_field);
+               
+               if (current_event_field.MemberName.Left != null) {
+                       Report.Error (71, current_event_field.Location, "`{0}': An explicit interface implementation of an event must use property syntax",
+                       current_event_field.GetSignatureForError ());
+               }
+               
+               $$ = current_event_field;
+         }
+         opt_event_initializer
+         opt_event_declarators
+         SEMICOLON
+         {
+               if (doc_support) {
+                       current_event_field.DocComment = Lexer.consume_doc_comment ();
+                       Lexer.doc_state = XmlCommentState.Allowed;
                }
+               
+               lbag.AddMember (current_event_field, mod_locations, GetLocation ($3), GetLocation ($9));
+               current_event_field = null;
          }
        | opt_attributes
          opt_modifiers
@@ -2341,7 +2319,7 @@ event_declaration
          }
          CLOSE_BRACE
          {
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        current_event.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
@@ -2350,21 +2328,76 @@ event_declaration
                current_event = null;   
                current_local_parameters = null;
          }
-       | opt_attributes opt_modifiers EVENT type member_declaration_name error
+       ;
+       
+opt_event_initializer
+       : /* empty */
+       | ASSIGN
          {
-               MemberName mn = (MemberName) $5;
-               if (mn.Left != null)
-                       Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
-
-               if (RootContext.Documentation != null)
-                       Lexer.doc_state = XmlCommentState.Allowed;
-
-               Error_SyntaxError (yyToken);
-               $$ = null;
+               ++lexer.parsing_block;
+         }
+         event_variable_initializer
+         {
+               --lexer.parsing_block;
+               current_event_field.Initializer = (Expression) $3;
          }
        ;
-
-event_accessor_declarations
+       
+opt_event_declarators
+       : /* empty */
+       | event_declarators
+       ;
+       
+event_declarators
+       : event_declarator
+         {
+               current_event_field.AddDeclarator ((FieldDeclarator) $1);
+         }
+       | event_declarators event_declarator
+         {
+               current_event_field.AddDeclarator ((FieldDeclarator) $2);
+         }
+       ;
+       
+event_declarator
+       : COMMA IDENTIFIER
+         {
+               var lt = (Tokenizer.LocatedToken) $2;
+               $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null);
+               lbag.AddLocation ($$, GetLocation ($1));
+         }
+       | COMMA IDENTIFIER ASSIGN
+         {
+               ++lexer.parsing_block;
+         }
+         event_variable_initializer
+         {
+               --lexer.parsing_block;
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) $5);
+               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
+         }
+       ;
+       
+event_variable_initializer
+       : {
+               if (current_container.Kind == MemberKind.Interface) {
+                       Report.Error (68, lexer.Location, "`{0}': event in interface cannot have an initializer",
+                               current_event_field.GetSignatureForError ());
+               }
+               
+               if ((current_event_field.ModFlags & Modifiers.ABSTRACT) != 0) {
+                       Report.Error (74, lexer.Location, "`{0}': abstract event cannot have an initializer",
+                               current_event_field.GetSignatureForError ());
+               }               
+         }
+         variable_initializer
+         {
+               $$ = $2;
+         }
+       ;
+       
+event_accessor_declarations
        : add_accessor_declaration remove_accessor_declaration
        | remove_accessor_declaration add_accessor_declaration
        | add_accessor_declaration
@@ -2455,54 +2488,39 @@ enum_declaration
          ENUM type_declaration_name
          opt_enum_base
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        enumTypeComment = Lexer.consume_doc_comment ();
          }
          OPEN_BRACE
          {
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
+
+               MemberName name = (MemberName) $4;
+               if (name.IsGeneric) {
+                       Report.Error (1675, name.Location, "Enums cannot have type parameters");
+               }
+               
+               push_current_class (new Enum (current_namespace, current_class, (TypeExpression) $5, (Modifiers) $2, MakeName (name), (Attributes) $1), null);
          }
          opt_enum_member_declarations
          {
                // here will be evaluated after CLOSE_BLACE is consumed.
-               if (RootContext.Documentation != null)
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
          CLOSE_BRACE opt_semicolon
          {
-               MemberName name = (MemberName) $4;
-               if (name.IsGeneric) {
-                       Report.Error (1675, name.Location, "Enums cannot have type parameters");
-               }
-
-               name = MakeName (name);
-               Enum e = new Enum (current_namespace, current_class, (TypeExpression) $5, (Modifiers) $2,
-                                  name, (Attributes) $1);
-                                       
-               if (RootContext.Documentation != null)
-                       e.DocComment = enumTypeComment;
-
-
-               EnumMember em = null;
-               foreach (VariableDeclaration ev in (IList<VariableDeclaration>) $9) {
-                       em = new EnumMember (
-                               e, em, ev.identifier, ev.GetInitializer (null),
-                               ev.OptAttributes, ev.Location);
-
-//                     if (RootContext.Documentation != null)
-                               em.DocComment = ev.DocComment;
-
-                       e.AddEnumMember (em);
-               }
-               if (RootContext.EvalMode)
-                       undo.AddTypeContainer (current_container, e);
-
-               current_container.AddTypeContainer (e);
-               lbag.AddMember (e, mod_locations, GetLocation ($3), GetLocation ($7), GetLocation ($11));
+               if (doc_support)
+                       current_class.DocComment = enumTypeComment;
+                       
+               --lexer.parsing_declaration;
 
-               $$ = e;
+//                     if (doc_support)
+//                             em.DocComment = ev.DocComment;
 
+               lbag.AddMember (current_class, mod_locations, GetLocation ($3), GetLocation ($7), GetLocation ($11));
+               $$ = pop_current_class ();
          }
        ;
 
@@ -2530,56 +2548,58 @@ opt_enum_base
        ;
 
 opt_enum_member_declarations
-       : /* empty */                   { $$ = new VariableDeclaration [0]; }
-       | enum_member_declarations opt_comma { $$ = $1; }
+       : /* empty */
+       | enum_member_declarations
+       | enum_member_declarations COMMA
+         {
+               lbag.AddLocation ($1, GetLocation ($2));
+         }
        ;
 
 enum_member_declarations
-       : enum_member_declaration 
-         {
-               var l = new List<VariableDeclaration> (4);
-               l.Add ((VariableDeclaration) $1);
-               $$ = l;
-         }
+       : enum_member_declaration
        | enum_member_declarations COMMA enum_member_declaration
          {
-               var l = (List<VariableDeclaration>) $1;
-               l.Add ((VariableDeclaration) $3);
-               $$ = l;
+               lbag.AddLocation ($1, GetLocation ($2));
+               $$ = $3;
          }
        ;
 
 enum_member_declaration
-       : opt_attributes IDENTIFIER 
+       : opt_attributes IDENTIFIER
          {
-               VariableDeclaration vd = new VariableDeclaration (
-                       (Tokenizer.LocatedToken) $2, null, (Attributes) $1);
+               var lt = (Tokenizer.LocatedToken) $2;
+               var em = new EnumMember ((Enum) current_class, new MemberName (lt.Value, lt.Location), (Attributes) $1);
+               ((Enum) current_class).AddEnumMember (em);
 
-               if (RootContext.Documentation != null) {
-                       vd.DocComment = Lexer.consume_doc_comment ();
+               if (doc_support) {
+                       em.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
 
-               $$ = vd;
+               $$ = em;
          }
        | opt_attributes IDENTIFIER
          {
                ++lexer.parsing_block;
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
          }
          ASSIGN constant_expression
          { 
-               --lexer.parsing_block;    
-               VariableDeclaration vd = new VariableDeclaration (
-                       (Tokenizer.LocatedToken) $2, (Expression) $5, (Attributes) $1);
-
-               if (RootContext.Documentation != null)
-                       vd.DocComment = ConsumeStoredComment ();
+               --lexer.parsing_block;
+               
+               var lt = (Tokenizer.LocatedToken) $2;
+               var em = new EnumMember ((Enum) current_class, new MemberName (lt.Value, lt.Location), (Attributes) $1);
+               em.Initializer = new ConstInitializer (em, (Expression) $5, GetLocation ($4));
+               ((Enum) current_class).AddEnumMember (em);
+               
+               if (doc_support)
+                       em.DocComment = ConsumeStoredComment ();
 
-               $$ = vd;
+               $$ = em;
          }
        ;
 
@@ -2602,7 +2622,7 @@ delegate_declaration
                Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
                                             (Modifiers) $2, name, p, (Attributes) $1);
 
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        del.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
@@ -2628,15 +2648,10 @@ delegate_declaration
 
 opt_nullable
        : /* empty */
-         {
-               $$ = null;
-         }
        | INTERR_NULLABLE
          {
-               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
-                       Report.FeatureIsNotSupported (GetLocation ($1), "nullable types");
-               else if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "nullable types");
          
                $$ = ComposedTypeSpecifier.CreateNullable (GetLocation ($1));
          }
@@ -2675,13 +2690,11 @@ type_name
 // Generics arguments  (any type, without attributes)
 //
 opt_type_argument_list
-       : /* empty */                { $$ = null; } 
+       : /* empty */
        | OP_GENERICS_LT type_arguments OP_GENERICS_GT
          {
-               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
-                       Report.FeatureIsNotSupported (GetLocation ($1), "generics");
-               else if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "generics");      
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "generics");     
          
                $$ = $2;
          }
@@ -2719,7 +2732,7 @@ type_declaration_name
          {
                lexer.parsing_generic_declaration = false;
                var lt = (Tokenizer.LocatedToken) $1;
-               $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);   
+               $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);
          }
        ;
 
@@ -2780,13 +2793,11 @@ explicit_interface
        ;
        
 opt_type_parameter_list
-       : /* empty */                { $$ = null; } 
+       : /* empty */
        | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
          {
-               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
-                       Report.FeatureIsNotSupported (GetLocation ($1), "generics");
-               else if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "generics");
          
                $$ = $2;
                lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
@@ -2896,24 +2907,23 @@ type_expression
                                $$ = name.GetTypeExpression ();
                }
          }
+       | namespace_or_type_name pointer_stars
+         {
+               $$ = new ComposedCast (((MemberName) $1).GetTypeExpression (), (ComposedTypeSpecifier) $2);
+         }
        | builtin_types opt_nullable
          {
                if ($2 != null)
                        $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
          }
-       | type_expression STAR
+       | builtin_types pointer_stars
          {
-               //
-               // Note that here only unmanaged types are allowed but we
-               // can't perform checks during this phase - we do it during
-               // semantic analysis.
-               //
-               $$ = new ComposedCast ((FullNamedExpression) $1, ComposedTypeSpecifier.CreatePointer (GetLocation ($2)));
+               $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
          }
-       | VOID STAR
+       | VOID pointer_stars
          {
-               $$ = new ComposedCast (new TypeExpression (TypeManager.void_type, GetLocation ($1)), ComposedTypeSpecifier.CreatePointer (GetLocation ($2)));
-         }     
+               $$ = new ComposedCast (new TypeExpression (TypeManager.void_type, GetLocation ($1)), (ComposedTypeSpecifier) $2);
+         }
        ;
 
 type_list
@@ -2978,24 +2988,11 @@ integral_type
 
 
 primary_expression
-       : primary_expression_no_array_creation
+       : primary_expression_or_type
+       | literal
        | array_creation_expression
-       ;
-
-primary_expression_no_array_creation
-       : literal
-       | IDENTIFIER opt_type_argument_list
-         {
-               var lt = (Tokenizer.LocatedToken) $1;
-               $$ = new SimpleName (lt.Value, (TypeArguments)$2, lt.Location);   
-         }
-       | IDENTIFIER GENERATE_COMPLETION {
-               var lt = (Tokenizer.LocatedToken) $1;
-              $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
-         }
        | parenthesized_expression
        | default_value_expression
-       | member_access
        | invocation_expression
        | element_access
        | this_access
@@ -3012,6 +3009,19 @@ primary_expression_no_array_creation
        | anonymous_method_expression
        ;
 
+primary_expression_or_type
+       : IDENTIFIER opt_type_argument_list
+         {
+               var lt = (Tokenizer.LocatedToken) $1;
+               $$ = new SimpleName (lt.Value, (TypeArguments)$2, lt.Location);   
+         }
+       | IDENTIFIER GENERATE_COMPLETION {
+               var lt = (Tokenizer.LocatedToken) $1;
+              $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
+         }
+       | member_access
+       ;
+
 literal
        : boolean_literal
        | LITERAL
@@ -3069,6 +3079,12 @@ member_access
                $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
                lbag.AddLocation ($$, GetLocation ($2));
          }
+       | BASE DOT IDENTIFIER opt_type_argument_list
+         {
+               var lt = (Tokenizer.LocatedToken) $3;
+               $$ = new MemberAccess (new BaseThis (GetLocation ($1)), lt.Value, (TypeArguments) $4, lt.Location);
+               lbag.AddLocation ($$, GetLocation ($2));
+         }
        | qualified_alias_member IDENTIFIER opt_type_argument_list
          {
                var lt1 = (Tokenizer.LocatedToken) $1;
@@ -3269,10 +3285,6 @@ non_simple_argument
                $$ = new Argument (new Arglist (GetLocation ($1)));
                lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
          }       
-       | ARGLIST
-         {
-               $$ = new Argument (new ArglistAccess (GetLocation ($1)));
-         }
        ;
 
 variable_reference
@@ -3316,6 +3328,9 @@ expression_list_arguments
        | expression_list_arguments COMMA expression_list_argument
          {
                Arguments args = (Arguments) $1;
+               if (args [args.Count - 1] is NamedArgument && !($3 is NamedArgument))
+                       Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
+         
                args.Add ((Argument) $3);
                $$ = args;        
          }
@@ -3337,21 +3352,15 @@ this_access
        ;
 
 base_access
-       : BASE DOT IDENTIFIER opt_type_argument_list
-         {
-               var lt = (Tokenizer.LocatedToken) $3;
-               $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
-               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2));
-         }
-       | BASE OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET
+       : BASE OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET
          {
-               $$ = new BaseIndexerAccess ((Arguments) $3, GetLocation ($1));
-               lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
+               $$ = new ElementAccess (new BaseThis (GetLocation ($1)), (Arguments) $3, GetLocation ($2));
+               lbag.AddLocation ($$, GetLocation ($4));
          }
        | BASE OPEN_BRACKET error
          {
                Error_SyntaxError (yyToken);
-               $$ = new BaseAccess (null, GetLocation ($1));
+               $$ = new ElementAccess (null, null, GetLocation ($2));
          }
        ;
 
@@ -3373,20 +3382,20 @@ object_or_delegate_creation_expression
        : NEW new_expr_type open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
          {
                if ($6 != null) {
-                       if (RootContext.Version <= LanguageVersion.ISO_2)
-                               Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
+                       if (lang_version <= LanguageVersion.ISO_2)
+                               FeatureIsNotAvailable (GetLocation ($1), "object initializers");
                                
                        $$ = new NewInitialize ((FullNamedExpression) $2, (Arguments) $4, (CollectionOrObjectInitializers) $6, GetLocation ($1));
                } else {
                        $$ = new New ((FullNamedExpression) $2, (Arguments) $4, GetLocation ($1));
                }
                
-               lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
+               lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
          }
        | NEW new_expr_type object_or_collection_initializer
          {
-               if (RootContext.Version <= LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
+               if (lang_version <= LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
          
                $$ = new NewInitialize ((FullNamedExpression) $2, null, (CollectionOrObjectInitializers) $3, GetLocation ($1));
          }
@@ -3410,10 +3419,10 @@ array_creation_expression
 
                $$ = new ArrayCreation ((FullNamedExpression) $2, (ComposedTypeSpecifier) $3, (ArrayInitializer) $4, GetLocation ($1));
          }
-       | NEW rank_specifiers array_initializer
+       | NEW rank_specifier array_initializer
          {
-               if (RootContext.Version <= LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
+               if (lang_version <= LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
          
                $$ = new ImplicitlyTypedArrayCreation ((ComposedTypeSpecifier) $2, (ArrayInitializer) $3, GetLocation ($1));
          }
@@ -3443,10 +3452,8 @@ new_expr_type
 anonymous_type_expression
        : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
          {
-               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
-                       Report.FeatureIsNotSupported (GetLocation ($1), "anonymous types");
-               else if (RootContext.Version <= LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
+               if (lang_version <= LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
 
                $$ = new NewAnonymousType ((List<AnonymousTypeParameter>) $3, current_container, GetLocation ($1));
                
@@ -3493,12 +3500,6 @@ anonymous_type_parameter
                $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
                        lt.Value, lt.Location);
          }
-       | BASE DOT IDENTIFIER opt_type_argument_list
-         {
-               var lt = (Tokenizer.LocatedToken) $3;
-               BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
-               $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
-         }       
        | member_access
          {
                MemberAccess ma = (MemberAccess) $1;
@@ -3514,25 +3515,9 @@ anonymous_type_parameter
 
 opt_rank_specifier
        : /* empty */
-         {
-               $$ = null;
-         }
        | rank_specifiers
        ;
 
-opt_rank_specifier_or_nullable
-       : opt_nullable
-       | opt_nullable rank_specifiers
-         {
-               if ($1 != null) {
-                       ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
-                       $$ = $1;
-               } else {
-                       $$ = $2;
-               }
-         }
-       ;
-
 rank_specifiers
        : rank_specifier
        | rank_specifier rank_specifiers
@@ -3546,10 +3531,12 @@ rank_specifier
        : OPEN_BRACKET CLOSE_BRACKET
          {
                $$ = ComposedTypeSpecifier.CreateArrayDimension (1, GetLocation ($1));
+               lbag.AddLocation ($$, GetLocation ($2));
          }
        | OPEN_BRACKET dim_separators CLOSE_BRACKET
          {
                $$ = ComposedTypeSpecifier.CreateArrayDimension ((int)$2, GetLocation ($1));
+               lbag.AddLocation ($$, GetLocation ($3));
          }
        ;
 
@@ -3578,13 +3565,21 @@ opt_array_initializer
 array_initializer
        : OPEN_BRACE CLOSE_BRACE
          {
-               $$ = new ArrayInitializer (0, GetLocation ($1));
-               lbag.AddLocation ($$, GetLocation ($2));
+               var ai = new ArrayInitializer (0, GetLocation ($1));
+               ai.VariableDeclaration = current_variable;
+               lbag.AddLocation (ai, GetLocation ($2));
+               $$ = ai;
          }
        | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
          {
-               $$ = new ArrayInitializer ((List<Expression>) $2, GetLocation ($1));
-               lbag.AddLocation ($$, GetLocation ($3));
+               var ai = new ArrayInitializer ((List<Expression>) $2, GetLocation ($1));
+               ai.VariableDeclaration = current_variable;
+               if ($3 != null) {
+                       lbag.AddLocation (ai, GetLocation ($3), GetLocation ($4));
+               } else {
+                       lbag.AddLocation (ai, GetLocation ($4));
+               }
+               $$ = ai;
          }
        ;
 
@@ -3601,11 +3596,6 @@ variable_initializer_list
                list.Add ((Expression) $3);
                $$ = list;
          }
-       | error
-         {
-               Error_SyntaxError (yyToken);
-               $$ = new List<Expression> ();
-         }
        ;
 
 typeof_expression
@@ -3659,20 +3649,20 @@ unbound_type_name
          }
        | namespace_or_type_name DOT IDENTIFIER generic_dimension
          {
-               var lt = (Tokenizer.LocatedToken) $3;
-               MemberName name = (MemberName) $1;
+               var te = ((MemberName) $1).GetTypeExpression ();
+               if (te.HasTypeArguments)
+                       Error_TypeExpected (GetLocation ($4));
 
-               $$ = new MemberAccess (name.GetTypeExpression (), lt.Value, (int) $4, lt.Location);             
+               var lt = (Tokenizer.LocatedToken) $3;
+               $$ = new MemberAccess (te, lt.Value, (int) $4, lt.Location);            
          }
        ;
 
 generic_dimension
        : GENERIC_DIMENSION
          {
-               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
-                       Report.FeatureIsNotSupported (GetLocation ($1), "generics");
-               else if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "generics");
 
                $$ = $1;
          }
@@ -3682,8 +3672,8 @@ qualified_alias_member
        : IDENTIFIER DOUBLE_COLON
          {
                var lt = (Tokenizer.LocatedToken) $1;
-               if (RootContext.Version == LanguageVersion.ISO_1)
-                       Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
+               if (lang_version == LanguageVersion.ISO_1)
+                       FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
 
                $$ = lt;                
          }
@@ -3728,8 +3718,8 @@ anonymous_method_expression
          }
          block
          {
-               $$ = end_anonymous ((ToplevelBlock) $4);
-       }
+               $$ = end_anonymous ((ParametersBlock) $4);
+         }
        ;
 
 opt_anonymous_method_signature
@@ -3755,8 +3745,8 @@ anonymous_method_signature
 default_value_expression
        : DEFAULT open_parens_any type CLOSE_PARENS
          {
-               if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "default value expression");
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "default value expression");
 
                $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
                lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
@@ -3782,11 +3772,6 @@ cast_expression
                $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
                lbag.AddLocation ($$, GetLocation ($3));
          }
-       | OPEN_PARENS builtin_types CLOSE_PARENS prefixed_unary_expression
-         {
-               $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
-               lbag.AddLocation ($$, GetLocation ($3));
-         }
        ;
 
        //
@@ -3967,8 +3952,8 @@ null_coalescing_expression
        : conditional_or_expression
        | conditional_or_expression OP_COALESCING null_coalescing_expression
          {
-               if (RootContext.Version < LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
+               if (lang_version < LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
                        
                $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, GetLocation ($2));
          }
@@ -4085,23 +4070,34 @@ opt_lambda_parameter_list
        : /* empty */                   { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
        | lambda_parameter_list         { 
                var pars_list = (List<Parameter>) $1;
-               $$ = new ParametersCompiled (compiler, pars_list.ToArray ());
+               $$ = new ParametersCompiled (pars_list.ToArray ());
          }
        ;
 
 lambda_expression_body
+       : lambda_expression_body_simple
+       | block
+       ;
+       
+lambda_expression_body_simple
        : {
                start_block (lexer.Location);
          }
-         expression 
+         expression_or_error   // Have to close block when error occurs
          {
                Block b = end_block (lexer.Location);
                b.AddStatement (new ContextualReturn ((Expression) $2));
                $$ = b;
          } 
-       | block { 
-               $$ = $1; 
-         } 
+       ;
+       
+expression_or_error
+       : expression
+       | error
+         {
+               Error_SyntaxError (yyToken);    
+               $$ = EmptyExpression.Null;
+         }
        ;
 
 lambda_expression
@@ -4109,16 +4105,17 @@ lambda_expression
          {
                var lt = (Tokenizer.LocatedToken) $1;
                Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
-               start_anonymous (true, new ParametersCompiled (compiler, p), GetLocation ($1));
+               start_anonymous (true, new ParametersCompiled (p), GetLocation ($1));
          }
          lambda_expression_body
          {
-               $$ = end_anonymous ((ToplevelBlock) $4);
+               $$ = end_anonymous ((ParametersBlock) $4);
+               lbag.AddLocation ($$, GetLocation ($2));
          }
        | OPEN_PARENS_LAMBDA
          {
-               if (RootContext.Version <= LanguageVersion.ISO_2)
-                       Report.FeatureIsNotAvailable (GetLocation ($1), "lambda expressions");
+               if (lang_version <= LanguageVersion.ISO_2)
+                       FeatureIsNotAvailable (GetLocation ($1), "lambda expressions");
          
                valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
          }
@@ -4129,19 +4126,24 @@ lambda_expression
          }
          lambda_expression_body 
          {
-               $$ = end_anonymous ((ToplevelBlock) $7);
+               $$ = end_anonymous ((ParametersBlock) $7);
+               lbag.AddLocation ($$, GetLocation ($4), GetLocation ($5));
          }
        ;
 
 expression
        : assignment_expression 
-       | non_assignment_expression 
+       | non_assignment_expression
        ;
        
 non_assignment_expression
        : conditional_expression
        | lambda_expression
-       | query_expression 
+       | query_expression
+       | ARGLIST
+         {
+               $$ = new ArglistAccess (GetLocation ($1));
+         }     
        ;
 
 constant_expression
@@ -4169,7 +4171,12 @@ class_declaration
          type_declaration_name
          {
                MemberName name = MakeName ((MemberName) $6);
-               push_current_class (new Class (current_namespace, current_class, name, (Modifiers) $2, (Attributes) $1), $3);
+               Class c = new Class (current_namespace, current_class, name, (Modifiers) $2, (Attributes) $1);
+               if (((c.ModFlags & Modifiers.STATIC) != 0) && lang_version == LanguageVersion.ISO_1) {
+                       FeatureIsNotAvailable (c.Location, "static classes");
+               }
+                       
+               push_current_class (c, $3);
          }
          opt_class_base
          opt_type_parameter_constraints_clauses
@@ -4179,20 +4186,20 @@ class_declaration
                current_class.SetParameterInfo ((List<Constraints>) $9);
                lbag.AddMember (current_class, mod_locations, GetLocation ($4));
 
-               if (RootContext.Documentation != null) {
+               if (doc_support) {
                        current_container.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
          OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
          {
-               --lexer.parsing_declaration;      
-               if (RootContext.Documentation != null)
+               --lexer.parsing_declaration;
+               if (doc_support)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
          opt_semicolon 
          {
-               lbag.AppendToMember (current_class, GetLocation ($11), GetLocation ($13));
+               lbag.AppendToMember (current_class, GetLocation ($11), GetLocation ($13), GetLocation ($15));
                $$ = pop_current_class ();
          }
        ;       
@@ -4306,9 +4313,14 @@ modifier
          {
                $$ = Modifiers.UNSAFE;
                StoreModifierLocation ($$, GetLocation ($1));
-               if (!RootContext.Unsafe)
+               if (!settings.Unsafe)
                        Error_UnsafeCodeNotAllowed (GetLocation ($1));
          }
+       | ASYNC
+         {
+               $$ = Modifiers.ASYNC;
+               StoreModifierLocation ($$, GetLocation ($1));
+         }
        ;
 
 opt_class_base
@@ -4320,11 +4332,16 @@ opt_class_base
        ;
 
 opt_type_parameter_constraints_clauses
-       : /* empty */           { $$ = null; }
+       : /* empty */
        | type_parameter_constraints_clauses 
          {
                $$ = $1;
          }
+       | error
+        {
+               Error_SyntaxError (yyToken);
+               $$ = null;
+        }
        ;
 
 type_parameter_constraints_clauses
@@ -4422,10 +4439,8 @@ opt_type_parameter_variance
          }
        | type_parameter_variance
          {
-               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
-                       Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
-               else if (RootContext.Version <= LanguageVersion.V_3)
-                       Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
+               if (lang_version <= LanguageVersion.V_3)
+                       FeatureIsNotAvailable (lexer.Location, "generic type variance");
 
                $$ = $1;
          }
@@ -4505,18 +4520,20 @@ statement_list
        ;
 
 statement
-       : declaration_statement
+       : block_variable_declaration
          {
-               if ($1 != null && (Block) $1 != current_block){
-                       current_block.AddStatement ((Statement) $1);
-                       current_block = (Block) $1;
-               }
+               current_block.AddStatement ((Statement) $1);
          }
        | valid_declaration_statement
          {
                current_block.AddStatement ((Statement) $1);
          }
        | labeled_statement
+       | error
+         {
+               Error_SyntaxError (yyToken);
+               $$ = null;
+         }
        ;
 
 //
@@ -4531,12 +4548,9 @@ interactive_statement_list
        ;
 
 interactive_statement
-       : declaration_statement
+       : block_variable_declaration
          {
-               if ($1 != null && (Block) $1 != current_block){
-                       current_block.AddStatement ((Statement) $1);
-                       current_block = (Block) $1;
-               }
+               current_block.AddStatement ((Statement) $1);
          }
        | interactive_valid_declaration_statement
          {
@@ -4579,7 +4593,7 @@ interactive_valid_declaration_statement
 
 embedded_statement
        : valid_declaration_statement
-       | declaration_statement
+       | block_variable_declaration
          {
                  Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
                  $$ = null;
@@ -4589,12 +4603,18 @@ embedded_statement
                  Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
                  $$ = null;
          }
+       | error
+         {
+               Error_SyntaxError (yyToken);
+               $$ = new EmptyStatement (GetLocation ($1));
+         }
        ;
 
 empty_statement
        : SEMICOLON
          {
-               $$ = new EmptyStatement (GetLocation ($1));
+               // Uses lexer.Location because semicolon location is not kept in quick mode
+               $$ = new EmptyStatement (lexer.Location);
          }
        ;
 
@@ -4602,30 +4622,22 @@ labeled_statement
        : IDENTIFIER COLON 
          {
                var lt = (Tokenizer.LocatedToken) $1;
-               LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
+               LabeledStatement labeled = new LabeledStatement (lt.Value, current_block, lt.Location);
 
-               if (current_block.AddLabel (labeled))
-                       current_block.AddStatement (labeled);
+               current_block.AddLabel (labeled);
+               current_block.AddStatement (labeled);
          }
          statement
        ;
 
-declaration_statement
-       : local_variable_declaration SEMICOLON
-         {
-               if ($1 != null){
-                       var de = (Tuple<FullNamedExpression, List<object>>) $1;
-                       $$ = declare_local_variables (de.Item1, de.Item2, de.Item1.Location);
-               }
-         }
-
-       | local_constant_declaration SEMICOLON
+variable_type
+       : variable_type_simple
+       | variable_type_simple rank_specifiers
          {
-               if ($1 != null){
-                       var de = (Tuple<FullNamedExpression, List<object>>) $1;
-
-                       $$ = declare_local_constants (de.Item1, de.Item2);
-               }
+               if ($1 is VarExpr)
+                       $1 = new SimpleName ("var", ((VarExpr) $1).Location);
+         
+               $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
          }
        ;
 
@@ -4635,8 +4647,8 @@ declaration_statement
  * > expressions to prevent reduce/reduce errors in the grammar.
  * > The expressions are converted into types during semantic analysis.
  */
-variable_type
-       : primary_expression_no_array_creation opt_rank_specifier_or_nullable
+variable_type_simple
+       : primary_expression_or_type opt_nullable
          { 
                // Ok, the above "primary_expression" is there to get rid of
                // both reduce/reduce and shift/reduces in the grammar, it should
@@ -4666,86 +4678,177 @@ variable_type
                        $$ = null;
                }
          }
-       | builtin_types opt_rank_specifier_or_nullable
+       | primary_expression_or_type pointer_stars
+         {
+               ATypeNameExpression expr = $1 as ATypeNameExpression;
+
+               if (expr != null) {
+                       $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
+               } else {
+                       Error_ExpectingTypeName ((Expression)$1);
+                       $$ = expr;
+               }
+         }
+       | builtin_types opt_nullable
          {
                if ($2 == null)
                        $$ = $1;
                else
                        $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
          }
-       | VOID opt_rank_specifier
+       | builtin_types pointer_stars
+         {
+               $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
+         }
+       | VOID pointer_stars
+         {
+               $$ = new ComposedCast (new TypeExpression (TypeManager.void_type, GetLocation ($1)), (ComposedTypeSpecifier) $2);
+         }       
+       | VOID
          {
                Expression.Error_VoidInvalidInTheContext (GetLocation ($1), Report);
                $$ = new TypeExpression (TypeManager.void_type, GetLocation ($1));
          }
        ;
+       
+pointer_stars
+       : pointer_star
+       | pointer_star pointer_stars
+         {
+               ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
+               $$ = $1;
+         }       
+       ;
 
-local_variable_pointer_type
-       : primary_expression_no_array_creation STAR
+pointer_star
+       : STAR
          {
-               ATypeNameExpression expr = $1 as ATypeNameExpression;
+               $$ = ComposedTypeSpecifier.CreatePointer (GetLocation ($1));
+         }
+       ;
 
-               if (expr != null) {
-                       $$ = new ComposedCast (expr, ComposedTypeSpecifier.CreatePointer (GetLocation ($2)));
-               } else {
-                       Error_ExpectingTypeName ((Expression)$1);
-                       $$ = expr;
-               }
+block_variable_declaration
+       : variable_type IDENTIFIER
+         {
+               var lt = (Tokenizer.LocatedToken) $2;
+               var li = new LocalVariable (current_block, lt.Value, lt.Location);
+               current_block.AddLocalName (li);
+               current_variable = new BlockVariableDeclaration ((FullNamedExpression) $1, li);
          }
-       | builtin_types STAR
+         opt_local_variable_initializer opt_variable_declarators SEMICOLON
          {
-               $$ = new ComposedCast ((FullNamedExpression) $1, ComposedTypeSpecifier.CreatePointer (GetLocation ($2)));
+               $$ = current_variable;
+               current_variable = null;
+               lbag.AddLocation ($$, GetLocation ($6));
          }
-       | VOID STAR
+       | CONST variable_type IDENTIFIER
          {
-               $$ = new ComposedCast (new TypeExpression (TypeManager.void_type, GetLocation ($1)), ComposedTypeSpecifier.CreatePointer (GetLocation ($2)));
+               var lt = (Tokenizer.LocatedToken) $3;
+               var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
+               current_block.AddLocalName (li);
+               current_variable = new BlockConstantDeclaration ((FullNamedExpression) $2, li);
          }
-       | local_variable_pointer_type STAR
+         const_variable_initializer opt_const_declarators SEMICOLON
          {
-               $$ = new ComposedCast ((FullNamedExpression) $1, ComposedTypeSpecifier.CreatePointer (GetLocation ($2)));
+               $$ = current_variable;
+               current_variable = null;
+               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($7));
          }
        ;
 
-local_variable_type
-       : variable_type
-       | local_variable_pointer_type opt_rank_specifier
+opt_local_variable_initializer
+       : /* empty */
+       | ASSIGN block_variable_initializer
          {
-               if ($1 != null){
-                       if ($2 == null)
-                               $$ = $1;
-                       else
-                               $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
+               current_variable.Initializer = (Expression) $2;
+               // TODO: lbag
+         }
+       | error
+         {
+               if (yyToken == Token.OPEN_BRACKET_EXPR) {
+                       Report.Error (650, lexer.Location,
+                               "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type");
                } else {
-                       $$ = null;
+                       Error_SyntaxError (yyToken);
                }
          }
        ;
 
-local_variable_declaration
-       : local_variable_type local_variable_declarators
-         {
-               if ($1 != null) {
-                       VarExpr ve = $1 as VarExpr;
-                       if (ve != null) {
-                               if (!((VariableDeclaration) ((List<object>)$2) [0]).HasInitializer)
-                                       ve.VariableInitializersCount = 0;
-                               else
-                                       ve.VariableInitializersCount = ((List<object>)$2).Count;
-                       }
-                               
-                       $$ = new Tuple<FullNamedExpression, List<object>> ((FullNamedExpression) $1, (List<object>) $2);
-               } else
-                       $$ = null;
+opt_variable_declarators
+       : /* empty */
+       | variable_declarators
+       ;
+       
+variable_declarators
+       : variable_declarator
+       | variable_declarators variable_declarator
+       ;
+       
+variable_declarator
+       : COMMA IDENTIFIER
+         {
+               var lt = (Tokenizer.LocatedToken) $2;     
+               var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
+               var d = new BlockVariableDeclaration.Declarator (li, null);
+               current_variable.AddDeclarator (d);
+               current_block.AddLocalName (li);
+               lbag.AddLocation (d, GetLocation ($1));
          }
-       ;
-
-local_constant_declaration
-       : CONST variable_type constant_declarators
+       | COMMA IDENTIFIER ASSIGN block_variable_initializer
          {
-               if ($2 != null)
-                       $$ = new Tuple<FullNamedExpression, List<object>> ((FullNamedExpression) $2, (List<object>) $3);
-               else
-                       $$ = null;
+               var lt = (Tokenizer.LocatedToken) $2;     
+               var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
+               var d = new BlockVariableDeclaration.Declarator (li, (Expression) $4);
+               current_variable.AddDeclarator (d);
+               current_block.AddLocalName (li);
+               lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
+         }
+       ;
+       
+const_variable_initializer
+       : /* empty */
+         {
+               Report.Error (145, lexer.Location, "A const field requires a value to be provided");
+         }
+       | ASSIGN constant_initializer_expr 
+         {
+               current_variable.Initializer = (Expression) $2;
+         }
+       ;
+       
+opt_const_declarators
+       : /* empty */
+       | const_declarators
+       ;
+       
+const_declarators
+       : const_declarator
+       | const_declarators const_declarator
+       ;
+       
+const_declarator
+       : COMMA IDENTIFIER ASSIGN constant_initializer_expr
+         {
+               var lt = (Tokenizer.LocatedToken) $2;     
+               var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
+               var d = new BlockVariableDeclaration.Declarator (li, (Expression) $4);
+               current_variable.AddDeclarator (d);
+               current_block.AddLocalName (li);
+               lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
+         }
+       ;
+       
+block_variable_initializer
+       : variable_initializer
+       | STACKALLOC simple_type OPEN_BRACKET_EXPR expression CLOSE_BRACKET
+         {
+               $$ = new StackAlloc ((Expression) $2, (Expression) $4, GetLocation ($1));
+               lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
+         }
+       | STACKALLOC simple_type
+         {
+               Report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
+               $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));          
          }
        ;
 
@@ -4778,11 +4881,6 @@ statement_expression
 
                $$ = new StatementExpression (s);
          }
-       | error
-         {
-               Error_SyntaxError (yyToken);
-               $$ = null;
-         }
        ;
 
 interactive_statement_expression
@@ -4811,7 +4909,7 @@ if_statement
          embedded_statement
          { 
                if ($5 is EmptyStatement)
-                       Report.Warning (642, 3, GetLocation ($5), "Possible mistaken empty statement");
+                       Warning_EmptyStatement (GetLocation ($5));
                
                $$ = new If ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
                lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
@@ -4820,36 +4918,32 @@ if_statement
          embedded_statement ELSE embedded_statement
          {
                $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($1));
-               lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4), GetLocation ($5));
+               lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
                
                if ($5 is EmptyStatement)
-                       Report.Warning (642, 3, GetLocation ($5), "Possible mistaken empty statement");
+                       Warning_EmptyStatement (GetLocation ($5));
                if ($7 is EmptyStatement)
-                       Report.Warning (642, 3, GetLocation ($7), "Possible mistaken empty statement");
+                       Warning_EmptyStatement (GetLocation ($7));
          }
        ;
 
 switch_statement
-       : SWITCH open_parens_any
-         { 
-               if (switch_stack == null)
-                       switch_stack = new Stack<Block> (2);
-               switch_stack.Push (current_block);
+       : SWITCH open_parens_any expression CLOSE_PARENS OPEN_BRACE
+         {
+               start_block (GetLocation ($5));
          }
-         expression CLOSE_PARENS 
-         OPEN_BRACE opt_switch_sections CLOSE_BRACE
+         opt_switch_sections CLOSE_BRACE
          {
-               $$ = new Switch ((Expression) $4, (List<SwitchSection>) $7, GetLocation ($1));
-               lbag.AddStatement ($$, GetLocation ($2), GetLocation ($5), GetLocation ($6), GetLocation ($8));
-               
-               current_block = (Block) switch_stack.Pop ();
+               $$ = new Switch ((Expression) $3, (ExplicitBlock) current_block.Explicit, (List<SwitchSection>) $7, GetLocation ($1));  
+               end_block (GetLocation ($8));
+               lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
          }
        ;
 
 opt_switch_sections
        : /* empty */           
       {
-               Report.Warning (1522, 1, lexer.Location, "Empty switch block"); 
+               Report.Warning (1522, 1, current_block.StartLocation, "Empty switch block"); 
                $$ = new List<SwitchSection> ();
          }
        | switch_sections
@@ -4870,6 +4964,11 @@ switch_sections
                sections.Add ((SwitchSection) $2);
                $$ = sections;
          }
+       | error
+         {
+               Error_SyntaxError (yyToken);      
+               $$ = new List<SwitchSection> ();
+         } 
        ;
 
 switch_section
@@ -4879,7 +4978,7 @@ switch_section
          }
          statement_list 
          {
-               $$ = new SwitchSection ((List<SwitchLabel>) $1, current_block.Explicit);
+               $$ = new SwitchSection ((List<SwitchLabel>) $1, current_block);
          }
        ;
 
@@ -4922,6 +5021,9 @@ iteration_statement
 while_statement
        : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
          {
+               if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($5));
+         
                $$ = new While ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
                lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
          }
@@ -4937,54 +5039,38 @@ do_statement
        ;
 
 for_statement
-       : FOR open_parens_any opt_for_initializer SEMICOLON
+       : FOR open_parens_any
          {
-               Location l = lexer.Location;
-               start_block (l);  
-               Block assign_block = current_block;
-
-               if ($3 is Tuple<FullNamedExpression, List<object>>){
-                       var de = (Tuple<FullNamedExpression, List<object>>) $3;
-                       
-                       var type = de.Item1;
-
-                       foreach (VariableDeclaration decl in de.Item2){
-
-                               LocalInfo vi;
-
-                               vi = current_block.AddVariable (type, decl.identifier, decl.Location);
-                               if (vi == null)
-                                       continue;
-
-                               Expression expr = decl.GetInitializer (type);
-                                       
-                               LocalVariableReference var;
-                               var = new LocalVariableReference (assign_block, decl.identifier, l);
-
-                               if (expr != null) {
-                                       Assign a = new SimpleAssign (var, expr, decl.Location);
-                                       
-                                       assign_block.AddStatement (new StatementExpression (a));
-                               }
-                       }
-                       
-                       // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
-                       // This can be referred to as $5 below.
-                       $$ = null;
-               } else {
-                       $$ = $3;
-               }
-         } 
+               start_block (GetLocation ($2));
+               current_block.IsCompilerGenerated = true;
+         }
+         for_statement_cont
+         {
+               $$ = $4;
+         }
+       ;
+       
+// Has to use be extra rule to recover started block
+for_statement_cont
+       : opt_for_initializer SEMICOLON
          opt_for_condition SEMICOLON
-         opt_for_iterator CLOSE_PARENS 
+         opt_for_iterator CLOSE_PARENS
          embedded_statement
          {
-               For f = new For ((Statement) $5, (BooleanExpression) $6, (Statement) $8, (Statement) $10, GetLocation ($1));
+               if ($7 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($7));
+         
+               For f = new For ((Statement) $1, (BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($-2));
                current_block.AddStatement (f);
                
-               lbag.AddStatement (f, GetLocation ($2), GetLocation ($4), GetLocation ($7), GetLocation ($9));
+               lbag.AddStatement (f, current_block.StartLocation, GetLocation ($2), GetLocation ($4), GetLocation ($6));
 
-               $$ = end_block (lexer.Location);
+               $$ = end_block (GetLocation ($2));
+         }
+       | error
+         {
+               Error_SyntaxError (yyToken);
+               $$ = end_block (current_block.StartLocation);
          }
        ;
 
@@ -4994,7 +5080,18 @@ opt_for_initializer
        ;
 
 for_initializer
-       : local_variable_declaration
+       : variable_type IDENTIFIER
+         {
+               var lt = (Tokenizer.LocatedToken) $2;
+               var li = new LocalVariable (current_block, lt.Value, lt.Location);
+               current_block.AddLocalName (li);
+               current_variable = new BlockVariableDeclaration ((FullNamedExpression) $1, li);
+         }
+         opt_local_variable_initializer opt_variable_declarators
+         {
+               $$ = current_variable;
+               current_variable = null;
+         }
        | statement_expression_list
        ;
 
@@ -5013,20 +5110,19 @@ for_iterator
        ;
 
 statement_expression_list
-       : statement_expression  
-         {
-               Statement s = (Statement) $1;
-               Block b = new Block (current_block, s.loc, lexer.Location);   
-
-               b.AddStatement (s);
-               $$ = b;
-         }
+       : statement_expression
        | statement_expression_list COMMA statement_expression
          {
-               ((Block) $1).AddStatement ((Statement) $3);
-               lbag.AddStatement ($3, GetLocation ($2));
-               
-               $$ = $1;
+               var sl = $1 as StatementList;
+               if (sl == null) {
+                       sl = new StatementList ((Statement) $1, (Statement) $3);
+                       lbag.AddStatement (sl, GetLocation ($2));
+               } else {
+                       sl.Add ((Statement) $3);
+                       lbag.AppendTo (sl, GetLocation ($2));
+               }
+                       
+               $$ = sl;
          }
        ;
 
@@ -5036,37 +5132,26 @@ foreach_statement
                Report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
                $$ = null;
          }
-       | FOREACH open_parens_any type IDENTIFIER IN
-         expression CLOSE_PARENS 
+       | FOREACH open_parens_any type IDENTIFIER IN expression CLOSE_PARENS 
          {
-               start_block (lexer.Location);
-               Block foreach_block = current_block;
-
+               start_block (GetLocation ($2));
+               current_block.IsCompilerGenerated = true;
+               
                var lt = (Tokenizer.LocatedToken) $4;
-               Location l = lt.Location;
-               LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
-               if (vi != null) {
-                       vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
-
-                       // Get a writable reference to this read-only variable.
-                       //
-                       // Note that the $$ here refers to the value of _this_ code block,
-                       // not the value of the LHS non-terminal.  This can be referred to as $8 below.
-                       $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
-               } else {
-                       $$ = null;
-               }
+               var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
+               current_block.AddLocalName (li);
+               $$ = li;
          } 
-         embedded_statement 
+         embedded_statement
          {
-               if ($8 != null) {
-                       Foreach f = new Foreach ((Expression) $3, (LocalVariableReference) $8, (Expression) $6, (Statement) $9, GetLocation ($1));
-                       lbag.AddStatement (f, GetLocation ($2), GetLocation ($5), GetLocation ($7));
-                       
-                       current_block.AddStatement (f);
-               }
-
-               $$ = end_block (lexer.Location);
+               if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($9));
+         
+               Foreach f = new Foreach ((Expression) $3, (LocalVariable) $8, (Expression) $6, (Statement) $9, GetLocation ($1));
+               current_block.AddStatement (f);
+               
+               lbag.AddStatement (f, GetLocation ($2), GetLocation ($5), GetLocation ($7));
+               $$ = end_block (GetLocation ($7));
          }
        ;
 
@@ -5139,11 +5224,11 @@ yield_statement
                        Report.Error (1003, lt.Location, "; expected");
                } else if ($3 == null) {
                        Report.Error (1627, GetLocation ($4), "Expression expected after yield return");
-               } else if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotAvailable (lt.Location, "iterators");
+               } else if (lang_version == LanguageVersion.ISO_1){
+                       FeatureIsNotAvailable (lt.Location, "iterators");
                }
                
-               current_block.Toplevel.IsIterator = true;
+               current_block.ParametersBlock.TopBlock.IsIterator = true;
                $$ = new Yield ((Expression) $3, lt.Location);
                lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
          }
@@ -5153,11 +5238,11 @@ yield_statement
                string s = lt.Value;
                if (s != "yield"){
                        Report.Error (1003, lt.Location, "; expected");
-               } else if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotAvailable (lt.Location, "iterators");
+               } else if (lang_version == LanguageVersion.ISO_1){
+                       FeatureIsNotAvailable (lt.Location, "iterators");
                }
                
-               current_block.Toplevel.IsIterator = true;
+               current_block.ParametersBlock.TopBlock.IsIterator = true;
                $$ = new YieldBreak (lt.Location);
                lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
          }
@@ -5217,61 +5302,44 @@ catch_clauses
        ;
 
 opt_identifier
-       : /* empty */   { $$ = null; }
+       : /* empty */
        | IDENTIFIER
        ;
 
 catch_clause 
-       : CATCH opt_catch_args 
+       : CATCH block
          {
-               if ($2 != null) {
-                       var cc = (Tuple<FullNamedExpression, Tokenizer.LocatedToken>) $2;
-                       var lt = cc.Item2;
-
-                       if (lt != null){
-                               List<object> one = new List<object> (1);
-
-                               one.Add (new VariableDeclaration (lt, null));
+               $$ = new Catch ((Block) $2, GetLocation ($1));
+         }
+       | CATCH open_parens_any type opt_identifier CLOSE_PARENS
+         {
+               start_block (GetLocation ($2));
+               var c = new Catch (current_block, GetLocation ($1));
+               c.TypeExpression = (FullNamedExpression) $3;
 
-                               start_block (lexer.Location);
-                               current_block = declare_local_variables (cc.Item1, one, lt.Location);
-                       }
-               }
-         } block {
-               Expression type = null;
-               string id = null;
-               Block var_block = null;
-
-               if ($2 != null){
-                       var cc = (Tuple<FullNamedExpression, Tokenizer.LocatedToken>) $2;
-                       type = cc.Item1;
-                       var lt = cc.Item2;
-
-                       if (lt != null){
-                               id = lt.Value;
-                               var_block = end_block (lexer.Location);
-                       }
+               if ($4 != null) {
+                       var lt = (Tokenizer.LocatedToken) $4;
+                       c.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
+                       current_block.AddLocalName (c.Variable);
                }
-
-               $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
+               
+               lbag.AddLocation (c, GetLocation ($2), GetLocation ($5));
+               $$ = c;
          }
-        ;
-
-opt_catch_args
-       : /* empty */ { $$ = null; }
-       | catch_args
-       ;         
-
-catch_args 
-       : open_parens_any type opt_identifier CLOSE_PARENS 
+         block_prepared
          {
-               $$ = new Tuple<FullNamedExpression, Tokenizer.LocatedToken> ((FullNamedExpression)$2, (Tokenizer.LocatedToken) $3);
-               lbag.AddLocation ($$, GetLocation ($1), GetLocation ($4));
+               $$ = $6;
          }
-       | open_parens_any CLOSE_PARENS 
+       | CATCH open_parens_any error
          {
-               Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
-               $$ = null;
+               if (yyToken == Token.CLOSE_PARENS) {
+                       Report.Error (1015, lexer.Location,
+                               "A type that derives from `System.Exception', `object', or `string' expected");
+               } else {
+                       Error_SyntaxError (yyToken);
+               }
+               
+               $$ = new Catch (null, GetLocation ($1));
          }
        ;
 
@@ -5292,142 +5360,98 @@ unchecked_statement
 unsafe_statement
        : UNSAFE
          {
-               if (!RootContext.Unsafe)
+               if (!settings.Unsafe)
                        Error_UnsafeCodeNotAllowed (GetLocation ($1));
          } block {
                $$ = new Unsafe ((Block) $3, GetLocation ($1));
          }
        ;
 
-fixed_statement
-       : FIXED open_parens_any 
-         type_and_void fixed_pointer_declarators 
-         CLOSE_PARENS
-         {
-               start_block (lexer.Location);
-         }
-         embedded_statement 
+lock_statement
+       : LOCK open_parens_any expression CLOSE_PARENS embedded_statement
          {
-               Expression type = (Expression) $3;
-               var list = (List<KeyValuePair<Tokenizer.LocatedToken, Expression>>) $4;
-               Fixed f = new Fixed (type,
-                       list.ConvertAll (i => {
-                               var v = new KeyValuePair<LocalInfo, Expression> (current_block.AddVariable (type, i.Key.Value, i.Key.Location), i.Value);
-                               if (v.Key != null) {
-                                       v.Key.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
-                                       v.Key.Pinned = true;
-                               }
-                               return v;
-                       }), (Statement) $7, GetLocation ($1));
-                       
-               lbag.AddStatement (f, GetLocation ($2), GetLocation ($5));
-
-               current_block.AddStatement (f);
-
-               $$ = end_block (lexer.Location);
+               if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($5));
+         
+               $$ = new Lock ((Expression) $3, (Statement) $5, GetLocation ($1));
+               lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
          }
        ;
 
-fixed_pointer_declarators
-       : fixed_pointer_declarator      { 
-               var declarators = new List<KeyValuePair<Tokenizer.LocatedToken, Expression>> (2);
-               if ($1 != null)
-                       declarators.Add ((KeyValuePair<Tokenizer.LocatedToken, Expression>)$1);
-               $$ = declarators;
-         }
-       | fixed_pointer_declarators COMMA fixed_pointer_declarator
+fixed_statement
+       : FIXED open_parens_any variable_type IDENTIFIER
          {
-               var declarators = (List<KeyValuePair<Tokenizer.LocatedToken, Expression>>) $1;
-               if ($3 != null)
-                       declarators.Add ((KeyValuePair<Tokenizer.LocatedToken, Expression>)$3);
-               $$ = declarators;
+           start_block (GetLocation ($2));
+           
+               current_block.IsCompilerGenerated = true;
+               var lt = (Tokenizer.LocatedToken) $4;
+               var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.FixedVariable | LocalVariable.Flags.Used, lt.Location);
+               current_block.AddLocalName (li);
+               current_variable = new Fixed.VariableDeclaration ((FullNamedExpression) $3, li);
          }
-       ;
-
-fixed_pointer_declarator
-       : IDENTIFIER ASSIGN expression
+         using_or_fixed_variable_initializer opt_variable_declarators CLOSE_PARENS
          {
-               var lt = (Tokenizer.LocatedToken) $1;
-               $$ = new KeyValuePair<Tokenizer.LocatedToken, Expression> (lt, (Expression) $3);
+               $$ = current_variable;
+               current_variable = null;
          }
-       | IDENTIFIER
+         embedded_statement
          {
-               Report.Error (210, ((Tokenizer.LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
-               $$ = null;
+               if ($10 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($10));
+         
+               Fixed f = new Fixed ((Fixed.VariableDeclaration) $9, (Statement) $10, GetLocation ($1));
+               current_block.AddStatement (f);
+               $$ = end_block (GetLocation ($8));
          }
        ;
 
-lock_statement
-       : LOCK open_parens_any expression CLOSE_PARENS embedded_statement
+using_statement
+       : USING open_parens_any variable_type IDENTIFIER
          {
-               $$ = new Lock ((Expression) $3, (Statement) $5, GetLocation ($1));
-               lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
+           start_block (GetLocation ($2));
+           
+               current_block.IsCompilerGenerated = true;
+               var lt = (Tokenizer.LocatedToken) $4;
+               var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.UsingVariable | LocalVariable.Flags.Used, lt.Location);
+               current_block.AddLocalName (li);
+               current_variable = new Using.VariableDeclaration ((FullNamedExpression) $3, li);
          }
-       ;
-
-using_statement
-       : USING open_parens_any local_variable_declaration CLOSE_PARENS
+         using_or_fixed_variable_initializer opt_variable_declarators CLOSE_PARENS
          {
-               start_block (lexer.Location);
-               Block assign_block = current_block;
-
-               var de = (Tuple<FullNamedExpression, List<object>>) $3;
-               Location l = GetLocation ($1);
-
-               var vars = new Stack<Tuple<LocalVariableReference, Expression>> ();
-
-               foreach (VariableDeclaration decl in de.Item2) {
-                       LocalInfo vi = current_block.AddVariable (de.Item1, decl.identifier, decl.Location);
-                       if (vi == null)
-                               continue;
-                       vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
-
-                       Expression expr = decl.GetInitializer (de.Item1);
-                       if (expr == null) {
-                               Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
-                               continue;
-                       }
-                       LocalVariableReference var;
-
-                       // Get a writable reference to this read-only variable.
-                       var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
-
-                       // This is so that it is not a warning on using variables
-                       vi.Used = true;
-
-                       vars.Push (new Tuple<LocalVariableReference, Expression> (var, expr));
-
-                       // Assign a = new SimpleAssign (var, expr, decl.Location);
-                       // assign_block.AddStatement (new StatementExpression (a));
-               }
-
-               // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
-               // It can be referred to as $5 below.
-               $$ = vars;
+               $$ = current_variable;    
+               current_variable = null;
          }
          embedded_statement
          {
-               Statement stmt = (Statement) $6;
-               var vars = (Stack<Tuple<LocalVariableReference, Expression>>) $5;
-               Location l = GetLocation ($1);
-
-               while (vars.Count > 0) {
-                         var de = vars.Pop ();
-                         stmt = new Using (de.Item1, de.Item2, stmt, l);
-               }
-               current_block.AddStatement (stmt);
-               $$ = end_block (lexer.Location);
+               if ($10 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($10));
+         
+               Using u = new Using ((Using.VariableDeclaration) $9, (Statement) $10, GetLocation ($1));
+               current_block.AddStatement (u);
+               $$ = end_block (GetLocation ($8));
          }
-       | USING open_parens_any expression CLOSE_PARENS
+       | USING open_parens_any expression CLOSE_PARENS embedded_statement
          {
-               start_block (lexer.Location);
+               if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
+                       Warning_EmptyStatement (GetLocation ($5));
+         
+               Using u = new Using ((Expression) $3, (Statement) $5, GetLocation ($1));
+               lbag.AddStatement (u, GetLocation ($2), GetLocation ($4));
+               $$ = u;
          }
-         embedded_statement
+       ;
+       
+using_or_fixed_variable_initializer
+       : /* empty */
          {
-               current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, GetLocation ($1)));
-               $$ = end_block (lexer.Location);
+               Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
          }
-       ; 
+       | ASSIGN variable_initializer
+         {
+               current_variable.Initializer = (Expression) $2;
+               $$ = current_variable;
+         }
+       ;
 
 
 // LINQ
@@ -5474,18 +5498,20 @@ query_expression
 first_from_clause
        : FROM_FIRST IDENTIFIER IN expression
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          
-               var lt = (Tokenizer.LocatedToken) $2;     
-               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
+               var lt = (Tokenizer.LocatedToken) $2;
+               var rv = new Linq.RangeVariable (lt.Value, lt.Location);
+               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1)));
          }
        | FROM_FIRST type IDENTIFIER IN expression
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          
                var lt = (Tokenizer.LocatedToken) $3;
+               var rv = new Linq.RangeVariable (lt.Value, lt.Location);
                $$ = new Linq.QueryExpression (
-                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
                                IdentifierType = (FullNamedExpression)$2
                        }
                );
@@ -5495,18 +5521,20 @@ first_from_clause
 nested_from_clause
        : FROM IDENTIFIER IN expression
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          
-               var lt = (Tokenizer.LocatedToken) $2;     
-               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
+               var lt = (Tokenizer.LocatedToken) $2;
+               var rv = new Linq.RangeVariable (lt.Value, lt.Location);
+               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1)));
          }
        | FROM type IDENTIFIER IN expression
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          
                var lt = (Tokenizer.LocatedToken) $3;
+               var rv = new Linq.RangeVariable (lt.Value, lt.Location);
                $$ = new Linq.QueryExpression (
-                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
                                IdentifierType = (FullNamedExpression)$2
                        }
                );
@@ -5516,12 +5544,12 @@ nested_from_clause
 from_clause
        : FROM IDENTIFIER IN
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $2;
-               var sn = new SimpleMemberName (lt.Value, lt.Location);
+               var sn = new Linq.RangeVariable (lt.Value, lt.Location);
                $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
@@ -5531,12 +5559,12 @@ from_clause
          }       
        | FROM type IDENTIFIER IN
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $3;
-               var sn = new SimpleMemberName (lt.Value, lt.Location);
+               var sn = new Linq.RangeVariable (lt.Value, lt.Location);
 
                $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
                        IdentifierType = (FullNamedExpression)$2
@@ -5566,12 +5594,17 @@ query_body
                $$ = head;
          }
        | opt_query_body_clauses COMPLETE_COMPLETION
+       | error
+         {
+               Error_SyntaxError (yyToken);
+               $$ = null;
+         }
        ;
        
 select_or_group_clause
        : SELECT
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          expression
          {
@@ -5585,7 +5618,7 @@ select_or_group_clause
                if (linq_clause_blocks == null)
                        linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
                linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
          }
          expression
@@ -5593,7 +5626,7 @@ select_or_group_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
          
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          BY expression
          {
@@ -5629,12 +5662,12 @@ query_body_clause
 let_clause
        : LET IDENTIFIER ASSIGN 
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $2;
-               var sn = new SimpleMemberName (lt.Value, lt.Location);
+               var sn = new Linq.RangeVariable (lt.Value, lt.Location);
                $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
@@ -5647,11 +5680,11 @@ let_clause
 where_clause
        : WHERE
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
-         boolean_expression
+         expression
          {
-               $$ = new Linq.Where ((Linq.QueryBlock)current_block, (BooleanExpression)$3, GetLocation ($1));
+               $$ = new Linq.Where ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
 
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5664,7 +5697,7 @@ join_clause
                if (linq_clause_blocks == null)
                        linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
                linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression ON
@@ -5672,7 +5705,7 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
                linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression EQUALS
@@ -5681,38 +5714,50 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          expression opt_join_into
          {
-               var lt = (Tokenizer.LocatedToken) $2;
-               var sn = new SimpleMemberName (lt.Value, lt.Location);
-               SimpleMemberName sn2 = null;
-               
+               current_block.AddStatement (new ContextualReturn ((Expression) $11));
+               current_block.SetEndLocation (lexer.Location);
+         
                var outer_selector = linq_clause_blocks.Pop ();
                var block = linq_clause_blocks.Pop ();
 
+               var lt = (Tokenizer.LocatedToken) $2;   
+               var sn = new Linq.RangeVariable (lt.Value, lt.Location);
+               Linq.RangeVariable into;
+               
                if ($12 == null) {
+                       into = sn;
                        $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
                } else {
-                       var lt2 = (Tokenizer.LocatedToken) $12;
-                       sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
-                       $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block,
-                               sn2, GetLocation ($1));
+                       //
+                       // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
+                       //
+                       var parent = block.Parent;
+                       while (parent is Linq.QueryBlock) {
+                               parent = parent.Parent;
+                       }
+                       current_block.Parent = parent;
+                       
+                       ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
+               
+                       lt = (Tokenizer.LocatedToken) $12;
+                       into = new Linq.RangeVariable (lt.Value, lt.Location);
+
+                       $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1));   
                }
 
-               current_block.AddStatement (new ContextualReturn ((Expression) $11));
-               current_block.SetEndLocation (lexer.Location);
-               current_block = current_block.Parent;
-                       
-               ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
+               current_block = block.Parent;
+               ((Linq.QueryBlock)current_block).AddRangeVariable (into);
          }
        | JOIN type IDENTIFIER IN
          {
                if (linq_clause_blocks == null)
                        linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
                linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression ON
@@ -5720,7 +5765,7 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
                linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression EQUALS
@@ -5729,33 +5774,47 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          expression opt_join_into
          {
-               var lt = (Tokenizer.LocatedToken) $3;
-               var sn = new SimpleMemberName (lt.Value, lt.Location);
-               SimpleMemberName sn2 = null;
+               current_block.AddStatement (new ContextualReturn ((Expression) $12));
+               current_block.SetEndLocation (lexer.Location);
+         
                var outer_selector = linq_clause_blocks.Pop ();
                var block = linq_clause_blocks.Pop ();
                
+               var lt = (Tokenizer.LocatedToken) $3;
+               var sn = new Linq.RangeVariable (lt.Value, lt.Location);
+               Linq.RangeVariable into;
+               
                if ($13 == null) {
+                       into = sn;              
                        $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
                                IdentifierType = (FullNamedExpression)$2
                        };
                } else {
-                       var lt2 = (Tokenizer.LocatedToken) $13;
-                       sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
-                       $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, sn2, GetLocation ($1)) {
+                       //
+                       // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
+                       //
+                       var parent = block.Parent;
+                       while (parent is Linq.QueryBlock) {
+                               parent = parent.Parent;
+                       }
+                       current_block.Parent = parent;
+               
+                       ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
+               
+                       lt = (Tokenizer.LocatedToken) $13;
+                       into = new Linq.RangeVariable (lt.Value, lt.Location); // TODO:
+                       
+                       $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1)) {
                                IdentifierType = (FullNamedExpression)$2
                        };                      
                }
                
-               current_block.AddStatement (new ContextualReturn ((Expression) $12));
-               current_block.SetEndLocation (lexer.Location);
-               current_block = current_block.Parent;
-                       
-               ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
+               current_block = block.Parent;
+               ((Linq.QueryBlock)current_block).AddRangeVariable (into);               
          }
        ;
        
@@ -5770,7 +5829,7 @@ opt_join_into
 orderby_clause
        : ORDERBY
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          orderings
          {
@@ -5788,7 +5847,7 @@ orderings
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
          
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
          }
          orderings_then_by
          {
@@ -5804,7 +5863,7 @@ orderings_then_by
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
          
-               current_block = new Linq.QueryBlock (compiler, (Linq.QueryBlock) current_block, lexer.Location);         
+               current_block = new Linq.QueryBlock ((Linq.QueryBlock) current_block, lexer.Location);   
         }
         then_by
         {
@@ -5855,7 +5914,7 @@ opt_query_continuation
                current_block.SetEndLocation (GetLocation ($1));
                current_block = current_block.Parent;
        
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+               current_block = new Linq.QueryBlock (current_block, lexer.Location);
                
                if (linq_clause_blocks == null)
                        linq_clause_blocks = new Stack<Linq.QueryBlock> ();
@@ -5866,7 +5925,8 @@ opt_query_continuation
          {
                var current_block = linq_clause_blocks.Pop ();    
                var lt = (Tokenizer.LocatedToken) $2;
-               $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+               var rv = new Linq.RangeVariable (lt.Value, lt.Location);
+               $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, rv, GetLocation ($1)) {
                        next = (Linq.AQueryClause)$4
                };
          }
@@ -5887,34 +5947,35 @@ opt_query_continuation
 
 interactive_parsing
        : EVAL_STATEMENT_PARSER EOF 
-       | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives 
-       | EVAL_STATEMENT_PARSER { 
-               Evaluator.LoadAliases (current_namespace);
-
-               push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
-                       Modifiers.PUBLIC, null), null);
-
-               var baseclass_list = new List<FullNamedExpression> ();
-               baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
-               current_container.AddBasesForPart (current_class, baseclass_list);
+       | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives opt_COMPLETE_COMPLETION
+       | EVAL_STATEMENT_PARSER
+        { 
+               current_container = new Class (current_namespace, current_class, new MemberName ("<InteractiveExpressionClass>"), Modifiers.PUBLIC, null);
+               current_class = current_container;
 
                // (ref object retval)
                Parameter [] mpar = new Parameter [1];
                mpar [0] = new Parameter (new TypeExpression (TypeManager.object_type, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null);
 
-               ParametersCompiled pars = new ParametersCompiled (compiler, mpar);
+               ParametersCompiled pars = new ParametersCompiled (mpar);
+               var mods = Modifiers.PUBLIC | Modifiers.STATIC;
+               if (settings.Unsafe)
+                       mods |= Modifiers.UNSAFE;
+
                current_local_parameters = pars;
                Method method = new Method (
                        current_class,
                        null, // generic
                        new TypeExpression (TypeManager.void_type, Location.Null),
-                       Modifiers.PUBLIC | Modifiers.STATIC,
+                       mods,
                        new MemberName ("Host"),
                        pars,
                        null /* attributes */);
+                       
+               current_container.AddMethod (method);                   
 
                oob_stack.Push (method);
-               ++lexer.parsing_block;
+               ++lexer.parsing_block;
                start_block (lexer.Location);
          }             
          interactive_statement_list opt_COMPLETE_COMPLETION
@@ -5923,16 +5984,11 @@ interactive_parsing
                Method method = (Method) oob_stack.Pop ();
 
                method.Block = (ToplevelBlock) end_block(lexer.Location);
-               current_container.AddMethod (method);
 
-               --lexer.parsing_declaration;
-               InteractiveResult = pop_current_class ();
+               InteractiveResult = (Class) pop_current_class ();
                current_local_parameters = null;
          } 
-       | EVAL_COMPILATION_UNIT_PARSER {
-               Evaluator.LoadAliases (current_namespace);
-         }
-         interactive_compilation_unit
+       | EVAL_COMPILATION_UNIT_PARSER interactive_compilation_unit
        ;
 
 interactive_compilation_unit
@@ -5953,67 +6009,10 @@ close_brace_or_complete_completion
        ;
 %%
 
-// <summary>
-//   A class used to pass around variable declarations and constants
-// </summary>
-class VariableDeclaration {
-       public string identifier;
-       Expression initializer;
-       public Location Location;
-       public Attributes OptAttributes;
-       public string DocComment;
-
-       public VariableDeclaration (Tokenizer.LocatedToken lt, Expression initializer, Attributes opt_attrs)
-       {
-               this.identifier = lt.Value;
-               this.initializer = initializer;
-               this.Location = lt.Location;
-               this.OptAttributes = opt_attrs;
-       }
-
-       public VariableDeclaration (Tokenizer.LocatedToken lt, Expression initializer)
-               : this (lt, initializer, null)
-       {
-       }
-
-       public Expression GetInitializer (FullNamedExpression type)
-       {
-               if (initializer is ArrayInitializer)
-                       return new ArrayCreation (type, (ArrayInitializer)initializer);
-
-               return initializer;
-       }
-
-       public bool HasInitializer {
-               get { return initializer != null; }
-       }
-}
-
-class VariableMemberDeclaration
-{
-       public readonly MemberName MemberName;
-       Expression initializer;
-       
-       public VariableMemberDeclaration (MemberName mn, Expression initializer)
-       {
-               MemberName = mn;
-               this.initializer = initializer;
-       }
-
-       public Expression GetInitializer (FullNamedExpression type)
-       {
-               if (initializer is ArrayInitializer)
-                       return new ArrayCreation (type, (ArrayInitializer)initializer);
-
-               return initializer;
-       }
-}
-
-
 // <summary>
 //  A class used to hold info about an operator declarator
 // </summary>
-struct OperatorDeclaration {
+class OperatorDeclaration {
        public readonly Operator.OpType optype;
        public readonly FullNamedExpression ret_type;
        public readonly Location location;
@@ -6057,6 +6056,11 @@ void Error_UnsafeCodeNotAllowed (Location loc)
        Report.Error (227, loc, "Unsafe code requires the `unsafe' command line option to be specified");
 }
 
+void Warning_EmptyStatement (Location loc)
+{
+       Report.Warning (642, 3, loc, "Possible mistaken empty statement");
+}
+
 void Error_NamedArgumentExpected (NamedArgument a)
 {
        Report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
@@ -6064,8 +6068,10 @@ void Error_NamedArgumentExpected (NamedArgument a)
 
 void push_current_class (TypeContainer tc, object partial_token)
 {
-       if (RootContext.EvalMode){
-               tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
+       if (module.Evaluator != null){
+               tc.Definition.Modifiers = tc.ModFlags = (tc.ModFlags & ~Modifiers.AccessibilityMask) | Modifiers.PUBLIC;
+               if (undo == null)
+                       undo = new Undo ();
                undo.AddTypeContainer (current_container, tc);
        }
 
@@ -6119,114 +6125,6 @@ void StoreModifierLocation (object token, Location loc)
        mod_locations.Add (Tuple.Create ((Modifiers) token, loc));
 }
 
-Block declare_local_variables (FullNamedExpression type, List<object> variable_declarators, Location loc)
-{
-       Block implicit_block;
-
-       //
-       // If we are doing interactive editing, we want variable declarations
-       // that are in the top block to be added instead to the class as 
-       // static variables
-       //
-       if (RootContext.StatementMode){
-               bool hoist = true;
-
-               for (Block b = current_block; b != null; b = b.Parent){
-                       if (b is ExplicitBlock && !(b is ToplevelBlock)){
-                               // There has been an explicit block, we cant add to the class
-                               hoist = false;
-                               break;
-                       }
-               }               
-               if (hoist){
-                       //
-                       // We can use "current_block" since we know there are no explicit blocks
-                       //
-                       foreach (VariableDeclaration decl in variable_declarators){
-                               // We can not use the super-handy f.Initializer, because
-                               // multiple lines would force code to be executed out of sync
-                               var init = decl.GetInitializer (type);
-                               if (init != null){
-                                       string id = "$" + decl.identifier;
-                                       LocalInfo vi = current_block.AddVariable (type, id, decl.Location);                                     
-
-                                       // Avoid warning about this variable not being used.
-                                       vi.Used = true;
-
-                                       LocalVariableReference var;
-                                       var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, vi, decl.Location);
-                                       Assign assign = new SimpleAssign (var, init, decl.Location);
-                                       current_block.AddStatement (new StatementExpression (assign));
-                                       assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
-                                       current_block.AddStatement (new StatementExpression (assign));
-                               } else {
-                                       Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
-                                               new MemberName (decl.identifier, loc), null);
-                                       current_container.AddField (f);
-
-                                       // Register the field to be visible later as a global variable
-                                       Evaluator.QueueField (f);
-                               }
-                       }
-
-                       return current_block;
-               }
-       }
-
-       //
-       // We use the `Used' property to check whether statements
-       // have been added to the current block.  If so, we need
-       // to create another block to contain the new declaration
-       // otherwise, as an optimization, we use the same block to
-       // add the declaration.
-       //
-       // FIXME: A further optimization is to check if the statements
-       // that were added were added as part of the initialization
-       // below.  In which case, no other statements have been executed
-       // and we might be able to reduce the number of blocks for
-       // situations like this:
-       //
-       // int j = 1;  int k = j + 1;
-       //
-       if (current_block.Used)
-               implicit_block = new Block (current_block, loc, lexer.Location);
-       else
-               implicit_block = current_block;
-
-       foreach (VariableDeclaration decl in variable_declarators){
-
-               if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
-                       if (decl.HasInitializer){
-                               Assign assign;
-                               
-                               var lvr = new LocalVariableReference (implicit_block, decl.identifier, loc);
-
-                               assign = new SimpleAssign (lvr, decl.GetInitializer (type), decl.Location);
-
-                               implicit_block.AddStatement (new StatementExpression (assign));
-                       }
-               }
-       }
-       
-       return implicit_block;
-}
-
-Block declare_local_constants (FullNamedExpression type, List<object> declarators)
-{
-       Block implicit_block;
-
-       if (current_block.Used)
-               implicit_block = new Block (current_block);
-       else
-               implicit_block = current_block;
-
-       foreach (VariableDeclaration decl in declarators){
-               implicit_block.AddConstant (type, decl.identifier, decl.GetInitializer (type), decl.Location);
-       }
-       
-       return implicit_block;
-}
-
 string CheckAttributeTarget (string a, Location l)
 {
        switch (a) {
@@ -6274,18 +6172,24 @@ static CSharpParser ()
        oob_stack = new Stack<object> ();
 }
 
-public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
+public CSharpParser (SeekableStreamReader reader, CompilationUnit file, ModuleContainer module)
+       : this (reader, file, module, new NamespaceEntry (module, null, file, null))
 {
-       if (RootContext.EvalMode)
-               undo = new Undo ();
+}
 
+public CSharpParser (SeekableStreamReader reader, CompilationUnit file, ModuleContainer module, NamespaceEntry ns)
+{
        this.file = file;
-       this.compiler = ctx;
-       current_namespace = new NamespaceEntry (null, file, null);
+       this.module = module;
+       this.compiler = module.Compiler;
+       this.settings = compiler.Settings;
+       lang_version = settings.Version;
+       doc_support = settings.Documentation != null;
+       current_namespace = ns;
        current_class = current_namespace.SlaveDeclSpace;
        current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
        oob_stack.Clear ();
-       lexer = new Tokenizer (reader, file, ctx);
+       lexer = new Tokenizer (reader, file, compiler);
        
        use_global_stacks = true;
 }
@@ -6304,15 +6208,21 @@ public void parse ()
                Tokenizer tokenizer = lexer as Tokenizer;
                tokenizer.cleanup ();           
        } catch (Exception e){
-               if (e is yyParser.yyUnexpectedEof)
+               if (e is yyParser.yyUnexpectedEof) {
+                       Error_SyntaxError (yyToken);
                        UnexpectedEOF = true;
-
-               if (e is yyParser.yyException)
+                       return;
+               }
+                       
+               if (e is yyParser.yyException) {
                        Report.Error (-25, lexer.Location, "Parsing error");
-               else if (yacc_verbose_flag > 0)
-                       throw;  // Used by compiler-tester to test internal errors
-               else 
+               } else {
+                       // Used by compiler-tester to test internal errors
+                       if (yacc_verbose_flag > 0)
+                               throw;
+               
                        Report.Error (589, lexer.Location, "Internal compiler error during parsing");
+               }
        }
 
        if (RootContext.ToplevelTypes.NamespaceEntry != null)
@@ -6327,11 +6237,6 @@ void CheckToken (int error, int yyToken, string msg, Location loc)
                Report.Error (error, loc, msg);
 }
 
-void CheckIdentifierToken (int yyToken, Location loc)
-{
-       CheckToken (1041, yyToken, "Identifier expected", loc);
-}
-
 string ConsumeStoredComment ()
 {
        string s = tmpComment;
@@ -6340,15 +6245,24 @@ string ConsumeStoredComment ()
        return s;
 }
 
-Location GetLocation (object obj)
+void FeatureIsNotAvailable (Location loc, string feature)
 {
-       if (obj is Tokenizer.LocatedToken)
-               return ((Tokenizer.LocatedToken) obj).Location;
-       if (obj is MemberName)
-               return ((MemberName) obj).Location;
+       compiler.Report.FeatureIsNotAvailable (compiler, loc, feature);
+}
 
-       if (obj is Expression)
-               return ((Expression) obj).Location;
+Location GetLocation (object obj)
+{
+       var lt = obj as Tokenizer.LocatedToken;
+       if (lt != null)
+               return lt.Location;
+               
+       var mn = obj as MemberName;
+       if (mn != null)
+               return mn.Location;
+               
+       var expr = obj as Expression;
+       if (expr != null)
+               return expr.Location;
 
        return lexer.Location;
 }
@@ -6368,8 +6282,11 @@ public LocationsBag LocationsBag {
 
 void start_block (Location loc)
 {
-       if (current_block == null || parsing_anonymous_method) {
-               current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, loc);
+       if (current_block == null) {
+               current_block = new ToplevelBlock (compiler, current_local_parameters, loc);
+               parsing_anonymous_method = false;
+       } else if (parsing_anonymous_method) {
+               current_block = new ParametersBlock (current_block, current_local_parameters, loc);
                parsing_anonymous_method = false;
        } else {
                current_block = new ExplicitBlock (current_block, loc, Location.Null);
@@ -6385,15 +6302,15 @@ end_block (Location loc)
        return retval;
 }
 
-void
-start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
+void start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
 {
-       if (RootContext.Version == LanguageVersion.ISO_1){
-               Report.FeatureIsNotAvailable (loc, "anonymous methods");
+       if (lang_version == LanguageVersion.ISO_1){
+               FeatureIsNotAvailable (loc, "anonymous methods");
        }
 
        oob_stack.Push (current_anonymous_method);
        oob_stack.Push (current_local_parameters);
+       oob_stack.Push (current_variable);
 
        current_local_parameters = parameters;
 
@@ -6409,13 +6326,14 @@ start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
  * 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)
+AnonymousMethodExpression end_anonymous (ParametersBlock anon_block)
 {
        AnonymousMethodExpression retval;
 
        current_anonymous_method.Block = anon_block;
        retval = current_anonymous_method;
 
+       current_variable = (BlockVariableDeclaration) oob_stack.Pop ();
        current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
        current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
 
@@ -6428,7 +6346,6 @@ public NamespaceEntry CurrentNamespace {
        }
 }
 
-
 void Error_SyntaxError (int token)
 {
        Error_SyntaxError (0, token, "Unexpected symbol");
@@ -6436,20 +6353,34 @@ void Error_SyntaxError (int token)
 
 void Error_SyntaxError (int error_code, int token, string msg)
 {
+       // An error message has been reported by tokenizer
+       if (token == Token.ERROR)
+               return;
+
        string symbol = GetSymbolName (token);
        string expecting = GetExpecting ();
+       var loc = lexer.Location - symbol.Length;
        
        if (error_code == 0) {
-               if (expecting == "`)'")
+               if (expecting == "`identifier'") {
+                       if (token > Token.FIRST_KEYWORD && token < Token.LAST_KEYWORD) {
+                               Report.Error (1041, loc, "Identifier expected, `{0}' is a keyword", symbol);
+                               return;
+                       }
+                       
+                       error_code = 1001;
+                       expecting = "identifier";
+               } else if (expecting == "`)'") {
                        error_code = 1026;
-               else
+               } else {
                        error_code = 1525;
+               }
        }
        
        if (string.IsNullOrEmpty (expecting))
-               Report.Error (error_code, lexer.Location, "{1} `{0}'", symbol, msg);
+               Report.Error (error_code, loc, "{1} `{0}'", symbol, msg);
        else
-               Report.Error (error_code, lexer.Location, "{2} `{0}', expecting {1}", symbol, expecting, msg);    
+               Report.Error (error_code, loc, "{2} `{0}', expecting {1}", symbol, expecting, msg);       
 }
 
 string GetExpecting ()
@@ -6622,6 +6553,8 @@ static string GetTokenName (int token)
                return "as";
        case Token.ADD:
                return "add";
+       case Token.ASYNC:
+               return "async";
        case Token.BASE:
                return "base";
        case Token.BREAK:
@@ -6878,11 +6811,13 @@ static string GetTokenName (int token)
        case Token.IDENTIFIER:
                return "identifier";
 
+       case Token.EOF:
+               return "end-of-file";
+
                // All of these are internal.
        case Token.NONE:
        case Token.ERROR:
        case Token.FIRST_KEYWORD:
-       case Token.EOF:
        case Token.EVAL_COMPILATION_UNIT_PARSER:
        case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
        case Token.EVAL_STATEMENT_PARSER: