2005-05-13 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / cs-parser.jay
1 %{
2 //
3 // cs-parser.jay: The Parser for the C# compiler
4 //
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 //          Ravi Pratap     (ravi@ximian.com)
7 //
8 // Licensed under the terms of the GNU GPL
9 //
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
12 //
13 // TODO:
14 //   (1) Figure out why error productions dont work.  `type-declaration' is a
15 //       great spot to put an `error' because you can reproduce it with this input:
16 //       "public X { }"
17 //
18 // Possible optimization:
19 //   Run memory profiler with parsing only, and consider dropping 
20 //   arraylists where not needed.   Some pieces can use linked lists.
21 //
22 using System.Text;
23 using System.IO;
24 using System;
25
26 namespace Mono.CSharp
27 {
28         using System.Collections;
29
30         /// <summary>
31         ///    The C# Parser
32         /// </summary>
33         public class CSharpParser {
34                 NamespaceEntry  current_namespace;
35                 TypeContainer   current_container;
36                 TypeContainer   current_class;
37         
38                 IIteratorContainer iterator_container;
39
40                 /// <summary>
41                 ///   Current block is used to add statements as we find
42                 ///   them.  
43                 /// </summary>
44                 Block      current_block, top_current_block;
45
46                 /// <summary>
47                 ///   This is used by the unary_expression code to resolve
48                 ///   a name against a parameter.  
49                 /// </summary>
50                 Parameters current_local_parameters;
51
52                 /// <summary>
53                 ///   Using during property parsing to describe the implicit
54                 ///   value parameter that is passed to the "set" and "get"accesor
55                 ///   methods (properties and indexers).
56                 /// </summary>
57                 Expression implicit_value_parameter_type;
58                 Parameters indexer_parameters;
59
60                 /// <summary>
61                 ///   Used to determine if we are parsing the get/set pair
62                 ///   of an indexer or a property
63                 /// </summmary>
64                 bool  parsing_indexer;
65
66                 ///
67                 /// An out-of-band stack.
68                 ///
69                 Stack oob_stack;
70
71                 ///
72                 /// Switch stack.
73                 ///
74                 Stack switch_stack;
75
76                 static public int yacc_verbose_flag;
77
78                 // Name of the file we are parsing
79                 public string name;
80
81                 ///
82                 /// The current file.
83                 ///
84                 SourceFile file;
85
86                 ///
87                 /// Temporary Xml documentation cache.
88                 /// For enum types, we need one more temporary store.
89                 ///
90                 string tmpComment;
91                 string enumTypeComment;
92                         
93                 /// Current attribute target
94                 string current_attr_target;
95                 
96                 /// assembly and module attribute definition is enabled
97                 bool global_attrs_enabled = true;
98
99                 bool has_get, has_set;
100
101 %}
102
103 %token EOF
104 %token NONE   /* This token is never returned by our lexer */
105 %token ERROR            // This is used not by the parser, but by the tokenizer.
106                         // do not remove.
107
108 /*
109  *These are the C# keywords
110  */
111 %token FIRST_KEYWORD
112 %token ABSTRACT 
113 %token AS
114 %token ADD
115 %token ASSEMBLY
116 %token BASE     
117 %token BOOL     
118 %token BREAK    
119 %token BYTE     
120 %token CASE     
121 %token CATCH    
122 %token CHAR     
123 %token CHECKED  
124 %token CLASS    
125 %token CONST    
126 %token CONTINUE 
127 %token DECIMAL  
128 %token DEFAULT  
129 %token DELEGATE 
130 %token DO       
131 %token DOUBLE   
132 %token ELSE     
133 %token ENUM     
134 %token EVENT    
135 %token EXPLICIT 
136 %token EXTERN   
137 %token FALSE    
138 %token FINALLY  
139 %token FIXED    
140 %token FLOAT    
141 %token FOR      
142 %token FOREACH  
143 %token GOTO     
144 %token IF       
145 %token IMPLICIT 
146 %token IN       
147 %token INT      
148 %token INTERFACE
149 %token INTERNAL 
150 %token IS       
151 %token LOCK     
152 %token LONG     
153 %token NAMESPACE
154 %token NEW      
155 %token NULL     
156 %token OBJECT   
157 %token OPERATOR 
158 %token OUT      
159 %token OVERRIDE 
160 %token PARAMS   
161 %token PRIVATE  
162 %token PROTECTED
163 %token PUBLIC   
164 %token READONLY 
165 %token REF      
166 %token RETURN   
167 %token REMOVE
168 %token SBYTE    
169 %token SEALED   
170 %token SHORT    
171 %token SIZEOF   
172 %token STACKALLOC
173 %token STATIC   
174 %token STRING   
175 %token STRUCT   
176 %token SWITCH   
177 %token THIS     
178 %token THROW    
179 %token TRUE     
180 %token TRY      
181 %token TYPEOF   
182 %token UINT     
183 %token ULONG    
184 %token UNCHECKED
185 %token UNSAFE   
186 %token USHORT   
187 %token USING    
188 %token VIRTUAL  
189 %token VOID     
190 %token VOLATILE
191 %token WHILE    
192 %token ARGLIST
193 %token PARTIAL
194
195 /* C# keywords which are not really keywords */
196 %token GET           "get"
197 %token SET           "set"
198
199 %left LAST_KEYWORD
200
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE    "{"
203 %token CLOSE_BRACE   "}"
204 %token OPEN_BRACKET  "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS   "("
207 %token CLOSE_PARENS  ")"
208 %token DOT           "."
209 %token COMMA         ","
210 %token COLON         ":"
211 %token SEMICOLON     ";"
212 %token TILDE         "~"
213
214 %token PLUS           "+"
215 %token MINUS          "-"
216 %token BANG           "!"
217 %token ASSIGN         "="
218 %token OP_LT          "<"
219 %token OP_GT          ">"
220 %token BITWISE_AND    "&"
221 %token BITWISE_OR     "|"
222 %token STAR           "*"
223 %token PERCENT        "%"
224 %token DIV            "/"
225 %token CARRET         "^"
226 %token INTERR         "?"
227
228 /* C# multi-character operators. */
229 %token OP_INC                 "++"
230 %token OP_DEC                 "--"
231 %token OP_SHIFT_LEFT          "<<"
232 %token OP_SHIFT_RIGHT         ">>"
233 %token OP_LE                  "<="
234 %token OP_GE                  ">="
235 %token OP_EQ                  "=="
236 %token OP_NE                  "!="
237 %token OP_AND                 "&&"
238 %token OP_OR                  "||"
239 %token OP_MULT_ASSIGN         "*="
240 %token OP_DIV_ASSIGN          "/="
241 %token OP_MOD_ASSIGN          "%="
242 %token OP_ADD_ASSIGN          "+="
243 %token OP_SUB_ASSIGN          "-="
244 %token OP_SHIFT_LEFT_ASSIGN   "<<="
245 %token OP_SHIFT_RIGHT_ASSIGN  ">>="
246 %token OP_AND_ASSIGN          "&="
247 %token OP_XOR_ASSIGN          "^="
248 %token OP_OR_ASSIGN           "|="
249 %token OP_PTR                 "->"
250
251 /* Numbers */
252 %token LITERAL_INTEGER           "int literal"
253 %token LITERAL_FLOAT             "float literal"
254 %token LITERAL_DOUBLE            "double literal"
255 %token LITERAL_DECIMAL           "decimal literal"
256 %token LITERAL_CHARACTER         "character literal"
257 %token LITERAL_STRING            "string literal"
258
259 %token IDENTIFIER
260 %token CLOSE_PARENS_CAST
261 %token CLOSE_PARENS_NO_CAST
262 %token CLOSE_PARENS_OPEN_PARENS
263 %token CLOSE_PARENS_MINUS
264
265 /* Add precedence rules to solve dangling else s/r conflict */
266 %nonassoc LOWPREC
267 %nonassoc IF
268 %nonassoc ELSE
269 %right ASSIGN
270 %left OP_OR
271 %left OP_AND
272 %left BITWISE_OR
273 %left BITWISE_AND
274 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
275 %left PLUS MINUS
276 %left STAR DIV PERCENT
277 %right BANG CARRET UMINUS
278 %nonassoc OP_INC OP_DEC
279 %left OPEN_PARENS
280 %left OPEN_BRACKET OPEN_BRACE
281 %left DOT
282 %nonassoc HIGHPREC
283
284 %start compilation_unit
285 %%
286
287 compilation_unit
288         : outer_declarations opt_EOF
289         | outer_declarations global_attributes opt_EOF
290         | global_attributes opt_EOF
291         | opt_EOF /* allow empty files */
292         ;
293         
294 opt_EOF
295         : /* empty */
296         {
297                 Lexer.check_incorrect_doc_comment ();
298         }
299         | EOF
300         {
301                 Lexer.check_incorrect_doc_comment ();
302         }
303         ;
304
305 outer_declarations
306         : outer_declaration
307         | outer_declarations outer_declaration
308         ;
309  
310 outer_declaration
311         : using_directive
312         | namespace_member_declaration
313         ;
314   
315 using_directives
316         : using_directive 
317         | using_directives using_directive
318         ;
319
320 using_directive
321         : using_alias_directive
322         {
323                 if (RootContext.Documentation != null)
324                         Lexer.doc_state = XmlCommentState.Allowed;
325         }
326         | using_namespace_directive
327         {
328                 if (RootContext.Documentation != null)
329                         Lexer.doc_state = XmlCommentState.Allowed;
330         }
331         ;
332
333 using_alias_directive
334         : USING IDENTIFIER ASSIGN 
335           namespace_or_type_name SEMICOLON
336           {
337                 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
338           }
339         | USING error {
340                 CheckIdentifierToken (yyToken);
341           }
342         ;
343
344 using_namespace_directive
345         : USING namespace_name SEMICOLON 
346           {
347                 current_namespace.Using ((MemberName) $2, lexer.Location);
348           }
349         ;
350
351 //
352 // Strictly speaking, namespaces don't have attributes but
353 // we parse global attributes along with namespace declarations and then
354 // detach them
355 // 
356 namespace_declaration
357         : opt_attributes NAMESPACE namespace_or_type_name
358         {
359                 if ($1 != null) {
360                         Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
361                 }
362
363                 MemberName name = (MemberName) $3;
364
365                 current_namespace = new NamespaceEntry (
366                         current_namespace, file, name.GetName (), lexer.Location);
367           } 
368           namespace_body opt_semicolon
369           { 
370                 current_namespace = current_namespace.Parent;
371           }
372         ;
373
374 opt_semicolon
375         : /* empty */
376         | SEMICOLON
377         ;
378
379 opt_comma
380         : /* empty */
381         | COMMA
382         ;
383
384 namespace_name
385         : namespace_or_type_name
386         ;
387
388 namespace_body
389         : OPEN_BRACE
390           {
391                 if (RootContext.Documentation != null)
392                         Lexer.doc_state = XmlCommentState.Allowed;
393           }
394           opt_using_directives
395           opt_namespace_member_declarations
396           CLOSE_BRACE
397           {
398           }
399         ;
400
401 opt_using_directives
402         : /* empty */
403         | using_directives
404         ;
405
406 opt_namespace_member_declarations
407         : /* empty */
408         | namespace_member_declarations
409         ;
410
411 namespace_member_declarations
412         : namespace_member_declaration
413         | namespace_member_declarations namespace_member_declaration
414         ;
415
416 namespace_member_declaration
417         : type_declaration
418           {
419                 if ($1 != null) {
420                         DeclSpace ds = (DeclSpace)$1;
421
422                         if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
423                                 Report.Error (1527, lexer.Location, 
424                                 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
425                         }
426                 }
427                 current_namespace.DeclarationFound = true;
428           }
429         | namespace_declaration {
430                 current_namespace.DeclarationFound = true;
431           }
432
433         | field_declaration {
434                 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
435           }
436         | method_declaration {
437                 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
438           }
439         ;
440
441 type_declaration
442         : class_declaration             
443         | struct_declaration            
444         | interface_declaration         
445         | enum_declaration              
446         | delegate_declaration
447 //
448 // Enable this when we have handled all errors, because this acts as a generic fallback
449 //
450 //      | error {
451 //              Console.WriteLine ("Token=" + yyToken);
452 //              Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
453 //        }
454         ;
455
456 //
457 // Attributes 17.2
458 //
459
460 global_attributes
461         : attribute_sections
462 {
463         if ($1 != null)
464                 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
465
466         $$ = $1;
467 }
468
469 opt_attributes
470         : /* empty */ 
471           {
472                 global_attrs_enabled = false;
473                 $$ = null;
474       }
475         | attribute_sections
476           { 
477                 global_attrs_enabled = false;
478                 $$ = $1;
479           }
480     ;
481  
482
483 attribute_sections
484         : attribute_section
485           {
486                 ArrayList sect = (ArrayList) $1;
487
488                 if (global_attrs_enabled) {
489                         if (current_attr_target == "module") {
490                                 CodeGen.Module.AddAttributes (sect);
491                                 $$ = null;
492                         } else if (current_attr_target == "assembly") {
493                                 CodeGen.Assembly.AddAttributes (sect);
494                                 $$ = null;
495                         } else {
496                                 $$ = new Attributes (sect);
497                         }
498                         if ($$ == null) {
499                                 if (RootContext.Documentation != null) {
500                                         Lexer.check_incorrect_doc_comment ();
501                                         Lexer.doc_state =
502                                                 XmlCommentState.Allowed;
503                                 }
504                         }
505                 } else {
506                         $$ = new Attributes (sect);
507                 }               
508                 current_attr_target = null;
509           }
510         | attribute_sections attribute_section
511           {
512                 Attributes attrs = $1 as Attributes;
513                 ArrayList sect = (ArrayList) $2;
514
515                 if (global_attrs_enabled) {
516                         if (current_attr_target == "module") {
517                                 CodeGen.Module.AddAttributes (sect);
518                                 $$ = null;
519                         } else if (current_attr_target == "assembly") {
520                                 CodeGen.Assembly.AddAttributes (sect);
521                                 $$ = null;
522                         } else {
523                                 if (attrs == null)
524                                         attrs = new Attributes (sect);
525                                 else
526                                         attrs.AddAttributes (sect);                     
527                         }
528                 } else {
529                         if (attrs == null)
530                                 attrs = new Attributes (sect);
531                         else
532                                 attrs.AddAttributes (sect);
533                 }               
534                 $$ = attrs;
535                 current_attr_target = null;
536           }
537         ;
538
539 attribute_section
540         : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
541           {
542                 $$ = $3;
543           }
544         | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
545           {
546                 $$ = $2;
547           }
548         ;
549  
550 attribute_target_specifier
551         : attribute_target COLON
552           {
553                 current_attr_target = (string)$1;
554                 $$ = $1;
555           }
556         ;
557
558 attribute_target
559         : IDENTIFIER
560           {
561                 CheckAttributeTarget ((string) $1);
562                 $$ = $1;
563           }
564         | EVENT  { $$ = "event"; }        
565         | RETURN { $$ = "return"; }
566         ;
567
568 attribute_list
569         : attribute
570           {
571                 ArrayList attrs = new ArrayList (4);
572                 attrs.Add ($1);
573
574                 $$ = attrs;
575                
576           }
577         | attribute_list COMMA attribute
578           {
579                 ArrayList attrs = (ArrayList) $1;
580                 attrs.Add ($3);
581
582                 $$ = attrs;
583           }
584         ;
585
586 attribute
587         : attribute_name
588           {
589                 $$ = lexer.Location;
590           }
591           opt_attribute_arguments
592           {
593                   Location loc = (Location) $2;
594                 MemberName mname = (MemberName) $1;
595                 MemberName left = mname.Left;
596                 string identifier = mname.Name;
597
598                 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
599
600                 if (current_attr_target == "assembly" || current_attr_target == "module")
601                         $$ = new GlobalAttribute (current_class, current_attr_target,
602                                                   left_expr, identifier, (ArrayList) $3, loc);
603                 else
604                         $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
605           }
606         ;
607
608 attribute_name
609         : namespace_or_type_name  { /* reserved attribute name or identifier: 17.4 */ }
610         ;
611
612 opt_attribute_arguments
613         : /* empty */   { $$ = null; }
614         | OPEN_PARENS attribute_arguments CLOSE_PARENS
615           {
616                 $$ = $2;
617           }
618         ;
619
620
621 attribute_arguments
622         : opt_positional_argument_list
623           {
624                 if ($1 == null)
625                         $$ = null;
626                 else {
627                         ArrayList args = new ArrayList (4);
628                         args.Add ($1);
629                 
630                         $$ = args;
631                 }
632           }
633         | positional_argument_list COMMA named_argument_list
634           {
635                 ArrayList args = new ArrayList (4);
636                 args.Add ($1);
637                 args.Add ($3);
638
639                 $$ = args;
640           }
641         | named_argument_list
642           {
643                 ArrayList args = new ArrayList (4);
644                 args.Add (null);
645                 args.Add ($1);
646                 
647                 $$ = args;
648           }
649         ;
650
651
652 opt_positional_argument_list
653         : /* empty */           { $$ = null; } 
654         | positional_argument_list
655         ;
656
657 positional_argument_list
658         : expression
659           {
660                 ArrayList args = new ArrayList (4);
661                 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
662
663                 $$ = args;
664           }
665         | positional_argument_list COMMA expression
666          {
667                 ArrayList args = (ArrayList) $1;
668                 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
669
670                 $$ = args;
671          }
672         ;
673
674 named_argument_list
675         : named_argument
676           {
677                 ArrayList args = new ArrayList (4);
678                 args.Add ($1);
679
680                 $$ = args;
681           }
682         | named_argument_list COMMA named_argument
683           {       
684                 ArrayList args = (ArrayList) $1;
685                 args.Add ($3);
686
687                 $$ = args;
688           }
689           | named_argument_list COMMA expression
690             {
691                   Report.Error (1016, lexer.Location, "Named attribute argument expected");
692                   $$ = null;
693                 }
694         ;
695
696 named_argument
697         : IDENTIFIER ASSIGN expression
698           {
699                 $$ = new DictionaryEntry (
700                         (string) $1, 
701                         new Argument ((Expression) $3, Argument.AType.Expression));
702           }
703         ;
704
705                   
706 class_body
707         :  OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
708         ;
709
710 opt_class_member_declarations
711         : /* empty */
712         | class_member_declarations
713         ;
714
715 class_member_declarations
716         : class_member_declaration
717         | class_member_declarations 
718           class_member_declaration
719         ;
720
721 class_member_declaration
722         : constant_declaration                  // done
723         | field_declaration                     // done
724         | method_declaration                    // done
725         | property_declaration                  // done
726         | event_declaration                     // done
727         | indexer_declaration                   // done
728         | operator_declaration                  // done
729         | constructor_declaration               // done
730         | destructor_declaration                // done
731         | type_declaration
732         ;
733
734 struct_declaration
735         : opt_attributes
736           opt_modifiers
737           opt_partial
738           STRUCT member_name
739           { 
740                 MemberName name = MakeName ((MemberName) $5);
741                 bool partial = (bool) $3;
742
743                 if (partial) {
744                         ClassPart part = PartialContainer.CreatePart (
745                                 current_namespace, current_class, name, (int) $2,
746                                 (Attributes) $1, Kind.Struct, lexer.Location);
747
748                         current_container = part.PartialContainer;
749                         current_class = part;
750                 } else {
751                         current_class = new Struct (
752                                 current_namespace, current_class, name, (int) $2,
753                                 (Attributes) $1, lexer.Location);
754
755                         current_container.AddClassOrStruct (current_class);
756                         current_container = current_class;
757                         RootContext.Tree.RecordDecl (name, current_class);
758                 }
759           }
760           opt_class_base
761           {
762                 if ($7 != null)
763                         current_class.Bases = (ArrayList) $7;
764
765                 if (RootContext.Documentation != null)
766                         current_class.DocComment = Lexer.consume_doc_comment ();
767           }
768           struct_body
769           {
770                 if (RootContext.Documentation != null)
771                         Lexer.doc_state = XmlCommentState.Allowed;
772           }
773           opt_semicolon
774           {
775                 $$ = pop_current_class ();
776           }
777         | opt_attributes opt_modifiers opt_partial STRUCT error {
778                 CheckIdentifierToken (yyToken);
779           }
780         ;
781
782 struct_body
783         : OPEN_BRACE
784           {
785                 if (RootContext.Documentation != null)
786                         Lexer.doc_state = XmlCommentState.Allowed;
787           }
788           opt_struct_member_declarations CLOSE_BRACE
789         ;
790
791 opt_struct_member_declarations
792         : /* empty */
793         | struct_member_declarations
794         ;
795
796 struct_member_declarations
797         : struct_member_declaration
798         | struct_member_declarations struct_member_declaration
799         ;
800
801 struct_member_declaration
802         : constant_declaration
803         | field_declaration
804         | method_declaration
805         | property_declaration
806         | event_declaration
807         | indexer_declaration
808         | operator_declaration
809         | constructor_declaration
810         | type_declaration
811
812         /*
813          * This is only included so we can flag error 575: 
814          * destructors only allowed on class types
815          */
816         | destructor_declaration 
817         ;
818
819 constant_declaration
820         : opt_attributes 
821           opt_modifiers
822           CONST
823           type
824           constant_declarators
825           SEMICOLON
826           {
827                 int modflags = (int) $2;
828                 foreach (VariableDeclaration constant in (ArrayList) $5){
829                         Location l = constant.Location;
830                         if ((modflags & Modifiers.STATIC) != 0) {
831                                 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
832                                 continue;
833                         }
834
835                         Const c = new Const (
836                                 current_class, (Expression) $4, (string) constant.identifier, 
837                                 (Expression) constant.expression_or_array_initializer, modflags, 
838                                 (Attributes) $1, l);
839
840                         if (RootContext.Documentation != null) {
841                                 c.DocComment = Lexer.consume_doc_comment ();
842                                 Lexer.doc_state = XmlCommentState.Allowed;
843                         }
844                         current_container.AddConstant (c);
845                 }
846           }
847         ;
848
849 constant_declarators
850         : constant_declarator 
851           {
852                 ArrayList constants = new ArrayList (4);
853                 if ($1 != null)
854                         constants.Add ($1);
855                 $$ = constants;
856           }
857         | constant_declarators COMMA constant_declarator
858           {
859                 if ($3 != null) {
860                         ArrayList constants = (ArrayList) $1;
861                         constants.Add ($3);
862                 }
863           }
864         ;
865
866 constant_declarator
867         : IDENTIFIER ASSIGN constant_expression
868           {
869                 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
870           }
871         | IDENTIFIER
872           {
873                 // A const field requires a value to be provided
874                 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
875                 $$ = null;
876           }
877         ;
878
879 field_declaration
880         : opt_attributes
881           opt_modifiers
882           type 
883           variable_declarators
884           SEMICOLON
885           { 
886                 Expression type = (Expression) $3;
887                 int mod = (int) $2;
888
889                 foreach (VariableDeclaration var in (ArrayList) $4){
890                         Field field = new Field (current_class, type, mod, var.identifier, 
891                                                  var.expression_or_array_initializer, 
892                                                  (Attributes) $1, var.Location);
893
894                         if (RootContext.Documentation != null) {
895                                 field.DocComment = Lexer.consume_doc_comment ();
896                                 Lexer.doc_state = XmlCommentState.Allowed;
897                         }
898                         current_container.AddField (field);
899                 }
900           }
901         | opt_attributes
902           opt_modifiers
903           FIXED
904           type 
905           fixed_variable_declarators
906           SEMICOLON
907           { 
908                         Expression type = (Expression) $4;
909                         int mod = (int) $2;
910
911                         foreach (VariableDeclaration var in (ArrayList) $5) {
912                                 FixedField field = new FixedField (current_class, type, mod, var.identifier,
913                                         (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
914
915                                 if (RootContext.Documentation != null) {
916                                         field.DocComment = Lexer.consume_doc_comment ();
917                                         Lexer.doc_state = XmlCommentState.Allowed;
918                                 }
919                                 current_container.AddField (field);
920                         }
921           }
922         | opt_attributes
923           opt_modifiers
924           VOID  
925           variable_declarators
926           SEMICOLON {
927                 Report.Error (670, lexer.Location, "void type is not allowed for fields");
928           }
929         ;
930
931 fixed_variable_declarators
932         : fixed_variable_declarator
933                 {
934                         ArrayList decl = new ArrayList (2);
935                         decl.Add ($1);
936                         $$ = decl;
937                 }
938         | fixed_variable_declarators COMMA fixed_variable_declarator
939                 {
940                         ArrayList decls = (ArrayList) $1;
941                         decls.Add ($3);
942                         $$ = $1;
943                 }
944         ;
945
946 fixed_variable_declarator
947         : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
948                 {
949                         $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
950                 }
951         ;
952
953 variable_declarators
954         : variable_declarator 
955           {
956                 ArrayList decl = new ArrayList (4);
957                 decl.Add ($1);
958                 $$ = decl;
959           }
960         | variable_declarators COMMA variable_declarator
961           {
962                 ArrayList decls = (ArrayList) $1;
963                 decls.Add ($3);
964                 $$ = $1;
965           }
966         ;
967
968 variable_declarator
969         : IDENTIFIER ASSIGN variable_initializer
970           {
971                 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
972           }
973         | IDENTIFIER
974           {
975                 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
976           }
977         | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
978           {
979                 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
980                         "To declare a fixed buffer field, use the fixed keyword before the field type");
981           }
982         ;
983
984 variable_initializer
985         : expression
986           {
987                 $$ = $1;
988           }
989         | array_initializer
990           {
991                 $$ = $1;
992           }
993         | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
994           {
995                 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
996           }
997         | STACKALLOC type
998           {
999                 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1000                 $$ = null;
1001           }
1002         ;
1003
1004 method_declaration
1005         : method_header {
1006                 iterator_container = (IIteratorContainer) $1;
1007                 if (RootContext.Documentation != null)
1008                         Lexer.doc_state = XmlCommentState.NotAllowed;
1009           }
1010           method_body
1011           {
1012                 Method method = (Method) $1;
1013                 Block b = (Block) $3;
1014                 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1015
1016                 if (b == null){
1017                         if ((method.ModFlags & extern_abstract) == 0){
1018                                 Report.Error (
1019                                         501, lexer.Location,  current_container.MakeName (method.Name) +
1020                                         "must declare a body because it is not marked abstract or extern");
1021                         }
1022                 } else {
1023                         if ((method.ModFlags & Modifiers.EXTERN) != 0){
1024                                 Report.Error (
1025                                         179, lexer.Location, current_container.MakeName (method.Name) +
1026                                         " is declared extern, but has a body");
1027                         }
1028                 }
1029
1030                 method.Block = (ToplevelBlock) $3;
1031                 current_container.AddMethod (method);
1032
1033                 current_local_parameters = null;
1034                 iterator_container = null;
1035
1036                 if (RootContext.Documentation != null)
1037                         Lexer.doc_state = XmlCommentState.Allowed;
1038           }
1039         ;
1040
1041 opt_error_modifier
1042         : /* empty */
1043         | modifiers 
1044           {
1045                 int m = (int) $1;
1046                 int i = 1;
1047
1048                 while (m != 0){
1049                         if ((i & m) != 0){
1050                                 Report.Error (
1051                                         1585, lexer.Location, "Member modifier `" + 
1052                                         Modifiers.Name (i) + "' must precede member type and name");
1053                         }
1054                         m &= ~i;
1055                         i = i << 1;
1056                 }
1057           }
1058         ;
1059
1060 method_header
1061         : opt_attributes
1062           opt_modifiers
1063           type namespace_or_type_name
1064           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
1065           {
1066                 MemberName name = (MemberName) $4;
1067
1068                 Method method = new Method (current_class, (Expression) $3, (int) $2,
1069                                             false, name,  (Parameters) $6, (Attributes) $1,
1070                                             lexer.Location);
1071
1072                 current_local_parameters = (Parameters) $6;
1073
1074                 if (RootContext.Documentation != null)
1075                         method.DocComment = Lexer.consume_doc_comment ();
1076
1077                 $$ = method;
1078           }
1079         | opt_attributes
1080           opt_modifiers
1081           VOID namespace_or_type_name
1082           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
1083           {
1084                 MemberName name = (MemberName) $4;
1085
1086                 Method method = new Method (current_class, TypeManager.system_void_expr,
1087                                             (int) $2, false, name, (Parameters) $6,
1088                                             (Attributes) $1, lexer.Location);
1089
1090                 current_local_parameters = (Parameters) $6;
1091
1092                 if (RootContext.Documentation != null)
1093                         method.DocComment = Lexer.consume_doc_comment ();
1094
1095                 $$ = method;
1096           }
1097         | opt_attributes
1098           opt_modifiers
1099           type 
1100           modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1101           {
1102                 Report.Error (1585, lexer.Location, 
1103                         String.Format ("Modifier {0} should appear before type", 
1104                                 Modifiers.Name ((int) $4)));
1105                 MemberName name = (MemberName) $4;
1106
1107                 Method method = new Method (current_class, TypeManager.system_void_expr,
1108                                             0, false, name, (Parameters) $7, (Attributes) $1,
1109                                             lexer.Location);
1110
1111                 current_local_parameters = (Parameters) $7;
1112
1113                 if (RootContext.Documentation != null)
1114                         method.DocComment = Lexer.consume_doc_comment ();
1115
1116                 $$ = method;
1117           }
1118         ;
1119
1120 method_body
1121         : block
1122         | SEMICOLON             { $$ = null; }
1123         ;
1124
1125 opt_formal_parameter_list
1126         : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
1127         | formal_parameter_list
1128         ;
1129
1130 formal_parameter_list
1131         : fixed_parameters              
1132           { 
1133                 ArrayList pars_list = (ArrayList) $1;
1134
1135                 Parameter [] pars = new Parameter [pars_list.Count];
1136                 pars_list.CopyTo (pars);
1137
1138                 $$ = new Parameters (pars, null, lexer.Location); 
1139           } 
1140         | fixed_parameters COMMA parameter_array
1141           {
1142                 ArrayList pars_list = (ArrayList) $1;
1143
1144                 Parameter [] pars = new Parameter [pars_list.Count];
1145                 pars_list.CopyTo (pars);
1146
1147                 $$ = new Parameters (pars, (Parameter) $3, lexer.Location); 
1148           }
1149         | fixed_parameters COMMA ARGLIST
1150           {
1151                 ArrayList pars_list = (ArrayList) $1;
1152
1153                 Parameter [] pars = new Parameter [pars_list.Count];
1154                 pars_list.CopyTo (pars);
1155
1156                 $$ = new Parameters (pars, true, lexer.Location);
1157           }
1158         | parameter_array COMMA fixed_parameters
1159           {
1160                 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1161                 $$ = null;
1162           }
1163         | ARGLIST COMMA fixed_parameters
1164           {
1165                 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1166                 $$ = null;
1167           }
1168         | parameter_array 
1169           {
1170                 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1171           }
1172         | ARGLIST
1173           {
1174                 $$ = new Parameters (null, true, lexer.Location);
1175           }
1176         ;
1177
1178 fixed_parameters
1179         : fixed_parameter       
1180           {
1181                 ArrayList pars = new ArrayList (4);
1182
1183                 pars.Add ($1);
1184                 $$ = pars;
1185           }
1186         | fixed_parameters COMMA fixed_parameter
1187           {
1188                 ArrayList pars = (ArrayList) $1;
1189
1190                 pars.Add ($3);
1191                 $$ = $1;
1192           }
1193         ;
1194
1195 fixed_parameter
1196         : opt_attributes
1197           opt_parameter_modifier
1198           type
1199           IDENTIFIER
1200           {
1201                 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1202           }
1203         | opt_attributes
1204           opt_parameter_modifier
1205           type
1206           {
1207                 Report.Error (1001, lexer.Location, "Identifier expected");
1208                 $$ = null;
1209           }
1210         | opt_attributes
1211           opt_parameter_modifier
1212           type
1213           error {
1214                 CheckIdentifierToken (yyToken);
1215                 $$ = null;
1216           }
1217         | opt_attributes
1218           opt_parameter_modifier
1219           type
1220           IDENTIFIER
1221           ASSIGN
1222           constant_expression
1223            {
1224                  Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1225                  $$ = null;
1226            }
1227         ;
1228
1229 opt_parameter_modifier
1230         : /* empty */           { $$ = Parameter.Modifier.NONE; }
1231         | parameter_modifier
1232         ;
1233
1234 parameter_modifier
1235         : REF                   { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1236         | OUT                   { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1237         ;
1238
1239 parameter_array
1240         : opt_attributes PARAMS type IDENTIFIER
1241           { 
1242                 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1243                 note ("type must be a single-dimension array type"); 
1244           }
1245         | opt_attributes PARAMS parameter_modifier type IDENTIFIER 
1246           {
1247                 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1248                 $$ = null;
1249           }
1250         | opt_attributes PARAMS type error {
1251                 CheckIdentifierToken (yyToken);
1252                 $$ = null;
1253           }
1254         ;
1255
1256 property_declaration
1257         : opt_attributes
1258           opt_modifiers
1259           type
1260           namespace_or_type_name
1261           {
1262                 if (RootContext.Documentation != null)
1263                         tmpComment = Lexer.consume_doc_comment ();
1264           }
1265           OPEN_BRACE 
1266           {
1267                 implicit_value_parameter_type = (Expression) $3;
1268
1269                 lexer.PropertyParsing = true;
1270
1271                 $$ = lexer.Location;
1272
1273                 iterator_container = SimpleIteratorContainer.GetSimple ();
1274           }
1275           accessor_declarations 
1276           {
1277                 lexer.PropertyParsing = false;
1278                 has_get = has_set = false;
1279           }
1280           CLOSE_BRACE
1281           { 
1282                 if ($8 == null)
1283                         break;
1284
1285                 Property prop;
1286                 Pair pair = (Pair) $8;
1287                 Accessor get_block = (Accessor) pair.First;
1288                 Accessor set_block = (Accessor) pair.Second;
1289
1290                 Location loc = (Location) $7;
1291                 MemberName name = (MemberName) $4;
1292
1293                 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1294                                      name, (Attributes) $1, get_block, set_block, loc);
1295                 if (SimpleIteratorContainer.Simple.Yields)
1296                         prop.SetYields ();
1297                 
1298                 current_container.AddProperty (prop);
1299                 implicit_value_parameter_type = null;
1300                 iterator_container = null;
1301
1302                 if (RootContext.Documentation != null)
1303                         prop.DocComment = ConsumeStoredComment ();
1304
1305           }
1306         ;
1307
1308 accessor_declarations
1309         : get_accessor_declaration
1310          {
1311                 $$ = new Pair ($1, null);
1312          }
1313         | get_accessor_declaration accessor_declarations
1314          { 
1315                 Pair pair = (Pair) $2;
1316                 pair.First = $1;
1317                 $$ = pair;
1318          }
1319         | set_accessor_declaration
1320          {
1321                 $$ = new Pair (null, $1);
1322          }
1323         | set_accessor_declaration accessor_declarations
1324          { 
1325                 Pair pair = (Pair) $2;
1326                 pair.Second = $1;
1327                 $$ = pair;
1328          }
1329         | error
1330           {
1331                 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1332                 $$ = null;
1333           }
1334         ;
1335
1336 get_accessor_declaration
1337         : opt_attributes opt_modifiers GET
1338           {
1339                 // If this is not the case, then current_local_parameters has already
1340                 // been set in indexer_declaration
1341                 if (parsing_indexer == false)
1342                         current_local_parameters = null;
1343                 else 
1344                         current_local_parameters = indexer_parameters;
1345                 lexer.PropertyParsing = false;
1346           }
1347           accessor_body
1348           {
1349                 if (has_get) {
1350                         Report.Error (1007, lexer.Location, "Property accessor already defined");
1351                         break;
1352                 }
1353                 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1354                 has_get = true;
1355                 current_local_parameters = null;
1356                 lexer.PropertyParsing = true;
1357
1358                 if (RootContext.Documentation != null)
1359                         if (Lexer.doc_state == XmlCommentState.Error)
1360                                 Lexer.doc_state = XmlCommentState.NotAllowed;
1361           }
1362         ;
1363
1364 set_accessor_declaration
1365         : opt_attributes opt_modifiers SET 
1366           {
1367                 Parameter [] args;
1368                 Parameter implicit_value_parameter = new Parameter (
1369                         implicit_value_parameter_type, "value", 
1370                         Parameter.Modifier.NONE, null);
1371
1372                 if (parsing_indexer == false) {
1373                         args  = new Parameter [1];
1374                         args [0] = implicit_value_parameter;
1375                         current_local_parameters = new Parameters (args, null, lexer.Location);
1376                 } else {
1377                         Parameter [] fpars = indexer_parameters.FixedParameters;
1378
1379                         if (fpars != null){
1380                                 int count = fpars.Length;
1381
1382                                 args = new Parameter [count + 1];
1383                                 fpars.CopyTo (args, 0);
1384                                 args [count] = implicit_value_parameter;
1385                         } else 
1386                                 args = null;
1387                         current_local_parameters = new Parameters (
1388                                 args, indexer_parameters.ArrayParameter, lexer.Location);
1389                 }
1390                 
1391                 lexer.PropertyParsing = false;
1392           }
1393           accessor_body
1394           {
1395                 if (has_set) {
1396                         Report.Error (1007, lexer.Location, "Property accessor already defined");
1397                         break;
1398                 }
1399                 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1400                 has_set = true;
1401                 current_local_parameters = null;
1402                 lexer.PropertyParsing = true;
1403
1404                 if (RootContext.Documentation != null
1405                         && Lexer.doc_state == XmlCommentState.Error)
1406                         Lexer.doc_state = XmlCommentState.NotAllowed;
1407           }
1408         ;
1409
1410 accessor_body
1411         : block 
1412         | SEMICOLON             { $$ = null; }
1413         ;
1414
1415 interface_declaration
1416         : opt_attributes
1417           opt_modifiers
1418           opt_partial
1419           INTERFACE member_name
1420           {
1421                 MemberName name = MakeName ((MemberName) $5);
1422                 bool partial = (bool) $3;
1423
1424                 if (partial) {
1425                         ClassPart part = PartialContainer.CreatePart (
1426                                 current_namespace, current_class, name, (int) $2,
1427                                 (Attributes) $1, Kind.Interface, lexer.Location);
1428
1429                         current_container = part.PartialContainer;
1430                         current_class = part;
1431                 } else {
1432                         current_class = new Interface (
1433                                 current_namespace, current_class, name, (int) $2,
1434                                 (Attributes) $1, lexer.Location);
1435
1436                         current_container.AddInterface (current_class);
1437                         current_container = current_class;
1438                         RootContext.Tree.RecordDecl (name, current_class);
1439                 }
1440           }
1441           opt_class_base
1442           {
1443                 current_class.Bases = (ArrayList) $7;
1444
1445                 if (RootContext.Documentation != null) {
1446                         current_class.DocComment = Lexer.consume_doc_comment ();
1447                         Lexer.doc_state = XmlCommentState.Allowed;
1448                 }
1449           }
1450           interface_body 
1451           {
1452                 if (RootContext.Documentation != null)
1453                         Lexer.doc_state = XmlCommentState.Allowed;
1454           }
1455           opt_semicolon 
1456           {
1457                 $$ = pop_current_class ();
1458           }
1459         | opt_attributes opt_modifiers opt_partial INTERFACE error {
1460                 CheckIdentifierToken (yyToken);
1461           }
1462         ;
1463
1464 interface_body
1465         : OPEN_BRACE
1466           opt_interface_member_declarations
1467           CLOSE_BRACE
1468         ;
1469
1470 opt_interface_member_declarations
1471         : /* empty */
1472         | interface_member_declarations
1473         ;
1474
1475 interface_member_declarations
1476         : interface_member_declaration
1477         | interface_member_declarations interface_member_declaration
1478         ;
1479
1480 interface_member_declaration
1481         : interface_method_declaration          
1482           { 
1483                 if ($1 == null)
1484                         break;
1485
1486                 Method m = (Method) $1;
1487
1488                 if (m.IsExplicitImpl)
1489                         Report.Error (541, lexer.Location, 
1490                                 "Explicit interface declaration can only be declared in a class or struct");
1491
1492                 current_container.AddMethod (m);
1493
1494                 if (RootContext.Documentation != null)
1495                         Lexer.doc_state = XmlCommentState.Allowed;
1496           }
1497         | interface_property_declaration        
1498           { 
1499                 if ($1 == null)
1500                         break;
1501
1502                 Property p = (Property) $1;
1503
1504                 if (p.IsExplicitImpl)
1505                         Report.Error (541, lexer.Location, 
1506                                 "Explicit interface declaration can only be declared in a class or struct");
1507
1508                 current_container.AddProperty (p);
1509
1510                 if (RootContext.Documentation != null)
1511                         Lexer.doc_state = XmlCommentState.Allowed;
1512        }
1513         | interface_event_declaration 
1514           { 
1515                 if ($1 != null){
1516                         Event e = (Event) $1;
1517
1518                         if (e.IsExplicitImpl)
1519                                 Report.Error (541, lexer.Location, 
1520                                     "Explicit interface declaration can only be declared in a class or struct");
1521                         
1522                         current_container.AddEvent (e);
1523                 }
1524
1525                 if (RootContext.Documentation != null)
1526                         Lexer.doc_state = XmlCommentState.Allowed;
1527           }
1528         | interface_indexer_declaration
1529           { 
1530                 if ($1 == null)
1531                         break;
1532
1533                 Indexer i = (Indexer) $1;
1534
1535                 if (i.IsExplicitImpl)
1536                         Report.Error (541, lexer.Location, 
1537                                 "Explicit interface declaration can only be declared in a class or struct");
1538
1539                 current_container.AddIndexer (i);
1540
1541                 if (RootContext.Documentation != null)
1542                         Lexer.doc_state = XmlCommentState.Allowed;
1543           }
1544         | delegate_declaration
1545           {
1546                 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1547           }
1548         | class_declaration
1549           {
1550                 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1551           }
1552         | struct_declaration
1553           {
1554                 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1555           }
1556         | enum_declaration 
1557           {
1558                 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1559           }
1560         | interface_declaration 
1561           {
1562                 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1563           } 
1564         | constant_declaration
1565           {
1566                 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1567           }
1568         ;
1569
1570 opt_new
1571         : opt_modifiers 
1572           {
1573                 int val = (int) $1;
1574                 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1575                 $$ = val;
1576           }
1577         ;
1578
1579 interface_method_declaration
1580         : opt_attributes opt_new type namespace_or_type_name
1581           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1582           SEMICOLON
1583           {
1584                 MemberName name = (MemberName) $4;
1585
1586                 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1587                                  name, (Parameters) $6, (Attributes) $1, lexer.Location);
1588                 if (RootContext.Documentation != null)
1589                         ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1590           }
1591         | opt_attributes opt_new type namespace_or_type_name
1592           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1593           OPEN_BRACE opt_statement_list CLOSE_BRACE
1594           {
1595                 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1596                 $$ = null;
1597           }
1598         ;
1599
1600 interface_property_declaration
1601         : opt_attributes
1602           opt_new
1603           type IDENTIFIER 
1604           OPEN_BRACE 
1605           { lexer.PropertyParsing = true; }
1606           interface_accessors 
1607           { lexer.PropertyParsing = false; }
1608           CLOSE_BRACE
1609           {
1610                 if ($3 == TypeManager.system_void_expr) {
1611                         Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1612                         break;
1613                 }
1614
1615                 if ($7 == null)
1616                         break;
1617
1618                 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1619
1620                 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1621                                    new MemberName ((string) $4), (Attributes) $1,
1622                                    pinfo.Get, pinfo.Set, lexer.Location);
1623                 if (RootContext.Documentation != null)
1624                         ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1625           }
1626         | opt_attributes
1627           opt_new
1628           type error {
1629                 CheckIdentifierToken (yyToken);
1630                 $$ = null;
1631           }
1632         ;
1633
1634 interface_accessors
1635         : opt_attributes opt_modifiers GET SEMICOLON    
1636         { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1637         | opt_attributes opt_modifiers GET OPEN_BRACE
1638          {  
1639                 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1640                 $$ = null;
1641          }
1642         | opt_attributes opt_modifiers SET SEMICOLON            
1643         { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1644         | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON 
1645           { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1646         | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1647           { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1648         |
1649           {
1650                 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1651           }
1652         ;
1653
1654 interface_event_declaration
1655         : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1656           {
1657                 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1658                                      new MemberName ((string) $5), null,
1659                                      (Attributes) $1, lexer.Location);
1660                 if (RootContext.Documentation != null)
1661                         ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1662           }
1663         | opt_attributes opt_new EVENT type error {
1664                 CheckIdentifierToken (yyToken);
1665                 $$ = null;
1666           }
1667         | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN  {
1668                 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1669                 $$ = null;
1670           }
1671         | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1672         {
1673                 lexer.EventParsing = true;
1674         }
1675         event_accessor_declarations
1676         {
1677                 lexer.EventParsing = false;
1678         }
1679         CLOSE_BRACE {
1680                 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1681                 $$ = null;
1682           }
1683         ;
1684
1685 interface_indexer_declaration 
1686         : opt_attributes opt_new type THIS 
1687           OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1688           OPEN_BRACE 
1689           { lexer.PropertyParsing = true; }
1690           interface_accessors 
1691           { lexer.PropertyParsing = false; }
1692           CLOSE_BRACE
1693           {
1694                 if ($10 == null)
1695                         break;
1696
1697                 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1698
1699                 $$ = new Indexer (current_class, (Expression) $3,
1700                                   new MemberName (TypeContainer.DefaultIndexerName),
1701                                   (int) $2, true, (Parameters) $6, (Attributes) $1,
1702                                   info.Get, info.Set, lexer.Location);
1703                 if (RootContext.Documentation != null)
1704                         ((Indexer) $$).DocComment = ConsumeStoredComment ();
1705           }
1706         ;
1707
1708 operator_declaration
1709         : opt_attributes opt_modifiers operator_declarator 
1710           {
1711                 iterator_container = SimpleIteratorContainer.GetSimple ();
1712           }
1713           operator_body
1714           {
1715                 if ($3 == null)
1716                         break;
1717
1718                 OperatorDeclaration decl = (OperatorDeclaration) $3;
1719                 
1720                 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1721
1722                 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1723                 if (decl.arg2type != null)
1724                         param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1725
1726                 Operator op = new Operator (
1727                         current_class, decl.optype, decl.ret_type, (int) $2, 
1728                         new Parameters (param_list, null, decl.location),
1729                         (ToplevelBlock) $5, (Attributes) $1, decl.location);
1730
1731                 if (RootContext.Documentation != null) {
1732                         op.DocComment = tmpComment;
1733                         Lexer.doc_state = XmlCommentState.Allowed;
1734                 }
1735
1736                 if (SimpleIteratorContainer.Simple.Yields)
1737                         op.SetYields ();
1738
1739                 // Note again, checking is done in semantic analysis
1740                 current_container.AddOperator (op);
1741
1742                 current_local_parameters = null;
1743                 iterator_container = null;
1744           }
1745         ;
1746
1747 operator_body 
1748         : block
1749         | SEMICOLON { $$ = null; }
1750         ; 
1751 operator_declarator
1752         : type OPERATOR overloadable_operator 
1753           OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1754         {
1755                 Operator.OpType op = (Operator.OpType) $3;
1756                 CheckUnaryOperator (op);
1757
1758                 if (op == Operator.OpType.Addition)
1759                         op = Operator.OpType.UnaryPlus;
1760
1761                 if (op == Operator.OpType.Subtraction)
1762                         op = Operator.OpType.UnaryNegation;
1763
1764                 Parameter [] pars = new Parameter [1];
1765                 Expression type = (Expression) $5;
1766
1767                 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1768
1769                 current_local_parameters = new Parameters (pars, null, lexer.Location);
1770
1771                 if (RootContext.Documentation != null) {
1772                         tmpComment = Lexer.consume_doc_comment ();
1773                         Lexer.doc_state = XmlCommentState.NotAllowed;
1774                 }
1775
1776                 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1777                                               null, null, lexer.Location);
1778         }
1779         | type OPERATOR overloadable_operator
1780           OPEN_PARENS 
1781                 type IDENTIFIER COMMA
1782                 type IDENTIFIER 
1783           CLOSE_PARENS
1784         {
1785                 CheckBinaryOperator ((Operator.OpType) $3);
1786
1787                 Parameter [] pars = new Parameter [2];
1788
1789                 Expression typeL = (Expression) $5;
1790                 Expression typeR = (Expression) $8;
1791
1792                pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1793                pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1794
1795                current_local_parameters = new Parameters (pars, null, lexer.Location);
1796
1797                 if (RootContext.Documentation != null) {
1798                         tmpComment = Lexer.consume_doc_comment ();
1799                         Lexer.doc_state = XmlCommentState.NotAllowed;
1800                 }
1801                
1802                $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1, 
1803                                              typeL, (string) $6,
1804                                              typeR, (string) $9, lexer.Location);
1805         }
1806         | conversion_operator_declarator
1807         | type OPERATOR overloadable_operator
1808           OPEN_PARENS 
1809                 type IDENTIFIER COMMA
1810                 type IDENTIFIER COMMA
1811                 type IDENTIFIER
1812           CLOSE_PARENS
1813         {
1814                 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1815                 $$ = null;
1816         }
1817
1818         ;
1819
1820 overloadable_operator
1821 // Unary operators:
1822         : BANG   { $$ = Operator.OpType.LogicalNot; }
1823         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
1824         | OP_INC { $$ = Operator.OpType.Increment; }
1825         | OP_DEC { $$ = Operator.OpType.Decrement; }
1826         | TRUE   { $$ = Operator.OpType.True; }
1827         | FALSE  { $$ = Operator.OpType.False; }
1828 // Unary and binary:
1829         | PLUS { $$ = Operator.OpType.Addition; }
1830         | MINUS { $$ = Operator.OpType.Subtraction; }
1831 // Binary:
1832         | STAR { $$ = Operator.OpType.Multiply; }
1833         | DIV {  $$ = Operator.OpType.Division; }
1834         | PERCENT { $$ = Operator.OpType.Modulus; }
1835         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1836         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1837         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1838         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1839         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1840         | OP_EQ { $$ = Operator.OpType.Equality; }
1841         | OP_NE { $$ = Operator.OpType.Inequality; }
1842         | OP_GT { $$ = Operator.OpType.GreaterThan; }
1843         | OP_LT { $$ = Operator.OpType.LessThan; }
1844         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1845         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1846         ;
1847
1848 conversion_operator_declarator
1849         : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1850           {
1851                 Parameter [] pars = new Parameter [1];
1852
1853                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1854
1855                 current_local_parameters = new Parameters (pars, null, lexer.Location);  
1856                   
1857                 if (RootContext.Documentation != null) {
1858                         tmpComment = Lexer.consume_doc_comment ();
1859                         Lexer.doc_state = XmlCommentState.NotAllowed;
1860                 }
1861
1862                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1863                                               null, null, lexer.Location);
1864           }
1865         | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1866           {
1867                 Parameter [] pars = new Parameter [1];
1868
1869                 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1870
1871                 current_local_parameters = new Parameters (pars, null, lexer.Location);  
1872                   
1873                 if (RootContext.Documentation != null) {
1874                         tmpComment = Lexer.consume_doc_comment ();
1875                         Lexer.doc_state = XmlCommentState.NotAllowed;
1876                 }
1877
1878                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1879                                               null, null, lexer.Location);
1880           }
1881         | IMPLICIT error 
1882           {
1883                 syntax_error (lexer.Location, "'operator' expected");
1884           }
1885         | EXPLICIT error 
1886           {
1887                 syntax_error (lexer.Location, "'operator' expected");
1888           }
1889         ;
1890
1891 constructor_declaration
1892         : opt_attributes
1893           opt_modifiers
1894           constructor_declarator
1895           constructor_body
1896           { 
1897                 Constructor c = (Constructor) $3;
1898                 c.Block = (ToplevelBlock) $4;
1899                 c.OptAttributes = (Attributes) $1;
1900                 c.ModFlags = (int) $2;
1901         
1902                 if (RootContext.Documentation != null)
1903                         c.DocComment = ConsumeStoredComment ();
1904
1905                 if (c.Name == current_container.Basename){
1906                         if ((c.ModFlags & Modifiers.STATIC) != 0){
1907                                 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1908                                         Report.Error (
1909                                                 515, c.Location, String.Format (
1910                                                 "`{0}.{1}': static constructor can not have access modifiers",
1911                                                 c.Name, current_container.Name));
1912                                 }
1913         
1914                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);   
1915         
1916                                 if (c.Initializer != null){
1917                                         Report.Error (
1918                                                 514, c.Location, 
1919                                                 "Static constructors can not have an explicit this or base " +
1920                                                 "constructor invocations");
1921                                 }
1922         
1923                                 if (!c.Parameters.Empty){
1924                                         Report.Error (
1925                                                 132, c.Location, "Static constructors should not have parameters");
1926                                 }
1927                         } else {
1928                                 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1929                         }
1930                 } else {
1931                         // We let another layer check the validity of the constructor.
1932                         //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1933                 }
1934
1935                 current_container.AddConstructor (c);
1936
1937                 current_local_parameters = null;
1938                 if (RootContext.Documentation != null)
1939                         Lexer.doc_state = XmlCommentState.Allowed;
1940           }
1941         ;
1942
1943 constructor_declarator
1944         : IDENTIFIER
1945           {
1946                 if (RootContext.Documentation != null) {
1947                         tmpComment = Lexer.consume_doc_comment ();
1948                         Lexer.doc_state = XmlCommentState.Allowed;
1949                 }
1950           }
1951           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1952           {
1953                 current_local_parameters = (Parameters) $4;
1954           }
1955           opt_constructor_initializer
1956           {
1957                 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1958                                       (ConstructorInitializer) $8, (Location) $6);
1959           }
1960         ;
1961
1962 constructor_body
1963         : block
1964         | SEMICOLON             { $$ = null; }
1965         ;
1966
1967 opt_constructor_initializer
1968         : /* empty */                   { $$ = null; }
1969         | constructor_initializer
1970         ;
1971
1972 constructor_initializer
1973         : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1974           {
1975                 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1976           }
1977         | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1978           {
1979                 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1980           }
1981         | COLON error {
1982                 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1983                 $$ = null;
1984           }
1985         ;
1986
1987 opt_finalizer
1988         : /* EMPTY */           { $$ = 0; }
1989         | UNSAFE                { $$ = Modifiers.UNSAFE; }
1990         | EXTERN                { $$ = Modifiers.EXTERN; }
1991         ;
1992         
1993 destructor_declaration
1994         : opt_attributes opt_finalizer TILDE 
1995           {
1996                 if (RootContext.Documentation != null) {
1997                         tmpComment = Lexer.consume_doc_comment ();
1998                         Lexer.doc_state = XmlCommentState.NotAllowed;
1999                 }
2000           }
2001           IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2002           {
2003                 if ((string) $5 != current_container.Basename){
2004                         Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2005                 } else if (current_container.Kind != Kind.Class){
2006                         Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2007                 } else {
2008                         Location l = lexer.Location;
2009
2010                         int m = (int) $2;
2011                         if (!RootContext.StdLib && current_container.Name == "System.Object")
2012                                 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2013                         else
2014                                 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2015                         
2016                         if ((m & Modifiers.UNSAFE) != 0){
2017                                 if (!RootContext.Unsafe){
2018                                         Report.Error (227, l,
2019                                               "Unsafe code requires the -unsafe command " +
2020                                               "line option to be specified");
2021                                 }
2022                         }
2023                         
2024                         Method d = new Destructor (
2025                                 current_class, TypeManager.system_void_expr, m, "Finalize", 
2026                                 new Parameters (null, null, l), (Attributes) $1, l);
2027                         if (RootContext.Documentation != null)
2028                                 d.DocComment = ConsumeStoredComment ();
2029                   
2030                         d.Block = (ToplevelBlock) $8;
2031                         current_container.AddMethod (d);
2032                 }
2033           }
2034         ;
2035
2036 event_declaration
2037         : opt_attributes
2038           opt_modifiers
2039           EVENT type variable_declarators SEMICOLON
2040           {
2041                 foreach (VariableDeclaration var in (ArrayList) $5) {
2042
2043                         MemberName name = new MemberName (var.identifier);
2044
2045                         Event e = new EventField (
2046                                 current_class, (Expression) $4, (int) $2, false, name,
2047                                 var.expression_or_array_initializer, (Attributes) $1,
2048                                 lexer.Location);
2049
2050                         current_container.AddEvent (e);
2051
2052                         if (RootContext.Documentation != null) {
2053                                 e.DocComment = Lexer.consume_doc_comment ();
2054                                 Lexer.doc_state = XmlCommentState.Allowed;
2055                         }
2056                 }
2057           }
2058         | opt_attributes
2059           opt_modifiers
2060           EVENT type namespace_or_type_name
2061           OPEN_BRACE _mark_
2062           {
2063                 implicit_value_parameter_type = (Expression) $4;  
2064                 lexer.EventParsing = true;
2065           }
2066           event_accessor_declarations
2067           {
2068                 lexer.EventParsing = false;  
2069           }
2070           CLOSE_BRACE
2071           {
2072                 Location loc = (Location) $7;
2073
2074                 if ($9 == null){
2075                         Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2076                         $$ = null;
2077                 } else {
2078                         Pair pair = (Pair) $9;
2079                         
2080                         MemberName name = (MemberName) $5;
2081
2082                         Event e = new EventProperty (
2083                                 current_class, (Expression) $4, (int) $2, false, name, null,
2084                                 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2085                                 loc);
2086                         if (RootContext.Documentation != null) {
2087                                 e.DocComment = Lexer.consume_doc_comment ();
2088                                 Lexer.doc_state = XmlCommentState.Allowed;
2089                         }
2090
2091                         current_container.AddEvent (e);
2092                         implicit_value_parameter_type = null;
2093                 }
2094           }
2095         | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2096                 MemberName mn = (MemberName) $5;
2097
2098                 if (mn.Left != null)
2099                         Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2100                 else 
2101                         Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2102
2103                 if (RootContext.Documentation != null)
2104                         Lexer.doc_state = XmlCommentState.Allowed;
2105           }
2106         ;
2107
2108 event_accessor_declarations
2109         : add_accessor_declaration remove_accessor_declaration
2110         {
2111                 $$ = new Pair ($1, $2);
2112         }
2113         | remove_accessor_declaration add_accessor_declaration
2114         {
2115                 $$ = new Pair ($2, $1);
2116         }       
2117         | add_accessor_declaration  { $$ = null; } 
2118         | remove_accessor_declaration { $$ = null; } 
2119         | error
2120         { 
2121                 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2122                 $$ = null;
2123         }
2124         | { $$ = null; }
2125         ;
2126
2127 add_accessor_declaration
2128         : opt_attributes ADD
2129           {
2130                 Parameter [] args = new Parameter [1];
2131                 Parameter implicit_value_parameter = new Parameter (
2132                         implicit_value_parameter_type, "value", 
2133                         Parameter.Modifier.NONE, null);
2134
2135                 args [0] = implicit_value_parameter;
2136                 
2137                 current_local_parameters = new Parameters (args, null, lexer.Location);  
2138                 lexer.EventParsing = false;
2139           }
2140           block
2141           {
2142                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2143                 lexer.EventParsing = true;
2144           }
2145         | opt_attributes ADD error {
2146                 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2147                 $$ = null;
2148           }
2149         | opt_attributes modifiers ADD {
2150                 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2151                 $$ = null;
2152           }
2153         ;
2154
2155 remove_accessor_declaration
2156         : opt_attributes REMOVE
2157           {
2158                 Parameter [] args = new Parameter [1];
2159                 Parameter implicit_value_parameter = new Parameter (
2160                         implicit_value_parameter_type, "value", 
2161                         Parameter.Modifier.NONE, null);
2162
2163                 args [0] = implicit_value_parameter;
2164                 
2165                 current_local_parameters = new Parameters (args, null, lexer.Location);  
2166                 lexer.EventParsing = false;
2167           }
2168           block
2169           {
2170                 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2171                 lexer.EventParsing = true;
2172           }
2173         | opt_attributes REMOVE error {
2174                 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2175                 $$ = null;
2176           }
2177         | opt_attributes modifiers REMOVE {
2178                 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2179                 $$ = null;
2180           }
2181         ;
2182
2183 indexer_declaration
2184         : opt_attributes opt_modifiers indexer_declarator 
2185           OPEN_BRACE _mark_
2186           {
2187                 IndexerDeclaration decl = (IndexerDeclaration) $3;
2188
2189                 implicit_value_parameter_type = decl.type;
2190                 
2191                 lexer.PropertyParsing = true;
2192                 parsing_indexer  = true;
2193                 
2194                 indexer_parameters = decl.param_list;
2195           }
2196           accessor_declarations 
2197           {
2198                   lexer.PropertyParsing = false;
2199                   has_get = has_set = false;
2200                   parsing_indexer  = false;
2201           }
2202           CLOSE_BRACE
2203           { 
2204                 if ($7 == null)
2205                         break;
2206
2207                 // The signature is computed from the signature of the indexer.  Look
2208                 // at section 3.6 on the spec
2209                 Location loc = (Location) $5;
2210                 Indexer indexer;
2211                 IndexerDeclaration decl = (IndexerDeclaration) $3;
2212                 Pair pair = (Pair) $7;
2213                 Accessor get_block = (Accessor) pair.First;
2214                 Accessor set_block = (Accessor) pair.Second;
2215
2216                 MemberName name;
2217                 if (decl.interface_type != null)
2218                         name = new MemberName (decl.interface_type,
2219                                                TypeContainer.DefaultIndexerName);
2220                 else
2221                         name = new MemberName (TypeContainer.DefaultIndexerName);
2222
2223                 indexer = new Indexer (current_class, decl.type, name,
2224                                        (int) $2, false, decl.param_list, (Attributes) $1,
2225                                        get_block, set_block, loc);
2226                 if (RootContext.Documentation != null)
2227                         indexer.DocComment = ConsumeStoredComment ();
2228
2229                 current_container.AddIndexer (indexer);
2230                 
2231                 current_local_parameters = null;
2232                 implicit_value_parameter_type = null;
2233                 indexer_parameters = null;
2234           }
2235         ;
2236
2237 indexer_declarator
2238         : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2239           {
2240                 Parameters pars = (Parameters) $4;
2241                 if (pars.HasArglist) {
2242                         // "__arglist is not valid in this context"
2243                         Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2244                 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2245                         Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2246                 }
2247                 if (RootContext.Documentation != null) {
2248                         tmpComment = Lexer.consume_doc_comment ();
2249                         Lexer.doc_state = XmlCommentState.Allowed;
2250                 }
2251
2252                 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2253           }
2254         | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2255           {
2256                 Parameters pars = (Parameters) $6;
2257
2258                 if (pars.HasArglist) {
2259                         // "__arglist is not valid in this context"
2260                         Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2261                 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2262                         Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2263                 }
2264
2265                 MemberName name = (MemberName) $2;
2266                 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2267
2268                 if (RootContext.Documentation != null) {
2269                         tmpComment = Lexer.consume_doc_comment ();
2270                         Lexer.doc_state = XmlCommentState.Allowed;
2271                 }
2272           }
2273         ;
2274
2275 enum_declaration
2276         : opt_attributes
2277           opt_modifiers
2278           opt_partial
2279           ENUM IDENTIFIER 
2280           opt_enum_base {
2281                 if (RootContext.Documentation != null)
2282                         enumTypeComment = Lexer.consume_doc_comment ();
2283           }
2284           enum_body
2285           opt_semicolon
2286           {
2287                 bool partial = (bool) $3;
2288
2289                 if (partial) {
2290                         Report.Error (267, lexer.Location, "The partial modifier can only appear before a 'class', 'struct', or 'interface'");
2291                         break;  // assumes that the parser put us in a switch
2292                 }
2293
2294                 Location enum_location = lexer.Location;
2295
2296                 MemberName name = MakeName (new MemberName ((string) $5));
2297                 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2298                                    name, (Attributes) $1, enum_location);
2299                 
2300                 if (RootContext.Documentation != null)
2301                         e.DocComment = enumTypeComment;
2302
2303                 foreach (VariableDeclaration ev in (ArrayList) $8) {
2304                         e.AddEnumMember (ev.identifier, 
2305                                          (Expression) ev.expression_or_array_initializer,
2306                                          ev.Location, ev.OptAttributes,
2307                                          ev.DocComment);
2308                 }
2309
2310                 current_container.AddEnum (e);
2311                 RootContext.Tree.RecordDecl (name, e);
2312                 $$ = e;
2313
2314           }
2315         ;
2316
2317 opt_enum_base
2318         : /* empty */           { $$ = TypeManager.system_int32_expr; }
2319         | COLON type            { $$ = $2;   }
2320         ;
2321
2322 enum_body
2323         : OPEN_BRACE
2324           {
2325                 if (RootContext.Documentation != null)
2326                         Lexer.doc_state = XmlCommentState.Allowed;
2327           }
2328           opt_enum_member_declarations
2329           {
2330                 // here will be evaluated after CLOSE_BLACE is consumed.
2331                 if (RootContext.Documentation != null)
2332                         Lexer.doc_state = XmlCommentState.Allowed;
2333           }
2334           CLOSE_BRACE
2335           {
2336                 $$ = $3;
2337           }
2338         ;
2339
2340 opt_enum_member_declarations
2341         : /* empty */                   { $$ = new ArrayList (4); }
2342         | enum_member_declarations opt_comma { $$ = $1; }
2343         ;
2344
2345 enum_member_declarations
2346         : enum_member_declaration 
2347           {
2348                 ArrayList l = new ArrayList (4);
2349
2350                 l.Add ($1);
2351                 $$ = l;
2352           }
2353         | enum_member_declarations COMMA enum_member_declaration
2354           {
2355                 ArrayList l = (ArrayList) $1;
2356
2357                 l.Add ($3);
2358
2359                 $$ = l;
2360           }
2361         ;
2362
2363 enum_member_declaration
2364         : opt_attributes IDENTIFIER 
2365           {
2366                 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2367
2368                 if (RootContext.Documentation != null) {
2369                         vd.DocComment = Lexer.consume_doc_comment ();
2370                         Lexer.doc_state = XmlCommentState.Allowed;
2371                 }
2372
2373                 $$ = vd;
2374           }
2375         | opt_attributes IDENTIFIER
2376           {
2377                 $$ = lexer.Location;
2378                 if (RootContext.Documentation != null) {
2379                         tmpComment = Lexer.consume_doc_comment ();
2380                         Lexer.doc_state = XmlCommentState.NotAllowed;
2381                 }
2382           }
2383           ASSIGN expression
2384           { 
2385                 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2386
2387                 if (RootContext.Documentation != null)
2388                         vd.DocComment = ConsumeStoredComment ();
2389
2390                 $$ = vd;
2391           }
2392         ;
2393
2394 delegate_declaration
2395         : opt_attributes
2396           opt_modifiers
2397           DELEGATE type member_name
2398           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
2399           SEMICOLON
2400           {
2401                 Location l = lexer.Location;
2402                 MemberName name = MakeName ((MemberName) $5);
2403                 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2404                                              (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2405
2406                 if (RootContext.Documentation != null) {
2407                         del.DocComment = Lexer.consume_doc_comment ();
2408                         Lexer.doc_state = XmlCommentState.Allowed;
2409                 }
2410
2411                 current_container.AddDelegate (del);
2412                 RootContext.Tree.RecordDecl (name, del);
2413                 $$ = del;
2414           }     
2415         ;
2416
2417 namespace_or_type_name
2418         : member_name
2419         | namespace_or_type_name DOT IDENTIFIER {
2420                 $$ = new MemberName ((MemberName) $1, (string) $3);
2421           }
2422         ;
2423
2424 member_name
2425         : IDENTIFIER {
2426                 $$ = new MemberName ((string) $1);
2427           }
2428         ;
2429
2430 /* 
2431  * Before you think of adding a return_type, notice that we have been
2432  * using two rules in the places where it matters (one rule using type
2433  * and another identical one that uses VOID as the return type).  This
2434  * gets rid of a shift/reduce couple
2435  */
2436 type
2437         : namespace_or_type_name
2438           {
2439                 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2440           }
2441         | builtin_types
2442         | array_type
2443         | pointer_type    
2444         ;
2445
2446
2447 pointer_type
2448         : type STAR
2449           {
2450                 //
2451                 // Note that here only unmanaged types are allowed but we
2452                 // can't perform checks during this phase - we do it during
2453                 // semantic analysis.
2454                 //
2455                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2456           }
2457         | VOID STAR
2458           {
2459                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2460           }
2461         ;
2462
2463 non_expression_type
2464         : builtin_types 
2465         | non_expression_type rank_specifier
2466           {
2467                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2468           }
2469         | non_expression_type STAR
2470           {
2471                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2472           }
2473         | expression rank_specifiers 
2474           {
2475                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2476           }
2477         | expression STAR 
2478           {
2479                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2480           }
2481         
2482         //
2483         // We need this because the parser will happily go and reduce IDENTIFIER STAR
2484         // through this different path
2485         //
2486         | multiplicative_expression STAR 
2487           {
2488                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2489           }
2490         ;
2491
2492 type_list
2493         : type
2494           {
2495                 ArrayList types = new ArrayList (4);
2496
2497                 types.Add ($1);
2498                 $$ = types;
2499           }
2500         | type_list COMMA type
2501           {
2502                 ArrayList types = (ArrayList) $1;
2503
2504                 types.Add ($3);
2505                 $$ = types;
2506           }
2507         ;
2508
2509 /*
2510  * replaces all the productions for isolating the various
2511  * simple types, but we need this to reuse it easily in local_variable_type
2512  */
2513 builtin_types
2514         : OBJECT        { $$ = TypeManager.system_object_expr; }
2515         | STRING        { $$ = TypeManager.system_string_expr; }
2516         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2517         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2518         | FLOAT         { $$ = TypeManager.system_single_expr; }
2519         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2520         | integral_type
2521         ;
2522
2523 integral_type
2524         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2525         | BYTE          { $$ = TypeManager.system_byte_expr; }
2526         | SHORT         { $$ = TypeManager.system_int16_expr; }
2527         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2528         | INT           { $$ = TypeManager.system_int32_expr; }
2529         | UINT          { $$ = TypeManager.system_uint32_expr; }
2530         | LONG          { $$ = TypeManager.system_int64_expr; }
2531         | ULONG         { $$ = TypeManager.system_uint64_expr; }
2532         | CHAR          { $$ = TypeManager.system_char_expr; }
2533         | VOID          { $$ = TypeManager.system_void_expr; }
2534         ;
2535
2536 array_type
2537         : type rank_specifiers
2538           {
2539                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2540           }
2541         ;
2542
2543 //
2544 // Expressions, section 7.5
2545 //
2546 primary_expression
2547         : literal
2548           {
2549                 // 7.5.1: Literals
2550           }
2551  
2552         | member_name
2553           {
2554                 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2555           }
2556         | parenthesized_expression
2557         | member_access
2558         | invocation_expression
2559         | element_access
2560         | this_access
2561         | base_access
2562         | post_increment_expression
2563         | post_decrement_expression
2564         | new_expression
2565         | typeof_expression
2566         | sizeof_expression
2567         | checked_expression
2568         | unchecked_expression
2569         | pointer_member_access
2570         | anonymous_method_expression
2571         ;
2572
2573 literal
2574         : boolean_literal
2575         | integer_literal
2576         | real_literal
2577         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value); }
2578         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value); }
2579         | NULL                  { $$ = NullLiteral.Null; }
2580         ;
2581
2582 real_literal
2583         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value); }
2584         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value); }
2585         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2586         ;
2587
2588 integer_literal
2589         : LITERAL_INTEGER       { 
2590                 object v = lexer.Value;
2591
2592                 if (v is int){
2593                         int i = (int) v;
2594
2595                         if (i == 0)
2596                                 $$ = IntLiteral.Zero;
2597                         else if (i == 1)
2598                                 $$ = IntLiteral.One;
2599                         else
2600                                 $$ = new IntLiteral (i);
2601                 } else if (v is uint)
2602                         $$ = new UIntLiteral ((UInt32) v);
2603                 else if (v is long)
2604                         $$ = new LongLiteral ((Int64) v);
2605                 else if (v is ulong)
2606                         $$ = new ULongLiteral ((UInt64) v);
2607                 else
2608                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
2609           }
2610         ;
2611
2612 boolean_literal
2613         : TRUE                  { $$ = new BoolLiteral (true); }
2614         | FALSE                 { $$ = new BoolLiteral (false); }
2615         ;
2616
2617 parenthesized_expression_0
2618         : OPEN_PARENS expression CLOSE_PARENS
2619           {
2620                 $$ = $2;
2621                 lexer.Deambiguate_CloseParens ();
2622                 // After this, the next token returned is one of
2623                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2624                 // or CLOSE_PARENS_MINUS.
2625           }
2626         ;
2627
2628 parenthesized_expression
2629         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2630           {
2631                 $$ = $1;
2632           }
2633         | parenthesized_expression_0 CLOSE_PARENS_MINUS
2634           {
2635                 // If a parenthesized expression is followed by a minus, we need to wrap
2636                 // the expression inside a ParenthesizedExpression for the CS0075 check
2637                 // in Binary.DoResolve().
2638                 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2639           }
2640         ;;
2641
2642 member_access
2643         : primary_expression DOT IDENTIFIER
2644           {
2645                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2646           }
2647         | predefined_type DOT IDENTIFIER
2648           {
2649                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2650           }
2651         ;
2652
2653 predefined_type
2654         : builtin_types
2655         ;
2656
2657 invocation_expression
2658         : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2659           {
2660                 if ($1 == null) {
2661                         Location l = lexer.Location;
2662                         Report.Error (1, l, "Parse error");
2663                 }
2664                 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2665           }
2666         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2667           {
2668                 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2669           }
2670         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2671           {
2672                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2673           }
2674         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2675           {
2676                 ArrayList args = new ArrayList (1);
2677                 args.Add ($4);
2678                 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2679           }
2680         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2681           {
2682                 ArrayList args = ((ArrayList) $4);
2683                 args.Add ($6);
2684                 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2685           }
2686         ;
2687
2688 opt_argument_list
2689         : /* empty */           { $$ = null; }
2690         | argument_list
2691         ;
2692
2693 argument_list
2694         : argument
2695           { 
2696                 ArrayList list = new ArrayList (4);
2697                 list.Add ($1);
2698                 $$ = list;
2699           }
2700         | argument_list COMMA argument
2701           {
2702                 ArrayList list = (ArrayList) $1;
2703                 list.Add ($3);
2704                 $$ = list;
2705           }
2706         | argument_list error {
2707                 CheckToken (1026, yyToken, ", or ) expected");
2708           }
2709         ;
2710
2711 argument
2712         : expression
2713           {
2714                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2715           }
2716         | non_simple_argument
2717           {
2718                 $$ = $1;
2719           }
2720         ;
2721
2722 non_simple_argument
2723         : REF variable_reference 
2724           { 
2725                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2726           }
2727         | OUT variable_reference 
2728           { 
2729                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2730           }
2731         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2732           {
2733                 ArrayList list = (ArrayList) $3;
2734                 Argument[] args = new Argument [list.Count];
2735                 list.CopyTo (args, 0);
2736
2737                 Expression expr = new Arglist (args, lexer.Location);
2738                 $$ = new Argument (expr, Argument.AType.Expression);
2739           }
2740         | ARGLIST
2741           {
2742                 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2743           }
2744         ;
2745
2746 variable_reference
2747         : expression { note ("section 5.4"); $$ = $1; }
2748         ;
2749
2750 element_access
2751         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
2752           {
2753                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2754           }
2755         | primary_expression rank_specifiers
2756           {
2757                 // So the super-trick is that primary_expression
2758                 // can only be either a SimpleName or a MemberAccess. 
2759                 // The MemberAccess case arises when you have a fully qualified type-name like :
2760                 // Foo.Bar.Blah i;
2761                 // SimpleName is when you have
2762                 // Blah i;
2763                   
2764                 Expression expr = (Expression) $1;  
2765                 if (expr is ComposedCast){
2766                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2767                 } else if (!(expr is SimpleName || expr is MemberAccess)){
2768                         Error_ExpectingTypeName (lexer.Location, expr);
2769                         $$ = TypeManager.system_object_expr;
2770                 } else {
2771                         //
2772                         // So we extract the string corresponding to the SimpleName
2773                         // or MemberAccess
2774                         // 
2775                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2776                 }
2777           }
2778         ;
2779
2780 expression_list
2781         : expression
2782           {
2783                 ArrayList list = new ArrayList (4);
2784                 list.Add ($1);
2785                 $$ = list;
2786           }
2787         | expression_list COMMA expression
2788           {
2789                 ArrayList list = (ArrayList) $1;
2790                 list.Add ($3);
2791                 $$ = list;
2792           }
2793         ;
2794
2795 this_access
2796         : THIS
2797           {
2798                 $$ = new This (current_block, lexer.Location);
2799           }
2800         ;
2801
2802 base_access
2803         : BASE DOT IDENTIFIER
2804           {
2805                 $$ = new BaseAccess ((string) $3, lexer.Location);
2806           }
2807         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2808           {
2809                 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2810           }
2811         | BASE error {
2812                 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2813                 $$ = null;
2814           }
2815         ;
2816
2817 post_increment_expression
2818         : primary_expression OP_INC
2819           {
2820                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2821                                        (Expression) $1, lexer.Location);
2822           }
2823         ;
2824
2825 post_decrement_expression
2826         : primary_expression OP_DEC
2827           {
2828                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2829                                        (Expression) $1, lexer.Location);
2830           }
2831         ;
2832
2833 new_expression
2834         : object_or_delegate_creation_expression
2835         | array_creation_expression
2836         ;
2837
2838 object_or_delegate_creation_expression
2839         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2840           {
2841                 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2842           }
2843         ;
2844
2845 array_creation_expression
2846         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
2847           opt_rank_specifier
2848           opt_array_initializer
2849           {
2850                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2851           }
2852         | NEW type rank_specifiers array_initializer
2853           {
2854                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2855           }
2856         | NEW error
2857           {
2858                 Report.Error (1031, lexer.Location, "Type expected");
2859                 $$ = null;
2860           }          
2861         | NEW type error 
2862           {
2863                 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2864           }
2865         ;
2866
2867 opt_rank_specifier
2868         : /* empty */
2869           {
2870                   $$ = "";
2871           }
2872         | rank_specifiers
2873           {
2874                         $$ = $1;
2875           }
2876         ;
2877
2878 rank_specifiers
2879         : rank_specifier opt_rank_specifier
2880           {
2881                   $$ = (string) $2 + (string) $1;
2882           }
2883         ;
2884
2885 rank_specifier
2886         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2887           {
2888                 $$ = "[" + (string) $2 + "]";
2889           }
2890         ;
2891
2892 opt_dim_separators
2893         : /* empty */
2894           {
2895                 $$ = "";
2896           }
2897         | dim_separators
2898           {
2899                   $$ = $1;
2900           }               
2901         ;
2902
2903 dim_separators
2904         : COMMA
2905           {
2906                 $$ = ",";
2907           }
2908         | dim_separators COMMA
2909           {
2910                 $$ = (string) $1 + ",";
2911           }
2912         ;
2913
2914 opt_array_initializer
2915         : /* empty */
2916           {
2917                 $$ = null;
2918           }
2919         | array_initializer
2920           {
2921                 $$ = $1;
2922           }
2923         ;
2924
2925 array_initializer
2926         : OPEN_BRACE CLOSE_BRACE
2927           {
2928                 ArrayList list = new ArrayList (4);
2929                 $$ = list;
2930           }
2931         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2932           {
2933                 $$ = (ArrayList) $2;
2934           }
2935         ;
2936
2937 variable_initializer_list
2938         : variable_initializer
2939           {
2940                 ArrayList list = new ArrayList (4);
2941                 list.Add ($1);
2942                 $$ = list;
2943           }
2944         | variable_initializer_list COMMA variable_initializer
2945           {
2946                 ArrayList list = (ArrayList) $1;
2947                 list.Add ($3);
2948                 $$ = list;
2949           }
2950         ;
2951
2952 typeof_expression
2953         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2954           {
2955                 $$ = new TypeOfVoid (lexer.Location);
2956           }
2957         | TYPEOF OPEN_PARENS type CLOSE_PARENS
2958           {
2959                 $$ = new TypeOf ((Expression) $3, lexer.Location);
2960           }
2961         ;
2962
2963 sizeof_expression
2964         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
2965                 $$ = new SizeOf ((Expression) $3, lexer.Location);
2966           }
2967         ;
2968
2969 checked_expression
2970         : CHECKED OPEN_PARENS expression CLOSE_PARENS
2971           {
2972                 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2973           }
2974         ;
2975
2976 unchecked_expression
2977         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2978           {
2979                 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2980           }
2981         ;
2982
2983 pointer_member_access 
2984         : primary_expression OP_PTR IDENTIFIER
2985           {
2986                 Expression deref;
2987
2988                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2989                 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2990           }
2991         ;
2992
2993 anonymous_method_expression
2994         : DELEGATE opt_anonymous_method_signature _mark_
2995           {
2996                 oob_stack.Push (current_local_parameters);
2997                 current_local_parameters = (Parameters)$2;
2998
2999                 // Force the next block to be created as a ToplevelBlock
3000                 oob_stack.Push (current_block);
3001                 oob_stack.Push (top_current_block);
3002                 current_block = null;
3003           } 
3004           block
3005           {
3006                 Location loc = (Location) $3;
3007                 top_current_block = (Block) oob_stack.Pop ();
3008                 current_block = (Block) oob_stack.Pop ();
3009                 if (RootContext.Version == LanguageVersion.ISO_1){
3010                         Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3011                         $$ = null;
3012                 } else  {
3013                         ToplevelBlock anon_block = (ToplevelBlock) $5;
3014
3015                         anon_block.Parent = current_block;
3016                         $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
3017                                 anon_block, loc);
3018                 }
3019                 current_local_parameters = (Parameters) oob_stack.Pop ();
3020           }
3021         ;
3022
3023 opt_anonymous_method_signature
3024         : /* empty */                   { $$ = null; } 
3025         | anonymous_method_signature
3026         ;
3027
3028 anonymous_method_signature
3029         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
3030           {
3031                 if ($2 == null)
3032                         $$ = Parameters.EmptyReadOnlyParameters;
3033                 else {
3034                         ArrayList par_list = (ArrayList) $2;
3035                         Parameter [] pars = new Parameter [par_list.Count];
3036                         par_list.CopyTo (pars);
3037                         $$ = new Parameters (pars, null, lexer.Location);
3038                 }
3039           }
3040         ;
3041
3042 opt_anonymous_method_parameter_list
3043         : /* empty */                      { $$ = null; } 
3044         | anonymous_method_parameter_list  { $$ = $1; }
3045         ;
3046
3047 anonymous_method_parameter_list
3048         : anonymous_method_parameter 
3049           {
3050                 ArrayList a = new ArrayList (4);
3051                 a.Add ($1);
3052                 $$ = a;
3053           }
3054         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
3055           {
3056                 ArrayList a = (ArrayList) $1;
3057                 a.Add ($3);
3058                 $$ = a;
3059           }
3060         ; 
3061
3062 anonymous_method_parameter
3063         : opt_parameter_modifier type IDENTIFIER {
3064                 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3065           }
3066         | PARAMS type IDENTIFIER {
3067                 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3068                 $$ = null;
3069           }
3070         ;
3071
3072 unary_expression
3073         : primary_expression
3074         | BANG prefixed_unary_expression
3075           {
3076                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3077           }
3078         | TILDE prefixed_unary_expression
3079           {
3080                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3081           }
3082         | cast_expression
3083         ;
3084
3085 cast_list
3086         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3087           {
3088                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3089           }
3090         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3091           {
3092                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3093           }     
3094         ;
3095
3096 cast_expression
3097         : cast_list
3098         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3099           {
3100                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3101           }
3102         ;
3103
3104         //
3105         // The idea to split this out is from Rhys' grammar
3106         // to solve the problem with casts.
3107         //
3108 prefixed_unary_expression
3109         : unary_expression
3110         | PLUS prefixed_unary_expression
3111           { 
3112                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3113           } 
3114         | MINUS prefixed_unary_expression 
3115           { 
3116                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3117           }
3118         | OP_INC prefixed_unary_expression 
3119           {
3120                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3121                                        (Expression) $2, lexer.Location);
3122           }
3123         | OP_DEC prefixed_unary_expression 
3124           {
3125                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3126                                        (Expression) $2, lexer.Location);
3127           }
3128         | STAR prefixed_unary_expression
3129           {
3130                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3131           }
3132         | BITWISE_AND prefixed_unary_expression
3133           {
3134                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3135           }
3136         ;
3137
3138 pre_increment_expression
3139         : OP_INC prefixed_unary_expression 
3140           {
3141                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3142                                        (Expression) $2, lexer.Location);
3143           }
3144         ;
3145
3146 pre_decrement_expression
3147         : OP_DEC prefixed_unary_expression 
3148           {
3149                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3150                                        (Expression) $2, lexer.Location);
3151           }
3152         ;
3153
3154 multiplicative_expression
3155         : prefixed_unary_expression
3156         | multiplicative_expression STAR prefixed_unary_expression
3157           {
3158                 $$ = new Binary (Binary.Operator.Multiply, 
3159                                  (Expression) $1, (Expression) $3, lexer.Location);
3160           }
3161         | multiplicative_expression DIV prefixed_unary_expression
3162           {
3163                 $$ = new Binary (Binary.Operator.Division, 
3164                                  (Expression) $1, (Expression) $3, lexer.Location);
3165           }
3166         | multiplicative_expression PERCENT prefixed_unary_expression 
3167           {
3168                 $$ = new Binary (Binary.Operator.Modulus, 
3169                                  (Expression) $1, (Expression) $3, lexer.Location);
3170           }
3171         ;
3172
3173 additive_expression
3174         : multiplicative_expression
3175         | additive_expression PLUS multiplicative_expression 
3176           {
3177                 $$ = new Binary (Binary.Operator.Addition, 
3178                                  (Expression) $1, (Expression) $3, lexer.Location);
3179           }
3180         | additive_expression MINUS multiplicative_expression
3181           {
3182                 $$ = new Binary (Binary.Operator.Subtraction, 
3183                                  (Expression) $1, (Expression) $3, lexer.Location);
3184           }
3185         ;
3186
3187 shift_expression
3188         : additive_expression
3189         | shift_expression OP_SHIFT_LEFT additive_expression
3190           {
3191                 $$ = new Binary (Binary.Operator.LeftShift, 
3192                                  (Expression) $1, (Expression) $3, lexer.Location);
3193           }
3194         | shift_expression OP_SHIFT_RIGHT additive_expression
3195           {
3196                 $$ = new Binary (Binary.Operator.RightShift, 
3197                                  (Expression) $1, (Expression) $3, lexer.Location);
3198           }
3199         ; 
3200
3201 relational_expression
3202         : shift_expression
3203         | relational_expression OP_LT shift_expression
3204           {
3205                 $$ = new Binary (Binary.Operator.LessThan, 
3206                                  (Expression) $1, (Expression) $3, lexer.Location);
3207           }
3208         | relational_expression OP_GT shift_expression
3209           {
3210                 $$ = new Binary (Binary.Operator.GreaterThan, 
3211                                  (Expression) $1, (Expression) $3, lexer.Location);
3212           }
3213         | relational_expression OP_LE shift_expression
3214           {
3215                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3216                                  (Expression) $1, (Expression) $3, lexer.Location);
3217           }
3218         | relational_expression OP_GE shift_expression
3219           {
3220                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3221                                  (Expression) $1, (Expression) $3, lexer.Location);
3222           }
3223         | relational_expression IS type
3224           {
3225                 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3226           }
3227         | relational_expression AS type
3228           {
3229                 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3230           }
3231         ;
3232
3233 equality_expression
3234         : relational_expression
3235         | equality_expression OP_EQ relational_expression
3236           {
3237                 $$ = new Binary (Binary.Operator.Equality, 
3238                                  (Expression) $1, (Expression) $3, lexer.Location);
3239           }
3240         | equality_expression OP_NE relational_expression
3241           {
3242                 $$ = new Binary (Binary.Operator.Inequality, 
3243                                  (Expression) $1, (Expression) $3, lexer.Location);
3244           }
3245         ; 
3246
3247 and_expression
3248         : equality_expression
3249         | and_expression BITWISE_AND equality_expression
3250           {
3251                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
3252                                  (Expression) $1, (Expression) $3, lexer.Location);
3253           }
3254         ;
3255
3256 exclusive_or_expression
3257         : and_expression
3258         | exclusive_or_expression CARRET and_expression
3259           {
3260                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
3261                                  (Expression) $1, (Expression) $3, lexer.Location);
3262           }
3263         ;
3264
3265 inclusive_or_expression
3266         : exclusive_or_expression
3267         | inclusive_or_expression BITWISE_OR exclusive_or_expression
3268           {
3269                 $$ = new Binary (Binary.Operator.BitwiseOr, 
3270                                  (Expression) $1, (Expression) $3, lexer.Location);
3271           }
3272         ;
3273
3274 conditional_and_expression
3275         : inclusive_or_expression
3276         | conditional_and_expression OP_AND inclusive_or_expression
3277           {
3278                 $$ = new Binary (Binary.Operator.LogicalAnd, 
3279                                  (Expression) $1, (Expression) $3, lexer.Location);
3280           }
3281         ;
3282
3283 conditional_or_expression
3284         : conditional_and_expression
3285         | conditional_or_expression OP_OR conditional_and_expression
3286           {
3287                 $$ = new Binary (Binary.Operator.LogicalOr, 
3288                                  (Expression) $1, (Expression) $3, lexer.Location);
3289           }
3290         ;
3291
3292 conditional_expression
3293         : conditional_or_expression
3294         | conditional_or_expression INTERR expression COLON expression 
3295           {
3296                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3297           }
3298         ;
3299
3300 assignment_expression
3301         : prefixed_unary_expression ASSIGN expression
3302           {
3303                 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3304           }
3305         | prefixed_unary_expression OP_MULT_ASSIGN expression
3306           {
3307                 Location l = lexer.Location;
3308
3309                 $$ = new CompoundAssign (
3310                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3311           }
3312         | prefixed_unary_expression OP_DIV_ASSIGN expression
3313           {
3314                 Location l = lexer.Location;
3315
3316                 $$ = new CompoundAssign (
3317                         Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3318           }
3319         | prefixed_unary_expression OP_MOD_ASSIGN expression
3320           {
3321                 Location l = lexer.Location;
3322
3323                 $$ = new CompoundAssign (
3324                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3325           }
3326         | prefixed_unary_expression OP_ADD_ASSIGN expression
3327           {
3328                 Location l = lexer.Location;
3329
3330                 $$ = new CompoundAssign (
3331                         Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3332           }
3333         | prefixed_unary_expression OP_SUB_ASSIGN expression
3334           {
3335                 Location l = lexer.Location;
3336
3337                 $$ = new CompoundAssign (
3338                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3339           }
3340         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3341           {
3342                 Location l = lexer.Location;
3343
3344                 $$ = new CompoundAssign (
3345                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3346           }
3347         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3348           {
3349                 Location l = lexer.Location;
3350
3351                 $$ = new CompoundAssign (
3352                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3353           }
3354         | prefixed_unary_expression OP_AND_ASSIGN expression
3355           {
3356                 Location l = lexer.Location;
3357
3358                 $$ = new CompoundAssign (
3359                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3360           }
3361         | prefixed_unary_expression OP_OR_ASSIGN expression
3362           {
3363                 Location l = lexer.Location;
3364
3365                 $$ = new CompoundAssign (
3366                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3367           }
3368         | prefixed_unary_expression OP_XOR_ASSIGN expression
3369           {
3370                 Location l = lexer.Location;
3371
3372                 $$ = new CompoundAssign (
3373                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3374           }
3375         ;
3376
3377 expression
3378         : conditional_expression
3379         | assignment_expression
3380         ;
3381
3382 constant_expression
3383         : expression
3384         ;
3385
3386 boolean_expression
3387         : expression
3388         ;
3389
3390 //
3391 // 10 classes
3392 //
3393 class_declaration
3394         : opt_attributes
3395           opt_modifiers
3396           opt_partial
3397           CLASS member_name
3398           {
3399                 MemberName name = MakeName ((MemberName) $5);
3400                 bool partial = (bool) $3;
3401                 int mod_flags = (int) $2;
3402
3403                 if (partial) {
3404                         ClassPart part = PartialContainer.CreatePart (
3405                                 current_namespace, current_class, name, mod_flags,
3406                                 (Attributes) $1, Kind.Class, lexer.Location);
3407
3408                         current_container = part.PartialContainer;
3409                         current_class = part;
3410                 } else {
3411                         if ((mod_flags & Modifiers.STATIC) != 0) {
3412                                 current_class = new StaticClass (
3413                                         current_namespace, current_class, name,
3414                                         mod_flags, (Attributes) $1, lexer.Location);
3415                         } else {
3416                                 current_class = new Class (
3417                                         current_namespace, current_class, name,
3418                                         mod_flags, (Attributes) $1, lexer.Location);
3419                         }
3420
3421                         current_container.AddClassOrStruct (current_class);
3422                         current_container = current_class;
3423                         RootContext.Tree.RecordDecl (name, current_class);
3424                 }
3425           }
3426           opt_class_base
3427           {
3428                 if ($7 != null) {
3429                         if (current_class.Name == "System.Object") {
3430                                 Report.Error (537, current_class.Location,
3431                                               "The class System.Object cannot have a base " +
3432                                               "class or implement an interface.");
3433                         }
3434                         current_class.Bases = (ArrayList) $7;
3435                 }
3436
3437                 if (RootContext.Documentation != null) {
3438                         current_class.DocComment = Lexer.consume_doc_comment ();
3439                         Lexer.doc_state = XmlCommentState.Allowed;
3440                 }
3441           }
3442           class_body
3443           {
3444                 if (RootContext.Documentation != null)
3445                         Lexer.doc_state = XmlCommentState.Allowed;
3446           }
3447           opt_semicolon 
3448           {
3449                 $$ = pop_current_class ();
3450           }
3451         ;       
3452
3453 opt_partial
3454         : /* empty */
3455           { $$ = (bool) false; }
3456         | PARTIAL
3457           { $$ = (bool) true; }
3458         ;
3459
3460 opt_modifiers
3461         : /* empty */           { $$ = (int) 0; }
3462         | modifiers
3463         ;
3464
3465 modifiers
3466         : modifier
3467         | modifiers modifier
3468           { 
3469                 int m1 = (int) $1;
3470                 int m2 = (int) $2;
3471
3472                 if ((m1 & m2) != 0) {
3473                         Location l = lexer.Location;
3474                         Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3475                 }
3476                 $$ = (int) (m1 | m2);
3477           }
3478         ;
3479
3480 modifier
3481         : NEW                   { $$ = Modifiers.NEW; }
3482         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3483         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3484         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3485         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3486         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3487         | SEALED                { $$ = Modifiers.SEALED; }
3488         | STATIC                { $$ = Modifiers.STATIC; }
3489         | READONLY              { $$ = Modifiers.READONLY; }
3490         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3491         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3492         | EXTERN                { $$ = Modifiers.EXTERN; }
3493         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3494         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3495         ;
3496
3497 opt_class_base
3498         : /* empty */           { $$ = null; }
3499         | class_base            { $$ = $1;   }
3500         ;
3501
3502 class_base
3503         : COLON type_list { $$ = $2; }
3504         ;
3505
3506 //
3507 // Statements (8.2)
3508 //
3509
3510 //
3511 // A block is "contained" on the following places:
3512 //      method_body
3513 //      property_declaration as part of the accessor body (get/set)
3514 //      operator_declaration
3515 //      constructor_declaration
3516 //      destructor_declaration
3517 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3518 //      
3519 block
3520         : OPEN_BRACE 
3521           {
3522                 if (current_block == null){
3523                         current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3524                         top_current_block = current_block;
3525                 } else {
3526                 current_block = new Block (current_block, lexer.Location, Location.Null);
3527                 }
3528           } 
3529           opt_statement_list CLOSE_BRACE 
3530           { 
3531                 while (current_block.Implicit)
3532                         current_block = current_block.Parent;
3533                 $$ = current_block;
3534                 current_block.SetEndLocation (lexer.Location);
3535                 current_block = current_block.Parent;
3536                 if (current_block == null)
3537                         top_current_block = null;
3538           }
3539         ;
3540
3541 opt_statement_list
3542         : /* empty */
3543         | statement_list 
3544         ;
3545
3546 statement_list
3547         : statement
3548         | statement_list statement
3549         ;
3550
3551 statement
3552         : declaration_statement
3553           {
3554                 if ($1 != null && (Block) $1 != current_block){
3555                         current_block.AddStatement ((Statement) $1);
3556                         current_block = (Block) $1;
3557                 }
3558           }
3559         | valid_declaration_statement
3560           {
3561                 current_block.AddStatement ((Statement) $1);
3562           }
3563         | labeled_statement
3564         ;
3565
3566 valid_declaration_statement
3567         : block
3568         | empty_statement
3569         | expression_statement
3570         | selection_statement
3571         | iteration_statement
3572         | jump_statement                  
3573         | try_statement
3574         | checked_statement
3575         | unchecked_statement
3576         | lock_statement
3577         | using_statement
3578         | unsafe_statement
3579         | fixed_statement
3580         ;
3581
3582 embedded_statement
3583         : valid_declaration_statement
3584         | declaration_statement
3585           {
3586                   Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3587                   $$ = null;
3588           }
3589         | labeled_statement
3590           {
3591                   Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3592                   $$ = null;
3593           }
3594         ;
3595
3596 empty_statement
3597         : SEMICOLON
3598           {
3599                   $$ = EmptyStatement.Value;
3600           }
3601         ;
3602
3603 labeled_statement
3604         : IDENTIFIER COLON 
3605           {
3606                 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3607
3608                 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3609                         current_block.AddStatement (labeled);
3610           }
3611           statement
3612         ;
3613
3614 declaration_statement
3615         : local_variable_declaration SEMICOLON
3616           {
3617                 if ($1 != null){
3618                         DictionaryEntry de = (DictionaryEntry) $1;
3619
3620                         $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3621                 }
3622           }
3623
3624         | local_constant_declaration SEMICOLON
3625           {
3626                 if ($1 != null){
3627                         DictionaryEntry de = (DictionaryEntry) $1;
3628
3629                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3630                 }
3631           }
3632         ;
3633
3634 /* 
3635  * The following is from Rhys' grammar:
3636  * > Types in local variable declarations must be recognized as 
3637  * > expressions to prevent reduce/reduce errors in the grammar.
3638  * > The expressions are converted into types during semantic analysis.
3639  */
3640 local_variable_type
3641         : primary_expression opt_rank_specifier
3642           { 
3643                 // FIXME: Do something smart here regarding the composition of the type.
3644
3645                 // Ok, the above "primary_expression" is there to get rid of
3646                 // both reduce/reduce and shift/reduces in the grammar, it should
3647                 // really just be "type_name".  If you use type_name, a reduce/reduce
3648                 // creeps up.  If you use namespace_or_type_name (which is all we need
3649                 // really) two shift/reduces appear.
3650                 // 
3651
3652                 // So the super-trick is that primary_expression
3653                 // can only be either a SimpleName or a MemberAccess. 
3654                 // The MemberAccess case arises when you have a fully qualified type-name like :
3655                 // Foo.Bar.Blah i;
3656                 // SimpleName is when you have
3657                 // Blah i;
3658                   
3659                 Expression expr = (Expression) $1;  
3660                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3661                         Error_ExpectingTypeName (lexer.Location, expr);
3662                         $$ = null;
3663                 } else {
3664                         //
3665                         // So we extract the string corresponding to the SimpleName
3666                         // or MemberAccess
3667                         // 
3668
3669                         if ((string) $2 == "")
3670                                 $$ = $1;
3671                         else
3672                                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3673                 }
3674           }
3675         | builtin_types opt_rank_specifier
3676           {
3677                 if ((string) $2 == "")
3678                         $$ = $1;
3679                 else
3680                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3681           }
3682         ;
3683
3684 local_variable_pointer_type
3685         : primary_expression STAR
3686           {
3687                 Expression expr = (Expression) $1;  
3688                 Location l = lexer.Location;
3689
3690                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3691                         Error_ExpectingTypeName (l, expr);
3692
3693                         $$ = null;
3694                 } else 
3695                         $$ = new ComposedCast ((Expression) $1, "*", l);
3696           }
3697         | builtin_types STAR
3698           {
3699                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3700           }
3701         | VOID STAR
3702           {
3703                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3704           }
3705         | local_variable_pointer_type STAR
3706           {
3707                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3708           }
3709         ;
3710
3711 local_variable_declaration
3712         : local_variable_type variable_declarators
3713           {
3714                 if ($1 != null)
3715                         $$ = new DictionaryEntry ($1, $2);
3716                 else
3717                         $$ = null;
3718           }
3719         | local_variable_pointer_type opt_rank_specifier variable_declarators
3720         {
3721                 if ($1 != null){
3722                         Expression t;
3723
3724                         if ((string) $2 == "")
3725                                 t = (Expression) $1;
3726                         else
3727                                 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3728                         $$ = new DictionaryEntry (t, $3);
3729                 } else 
3730                         $$ = null;
3731         }
3732         ;
3733
3734 local_constant_declaration
3735         : CONST local_variable_type constant_declarators
3736           {
3737                 if ($2 != null)
3738                         $$ = new DictionaryEntry ($2, $3);
3739                 else
3740                         $$ = null;
3741           }
3742         ;
3743
3744 expression_statement
3745         : statement_expression SEMICOLON
3746           {
3747                 $$ = $1;
3748           }
3749         ;
3750
3751         //
3752         // We have to do the wrapping here and not in the case above,
3753         // because statement_expression is used for example in for_statement
3754         //
3755 statement_expression
3756         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3757         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3758         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3759         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3760         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3761         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3762         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3763         | error {
3764                 Report.Error (1002, lexer.Location, "Expecting `;'");
3765                 $$ = null;
3766           }
3767         ;
3768
3769 object_creation_expression
3770         : object_or_delegate_creation_expression
3771           { note ("complain if this is a delegate maybe?"); } 
3772         ;
3773
3774 selection_statement
3775         : if_statement
3776         | switch_statement
3777         ; 
3778
3779 if_statement
3780         : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS 
3781           embedded_statement
3782           { 
3783                 Location l = (Location) $3;
3784
3785                 $$ = new If ((Expression) $4, (Statement) $6, l);
3786
3787                 if (RootContext.WarningLevel >= 4){
3788                         if ($6 == EmptyStatement.Value)
3789                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3790                 }
3791
3792           }
3793         | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3794           embedded_statement ELSE embedded_statement
3795           {
3796                 Location l = (Location) $3;
3797
3798                 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3799           }
3800         ;
3801
3802 switch_statement
3803         : SWITCH OPEN_PARENS _mark_
3804           { 
3805                 switch_stack.Push (current_block);
3806           }
3807           expression CLOSE_PARENS 
3808           switch_block
3809           {
3810                 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3811                 current_block = (Block) switch_stack.Pop ();
3812           }
3813         ;
3814
3815 switch_block
3816         : OPEN_BRACE
3817           opt_switch_sections
3818           CLOSE_BRACE
3819           {
3820                 $$ = $2;
3821           }
3822         ;
3823
3824 opt_switch_sections
3825         : /* empty */           
3826           {
3827                 Report.Error (1522, lexer.Location, "Empty switch block"); 
3828           }
3829         | switch_sections
3830         ;
3831
3832 switch_sections
3833         : switch_section 
3834           {
3835                 ArrayList sections = new ArrayList (4);
3836
3837                 sections.Add ($1);
3838                 $$ = sections;
3839           }
3840         | switch_sections switch_section
3841           {
3842                 ArrayList sections = (ArrayList) $1;
3843
3844                 sections.Add ($2);
3845                 $$ = sections;
3846           }
3847         ;
3848
3849 switch_section
3850         : switch_labels
3851           {
3852                 current_block = current_block.CreateSwitchBlock (lexer.Location);
3853           }
3854           statement_list 
3855           {
3856                 Block topmost = current_block;
3857
3858                 while (topmost.Implicit)
3859                         topmost = topmost.Parent;
3860                 $$ = new SwitchSection ((ArrayList) $1, topmost);
3861           }
3862         ;
3863
3864 switch_labels
3865         : switch_label 
3866           {
3867                 ArrayList labels = new ArrayList (4);
3868
3869                 labels.Add ($1);
3870                 $$ = labels;
3871           }
3872         | switch_labels switch_label 
3873           {
3874                 ArrayList labels = (ArrayList) ($1);
3875                 labels.Add ($2);
3876
3877                 $$ = labels;
3878           }
3879         ;
3880
3881 switch_label
3882         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3883         | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
3884         | error {
3885                 Report.Error (
3886                         1523, lexer.Location, 
3887                         "The keyword case or default must precede code in switch block");
3888           }
3889         ;
3890
3891 iteration_statement
3892         : while_statement
3893         | do_statement
3894         | for_statement
3895         | foreach_statement
3896         ;
3897
3898 while_statement
3899         : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3900         {
3901                 Location l = (Location) $3;
3902                 $$ = new While ((Expression) $4, (Statement) $6, l);
3903         
3904                 if (RootContext.WarningLevel >= 4){
3905                         if ($6 == EmptyStatement.Value)
3906                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3907                 }
3908         }
3909         ;
3910
3911 do_statement
3912         : DO embedded_statement 
3913           WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3914           {
3915                 Location l = (Location) $5;
3916
3917                 $$ = new Do ((Statement) $2, (Expression) $6, l);
3918           }
3919         ;
3920
3921 for_statement
3922         : FOR OPEN_PARENS 
3923           opt_for_initializer SEMICOLON _mark_
3924           {
3925                 Block assign_block = new Block (current_block);
3926                 current_block = assign_block;
3927
3928                 if ($3 is DictionaryEntry){
3929                         DictionaryEntry de = (DictionaryEntry) $3;
3930                         
3931                         Expression type = (Expression) de.Key;
3932                         ArrayList var_declarators = (ArrayList) de.Value;
3933
3934                         foreach (VariableDeclaration decl in var_declarators){
3935
3936                                 LocalInfo vi;
3937
3938                                 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
3939                                 if (vi == null)
3940                                         continue;
3941
3942                                 Location l = lexer.Location;
3943                                 Expression expr;
3944                                 if (decl.expression_or_array_initializer is Expression){
3945                                         expr = (Expression) decl.expression_or_array_initializer;
3946                                 } else if (decl.expression_or_array_initializer == null) {
3947                                         expr = null;
3948                                 } else {
3949                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3950                                         expr = new ArrayCreation (type, "", init, decl.Location);
3951                                 }
3952                                         
3953                                 LocalVariableReference var;
3954                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
3955
3956                                 if (expr != null) {
3957                                         Assign a = new Assign (var, expr, decl.Location);
3958                                         
3959                                         assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3960                                 }
3961                         }
3962                         
3963                         // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3964                         // This can be referred to as $6 below.
3965                         $$ = null;
3966                 } else {
3967                         $$ = $3;
3968                 }
3969           } 
3970           opt_for_condition SEMICOLON
3971           opt_for_iterator CLOSE_PARENS 
3972           embedded_statement
3973           {
3974                 Location l = (Location) $5;
3975
3976                 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3977
3978                 if (RootContext.WarningLevel >= 4){
3979                         if ($11 == EmptyStatement.Value)
3980                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3981                 }
3982
3983                 current_block.AddStatement (f);
3984                 while (current_block.Implicit)
3985                         current_block = current_block.Parent;
3986                 $$ = current_block;
3987                 current_block = current_block.Parent;
3988           }
3989         ;
3990
3991 opt_for_initializer
3992         : /* empty */           { $$ = EmptyStatement.Value; }
3993         | for_initializer       
3994         ;
3995
3996 for_initializer
3997         : local_variable_declaration
3998         | statement_expression_list
3999         ;
4000
4001 opt_for_condition
4002         : /* empty */           { $$ = null; }
4003         | boolean_expression
4004         ;
4005
4006 opt_for_iterator
4007         : /* empty */           { $$ = EmptyStatement.Value; }
4008         | for_iterator
4009         ;
4010
4011 for_iterator
4012         : statement_expression_list
4013         ;
4014
4015 statement_expression_list
4016         : statement_expression  
4017           {
4018                 // CHANGE: was `null'
4019                 Block b = new Block (current_block, Block.Flags.Implicit);   
4020
4021                 b.AddStatement ((Statement) $1);
4022                 $$ = b;
4023           }
4024         | statement_expression_list COMMA statement_expression
4025           {
4026                 Block b = (Block) $1;
4027
4028                 b.AddStatement ((Statement) $3);
4029                 $$ = $1;
4030           }
4031         ;
4032
4033 foreach_statement
4034         : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4035         {
4036                 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4037                 $$ = null;
4038         }
4039         | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4040           expression CLOSE_PARENS 
4041           {
4042                 Block foreach_block = new Block (current_block);
4043                 current_block = foreach_block;
4044
4045                 Location l = lexer.Location;
4046                 LocalInfo vi;
4047
4048                 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
4049                 if (vi != null) {
4050                         vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4051
4052                         // Get a writable reference to this read-only variable.
4053                         //
4054                         // Note that the $$ here refers to the value of _this_ code block,
4055                         // not the value of the LHS non-terminal.  This can be referred to as $9 below.
4056                         $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4057                 } else {
4058                         $$ = null;
4059                 }
4060           } 
4061           embedded_statement 
4062           {
4063                 LocalVariableReference v = (LocalVariableReference) $9;
4064                 Location l = (Location) $6;
4065
4066                 if (v != null) {
4067                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4068                         current_block.AddStatement (f);
4069                 }
4070
4071                 while (current_block.Implicit)
4072                           current_block = current_block.Parent;
4073                 $$ = current_block;
4074                 current_block = current_block.Parent;
4075           }
4076         ;
4077
4078 jump_statement
4079         : break_statement
4080         | continue_statement
4081         | goto_statement
4082         | return_statement
4083         | throw_statement
4084         | yield_statement
4085         ;
4086
4087 break_statement
4088         : BREAK SEMICOLON
4089           {
4090                 $$ = new Break (lexer.Location);
4091           }
4092         ;
4093
4094 continue_statement
4095         : CONTINUE SEMICOLON
4096           {
4097                 $$ = new Continue (lexer.Location);
4098           }
4099         ;
4100
4101 goto_statement
4102         : GOTO IDENTIFIER SEMICOLON 
4103           {
4104                 $$ = new Goto ((string) $2, lexer.Location);
4105           }
4106         | GOTO CASE constant_expression SEMICOLON
4107           {
4108                 $$ = new GotoCase ((Expression) $3, lexer.Location);
4109           }
4110         | GOTO DEFAULT SEMICOLON 
4111           {
4112                 $$ = new GotoDefault (lexer.Location);
4113           }
4114         ; 
4115
4116 return_statement
4117         : RETURN opt_expression SEMICOLON
4118           {
4119                 $$ = new Return ((Expression) $2, lexer.Location);
4120           }
4121         ;
4122
4123 throw_statement
4124         : THROW opt_expression SEMICOLON
4125           {
4126                 $$ = new Throw ((Expression) $2, lexer.Location);
4127           }
4128         ;
4129
4130 yield_statement 
4131         : IDENTIFIER RETURN expression SEMICOLON
4132           {
4133                 string s = (string) $1;
4134                 if (s != "yield"){
4135                         Report.Error (1003, lexer.Location, "; expected");
4136                         $$ = null;
4137                 }
4138                 if (RootContext.Version == LanguageVersion.ISO_1){
4139                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4140                         $$ = null;
4141                 }
4142                 if (iterator_container == null){
4143                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4144                         $$ = null;
4145                 } else {
4146                         iterator_container.SetYields ();
4147                         $$ = new Yield ((Expression) $3, lexer.Location); 
4148                 }
4149           }
4150         | IDENTIFIER RETURN SEMICOLON
4151         {
4152                 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4153                 $$ = null;
4154         }
4155         | IDENTIFIER BREAK SEMICOLON
4156           {
4157                 string s = (string) $1;
4158                 if (s != "yield"){
4159                         Report.Error (1003, lexer.Location, "; expected");
4160                         $$ = null;
4161                 }
4162                 if (RootContext.Version == LanguageVersion.ISO_1){
4163                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4164                         $$ = null;
4165                 }
4166                 if (iterator_container == null){
4167                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4168                         $$ = null;
4169                 } else {
4170                         iterator_container.SetYields ();
4171                         $$ = new YieldBreak (lexer.Location);
4172                 }
4173           }
4174         ;
4175
4176 opt_expression
4177         : /* empty */
4178         | expression
4179         ;
4180
4181 try_statement
4182         : TRY block catch_clauses 
4183         {
4184                 Catch g = null;
4185                 
4186                 ArrayList c = (ArrayList)$3;
4187                 for (int i = 0; i < c.Count; ++i) {
4188                         Catch cc = (Catch) c [i];
4189                         if (cc.IsGeneral) {
4190                                 if (i != c.Count - 1)
4191                                         Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4192                                 g = cc;
4193                                 c.RemoveAt (i);
4194                                 i--;
4195                         }
4196                 }
4197
4198                 // Now s contains the list of specific catch clauses
4199                 // and g contains the general one.
4200                 
4201                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4202         }
4203         | TRY block opt_catch_clauses FINALLY block
4204           {
4205                 Catch g = null;
4206                 ArrayList s = new ArrayList (4);
4207                 ArrayList catch_list = (ArrayList) $3;
4208
4209                 if (catch_list != null){
4210                         foreach (Catch cc in catch_list) {
4211                                 if (cc.IsGeneral)
4212                                         g = cc;
4213                                 else
4214                                         s.Add (cc);
4215                         }
4216                 }
4217
4218                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4219           }
4220         | TRY block error 
4221           {
4222                 Report.Error (1524, lexer.Location, "Expected catch or finally");
4223           }
4224         ;
4225
4226 opt_catch_clauses
4227         : /* empty */  { $$ = null; }
4228         | catch_clauses
4229         ;
4230
4231 catch_clauses
4232         : catch_clause 
4233           {
4234                 ArrayList l = new ArrayList (4);
4235
4236                 l.Add ($1);
4237                 $$ = l;
4238           }
4239         | catch_clauses catch_clause
4240           {
4241                 ArrayList l = (ArrayList) $1;
4242
4243                 l.Add ($2);
4244                 $$ = l;
4245           }
4246         ;
4247
4248 opt_identifier
4249         : /* empty */   { $$ = null; }
4250         | IDENTIFIER
4251         ;
4252
4253 catch_clause 
4254         : CATCH opt_catch_args 
4255         {
4256                 Expression type = null;
4257                 string id = null;
4258                 
4259                 if ($2 != null) {
4260                         DictionaryEntry cc = (DictionaryEntry) $2;
4261                         type = (Expression) cc.Key;
4262                         id   = (string) cc.Value;
4263
4264                         if (id != null){
4265                                 ArrayList one = new ArrayList (4);
4266                                 Location loc = lexer.Location;
4267
4268                                 one.Add (new VariableDeclaration (id, null, loc));
4269
4270                                 current_block = new Block (current_block);
4271                                 Block b = declare_local_variables (type, one, loc);
4272                                 current_block = b;
4273                         }
4274                 }
4275         } block {
4276                 Expression type = null;
4277                 string id = null;
4278
4279                 if ($2 != null){
4280                         DictionaryEntry cc = (DictionaryEntry) $2;
4281                         type = (Expression) cc.Key;
4282                         id   = (string) cc.Value;
4283
4284                         if (id != null){
4285                                 while (current_block.Implicit)
4286                                         current_block = current_block.Parent;
4287                                 current_block = current_block.Parent;
4288                         }
4289                 }
4290
4291                 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4292         }
4293         ;
4294
4295 opt_catch_args
4296         : /* empty */ { $$ = null; }
4297         | catch_args
4298         ;         
4299
4300 catch_args 
4301         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
4302         {
4303                 $$ = new DictionaryEntry ($2, $3);
4304         }
4305         ;
4306
4307 checked_statement
4308         : CHECKED block
4309           {
4310                 $$ = new Checked ((Block) $2);
4311           }
4312         ;
4313
4314 unchecked_statement
4315         : UNCHECKED block
4316           {
4317                 $$ = new Unchecked ((Block) $2);
4318           }
4319         ;
4320
4321 unsafe_statement
4322         : UNSAFE 
4323         {
4324                 if (!RootContext.Unsafe){
4325                         Report.Error (227, lexer.Location, 
4326                                 "Unsafe code can only be used if -unsafe is used");
4327                 }
4328         } block {
4329                 $$ = new Unsafe ((Block) $3);
4330         }
4331         ;
4332
4333 fixed_statement
4334         : FIXED OPEN_PARENS 
4335           type fixed_pointer_declarators 
4336           CLOSE_PARENS _mark_
4337           {
4338                 ArrayList list = (ArrayList) $4;
4339                 Expression type = (Expression) $3;
4340                 Location l = lexer.Location;
4341                 int top = list.Count;
4342
4343                 Block assign_block = new Block (current_block);
4344                 current_block = assign_block;
4345
4346                 for (int i = 0; i < top; i++){
4347                         Pair p = (Pair) list [i];
4348                         LocalInfo v;
4349
4350                         v = current_block.AddVariable (type, (string) p.First, l);
4351                         if (v == null)
4352                                 continue;
4353
4354                         v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4355                         v.Pinned = true;
4356                         p.First = v;
4357                         list [i] = p;
4358                 }
4359           }
4360           embedded_statement 
4361           {
4362                 Location l = (Location) $6;
4363
4364                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4365
4366                 if (RootContext.WarningLevel >= 4){
4367                         if ($8 == EmptyStatement.Value)
4368                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4369                 }
4370
4371                 current_block.AddStatement (f);
4372                 while (current_block.Implicit)
4373                         current_block = current_block.Parent;
4374                 $$ = current_block;
4375                 current_block = current_block.Parent;
4376           }
4377         ;
4378
4379 fixed_pointer_declarators
4380         : fixed_pointer_declarator      { 
4381                 ArrayList declarators = new ArrayList (4);
4382                 if ($1 != null)
4383                         declarators.Add ($1);
4384                 $$ = declarators;
4385           }
4386         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4387           {
4388                 ArrayList declarators = (ArrayList) $1;
4389                 if ($3 != null)
4390                         declarators.Add ($3);
4391                 $$ = declarators;
4392           }
4393         ;
4394
4395 fixed_pointer_declarator
4396         : IDENTIFIER ASSIGN expression
4397           {     
4398                 $$ = new Pair ($1, $3);
4399           }
4400         | IDENTIFIER
4401           {
4402                 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4403                 $$ = null;
4404           }
4405         ;
4406
4407 lock_statement
4408         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4409           {
4410                 //
4411           } 
4412           embedded_statement
4413           {
4414                 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4415           }
4416         ;
4417
4418 using_statement
4419         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4420           {
4421                 Block assign_block = new Block (current_block);
4422                 current_block = assign_block;
4423
4424                 if ($3 is DictionaryEntry){
4425                         DictionaryEntry de = (DictionaryEntry) $3;
4426                         Location l = lexer.Location;
4427
4428                         Expression type = (Expression) de.Key;
4429                         ArrayList var_declarators = (ArrayList) de.Value;
4430
4431                         ArrayList vars = new ArrayList (4);
4432
4433                         foreach (VariableDeclaration decl in var_declarators){
4434
4435                                 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4436                                 if (vi == null)
4437                                         continue;
4438                                 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4439
4440                                 Expression expr;
4441                                 if (decl.expression_or_array_initializer is Expression){
4442                                         expr = (Expression) decl.expression_or_array_initializer;
4443                                 } else {
4444                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4445                                         if (init == null) {
4446                                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4447                                         }
4448                                         
4449                                         expr = new ArrayCreation (type, "", init, decl.Location);
4450                                 }
4451
4452                                 LocalVariableReference var;
4453
4454                                 // Get a writable reference to this read-only variable.
4455                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4456
4457                                 // This is so that it is not a warning on using variables
4458                                 vi.Used = true;
4459
4460                                 vars.Add (new DictionaryEntry (var, expr));                             
4461
4462                                 // Assign a = new Assign (var, expr, decl.Location);
4463                                 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4464                         }
4465
4466                         // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4467                         // It can be referred to as $6 below.
4468                         $$ = new DictionaryEntry (type, vars);
4469                  } else {
4470                         $$ = $3;
4471                  }
4472           } 
4473           embedded_statement
4474           {
4475                 Using u = new Using ($6, (Statement) $7, (Location) $5);
4476                 current_block.AddStatement (u);
4477                 while (current_block.Implicit)
4478                         current_block = current_block.Parent;
4479                 $$ = current_block;
4480                 current_block = current_block.Parent;
4481           }
4482         ; 
4483
4484 resource_acquisition
4485         : local_variable_declaration
4486         | expression
4487         ;
4488
4489 // Utility rule to save location information
4490 _mark_
4491         : /* empty */
4492         { $$ = lexer.Location; }
4493
4494 %%
4495
4496 // <summary>
4497 //   A class used to pass around variable declarations and constants
4498 // </summary>
4499 public class VariableDeclaration {
4500         public string identifier;
4501         public object expression_or_array_initializer;
4502         public Location Location;
4503         public Attributes OptAttributes;
4504         public string DocComment;
4505
4506         public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4507         {
4508                 this.identifier = id;
4509                 this.expression_or_array_initializer = eoai;
4510                 this.Location = l;
4511                 this.OptAttributes = opt_attrs;
4512         }
4513
4514         public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4515         {
4516         }
4517 }
4518
4519 /// <summary>
4520 ///  Used to pass around interface property information
4521 /// </summary>
4522 public class InterfaceAccessorInfo {
4523
4524         public readonly Accessor Get, Set;
4525
4526         public InterfaceAccessorInfo (bool has_get, bool has_set,
4527                                       Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4528         {
4529                 if (get_mod != 0)
4530                         Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4531                 if (set_mod != 0)
4532                         Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4533                         
4534                 if (has_get)
4535                         Get = new Accessor (null, 0, get_attrs, get_loc);
4536                 if (has_set)
4537                         Set = new Accessor (null, 0, set_attrs, set_loc);
4538         }
4539
4540 }
4541
4542
4543 // <summary>
4544 //   A class used to hold info about an indexer declarator
4545 // </summary>
4546 public class IndexerDeclaration {
4547         public Expression type;
4548         public MemberName interface_type;
4549         public Parameters param_list;
4550
4551         public IndexerDeclaration (Expression type, MemberName interface_type,
4552                                    Parameters param_list)
4553         {
4554                 this.type = type;
4555                 this.interface_type = interface_type;
4556                 this.param_list = param_list;
4557         }
4558 }
4559
4560 //
4561 // We use this when we do not have an object in advance that is an IIteratorContainer
4562 //
4563 public class SimpleIteratorContainer : IIteratorContainer {
4564         public bool Yields;
4565
4566         public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4567
4568         //
4569         // Reset and return
4570         //
4571         public static SimpleIteratorContainer GetSimple () { 
4572                 Simple.Yields = false;
4573                 return Simple;
4574         }
4575
4576         public void SetYields () { Yields = true; } 
4577 }
4578
4579 // <summary>
4580 //  A class used to hold info about an operator declarator
4581 // </summary>
4582 public class OperatorDeclaration {
4583         public Operator.OpType optype;
4584         public Expression ret_type, arg1type, arg2type;
4585         public string arg1name, arg2name;
4586         public Location location;
4587
4588         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
4589                                     Expression arg1type, string arg1name,
4590                                     Expression arg2type, string arg2name, Location location)
4591         {
4592                 optype = op;
4593                 this.ret_type = ret_type;
4594                 this.arg1type = arg1type;
4595                 this.arg1name = arg1name;
4596                 this.arg2type = arg2type;
4597                 this.arg2name = arg2name;
4598                 this.location = location;
4599         }
4600
4601 }
4602
4603 void Error_ExpectingTypeName (Location l, Expression expr)
4604 {
4605         if (expr is Invocation){
4606                 Report.Error (1002, l, "; expected");
4607         } else {
4608                 Report.Error (-1, l, "Invalid Type definition");
4609         }
4610 }
4611
4612 TypeContainer pop_current_class ()
4613 {
4614         TypeContainer retval = current_class;
4615
4616         current_class = current_class.Parent;
4617         current_container = current_container.Parent;
4618         
4619         if (current_class != current_container) {
4620                 if (!(current_class is ClassPart) ||
4621                     ((ClassPart) current_class).PartialContainer != current_container)
4622                         throw new InternalErrorException ();
4623         } else if (current_container is ClassPart)
4624                 current_container = ((ClassPart) current_class).PartialContainer;
4625
4626         return retval;
4627 }
4628
4629 // <summary>
4630 //   Given the @class_name name, it creates a fully qualified name
4631 //   based on the containing declaration space
4632 // </summary>
4633 MemberName
4634 MakeName (MemberName class_name)
4635 {
4636         Namespace ns = current_namespace.NS;
4637
4638         if (current_container.Name == ""){
4639                 if (ns.Name != "")
4640                         return new MemberName (ns.MemberName, class_name);
4641                 else
4642                         return class_name;
4643         } else {
4644                 return new MemberName (current_container.MemberName, class_name);
4645         }
4646 }
4647
4648 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4649 {
4650         Block implicit_block;
4651         ArrayList inits = null;
4652
4653         //
4654         // We use the `Used' property to check whether statements
4655         // have been added to the current block.  If so, we need
4656         // to create another block to contain the new declaration
4657         // otherwise, as an optimization, we use the same block to
4658         // add the declaration.
4659         //
4660         // FIXME: A further optimization is to check if the statements
4661         // that were added were added as part of the initialization
4662         // below.  In which case, no other statements have been executed
4663         // and we might be able to reduce the number of blocks for
4664         // situations like this:
4665         //
4666         // int j = 1;  int k = j + 1;
4667         //
4668         if (current_block.Used)
4669                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4670         else
4671                 implicit_block = current_block;
4672
4673         foreach (VariableDeclaration decl in variable_declarators){
4674
4675                 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4676                         if (decl.expression_or_array_initializer != null){
4677                                 if (inits == null)
4678                                         inits = new ArrayList (4);
4679                                 inits.Add (decl);
4680                         }
4681                 }
4682         }
4683
4684         if (inits == null)
4685                 return implicit_block;
4686
4687         foreach (VariableDeclaration decl in inits){
4688                 Assign assign;
4689                 Expression expr;
4690                 
4691                 if (decl.expression_or_array_initializer is Expression){
4692                         expr = (Expression) decl.expression_or_array_initializer;
4693
4694                 } else {
4695                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4696                         
4697                         expr = new ArrayCreation (type, "", init, decl.Location);
4698                 }
4699
4700                 LocalVariableReference var;
4701                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4702
4703                 assign = new Assign (var, expr, decl.Location);
4704
4705                 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4706         }
4707         
4708         return implicit_block;
4709 }
4710
4711 Block declare_local_constants (Expression type, ArrayList declarators)
4712 {
4713         Block implicit_block;
4714
4715         if (current_block.Used)
4716                 implicit_block = new Block (current_block, Block.Flags.Implicit);
4717         else
4718                 implicit_block = current_block;
4719
4720         foreach (VariableDeclaration decl in declarators){
4721                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4722         }
4723         
4724         return implicit_block;
4725 }
4726
4727 void CheckAttributeTarget (string a)
4728 {
4729         switch (a) {
4730
4731         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4732                 return;
4733                 
4734         default :
4735                 Location l = lexer.Location;
4736                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4737                 break;
4738         }
4739
4740 }
4741
4742 void CheckUnaryOperator (Operator.OpType op)
4743 {
4744         switch (op) {
4745                 
4746         case Operator.OpType.LogicalNot: 
4747         case Operator.OpType.OnesComplement: 
4748         case Operator.OpType.Increment:
4749         case Operator.OpType.Decrement:
4750         case Operator.OpType.True: 
4751         case Operator.OpType.False: 
4752         case Operator.OpType.Addition: 
4753         case Operator.OpType.Subtraction:
4754                 
4755                 break;
4756                 
4757         default :
4758                 Location l = lexer.Location;
4759                 Report.Error (1019, l, "Overloadable unary operator expected"); 
4760                 break;
4761                 
4762         }
4763 }
4764
4765 void CheckBinaryOperator (Operator.OpType op)
4766 {
4767         switch (op) {
4768                 
4769         case Operator.OpType.Addition: 
4770         case Operator.OpType.Subtraction: 
4771         case Operator.OpType.Multiply:
4772         case Operator.OpType.Division:
4773         case Operator.OpType.Modulus: 
4774         case Operator.OpType.BitwiseAnd: 
4775         case Operator.OpType.BitwiseOr:
4776         case Operator.OpType.ExclusiveOr: 
4777         case Operator.OpType.LeftShift: 
4778         case Operator.OpType.RightShift:
4779         case Operator.OpType.Equality: 
4780         case Operator.OpType.Inequality:
4781         case Operator.OpType.GreaterThan: 
4782         case Operator.OpType.LessThan: 
4783         case Operator.OpType.GreaterThanOrEqual:
4784         case Operator.OpType.LessThanOrEqual:
4785                 break;
4786                 
4787         default :
4788                 Location l = lexer.Location;
4789                 Report.Error (1020, l, "Overloadable binary operator expected");
4790                 break;
4791         }
4792         
4793 }
4794
4795 void syntax_error (Location l, string msg)
4796 {
4797         Report.Error (1003, l, "Syntax error, " + msg);
4798 }
4799
4800 void note (string s)
4801 {
4802         // Used to put annotations
4803 }
4804
4805 Tokenizer lexer;
4806
4807 public Tokenizer Lexer {
4808         get {
4809                 return lexer;
4810         }
4811 }                  
4812
4813 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4814 {
4815         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4816         this.name = file.Name;
4817         this.file = file;
4818         current_container = RootContext.Tree.Types;
4819         // TODO: Make RootContext.Tree.Types a PartialContainer.
4820         current_class = current_container;
4821         current_container.NamespaceEntry = current_namespace;
4822         oob_stack = new Stack ();
4823         switch_stack = new Stack ();
4824
4825         lexer = new Tokenizer (reader, file, defines);
4826 }
4827
4828 public void parse ()
4829 {
4830         try {
4831                 if (yacc_verbose_flag > 1)
4832                         yyparse (lexer, new yydebug.yyDebugSimple ());
4833                 else
4834                         yyparse (lexer);
4835                 Tokenizer tokenizer = lexer as Tokenizer;
4836                 tokenizer.cleanup ();
4837         } catch (Exception e){
4838                 // 
4839                 // Removed for production use, use parser verbose to get the output.
4840                 //
4841                 // Console.WriteLine (e);
4842                 Report.Error (-25, lexer.Location, "Parsing error");
4843                 if (yacc_verbose_flag > 0)
4844                         Console.WriteLine (e);
4845         }
4846
4847         RootContext.Tree.Types.NamespaceEntry = null;
4848 }
4849
4850 void CheckToken (int error, int yyToken, string msg)
4851 {
4852         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4853                 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4854                 return;
4855         }               
4856         Report.Error (error, lexer.Location, msg);
4857 }
4858
4859 void CheckIdentifierToken (int yyToken)
4860 {
4861         CheckToken (1041, yyToken, "Identifier expected");
4862 }
4863
4864 string ConsumeStoredComment ()
4865 {
4866         string s = tmpComment;
4867         tmpComment = null;
4868         Lexer.doc_state = XmlCommentState.Allowed;
4869         return s;
4870 }
4871
4872 /* end end end */
4873 }