2002-05-11 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / cs-parser.jay
index b6605e7dbe73cc28ecd96a1041104abc76222a3f..66ad7e76d3f4f87ad1f90baaf057e20e65082a16 100755 (executable)
@@ -1,8 +1,14 @@
 %{
 //
+// OPTIMIZATION:
+//   This loop is pointless:
+//    while (current_block != prev_block)
+//       current_block = current_block.Parent;
+//
 // cs-parser.jay: The Parser for the C# compiler
 //
 // Author: Miguel de Icaza (miguel@gnu.org)
+//         Ravi Pratap     (ravi@ximian.com)
 //
 // Licensed under the terms of the GNU GPL
 //
@@ -89,6 +95,7 @@ namespace Mono.CSharp
 %token ABSTRACT        
 %token AS
 %token ADD
+%token ASSEMBLY
 %token BASE    
 %token BOOL    
 %token BREAK   
@@ -145,6 +152,7 @@ namespace Mono.CSharp
 %token SEALED  
 %token SHORT   
 %token SIZEOF  
+%token STACKALLOC
 %token STATIC  
 %token STRING  
 %token STRUCT  
@@ -249,21 +257,29 @@ namespace Mono.CSharp
 %nonassoc HIGHPREC
 
 %start compilation_unit
-/*%start namespace_declaration */
 %%
 
 compilation_unit
-       : opt_using_directives opt_namespace_member_declarations opt_EOF
-         {
-               // Check that using comes only before namespace elements
-         }
-       ;
-
+        : outer_declarations opt_EOF
+        | outer_declarations attribute_sections opt_EOF
+        | attribute_sections opt_EOF
+        ;
+       
 opt_EOF
        : /* empty */
        | EOF
        ;
 
+outer_declarations
+        : outer_declaration
+        | outer_declarations outer_declaration
+        ;
+outer_declaration
+        : using_directive
+        | namespace_member_declaration
+        ;
+  
 using_directives
        : using_directive 
        | using_directives using_directive
@@ -289,15 +305,25 @@ using_namespace_directive
           }
        ;
 
-//  namespace_declarations
-//     : namespace_declaration
-//     | namespace_declarations namespace_declaration
-
+//
+// Strictly speaking, namespaces don't have attributes but
+// we parse global attributes along with namespace declarations and then
+// detach them
+// 
 namespace_declaration
-       : NAMESPACE qualified_identifier 
-         {
-               current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $2);
-         } 
+       : opt_attributes NAMESPACE qualified_identifier 
+       {
+               Attributes attrs = (Attributes) $1;
+
+               if (attrs != null) {
+                       foreach (AttributeSection asec in attrs.AttributeSections)
+                               if (asec.Target == "assembly")
+                                       RootContext.AddGlobalAttribute (
+                                               current_container, asec, lexer.Location);
+               }
+
+               current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
+       } 
          namespace_body opt_semicolon
          { 
                current_namespace = current_namespace.Parent;
@@ -393,17 +419,34 @@ type_declaration
 //
 // Attributes 17.2
 //
+
 opt_attributes
-       : /* empty */ { $$ = null; }
-       | attribute_section opt_attributes
+        : /* empty */
+       | attribute_sections { $$ = $1; }
+        ;
+attribute_sections
+       : attribute_section
+          {
+               AttributeSection sect = (AttributeSection) $1;
+
+               if (sect.Target == "assembly")
+                       RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
+                 
+               $$ = new Attributes ((AttributeSection) $1, lexer.Location);
+          }
+       | attribute_sections attribute_section
          {
-               Attributes attrs;
-               
-               if ($2 != null) {
-                       attrs = (Attributes) $2;
-                       attrs.AddAttribute ((AttributeSection) $1);
-               } else
-                       attrs = new Attributes ((AttributeSection) $1);
+               Attributes attrs = null;
+               AttributeSection sect = (AttributeSection) $2;
+
+               if (sect.Target == "assembly")
+                       RootContext.AddGlobalAttribute (current_container, sect, lexer.Location);
+
+               if ($1 != null) {
+                       attrs = (Attributes) $1;
+                       attrs.AddAttribute (sect);
+               }
                
                $$ = attrs;
          }
@@ -485,12 +528,16 @@ opt_attribute_arguments
 
 
 attribute_arguments
-       : positional_argument_list
+       : opt_positional_argument_list
          {
-               ArrayList args = new ArrayList ();
-               args.Add ($1);
+               if ($1 == null)
+                       $$ = null;
+               else {
+                       ArrayList args = new ArrayList ();
+                       args.Add ($1);
                
-               $$ = args;
+                       $$ = args;
+               }
          }
         | positional_argument_list COMMA named_argument_list
          {
@@ -511,6 +558,11 @@ attribute_arguments
         ;
 
 
+opt_positional_argument_list
+       : /* empty */           { $$ = null; } 
+       | positional_argument_list
+       ;
+
 positional_argument_list
        : expression
          {
@@ -595,34 +647,23 @@ struct_declaration
                                         (Attributes) $1, lexer.Location);
                current_container = new_struct;
                current_container.Namespace = current_namespace;
-               RootContext.Tree.RecordStruct (full_struct_name, new_struct);
+               RootContext.Tree.RecordDecl (full_struct_name, new_struct);
          }
-         opt_struct_interfaces
+         opt_class_base
          struct_body
          opt_semicolon
          {
                Struct new_struct = (Struct) current_container;
 
+               if ($6 != null)
+                       new_struct.Bases = (ArrayList) $6;
+
                current_container = current_container.Parent;
-               CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
+               CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
                $$ = new_struct;
          }
        ;
 
-opt_struct_interfaces
-       : /* empty */
-       | struct_interfaces
-       ;
-
-struct_interfaces
-       : struct_interface
-       | struct_interfaces struct_interface
-       ; 
-
-struct_interface
-       : COLON type_list
-       ;
-
 struct_body
        : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
        ;
@@ -664,12 +705,14 @@ constant_declaration
          SEMICOLON
          {
                foreach (VariableDeclaration constant in (ArrayList) $5){
+                       Location l = constant.Location;
+
                        Const c = new Const (
                                (string) $4, (string) constant.identifier, 
-                               (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
-                               constant.Location);
+                               (Expression) constant.expression_or_array_initializer, (int) $2, 
+                               (Attributes) $1, l);
 
-                       CheckDef (current_container.AddConstant (c), c.Name);
+                       CheckDef (current_container.AddConstant (c), c.Name, l);
                }
          }
        ;
@@ -707,10 +750,13 @@ field_declaration
                int mod = (int) $2;
 
                foreach (VariableDeclaration var in (ArrayList) $4){
+                       Location l = var.Location;
+
                        Field field = new Field (type, mod, var.identifier, 
-                                                var.expression_or_array_initializer, (Attributes) $1, var.Location);
+                                                var.expression_or_array_initializer, 
+                                                (Attributes) $1, l);
 
-                       CheckDef (current_container.AddField (field), field.Name);
+                       CheckDef (current_container.AddField (field), field.Name, l);
                }
          }
        ;
@@ -747,9 +793,13 @@ variable_initializer
                $$ = $1;
          }
        | array_initializer
-       {
+         {
                $$ = $1;
-       }
+         }
+       | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
+         {
+               $$ = new StackAlloc ((string) $2, (Expression) $4, lexer.Location);
+         }
        ;
 
 method_declaration
@@ -775,7 +825,7 @@ method_declaration
                }
 
                method.Block = (Block) $2;
-               CheckDef (current_container.AddMethod (method), method.Name);
+               CheckDef (current_container.AddMethod (method), method.Name, method.Location);
 
                current_local_parameters = null;
          }
@@ -932,18 +982,14 @@ property_declaration
          { 
                Property prop;
                Pair pair = (Pair) $7;
-               Block get_block = null;
-               Block set_block = null;
-
-               if (pair.First != null)
-                       get_block = (Block) pair.First;
-               if (pair.Second != null)
-                       set_block = (Block) pair.Second;
+               Accessor get_block = (Accessor) pair.First;
+               Accessor set_block = (Accessor) pair.Second;
 
+               Location loc = (Location) $6;
                prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
-                                    (Attributes) $1, (Location) $6);
+                                    (Attributes) $1, loc);
                
-               CheckDef (current_container.AddProperty (prop), prop.Name);
+               CheckDef (current_container.AddProperty (prop), prop.Name, loc);
                implicit_value_parameter_type = null;
          }
        ;
@@ -982,7 +1028,7 @@ get_accessor_declaration
          }
           accessor_body
          {
-               $$ = $4;
+               $$ = new Accessor ((Block) $4);
                current_local_parameters = null;
                lexer.PropertyParsing = true;
          }
@@ -1014,7 +1060,7 @@ set_accessor_declaration
          }
          accessor_body
          {
-               $$ = $4;
+               $$ = new Accessor ((Block) $4);
                current_local_parameters = null;
                lexer.PropertyParsing = true;
          }
@@ -1022,7 +1068,7 @@ set_accessor_declaration
 
 accessor_body
        : block 
-       | SEMICOLON             { $$ = new Block (null); }
+       | SEMICOLON             { $$ = null; }
        ;
 
 interface_declaration
@@ -1041,10 +1087,10 @@ interface_declaration
                }
                current_interface = new_interface;
                new_interface.Namespace = current_namespace;
-               RootContext.Tree.RecordInterface (full_interface_name, new_interface);
+               RootContext.Tree.RecordDecl (full_interface_name, new_interface);
          }
          opt_interface_base
-         interface_body
+         interface_body opt_semicolon
          { 
                Interface new_interface = (Interface) current_interface;
 
@@ -1052,7 +1098,8 @@ interface_declaration
                        new_interface.Bases = (ArrayList) $6;
 
                current_interface = null;
-               CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
+               CheckDef (current_container.AddInterface (new_interface), 
+                         new_interface.Name, new_interface.Location);
          }
        ;
 
@@ -1102,25 +1149,25 @@ interface_member_declaration
          { 
                InterfaceMethod m = (InterfaceMethod) $1;
 
-               CheckDef (current_interface.AddMethod (m), m.Name);
+               CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
          }
        | interface_property_declaration        
          { 
                InterfaceProperty p = (InterfaceProperty) $1;
 
-               CheckDef (current_interface.AddProperty (p), p.Name);
+               CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
           }
        | interface_event_declaration 
           { 
                InterfaceEvent e = (InterfaceEvent) $1;
 
-               CheckDef (current_interface.AddEvent (e), e.Name);
+               CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
          }
        | interface_indexer_declaration
          { 
                InterfaceIndexer i = (InterfaceIndexer) $1;
 
-               CheckDef (current_interface.AddIndexer (i), "indexer");
+               CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
          }
        ;
 
@@ -1200,7 +1247,7 @@ interface_indexer_declaration
        ;
 
 operator_declaration
-       : opt_attributes opt_modifiers operator_declarator block
+       : opt_attributes opt_modifiers operator_declarator operator_body
          {
                OperatorDeclaration decl = (OperatorDeclaration) $3;
                
@@ -1214,6 +1261,10 @@ operator_declaration
          }
        ;
 
+operator_body 
+       : block
+       | SEMICOLON { $$ = null; }
+       ; 
 operator_declarator
        : type OPERATOR overloadable_operator 
          OPEN_PARENS type IDENTIFIER CLOSE_PARENS
@@ -1322,7 +1373,7 @@ constructor_declaration
        : opt_attributes
          opt_modifiers
          constructor_declarator
-         block
+         constructor_body
          { 
                Constructor c = (Constructor) $3;
                c.Block = (Block) $4;
@@ -1331,22 +1382,25 @@ constructor_declaration
 
                if ((c.ModFlags & Modifiers.STATIC) != 0){
                        if ((c.ModFlags & Modifiers.Accessibility) != 0) {
-                               Location l = lexer.Location;
-                               Report.Error (515, l, "Access modifiers are not allowed on static constructors");
+                               Report.Error (
+                                       515, c.Location, 
+                                       "Access modifiers are not allowed on static constructors");
                        }
 
                        if (c.Initializer != null){
-                               Location l = lexer.Location;
-                               Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
+                               Report.Error (
+                                       514, c.Location, 
+                                       "Static constructors can not have an explicit this or base " +
+                                       "constructor invocations");
                        }
 
                        if (!c.Parameters.Empty){
-                               Location l = lexer.Location;
-                               Report.Error (132, l, "Static constructors should not have parameters");
+                               Report.Error (
+                                       132, c.Location, "Static constructors should not have parameters");
                        }
                } 
                
-               CheckDef (current_container.AddConstructor (c), c.Name);
+               CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
 
                current_local_parameters = null;
          }
@@ -1367,6 +1421,11 @@ constructor_declarator
          }
        ;
 
+constructor_body
+       : block
+       | SEMICOLON             { $$ = null; }
+       ;
+
 opt_constructor_initializer
        : /* empty */                   { $$ = null; }
        | constructor_initializer
@@ -1393,12 +1452,18 @@ destructor_declaration
                } else {
                        Location l = lexer.Location;
 
+                       int m;
+                       if (!RootContext.StdLib && current_container.Name == "System.Object")
+                               m = Modifiers.PROTECTED | Modifiers.VIRTUAL;
+                       else
+                               m = Modifiers.PROTECTED | Modifiers.OVERRIDE;
+
                        Method d = new Method (
-                               "System.Void", 0, "Finalize", 
+                               "System.Void", m, "Finalize", 
                                new Parameters (null, null, l), (Attributes) $1, l);
                  
                        d.Block = (Block) $6;
-                       CheckDef (current_container.AddMethod (d), d.Name);
+                       CheckDef (current_container.AddMethod (d), d.Name, d.Location);
                }
          }
        ;
@@ -1410,10 +1475,11 @@ event_declaration
          {
                foreach (VariableDeclaration var in (ArrayList) $5) {
 
-                       Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
+                       Event e = new Event ((string) $4, var.identifier, 
+                                            var.expression_or_array_initializer,
                                             (int) $2, null, null, (Attributes) $1, lexer.Location);
 
-                       CheckDef (current_container.AddEvent (e), e.Name);
+                       CheckDef (current_container.AddEvent (e), e.Name, e.Location);
                                       
                }
          }
@@ -1424,6 +1490,7 @@ event_declaration
          {
                implicit_value_parameter_type = (string) $4;  
                lexer.EventParsing = true;
+               oob_stack.Push (lexer.Location);
          }
          event_accessor_declarations
          {
@@ -1431,6 +1498,8 @@ event_declaration
          }
          CLOSE_BRACE
          {
+               Location loc = (Location) oob_stack.Pop ();
+
                Pair pair = (Pair) $8;
                Block add_block = null;
                Block rem_block = null;
@@ -1441,9 +1510,9 @@ event_declaration
                        rem_block = (Block) pair.Second;
                
                Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
-                                    (Attributes) $1, lexer.Location);
+                                    (Attributes) $1, loc);
                
-               CheckDef (current_container.AddEvent (e), e.Name);
+               CheckDef (current_container.AddEvent (e), e.Name, loc);
                implicit_value_parameter_type = null;
          }
        ;
@@ -1526,13 +1595,8 @@ indexer_declaration
                Indexer indexer;
                IndexerDeclaration decl = (IndexerDeclaration) $3;
                Pair pair = (Pair) $6;
-               Block get_block = null;
-               Block set_block = null;
-
-               if (pair.First != null)
-                       get_block = (Block) pair.First;
-               if (pair.Second != null)
-                       set_block = (Block) pair.Second;
+               Accessor get_block = (Accessor) pair.First;
+               Accessor set_block = (Accessor) pair.Second;
 
                indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
                                       get_block, set_block, (Attributes) $1, (Location) $5);
@@ -1573,22 +1637,32 @@ indexer_declarator
 enum_declaration
        : opt_attributes
          opt_modifiers
-         ENUM IDENTIFIER
+         ENUM IDENTIFIER 
          opt_enum_base
          enum_body
          opt_semicolon
          { 
+               Location enum_location = lexer.Location;
+
                string full_name = MakeName ((string) $4);
-               Enum e = new Enum (current_container, (string) $5, (int) $2, full_name, (Attributes) $1, lexer.Location);
+               Enum e = new Enum (
+                               current_container, (string) $5, (int) $2, full_name, 
+                               (Attributes) $1, enum_location);
 
                foreach (VariableDeclaration ev in (ArrayList) $6){
+                       Location loc = (Location) ev.Location;
+
                        CheckDef (e.AddEnumMember (ev.identifier, 
                                                   (Expression) ev.expression_or_array_initializer,
-                                                  (Location) ev.Location),
-                                 ev.identifier);
+                                                  loc),
+                                 ev.identifier, loc);
                }
 
-               CheckDef (current_container.AddEnum (e), name);
+               e.Namespace = current_namespace;
+
+               CheckDef (current_container.AddEnum (e), full_name, enum_location);
+               RootContext.Tree.RecordDecl (full_name, e);
+
          }
        ;
 
@@ -1651,11 +1725,13 @@ delegate_declaration
          CLOSE_PARENS 
          SEMICOLON
          {
-               Delegate del = new Delegate ((string) $4, (int) $2, 
+               Location l = lexer.Location;
+               Delegate del = new Delegate (current_container, (string) $4, (int) $2, 
                                             MakeName ((string) $5), (Parameters) $7, 
-                                            (Attributes) $1, lexer.Location);
+                                            (Attributes) $1, l);
                  
-               CheckDef (current_container.AddDelegate (del), del.Name);
+               del.Namespace = current_namespace;
+               CheckDef (current_container.AddDelegate (del), del.Name, l);
          }     
        | opt_attributes
          opt_modifiers
@@ -1665,10 +1741,14 @@ delegate_declaration
          CLOSE_PARENS 
          SEMICOLON
          {
-               Delegate del = new Delegate ("System.Void", (int) $2, (string) $5, (Parameters) $7, 
-                                            (Attributes) $1, lexer.Location);
+               Location l = lexer.Location;
+               Delegate del = new Delegate (
+                       current_container,
+                       "System.Void", (int) $2, MakeName ((string) $5), (Parameters) $7, 
+                       (Attributes) $1, l);
 
-               CheckDef (current_container.AddDelegate (del), del.Name);
+               del.Namespace = current_namespace;
+               CheckDef (current_container.AddDelegate (del), del.Name, l);
          }
        ;
 
@@ -1780,6 +1860,7 @@ integral_type
        | LONG          { $$ = "System.Int64"; }
        | ULONG         { $$ = "System.UInt64"; }
        | CHAR          { $$ = "System.Char"; }
+       | VOID          { $$ = "System.Void"; }
        ;
 
 interface_type
@@ -1880,7 +1961,7 @@ member_access
          }
        | predefined_type DOT IDENTIFIER
          {
-               $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
+               $$ = new MemberAccess (new SimpleName ((string) $1, lexer.Location), (string) $3, lexer.Location);
          }
        ;
 
@@ -2399,91 +2480,111 @@ assignment_expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.Multiply, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.Multiply, 
+                                    (Expression) $1,
+                                    (Expression) $3, l), 
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_DIV_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.Division, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.Division, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_MOD_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.Modulus, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.Modulus, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_ADD_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.Addition, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.Addition, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_SUB_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.Subtraction, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.Subtraction, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.LeftShift, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.LeftShift, 
+                                    (Expression) $1,
+                                    (Expression) $3, l), 
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.RightShift, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.RightShift, 
+                                    (Expression) $1,
+                                    (Expression) $3, l), 
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_AND_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.BitwiseAnd, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.BitwiseAnd, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_OR_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.BitwiseOr, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.BitwiseOr, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        | prefixed_unary_expression OP_XOR_ASSIGN expression
          {
                Location l = lexer.Location;
 
-               $$ = new Assign ((Expression) $1,
-                                new Binary (Binary.Operator.ExclusiveOr, 
-                                            (Expression) $1,
-                                            (Expression) $3, l), l);
+               $$ = new CompoundAssign (
+                       (Expression) $1,
+                       new Binary (Binary.Operator.ExclusiveOr, 
+                                    (Expression) $1,
+                                    (Expression) $3, l),
+                       (Expression) $3, l);
          }
        ;
 
@@ -2517,7 +2618,7 @@ class_declaration
                                       (Attributes) $1, lexer.Location);
                current_container = new_class;
                current_container.Namespace = current_namespace;
-               RootContext.Tree.RecordClass (name, new_class);
+               RootContext.Tree.RecordDecl (name, new_class);
          }
          opt_class_base
          class_body 
@@ -2529,7 +2630,7 @@ class_declaration
                        new_class.Bases = (ArrayList) $6;
 
                current_container = current_container.Parent;
-               CheckDef (current_container.AddClass (new_class), new_class.Name);
+               CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
 
                $$ = new_class;
          }
@@ -2597,13 +2698,14 @@ class_base
 block
        : OPEN_BRACE 
          {
-               current_block = new Block (current_block);
+               current_block = new Block (current_block, lexer.Location, Location.Null);
          } 
          opt_statement_list CLOSE_BRACE 
          { 
                while (current_block.Implicit)
                        current_block = current_block.Parent;
                $$ = current_block;
+               current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
          }
        ;
@@ -2789,13 +2891,13 @@ expression_statement
        // because statement_expression is used for example in for_statement
        //
 statement_expression
-       : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
-       | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
+       : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+       | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+       | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+       | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+       | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+       | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+       | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
        | error {
                Report.Error (1002, lexer.Location, "Expecting `;'");
          }
@@ -2812,21 +2914,39 @@ selection_statement
        ; 
 
 if_statement
-       : IF OPEN_PARENS boolean_expression CLOSE_PARENS 
+       : if_statement_open if_statement_rest
+         {
+               $$ = $2;
+         }
+       ;
+
+if_statement_open
+       : IF OPEN_PARENS 
+         {
+               oob_stack.Push (lexer.Location);
+         }
+       ;
+
+if_statement_rest
+       : boolean_expression CLOSE_PARENS 
          embedded_statement
          { 
-               $$ = new If ((Expression) $3, (Statement) $5);
+               Location l = (Location) oob_stack.Pop ();
+
+               $$ = new If ((Expression) $1, (Statement) $3, l);
 
                if (RootContext.WarningLevel >= 3){
-                       if ($5 is EmptyStatement)
+                       if ($3 is EmptyStatement)
                                Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
                }
 
          }
-       | IF OPEN_PARENS boolean_expression CLOSE_PARENS
+       | boolean_expression CLOSE_PARENS
          embedded_statement ELSE embedded_statement
          {
-               $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
+               Location l = (Location) oob_stack.Pop ();
+
+               $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
          }
        ;
 
@@ -2925,12 +3045,17 @@ iteration_statement
        ;
 
 while_statement
-       : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
+       : WHILE OPEN_PARENS 
+       {
+               oob_stack.Push (lexer.Location);
+       }
+       boolean_expression CLOSE_PARENS embedded_statement
        {
-               $$ = new While ((Expression) $3, (Statement) $5);
+               Location l = (Location) oob_stack.Pop ();
+               $$ = new While ((Expression) $4, (Statement) $6, l);
        
                if (RootContext.WarningLevel >= 3){
-                       if ($5 is EmptyStatement)
+                       if ($6 is EmptyStatement)
                                Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
                }
        }
@@ -2940,7 +3065,7 @@ do_statement
        : DO embedded_statement 
          WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
          {
-               $$ = new Do ((Statement) $2, (Expression) $5);
+               $$ = new Do ((Statement) $2, (Expression) $5, lexer.Location);
          }
        ;
 
@@ -2989,18 +3114,21 @@ for_statement
                                        
                                        Assign a = new Assign (var, expr, decl.Location);
                                        
-                                       assign_block.AddStatement (new StatementExpression (a));
+                                       assign_block.AddStatement (new StatementExpression (a, lexer.Location));
                                }
                        }
                        
                        $3 = null;
                } 
+               oob_stack.Push (lexer.Location);
          } 
          opt_for_condition SEMICOLON
          opt_for_iterator CLOSE_PARENS 
          embedded_statement
          {
-               For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10);
+               Location l = (Location) oob_stack.Pop ();
+
+               For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
 
                if (RootContext.WarningLevel >= 3){
                        if ($10 is EmptyStatement)
@@ -3042,7 +3170,8 @@ for_iterator
 statement_expression_list
        : statement_expression  
          {
-               Block b = new Block (null, true);
+               // CHANGE: was `null'
+               Block b = new Block (current_block, true);   
 
                b.AddStatement ((Statement) $1);
                $$ = b;
@@ -3078,22 +3207,24 @@ foreach_statement
                }
 
                v = new LocalVariableReference (foreach_block, (string) $4, l);
-
-               current_block.AddStatement (foreach_block);
                current_block = foreach_block;
 
                oob_stack.Push (v);
+               oob_stack.Push (current_block);
          } 
          embedded_statement 
          {
+               Block foreach_block = (Block) oob_stack.Pop ();
                LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
                Block prev_block = (Block) oob_stack.Pop ();
                Location l = (Location) oob_stack.Pop ();
 
-               while (current_block != prev_block)
-                       current_block = current_block.Parent;
+               current_block = prev_block;
 
-               $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
+               Foreach f = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
+               foreach_block.AddStatement (f);
+
+               $$ = foreach_block;
          }
        ;
 
@@ -3175,12 +3306,15 @@ try_statement
          {
                Catch g = null;
                ArrayList s = new ArrayList ();
-               
-               foreach (Catch cc in (ArrayList) $3) {
-                       if (cc.Type == null)
-                               g = cc;
-                       else
-                               s.Add (cc);
+               ArrayList catch_list = (ArrayList) $3;
+
+               if (catch_list != null){
+                       foreach (Catch cc in catch_list) {
+                               if (cc.Type == null)
+                                       g = cc;
+                               else
+                                       s.Add (cc);
+                       }
                }
 
                $$ = new Try ((Block) $2, s, g, (Block) $5);
@@ -3305,11 +3439,13 @@ fixed_statement
          CLOSE_PARENS 
          {
                Block assign_block = new Block (current_block, true);
-
+               ArrayList list = (ArrayList) $4;
                string type = (string) $3;
                Location l = lexer.Location;
+               int top = list.Count;
 
-               foreach (Pair p in (ArrayList) $4){
+               for (int i = 0; i < top; i++){
+                       Pair p = (Pair) list [i];
                        VariableInfo v;
 
                        v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
@@ -3319,19 +3455,23 @@ fixed_statement
                                        "defined in this scope");
                        }
                        v.ReadOnly = true;
-                       // Replace the name with the VariableInfo.
-                       p.SetFirst (v);
+                       p.First = v;
+                       list [i] = p;
                }
                current_block.AddStatement (assign_block);
                current_block = assign_block;
+               oob_stack.Push (assign_block);
                oob_stack.Push (l);
          }
          embedded_statement 
          {
-               Block assign_block = (Block) oob_stack.Pop ();
                Location l = (Location) oob_stack.Pop ();
+               Block assign_block = (Block) oob_stack.Pop ();
+
+               ArrayList list = (ArrayList) $4;
+               int top = list.Count;
 
-               $$ = new Fixed ((string) $3, (ArrayList) $4, (Statement) $6, l);
+               $$ = new Fixed ((string) $3, (ArrayList) $4, (Statement) $7, l);
          }
        ;
 
@@ -3350,7 +3490,7 @@ fixed_pointer_declarators
        ;
 
 fixed_pointer_declarator
-       : IDENTIFIER EQUALS expression
+       : IDENTIFIER ASSIGN expression
          {     
                $$ = new Pair ($1, $3);
          }
@@ -3423,7 +3563,7 @@ using_statement
                                vars.Add (new DictionaryEntry (var, expr));                             
 
                                // Assign a = new Assign (var, expr, decl.Location);
-                               // assign_block.AddStatement (new StatementExpression (a));
+                               // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
                        }
                        $3 = new DictionaryEntry (type, vars);
                 }
@@ -3529,16 +3669,14 @@ MakeName (string class_name)
 //   in the current declaration space
 // </summary>
 void 
-CheckDef (AdditionResult result, string name)
+CheckDef (AdditionResult result, string name, Location l)
 {
        if (result == AdditionResult.Success)
                return;
 
-       Location l = lexer.Location;
-       
        switch (result){
        case AdditionResult.NameExists:
-               Report.Error (102, l, "The namespace `" + current_container.Name + 
+               Report.Error (102, l, "The container `" + current_container.Name + 
                                 "' already contains a definition for `"+
                                 name + "'");
                break;
@@ -3566,13 +3704,18 @@ CheckDef (AdditionResult result, string name)
 }
 
 void 
-CheckDef (bool result, string name)
+CheckDef (bool result, string name, Location l)
 {
        if (result)
                return;
-       CheckDef (AdditionResult.NameExists, name);
+       CheckDef (AdditionResult.NameExists, name, l);
 }
 
+//
+// This routine should be removed soon.  I am in the process of making
+// changes to never keep anything but SimpleNames during parsing, as
+// that breaks some kinds of code (documented in the ChangeLog).
+//
 Expression
 SimpleLookup (string name, Location loc)
 {
@@ -3581,10 +3724,6 @@ SimpleLookup (string name, Location loc)
        // as `expression' is allowed in argument_lists, which 
        // do not exist inside a block.  
        //
-       if (current_block != null){
-               if (current_block.IsVariableDefined (name))
-                       return new LocalVariableReference (current_block, name, loc);
-       }
 
        if (current_local_parameters != null){
                int idx;
@@ -3653,7 +3792,7 @@ Block declare_local_variables (string type, ArrayList variable_declarators, Loca
        // int j = 1;  int k = j + 1;
        //
        if (current_block.Used)
-               implicit_block = new Block (current_block, true);
+               implicit_block = new Block (current_block, true, loc, Location.Null);
        else
                implicit_block = current_block;
 
@@ -3696,7 +3835,7 @@ Block declare_local_variables (string type, ArrayList variable_declarators, Loca
 
                assign = new Assign (var, expr, decl.Location);
 
-               implicit_block.AddStatement (new StatementExpression (assign));
+               implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
        }
        
        return implicit_block;
@@ -3729,7 +3868,7 @@ void CheckAttributeTarget (string a)
                
        default :
                Location l = lexer.Location;
-               Report.Error (658, l, "Invalid attribute target");
+               Report.Error (658, l, "`" + a + "' is an invalid attribute target");
                break;
        }
 
@@ -3825,8 +3964,6 @@ public CSharpParser (string name, System.IO.Stream input, ArrayList defines)
 
 public override int parse ()
 {
-       StringBuilder value = new StringBuilder ();
-
        global_errors = 0;
        try {
                if (yacc_verbose_flag)