2005-12-23 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mcs / cs-parser.jay
index d4e0910249e79554406df918f539fe956dda0ea2..a88ca9764f7b258086673b77f784f50b4bd68681 100644 (file)
@@ -57,6 +57,11 @@ namespace Mono.CSharp
                Expression implicit_value_parameter_type;
                Parameters indexer_parameters;
 
+               /// <summary>
+               ///   Hack to help create non-typed array initializer
+               /// </summary>
+               public static Expression current_array_type;
+
                /// <summary>
                ///   Used to determine if we are parsing the get/set pair
                ///   of an indexer or a property
@@ -358,7 +363,6 @@ using_alias_directive
          {
                LocatedToken lt = (LocatedToken) $2;
                current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
-               current_namespace.UsingFound = true;
          }
        | USING error {
                CheckIdentifierToken (yyToken, GetLocation ($2));
@@ -369,7 +373,6 @@ using_namespace_directive
        : USING namespace_name SEMICOLON 
          {
                current_namespace.Using ((MemberName) $2, (Location) $1);
-               current_namespace.UsingFound = true;
           }
        ;
 
@@ -625,7 +628,7 @@ attribute
 
                if (current_attr_target == "assembly" || current_attr_target == "module")
                        // FIXME: supply "nameEscaped" parameter here.
-                       $$ = new GlobalAttribute (current_class, current_attr_target,
+                       $$ = new GlobalAttribute (current_namespace, current_attr_target,
                                                  left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
                else
                        $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
@@ -914,9 +917,10 @@ field_declaration
 
                foreach (VariableDeclaration var in (ArrayList) $4){
                        Field field = new Field (current_class, type, mod, var.identifier, 
-                                                var.expression_or_array_initializer, 
                                                 (Attributes) $1, var.Location);
 
+                       field.Initializer = var.expression_or_array_initializer;
+
                        if (RootContext.Documentation != null) {
                                field.DocComment = Lexer.consume_doc_comment ();
                                Lexer.doc_state = XmlCommentState.Allowed;
@@ -1150,20 +1154,22 @@ formal_parameter_list
                Parameter [] pars = new Parameter [pars_list.Count];
                pars_list.CopyTo (pars);
 
-               $$ = new Parameters (pars, null); 
+               $$ = new Parameters (pars); 
          } 
        | fixed_parameters COMMA parameter_array
          {
                ArrayList pars_list = (ArrayList) $1;
+               pars_list.Add ($3);
 
                Parameter [] pars = new Parameter [pars_list.Count];
                pars_list.CopyTo (pars);
 
-               $$ = new Parameters (pars, (Parameter) $3); 
+               $$ = new Parameters (pars); 
          }
        | fixed_parameters COMMA ARGLIST
          {
                ArrayList pars_list = (ArrayList) $1;
+               //pars_list.Add (new ArglistParameter (GetLocation ($3)));
 
                Parameter [] pars = new Parameter [pars_list.Count];
                pars_list.CopyTo (pars);
@@ -1183,11 +1189,11 @@ formal_parameter_list
          }
        | parameter_array 
          {
-               $$ = new Parameters (null, (Parameter) $1);
+               $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
          }
        | ARGLIST
          {
-               $$ = new Parameters (null, true);
+               $$ = new Parameters (new Parameter[0], true);
          }
        ;
 
@@ -1259,15 +1265,15 @@ opt_parameter_modifier
        ;
 
 parameter_modifier
-       : REF                   { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
-       | OUT                   { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
+       : REF                   { $$ = Parameter.Modifier.REF; }
+       | OUT                   { $$ = Parameter.Modifier.OUT; }
        ;
 
 parameter_array
        : opt_attributes PARAMS type IDENTIFIER
          { 
                LocatedToken lt = (LocatedToken) $4;
-               $$ = new Parameter ((Expression) $3, lt.Value, Parameter.Modifier.PARAMS, (Attributes) $1, lt.Location);
+               $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
                note ("type must be a single-dimension array type"); 
          }
        | opt_attributes PARAMS parameter_modifier type IDENTIFIER 
@@ -1401,7 +1407,7 @@ set_accessor_declaration
                if (parsing_indexer == false) {
                        args  = new Parameter [1];
                        args [0] = implicit_value_parameter;
-                       current_local_parameters = new Parameters (args, null);
+                       current_local_parameters = new Parameters (args);
                } else {
                        Parameter [] fpars = indexer_parameters.FixedParameters;
 
@@ -1414,7 +1420,7 @@ set_accessor_declaration
                        } else 
                                args = null;
                        current_local_parameters = new Parameters (
-                               args, indexer_parameters.ArrayParameter);
+                               args);
                }
                
                lexer.PropertyParsing = false;
@@ -1717,7 +1723,7 @@ interface_event_declaration
          {
                LocatedToken lt = (LocatedToken) $5;
                $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
-                                    new MemberName (lt.Value, lt.Location), null,
+                                    new MemberName (lt.Value, lt.Location),
                                     (Attributes) $1);
                if (RootContext.Documentation != null)
                        ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
@@ -1813,7 +1819,7 @@ operator_declaration
 
                Operator op = new Operator (
                        current_class, decl.optype, decl.ret_type, (int) $2, 
-                       new Parameters (param_list, null),
+                       new Parameters (param_list),
                        (ToplevelBlock) $5, (Attributes) $1, decl.location);
 
                if (RootContext.Documentation != null) {
@@ -1855,7 +1861,7 @@ operator_declarator
 
                pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
 
-               current_local_parameters = new Parameters (pars, null);
+               current_local_parameters = new Parameters (pars);
 
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
@@ -1883,7 +1889,7 @@ operator_declarator
               pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
               pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
 
-              current_local_parameters = new Parameters (pars, null);
+              current_local_parameters = new Parameters (pars);
 
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
@@ -1951,7 +1957,7 @@ conversion_operator_declarator
 
                pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
 
-               current_local_parameters = new Parameters (pars, null);  
+               current_local_parameters = new Parameters (pars);  
                  
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
@@ -1968,7 +1974,7 @@ conversion_operator_declarator
 
                pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
 
-               current_local_parameters = new Parameters (pars, null);  
+               current_local_parameters = new Parameters (pars);  
                  
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
@@ -2115,7 +2121,7 @@ destructor_declaration
                         
                        Method d = new Destructor (
                                current_class, TypeManager.system_void_expr, m, "Finalize", 
-                               new Parameters (null, null), (Attributes) $1, l);
+                               Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
                        if (RootContext.Documentation != null)
                                d.DocComment = ConsumeStoredComment ();
                  
@@ -2135,9 +2141,11 @@ event_declaration
                        MemberName name = new MemberName (var.identifier,
                                var.Location);
 
-                       Event e = new EventField (
+                       EventField e = new EventField (
                                current_class, (Expression) $4, (int) $2, false, name,
-                               var.expression_or_array_initializer, (Attributes) $1);
+                               (Attributes) $1);
+
+                       e.Initializer = var.expression_or_array_initializer;
 
                        current_container.AddEvent (e);
 
@@ -2165,7 +2173,7 @@ event_declaration
 
                if ($8 == null){
                        Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
-                               current_container.Name, name);
+                               current_container.Name, name.ToString ());
                        $$ = null;
                } else {
                        Pair pair = (Pair) $8;
@@ -2174,7 +2182,7 @@ event_declaration
                                $$ = null;
                        else {
                                Event e = new EventProperty (
-                                       current_class, (Expression) $4, (int) $2, false, name, null,
+                                       current_class, (Expression) $4, (int) $2, false, name,
                                        (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
                                if (RootContext.Documentation != null) {
                                        e.DocComment = Lexer.consume_doc_comment ();
@@ -2228,7 +2236,7 @@ add_accessor_declaration
 
                args [0] = implicit_value_parameter;
                
-               current_local_parameters = new Parameters (args, null);  
+               current_local_parameters = new Parameters (args);  
                lexer.EventParsing = false;
          }
           block
@@ -2256,7 +2264,7 @@ remove_accessor_declaration
 
                args [0] = implicit_value_parameter;
                
-               current_local_parameters = new Parameters (args, null);  
+               current_local_parameters = new Parameters (args);  
                lexer.EventParsing = false;
          }
           block
@@ -2335,7 +2343,7 @@ indexer_declarator
                if (pars.HasArglist) {
                        // "__arglist is not valid in this context"
                        Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
-               } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
+               } else if (pars.Empty){
                        Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
                }
                if (RootContext.Documentation != null) {
@@ -2352,7 +2360,7 @@ indexer_declarator
                if (pars.HasArglist) {
                        // "__arglist is not valid in this context"
                        Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
-               } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
+               } else if (pars.Empty){
                        Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
                }
 
@@ -2648,7 +2656,7 @@ integral_type
 array_type
        : type rank_specifiers
          {
-               $$ = new ComposedCast ((Expression) $1, (string) $2);
+               $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
          }
        ;
 
@@ -2891,6 +2899,7 @@ element_access
                        // 
                        $$ = new ComposedCast (expr, (string) $2);
                }
+               current_array_type = (Expression)$$;
          }
        ;
 
@@ -3154,7 +3163,7 @@ anonymous_method_signature
                        ArrayList par_list = (ArrayList) $2;
                        Parameter [] pars = new Parameter [par_list.Count];
                        par_list.CopyTo (pars);
-                       $$ = new Parameters (pars, null);
+                       $$ = new Parameters (pars);
                }
          }
        ;
@@ -3780,7 +3789,7 @@ local_variable_type
                if ((string) $2 == "")
                        $$ = $1;
                else
-                       $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+                       $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
          }
         ;
 
@@ -3886,11 +3895,9 @@ if_statement
 
                $$ = new If ((Expression) $3, (Statement) $5, l);
 
-               if (RootContext.WarningLevel >= 3){
-                       // FIXME: location for warning should be loc property of $5.
-                       if ($5 == EmptyStatement.Value)
-                               Report.Warning (642, l, "Possible mistaken empty statement");
-               }
+               // FIXME: location for warning should be loc property of $5.
+               if ($5 == EmptyStatement.Value)
+                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
 
          }
        | IF OPEN_PARENS boolean_expression CLOSE_PARENS
@@ -3900,13 +3907,11 @@ if_statement
 
                $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
 
-               if (RootContext.WarningLevel >= 3){
-                       // FIXME: location for warning should be loc property of $5 and $7.
-                       if ($5 == EmptyStatement.Value)
-                               Report.Warning (642, l, "Possible mistaken empty statement");
-                       if ($7 == EmptyStatement.Value)
-                               Report.Warning (642, l, "Possible mistaken empty statement");
-               }
+               // FIXME: location for warning should be loc property of $5 and $7.
+               if ($5 == EmptyStatement.Value)
+                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
+               if ($7 == EmptyStatement.Value)
+                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
          }
        ;
 
@@ -4046,15 +4051,7 @@ for_statement
                                        continue;
 
                                Location l = lexer.Location;
-                               Expression expr;
-                               if (decl.expression_or_array_initializer is Expression){
-                                       expr = (Expression) decl.expression_or_array_initializer;
-                               } else if (decl.expression_or_array_initializer == null) {
-                                       expr = null;
-                               } else {
-                                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                                       expr = new ArrayCreation (type, "", init, decl.Location);
-                               }
+                               Expression expr = decl.expression_or_array_initializer;
                                        
                                LocalVariableReference var;
                                var = new LocalVariableReference (assign_block, decl.identifier, l);
@@ -4117,9 +4114,10 @@ statement_expression_list
        : statement_expression  
          {
                // CHANGE: was `null'
-               Block b = new Block (current_block, Block.Flags.Implicit);   
+               Statement s = (Statement) $1;
+               Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);   
 
-               b.AddStatement ((Statement) $1);
+               b.AddStatement (s);
                $$ = b;
          }
        | statement_expression_list COMMA statement_expression
@@ -4537,16 +4535,9 @@ using_statement
                                        continue;
                                vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
 
-                               Expression expr;
-                               if (decl.expression_or_array_initializer is Expression){
-                                       expr = (Expression) decl.expression_or_array_initializer;
-                               } else {
-                                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                                       if (init == null) {
-                                               Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
-                                       }
-                                       
-                                       expr = new ArrayCreation (type, "", init, decl.Location);
+                               Expression expr = decl.expression_or_array_initializer;
+                               if (expr == null) {
+                                       Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
                                }
 
                                LocalVariableReference var;
@@ -4593,7 +4584,7 @@ resource_acquisition
 // </summary>
 public class VariableDeclaration {
        public string identifier;
-       public object expression_or_array_initializer;
+       public Expression expression_or_array_initializer;
        public Location Location;
        public Attributes OptAttributes;
        public string DocComment;
@@ -4601,7 +4592,15 @@ public class VariableDeclaration {
        public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
        {
                this.identifier = lt.Value;
-               this.expression_or_array_initializer = eoai;
+               if (eoai is ArrayList) {
+                       if (CSharpParser.current_array_type == null)
+                               Report.Error (622, lt.Location,
+                                       "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
+                       this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
+               } else {
+                       this.expression_or_array_initializer = (Expression)eoai;
+               }
+               CSharpParser.current_array_type = null;
                this.Location = lt.Location;
                this.OptAttributes = opt_attrs;
        }
@@ -4686,13 +4685,12 @@ TypeContainer pop_current_class ()
 {
        TypeContainer retval = current_class;
 
-       current_class = current_class.Parent;
-       current_container = current_container.Parent;
-       
+       current_class = (TypeContainer) current_class.Parent;
+       current_container = (TypeContainer) current_container.Parent;
+
        if (current_class != current_container) {
-               if (!(current_class is ClassPart) ||
-                   ((ClassPart) current_class).PartialContainer != current_container)
-                       throw new InternalErrorException ();
+               if (((ClassPart) current_class).PartialContainer != current_container)
+                       throw new InternalErrorException ("current_container and current_class are out of sync");
        } else if (current_container is ClassPart)
                current_container = ((ClassPart) current_class).PartialContainer;
 
@@ -4759,17 +4757,8 @@ Block declare_local_variables (Expression type, ArrayList variable_declarators,
 
        foreach (VariableDeclaration decl in inits){
                Assign assign;
-               Expression expr;
+               Expression expr = decl.expression_or_array_initializer;
                
-               if (decl.expression_or_array_initializer is Expression){
-                       expr = (Expression) decl.expression_or_array_initializer;
-
-               } else {
-                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                       
-                       expr = new ArrayCreation (type, "", init, decl.Location);
-               }
-
                LocalVariableReference var;
                var = new LocalVariableReference (implicit_block, decl.identifier, loc);