* TabControl.cs: Fix typo, emilinates an unneeded expose event.
[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_container, 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           ENUM IDENTIFIER 
2279           opt_enum_base {
2280                 if (RootContext.Documentation != null)
2281                         enumTypeComment = Lexer.consume_doc_comment ();
2282           }
2283           enum_body
2284           opt_semicolon
2285           { 
2286                 Location enum_location = lexer.Location;
2287
2288                 MemberName name = MakeName (new MemberName ((string) $4));
2289                 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2290                                    name, (Attributes) $1, enum_location);
2291                 
2292                 if (RootContext.Documentation != null)
2293                         e.DocComment = enumTypeComment;
2294
2295                 foreach (VariableDeclaration ev in (ArrayList) $7) {
2296                         e.AddEnumMember (ev.identifier, 
2297                                          (Expression) ev.expression_or_array_initializer,
2298                                          ev.Location, ev.OptAttributes,
2299                                          ev.DocComment);
2300                 }
2301
2302                 current_container.AddEnum (e);
2303                 RootContext.Tree.RecordDecl (name, e);
2304                 $$ = e;
2305
2306           }
2307         ;
2308
2309 opt_enum_base
2310         : /* empty */           { $$ = TypeManager.system_int32_expr; }
2311         | COLON type            { $$ = $2;   }
2312         ;
2313
2314 enum_body
2315         : OPEN_BRACE
2316           {
2317                 if (RootContext.Documentation != null)
2318                         Lexer.doc_state = XmlCommentState.Allowed;
2319           }
2320           opt_enum_member_declarations
2321           {
2322                 // here will be evaluated after CLOSE_BLACE is consumed.
2323                 if (RootContext.Documentation != null)
2324                         Lexer.doc_state = XmlCommentState.Allowed;
2325           }
2326           CLOSE_BRACE
2327           {
2328                 $$ = $3;
2329           }
2330         ;
2331
2332 opt_enum_member_declarations
2333         : /* empty */                   { $$ = new ArrayList (4); }
2334         | enum_member_declarations opt_comma { $$ = $1; }
2335         ;
2336
2337 enum_member_declarations
2338         : enum_member_declaration 
2339           {
2340                 ArrayList l = new ArrayList (4);
2341
2342                 l.Add ($1);
2343                 $$ = l;
2344           }
2345         | enum_member_declarations COMMA enum_member_declaration
2346           {
2347                 ArrayList l = (ArrayList) $1;
2348
2349                 l.Add ($3);
2350
2351                 $$ = l;
2352           }
2353         ;
2354
2355 enum_member_declaration
2356         : opt_attributes IDENTIFIER 
2357           {
2358                 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2359
2360                 if (RootContext.Documentation != null) {
2361                         vd.DocComment = Lexer.consume_doc_comment ();
2362                         Lexer.doc_state = XmlCommentState.Allowed;
2363                 }
2364
2365                 $$ = vd;
2366           }
2367         | opt_attributes IDENTIFIER
2368           {
2369                 $$ = lexer.Location;
2370                 if (RootContext.Documentation != null) {
2371                         tmpComment = Lexer.consume_doc_comment ();
2372                         Lexer.doc_state = XmlCommentState.NotAllowed;
2373                 }
2374           }
2375           ASSIGN expression
2376           { 
2377                 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2378
2379                 if (RootContext.Documentation != null)
2380                         vd.DocComment = ConsumeStoredComment ();
2381
2382                 $$ = vd;
2383           }
2384         ;
2385
2386 delegate_declaration
2387         : opt_attributes
2388           opt_modifiers
2389           DELEGATE type member_name
2390           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
2391           SEMICOLON
2392           {
2393                 Location l = lexer.Location;
2394                 MemberName name = MakeName ((MemberName) $5);
2395                 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2396                                              (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2397
2398                 if (RootContext.Documentation != null) {
2399                         del.DocComment = Lexer.consume_doc_comment ();
2400                         Lexer.doc_state = XmlCommentState.Allowed;
2401                 }
2402
2403                 current_container.AddDelegate (del);
2404                 RootContext.Tree.RecordDecl (name, del);
2405                 $$ = del;
2406           }     
2407         ;
2408
2409 namespace_or_type_name
2410         : member_name
2411         | namespace_or_type_name DOT IDENTIFIER {
2412                 $$ = new MemberName ((MemberName) $1, (string) $3);
2413           }
2414         ;
2415
2416 member_name
2417         : IDENTIFIER {
2418                 $$ = new MemberName ((string) $1);
2419           }
2420         ;
2421
2422 /* 
2423  * Before you think of adding a return_type, notice that we have been
2424  * using two rules in the places where it matters (one rule using type
2425  * and another identical one that uses VOID as the return type).  This
2426  * gets rid of a shift/reduce couple
2427  */
2428 type
2429         : namespace_or_type_name
2430           {
2431                 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2432           }
2433         | builtin_types
2434         | array_type
2435         | pointer_type    
2436         ;
2437
2438
2439 pointer_type
2440         : type STAR
2441           {
2442                 //
2443                 // Note that here only unmanaged types are allowed but we
2444                 // can't perform checks during this phase - we do it during
2445                 // semantic analysis.
2446                 //
2447                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2448           }
2449         | VOID STAR
2450           {
2451                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2452           }
2453         ;
2454
2455 non_expression_type
2456         : builtin_types 
2457         | non_expression_type rank_specifier
2458           {
2459                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2460           }
2461         | non_expression_type STAR
2462           {
2463                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2464           }
2465         | expression rank_specifiers 
2466           {
2467                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2468           }
2469         | expression STAR 
2470           {
2471                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2472           }
2473         
2474         //
2475         // We need this because the parser will happily go and reduce IDENTIFIER STAR
2476         // through this different path
2477         //
2478         | multiplicative_expression STAR 
2479           {
2480                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2481           }
2482         ;
2483
2484 type_list
2485         : type
2486           {
2487                 ArrayList types = new ArrayList (4);
2488
2489                 types.Add ($1);
2490                 $$ = types;
2491           }
2492         | type_list COMMA type
2493           {
2494                 ArrayList types = (ArrayList) $1;
2495
2496                 types.Add ($3);
2497                 $$ = types;
2498           }
2499         ;
2500
2501 /*
2502  * replaces all the productions for isolating the various
2503  * simple types, but we need this to reuse it easily in local_variable_type
2504  */
2505 builtin_types
2506         : OBJECT        { $$ = TypeManager.system_object_expr; }
2507         | STRING        { $$ = TypeManager.system_string_expr; }
2508         | BOOL          { $$ = TypeManager.system_boolean_expr; }
2509         | DECIMAL       { $$ = TypeManager.system_decimal_expr; }
2510         | FLOAT         { $$ = TypeManager.system_single_expr; }
2511         | DOUBLE        { $$ = TypeManager.system_double_expr; }
2512         | integral_type
2513         ;
2514
2515 integral_type
2516         : SBYTE         { $$ = TypeManager.system_sbyte_expr; }
2517         | BYTE          { $$ = TypeManager.system_byte_expr; }
2518         | SHORT         { $$ = TypeManager.system_int16_expr; }
2519         | USHORT        { $$ = TypeManager.system_uint16_expr; }
2520         | INT           { $$ = TypeManager.system_int32_expr; }
2521         | UINT          { $$ = TypeManager.system_uint32_expr; }
2522         | LONG          { $$ = TypeManager.system_int64_expr; }
2523         | ULONG         { $$ = TypeManager.system_uint64_expr; }
2524         | CHAR          { $$ = TypeManager.system_char_expr; }
2525         | VOID          { $$ = TypeManager.system_void_expr; }
2526         ;
2527
2528 array_type
2529         : type rank_specifiers
2530           {
2531                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2532           }
2533         ;
2534
2535 //
2536 // Expressions, section 7.5
2537 //
2538 primary_expression
2539         : literal
2540           {
2541                 // 7.5.1: Literals
2542           }
2543  
2544         | member_name
2545           {
2546                 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2547           }
2548         | parenthesized_expression
2549         | member_access
2550         | invocation_expression
2551         | element_access
2552         | this_access
2553         | base_access
2554         | post_increment_expression
2555         | post_decrement_expression
2556         | new_expression
2557         | typeof_expression
2558         | sizeof_expression
2559         | checked_expression
2560         | unchecked_expression
2561         | pointer_member_access
2562         | anonymous_method_expression
2563         ;
2564
2565 literal
2566         : boolean_literal
2567         | integer_literal
2568         | real_literal
2569         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value); }
2570         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value); }
2571         | NULL                  { $$ = NullLiteral.Null; }
2572         ;
2573
2574 real_literal
2575         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value); }
2576         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value); }
2577         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2578         ;
2579
2580 integer_literal
2581         : LITERAL_INTEGER       { 
2582                 object v = lexer.Value;
2583
2584                 if (v is int){
2585                         int i = (int) v;
2586
2587                         if (i == 0)
2588                                 $$ = IntLiteral.Zero;
2589                         else if (i == 1)
2590                                 $$ = IntLiteral.One;
2591                         else
2592                                 $$ = new IntLiteral (i);
2593                 } else if (v is uint)
2594                         $$ = new UIntLiteral ((UInt32) v);
2595                 else if (v is long)
2596                         $$ = new LongLiteral ((Int64) v);
2597                 else if (v is ulong)
2598                         $$ = new ULongLiteral ((UInt64) v);
2599                 else
2600                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
2601           }
2602         ;
2603
2604 boolean_literal
2605         : TRUE                  { $$ = new BoolLiteral (true); }
2606         | FALSE                 { $$ = new BoolLiteral (false); }
2607         ;
2608
2609 parenthesized_expression_0
2610         : OPEN_PARENS expression CLOSE_PARENS
2611           {
2612                 $$ = $2;
2613                 lexer.Deambiguate_CloseParens ();
2614                 // After this, the next token returned is one of
2615                 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2616                 // or CLOSE_PARENS_MINUS.
2617           }
2618         ;
2619
2620 parenthesized_expression
2621         : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2622           {
2623                 $$ = $1;
2624           }
2625         | parenthesized_expression_0 CLOSE_PARENS_MINUS
2626           {
2627                 // If a parenthesized expression is followed by a minus, we need to wrap
2628                 // the expression inside a ParenthesizedExpression for the CS0075 check
2629                 // in Binary.DoResolve().
2630                 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2631           }
2632         ;;
2633
2634 member_access
2635         : primary_expression DOT IDENTIFIER
2636           {
2637                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2638           }
2639         | predefined_type DOT IDENTIFIER
2640           {
2641                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2642           }
2643         ;
2644
2645 predefined_type
2646         : builtin_types
2647         ;
2648
2649 invocation_expression
2650         : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2651           {
2652                 if ($1 == null) {
2653                         Location l = lexer.Location;
2654                         Report.Error (1, l, "Parse error");
2655                 }
2656                 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2657           }
2658         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2659           {
2660                 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2661           }
2662         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2663           {
2664                 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2665           }
2666         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2667           {
2668                 ArrayList args = new ArrayList (1);
2669                 args.Add ($4);
2670                 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2671           }
2672         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2673           {
2674                 ArrayList args = ((ArrayList) $4);
2675                 args.Add ($6);
2676                 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2677           }
2678         ;
2679
2680 opt_argument_list
2681         : /* empty */           { $$ = null; }
2682         | argument_list
2683         ;
2684
2685 argument_list
2686         : argument
2687           { 
2688                 ArrayList list = new ArrayList (4);
2689                 list.Add ($1);
2690                 $$ = list;
2691           }
2692         | argument_list COMMA argument
2693           {
2694                 ArrayList list = (ArrayList) $1;
2695                 list.Add ($3);
2696                 $$ = list;
2697           }
2698         | argument_list error {
2699                 CheckToken (1026, yyToken, ", or ) expected");
2700           }
2701         ;
2702
2703 argument
2704         : expression
2705           {
2706                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2707           }
2708         | non_simple_argument
2709           {
2710                 $$ = $1;
2711           }
2712         ;
2713
2714 non_simple_argument
2715         : REF variable_reference 
2716           { 
2717                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2718           }
2719         | OUT variable_reference 
2720           { 
2721                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2722           }
2723         | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2724           {
2725                 ArrayList list = (ArrayList) $3;
2726                 Argument[] args = new Argument [list.Count];
2727                 list.CopyTo (args, 0);
2728
2729                 Expression expr = new Arglist (args, lexer.Location);
2730                 $$ = new Argument (expr, Argument.AType.Expression);
2731           }
2732         | ARGLIST
2733           {
2734                 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2735           }
2736         ;
2737
2738 variable_reference
2739         : expression { note ("section 5.4"); $$ = $1; }
2740         ;
2741
2742 element_access
2743         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
2744           {
2745                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2746           }
2747         | primary_expression rank_specifiers
2748           {
2749                 // So the super-trick is that primary_expression
2750                 // can only be either a SimpleName or a MemberAccess. 
2751                 // The MemberAccess case arises when you have a fully qualified type-name like :
2752                 // Foo.Bar.Blah i;
2753                 // SimpleName is when you have
2754                 // Blah i;
2755                   
2756                 Expression expr = (Expression) $1;  
2757                 if (expr is ComposedCast){
2758                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2759                 } else if (!(expr is SimpleName || expr is MemberAccess)){
2760                         Error_ExpectingTypeName (lexer.Location, expr);
2761                         $$ = TypeManager.system_object_expr;
2762                 } else {
2763                         //
2764                         // So we extract the string corresponding to the SimpleName
2765                         // or MemberAccess
2766                         // 
2767                         $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2768                 }
2769           }
2770         ;
2771
2772 expression_list
2773         : expression
2774           {
2775                 ArrayList list = new ArrayList (4);
2776                 list.Add ($1);
2777                 $$ = list;
2778           }
2779         | expression_list COMMA expression
2780           {
2781                 ArrayList list = (ArrayList) $1;
2782                 list.Add ($3);
2783                 $$ = list;
2784           }
2785         ;
2786
2787 this_access
2788         : THIS
2789           {
2790                 $$ = new This (current_block, lexer.Location);
2791           }
2792         ;
2793
2794 base_access
2795         : BASE DOT IDENTIFIER
2796           {
2797                 $$ = new BaseAccess ((string) $3, lexer.Location);
2798           }
2799         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2800           {
2801                 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2802           }
2803         | BASE error {
2804                 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2805                 $$ = null;
2806           }
2807         ;
2808
2809 post_increment_expression
2810         : primary_expression OP_INC
2811           {
2812                 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2813                                        (Expression) $1, lexer.Location);
2814           }
2815         ;
2816
2817 post_decrement_expression
2818         : primary_expression OP_DEC
2819           {
2820                 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2821                                        (Expression) $1, lexer.Location);
2822           }
2823         ;
2824
2825 new_expression
2826         : object_or_delegate_creation_expression
2827         | array_creation_expression
2828         ;
2829
2830 object_or_delegate_creation_expression
2831         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2832           {
2833                 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2834           }
2835         ;
2836
2837 array_creation_expression
2838         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
2839           opt_rank_specifier
2840           opt_array_initializer
2841           {
2842                 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2843           }
2844         | NEW type rank_specifiers array_initializer
2845           {
2846                 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2847           }
2848         | NEW error
2849           {
2850                 Report.Error (1031, lexer.Location, "Type expected");
2851                 $$ = null;
2852           }          
2853         | NEW type error 
2854           {
2855                 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2856           }
2857         ;
2858
2859 opt_rank_specifier
2860         : /* empty */
2861           {
2862                   $$ = "";
2863           }
2864         | rank_specifiers
2865           {
2866                         $$ = $1;
2867           }
2868         ;
2869
2870 rank_specifiers
2871         : rank_specifier opt_rank_specifier
2872           {
2873                   $$ = (string) $2 + (string) $1;
2874           }
2875         ;
2876
2877 rank_specifier
2878         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2879           {
2880                 $$ = "[" + (string) $2 + "]";
2881           }
2882         ;
2883
2884 opt_dim_separators
2885         : /* empty */
2886           {
2887                 $$ = "";
2888           }
2889         | dim_separators
2890           {
2891                   $$ = $1;
2892           }               
2893         ;
2894
2895 dim_separators
2896         : COMMA
2897           {
2898                 $$ = ",";
2899           }
2900         | dim_separators COMMA
2901           {
2902                 $$ = (string) $1 + ",";
2903           }
2904         ;
2905
2906 opt_array_initializer
2907         : /* empty */
2908           {
2909                 $$ = null;
2910           }
2911         | array_initializer
2912           {
2913                 $$ = $1;
2914           }
2915         ;
2916
2917 array_initializer
2918         : OPEN_BRACE CLOSE_BRACE
2919           {
2920                 ArrayList list = new ArrayList (4);
2921                 $$ = list;
2922           }
2923         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2924           {
2925                 $$ = (ArrayList) $2;
2926           }
2927         ;
2928
2929 variable_initializer_list
2930         : variable_initializer
2931           {
2932                 ArrayList list = new ArrayList (4);
2933                 list.Add ($1);
2934                 $$ = list;
2935           }
2936         | variable_initializer_list COMMA variable_initializer
2937           {
2938                 ArrayList list = (ArrayList) $1;
2939                 list.Add ($3);
2940                 $$ = list;
2941           }
2942         ;
2943
2944 typeof_expression
2945         : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2946           {
2947                 $$ = new TypeOfVoid (lexer.Location);
2948           }
2949         | TYPEOF OPEN_PARENS type CLOSE_PARENS
2950           {
2951                 $$ = new TypeOf ((Expression) $3, lexer.Location);
2952           }
2953         ;
2954
2955 sizeof_expression
2956         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
2957                 $$ = new SizeOf ((Expression) $3, lexer.Location);
2958           }
2959         ;
2960
2961 checked_expression
2962         : CHECKED OPEN_PARENS expression CLOSE_PARENS
2963           {
2964                 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2965           }
2966         ;
2967
2968 unchecked_expression
2969         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2970           {
2971                 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2972           }
2973         ;
2974
2975 pointer_member_access 
2976         : primary_expression OP_PTR IDENTIFIER
2977           {
2978                 Expression deref;
2979
2980                 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2981                 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2982           }
2983         ;
2984
2985 anonymous_method_expression
2986         : DELEGATE opt_anonymous_method_signature _mark_
2987           {
2988                 oob_stack.Push (current_local_parameters);
2989                 current_local_parameters = (Parameters)$2;
2990
2991                 // Force the next block to be created as a ToplevelBlock
2992                 oob_stack.Push (current_block);
2993                 oob_stack.Push (top_current_block);
2994                 current_block = null;
2995           } 
2996           block
2997           {
2998                 Location loc = (Location) $3;
2999                 top_current_block = (Block) oob_stack.Pop ();
3000                 current_block = (Block) oob_stack.Pop ();
3001                 if (RootContext.Version == LanguageVersion.ISO_1){
3002                         Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3003                         $$ = null;
3004                 } else  {
3005                         ToplevelBlock anon_block = (ToplevelBlock) $5;
3006
3007                         anon_block.Parent = current_block;
3008                         $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
3009                                 anon_block, loc);
3010                 }
3011                 current_local_parameters = (Parameters) oob_stack.Pop ();
3012           }
3013         ;
3014
3015 opt_anonymous_method_signature
3016         : /* empty */                   { $$ = null; } 
3017         | anonymous_method_signature
3018         ;
3019
3020 anonymous_method_signature
3021         : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
3022           {
3023                 if ($2 == null)
3024                         $$ = Parameters.EmptyReadOnlyParameters;
3025                 else {
3026                         ArrayList par_list = (ArrayList) $2;
3027                         Parameter [] pars = new Parameter [par_list.Count];
3028                         par_list.CopyTo (pars);
3029                         $$ = new Parameters (pars, null, lexer.Location);
3030                 }
3031           }
3032         ;
3033
3034 opt_anonymous_method_parameter_list
3035         : /* empty */                      { $$ = null; } 
3036         | anonymous_method_parameter_list  { $$ = $1; }
3037         ;
3038
3039 anonymous_method_parameter_list
3040         : anonymous_method_parameter 
3041           {
3042                 ArrayList a = new ArrayList (4);
3043                 a.Add ($1);
3044                 $$ = a;
3045           }
3046         | anonymous_method_parameter_list COMMA anonymous_method_parameter 
3047           {
3048                 ArrayList a = (ArrayList) $1;
3049                 a.Add ($3);
3050                 $$ = a;
3051           }
3052         ; 
3053
3054 anonymous_method_parameter
3055         : opt_parameter_modifier type IDENTIFIER {
3056                 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3057           }
3058         | PARAMS type IDENTIFIER {
3059                 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3060                 $$ = null;
3061           }
3062         ;
3063
3064 unary_expression
3065         : primary_expression
3066         | BANG prefixed_unary_expression
3067           {
3068                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3069           }
3070         | TILDE prefixed_unary_expression
3071           {
3072                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3073           }
3074         | cast_expression
3075         ;
3076
3077 cast_list
3078         : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3079           {
3080                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3081           }
3082         | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3083           {
3084                 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3085           }     
3086         ;
3087
3088 cast_expression
3089         : cast_list
3090         | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3091           {
3092                 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3093           }
3094         ;
3095
3096         //
3097         // The idea to split this out is from Rhys' grammar
3098         // to solve the problem with casts.
3099         //
3100 prefixed_unary_expression
3101         : unary_expression
3102         | PLUS prefixed_unary_expression
3103           { 
3104                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3105           } 
3106         | MINUS prefixed_unary_expression 
3107           { 
3108                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3109           }
3110         | OP_INC prefixed_unary_expression 
3111           {
3112                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3113                                        (Expression) $2, lexer.Location);
3114           }
3115         | OP_DEC prefixed_unary_expression 
3116           {
3117                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3118                                        (Expression) $2, lexer.Location);
3119           }
3120         | STAR prefixed_unary_expression
3121           {
3122                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3123           }
3124         | BITWISE_AND prefixed_unary_expression
3125           {
3126                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3127           }
3128         ;
3129
3130 pre_increment_expression
3131         : OP_INC prefixed_unary_expression 
3132           {
3133                 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3134                                        (Expression) $2, lexer.Location);
3135           }
3136         ;
3137
3138 pre_decrement_expression
3139         : OP_DEC prefixed_unary_expression 
3140           {
3141                 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3142                                        (Expression) $2, lexer.Location);
3143           }
3144         ;
3145
3146 multiplicative_expression
3147         : prefixed_unary_expression
3148         | multiplicative_expression STAR prefixed_unary_expression
3149           {
3150                 $$ = new Binary (Binary.Operator.Multiply, 
3151                                  (Expression) $1, (Expression) $3, lexer.Location);
3152           }
3153         | multiplicative_expression DIV prefixed_unary_expression
3154           {
3155                 $$ = new Binary (Binary.Operator.Division, 
3156                                  (Expression) $1, (Expression) $3, lexer.Location);
3157           }
3158         | multiplicative_expression PERCENT prefixed_unary_expression 
3159           {
3160                 $$ = new Binary (Binary.Operator.Modulus, 
3161                                  (Expression) $1, (Expression) $3, lexer.Location);
3162           }
3163         ;
3164
3165 additive_expression
3166         : multiplicative_expression
3167         | additive_expression PLUS multiplicative_expression 
3168           {
3169                 $$ = new Binary (Binary.Operator.Addition, 
3170                                  (Expression) $1, (Expression) $3, lexer.Location);
3171           }
3172         | additive_expression MINUS multiplicative_expression
3173           {
3174                 $$ = new Binary (Binary.Operator.Subtraction, 
3175                                  (Expression) $1, (Expression) $3, lexer.Location);
3176           }
3177         ;
3178
3179 shift_expression
3180         : additive_expression
3181         | shift_expression OP_SHIFT_LEFT additive_expression
3182           {
3183                 $$ = new Binary (Binary.Operator.LeftShift, 
3184                                  (Expression) $1, (Expression) $3, lexer.Location);
3185           }
3186         | shift_expression OP_SHIFT_RIGHT additive_expression
3187           {
3188                 $$ = new Binary (Binary.Operator.RightShift, 
3189                                  (Expression) $1, (Expression) $3, lexer.Location);
3190           }
3191         ; 
3192
3193 relational_expression
3194         : shift_expression
3195         | relational_expression OP_LT shift_expression
3196           {
3197                 $$ = new Binary (Binary.Operator.LessThan, 
3198                                  (Expression) $1, (Expression) $3, lexer.Location);
3199           }
3200         | relational_expression OP_GT shift_expression
3201           {
3202                 $$ = new Binary (Binary.Operator.GreaterThan, 
3203                                  (Expression) $1, (Expression) $3, lexer.Location);
3204           }
3205         | relational_expression OP_LE shift_expression
3206           {
3207                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
3208                                  (Expression) $1, (Expression) $3, lexer.Location);
3209           }
3210         | relational_expression OP_GE shift_expression
3211           {
3212                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
3213                                  (Expression) $1, (Expression) $3, lexer.Location);
3214           }
3215         | relational_expression IS type
3216           {
3217                 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3218           }
3219         | relational_expression AS type
3220           {
3221                 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3222           }
3223         ;
3224
3225 equality_expression
3226         : relational_expression
3227         | equality_expression OP_EQ relational_expression
3228           {
3229                 $$ = new Binary (Binary.Operator.Equality, 
3230                                  (Expression) $1, (Expression) $3, lexer.Location);
3231           }
3232         | equality_expression OP_NE relational_expression
3233           {
3234                 $$ = new Binary (Binary.Operator.Inequality, 
3235                                  (Expression) $1, (Expression) $3, lexer.Location);
3236           }
3237         ; 
3238
3239 and_expression
3240         : equality_expression
3241         | and_expression BITWISE_AND equality_expression
3242           {
3243                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
3244                                  (Expression) $1, (Expression) $3, lexer.Location);
3245           }
3246         ;
3247
3248 exclusive_or_expression
3249         : and_expression
3250         | exclusive_or_expression CARRET and_expression
3251           {
3252                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
3253                                  (Expression) $1, (Expression) $3, lexer.Location);
3254           }
3255         ;
3256
3257 inclusive_or_expression
3258         : exclusive_or_expression
3259         | inclusive_or_expression BITWISE_OR exclusive_or_expression
3260           {
3261                 $$ = new Binary (Binary.Operator.BitwiseOr, 
3262                                  (Expression) $1, (Expression) $3, lexer.Location);
3263           }
3264         ;
3265
3266 conditional_and_expression
3267         : inclusive_or_expression
3268         | conditional_and_expression OP_AND inclusive_or_expression
3269           {
3270                 $$ = new Binary (Binary.Operator.LogicalAnd, 
3271                                  (Expression) $1, (Expression) $3, lexer.Location);
3272           }
3273         ;
3274
3275 conditional_or_expression
3276         : conditional_and_expression
3277         | conditional_or_expression OP_OR conditional_and_expression
3278           {
3279                 $$ = new Binary (Binary.Operator.LogicalOr, 
3280                                  (Expression) $1, (Expression) $3, lexer.Location);
3281           }
3282         ;
3283
3284 conditional_expression
3285         : conditional_or_expression
3286         | conditional_or_expression INTERR expression COLON expression 
3287           {
3288                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3289           }
3290         ;
3291
3292 assignment_expression
3293         : prefixed_unary_expression ASSIGN expression
3294           {
3295                 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3296           }
3297         | prefixed_unary_expression OP_MULT_ASSIGN expression
3298           {
3299                 Location l = lexer.Location;
3300
3301                 $$ = new CompoundAssign (
3302                         Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3303           }
3304         | prefixed_unary_expression OP_DIV_ASSIGN expression
3305           {
3306                 Location l = lexer.Location;
3307
3308                 $$ = new CompoundAssign (
3309                         Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3310           }
3311         | prefixed_unary_expression OP_MOD_ASSIGN expression
3312           {
3313                 Location l = lexer.Location;
3314
3315                 $$ = new CompoundAssign (
3316                         Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3317           }
3318         | prefixed_unary_expression OP_ADD_ASSIGN expression
3319           {
3320                 Location l = lexer.Location;
3321
3322                 $$ = new CompoundAssign (
3323                         Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3324           }
3325         | prefixed_unary_expression OP_SUB_ASSIGN expression
3326           {
3327                 Location l = lexer.Location;
3328
3329                 $$ = new CompoundAssign (
3330                         Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3331           }
3332         | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3333           {
3334                 Location l = lexer.Location;
3335
3336                 $$ = new CompoundAssign (
3337                         Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3338           }
3339         | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3340           {
3341                 Location l = lexer.Location;
3342
3343                 $$ = new CompoundAssign (
3344                         Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3345           }
3346         | prefixed_unary_expression OP_AND_ASSIGN expression
3347           {
3348                 Location l = lexer.Location;
3349
3350                 $$ = new CompoundAssign (
3351                         Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3352           }
3353         | prefixed_unary_expression OP_OR_ASSIGN expression
3354           {
3355                 Location l = lexer.Location;
3356
3357                 $$ = new CompoundAssign (
3358                         Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3359           }
3360         | prefixed_unary_expression OP_XOR_ASSIGN expression
3361           {
3362                 Location l = lexer.Location;
3363
3364                 $$ = new CompoundAssign (
3365                         Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3366           }
3367         ;
3368
3369 expression
3370         : conditional_expression
3371         | assignment_expression
3372         ;
3373
3374 constant_expression
3375         : expression
3376         ;
3377
3378 boolean_expression
3379         : expression
3380         ;
3381
3382 //
3383 // 10 classes
3384 //
3385 class_declaration
3386         : opt_attributes
3387           opt_modifiers
3388           opt_partial
3389           CLASS member_name
3390           {
3391                 MemberName name = MakeName ((MemberName) $5);
3392                 bool partial = (bool) $3;
3393                 int mod_flags = (int) $2;
3394
3395                 if (partial) {
3396                         ClassPart part = PartialContainer.CreatePart (
3397                                 current_namespace, current_class, name, mod_flags,
3398                                 (Attributes) $1, Kind.Class, lexer.Location);
3399
3400                         current_container = part.PartialContainer;
3401                         current_class = part;
3402                 } else {
3403                         if ((mod_flags & Modifiers.STATIC) != 0) {
3404                                 current_class = new StaticClass (
3405                                         current_namespace, current_class, name,
3406                                         mod_flags, (Attributes) $1, lexer.Location);
3407                         } else {
3408                                 current_class = new Class (
3409                                         current_namespace, current_class, name,
3410                                         mod_flags, (Attributes) $1, lexer.Location);
3411                         }
3412
3413                         current_container.AddClassOrStruct (current_class);
3414                         current_container = current_class;
3415                         RootContext.Tree.RecordDecl (name, current_class);
3416                 }
3417           }
3418           opt_class_base
3419           {
3420                 if ($7 != null) {
3421                         if (current_class.Name == "System.Object") {
3422                                 Report.Error (537, current_class.Location,
3423                                               "The class System.Object cannot have a base " +
3424                                               "class or implement an interface.");
3425                         }
3426                         current_class.Bases = (ArrayList) $7;
3427                 }
3428
3429                 if (RootContext.Documentation != null) {
3430                         current_class.DocComment = Lexer.consume_doc_comment ();
3431                         Lexer.doc_state = XmlCommentState.Allowed;
3432                 }
3433           }
3434           class_body
3435           {
3436                 if (RootContext.Documentation != null)
3437                         Lexer.doc_state = XmlCommentState.Allowed;
3438           }
3439           opt_semicolon 
3440           {
3441                 $$ = pop_current_class ();
3442           }
3443         ;       
3444
3445 opt_partial
3446         : /* empty */
3447           { $$ = (bool) false; }
3448         | PARTIAL
3449           { $$ = (bool) true; }
3450         ;
3451
3452 opt_modifiers
3453         : /* empty */           { $$ = (int) 0; }
3454         | modifiers
3455         ;
3456
3457 modifiers
3458         : modifier
3459         | modifiers modifier
3460           { 
3461                 int m1 = (int) $1;
3462                 int m2 = (int) $2;
3463
3464                 if ((m1 & m2) != 0) {
3465                         Location l = lexer.Location;
3466                         Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3467                 }
3468                 $$ = (int) (m1 | m2);
3469           }
3470         ;
3471
3472 modifier
3473         : NEW                   { $$ = Modifiers.NEW; }
3474         | PUBLIC                { $$ = Modifiers.PUBLIC; }
3475         | PROTECTED             { $$ = Modifiers.PROTECTED; }
3476         | INTERNAL              { $$ = Modifiers.INTERNAL; }
3477         | PRIVATE               { $$ = Modifiers.PRIVATE; }
3478         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
3479         | SEALED                { $$ = Modifiers.SEALED; }
3480         | STATIC                { $$ = Modifiers.STATIC; }
3481         | READONLY              { $$ = Modifiers.READONLY; }
3482         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
3483         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
3484         | EXTERN                { $$ = Modifiers.EXTERN; }
3485         | VOLATILE              { $$ = Modifiers.VOLATILE; }
3486         | UNSAFE                { $$ = Modifiers.UNSAFE; }
3487         ;
3488
3489 opt_class_base
3490         : /* empty */           { $$ = null; }
3491         | class_base            { $$ = $1;   }
3492         ;
3493
3494 class_base
3495         : COLON type_list { $$ = $2; }
3496         ;
3497
3498 //
3499 // Statements (8.2)
3500 //
3501
3502 //
3503 // A block is "contained" on the following places:
3504 //      method_body
3505 //      property_declaration as part of the accessor body (get/set)
3506 //      operator_declaration
3507 //      constructor_declaration
3508 //      destructor_declaration
3509 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3510 //      
3511 block
3512         : OPEN_BRACE 
3513           {
3514                 if (current_block == null){
3515                         current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3516                         top_current_block = current_block;
3517                 } else {
3518                 current_block = new Block (current_block, current_local_parameters,
3519                                            lexer.Location, Location.Null);
3520                 }
3521           } 
3522           opt_statement_list CLOSE_BRACE 
3523           { 
3524                 while (current_block.Implicit)
3525                         current_block = current_block.Parent;
3526                 $$ = current_block;
3527                 current_block.SetEndLocation (lexer.Location);
3528                 current_block = current_block.Parent;
3529                 if (current_block == null)
3530                         top_current_block = null;
3531           }
3532         ;
3533
3534 opt_statement_list
3535         : /* empty */
3536         | statement_list 
3537         ;
3538
3539 statement_list
3540         : statement
3541         | statement_list statement
3542         ;
3543
3544 statement
3545         : declaration_statement
3546           {
3547                 if ($1 != null && (Block) $1 != current_block){
3548                         current_block.AddStatement ((Statement) $1);
3549                         current_block = (Block) $1;
3550                 }
3551           }
3552         | valid_declaration_statement
3553           {
3554                 current_block.AddStatement ((Statement) $1);
3555           }
3556         | labeled_statement
3557         ;
3558
3559 valid_declaration_statement
3560         : block
3561         | empty_statement
3562         | expression_statement
3563         | selection_statement
3564         | iteration_statement
3565         | jump_statement                  
3566         | try_statement
3567         | checked_statement
3568         | unchecked_statement
3569         | lock_statement
3570         | using_statement
3571         | unsafe_statement
3572         | fixed_statement
3573         ;
3574
3575 embedded_statement
3576         : valid_declaration_statement
3577         | declaration_statement
3578           {
3579                   Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3580                   $$ = null;
3581           }
3582         | labeled_statement
3583           {
3584                   Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3585                   $$ = null;
3586           }
3587         ;
3588
3589 empty_statement
3590         : SEMICOLON
3591           {
3592                   $$ = EmptyStatement.Value;
3593           }
3594         ;
3595
3596 labeled_statement
3597         : IDENTIFIER COLON 
3598           {
3599                 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3600
3601                 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3602                         current_block.AddStatement (labeled);
3603           }
3604           statement
3605         ;
3606
3607 declaration_statement
3608         : local_variable_declaration SEMICOLON
3609           {
3610                 if ($1 != null){
3611                         DictionaryEntry de = (DictionaryEntry) $1;
3612
3613                         $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3614                 }
3615           }
3616
3617         | local_constant_declaration SEMICOLON
3618           {
3619                 if ($1 != null){
3620                         DictionaryEntry de = (DictionaryEntry) $1;
3621
3622                         $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3623                 }
3624           }
3625         ;
3626
3627 /* 
3628  * The following is from Rhys' grammar:
3629  * > Types in local variable declarations must be recognized as 
3630  * > expressions to prevent reduce/reduce errors in the grammar.
3631  * > The expressions are converted into types during semantic analysis.
3632  */
3633 local_variable_type
3634         : primary_expression opt_rank_specifier
3635           { 
3636                 // FIXME: Do something smart here regarding the composition of the type.
3637
3638                 // Ok, the above "primary_expression" is there to get rid of
3639                 // both reduce/reduce and shift/reduces in the grammar, it should
3640                 // really just be "type_name".  If you use type_name, a reduce/reduce
3641                 // creeps up.  If you use namespace_or_type_name (which is all we need
3642                 // really) two shift/reduces appear.
3643                 // 
3644
3645                 // So the super-trick is that primary_expression
3646                 // can only be either a SimpleName or a MemberAccess. 
3647                 // The MemberAccess case arises when you have a fully qualified type-name like :
3648                 // Foo.Bar.Blah i;
3649                 // SimpleName is when you have
3650                 // Blah i;
3651                   
3652                 Expression expr = (Expression) $1;  
3653                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3654                         Error_ExpectingTypeName (lexer.Location, expr);
3655                         $$ = null;
3656                 } else {
3657                         //
3658                         // So we extract the string corresponding to the SimpleName
3659                         // or MemberAccess
3660                         // 
3661
3662                         if ((string) $2 == "")
3663                                 $$ = $1;
3664                         else
3665                                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3666                 }
3667           }
3668         | builtin_types opt_rank_specifier
3669           {
3670                 if ((string) $2 == "")
3671                         $$ = $1;
3672                 else
3673                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3674           }
3675         ;
3676
3677 local_variable_pointer_type
3678         : primary_expression STAR
3679           {
3680                 Expression expr = (Expression) $1;  
3681                 Location l = lexer.Location;
3682
3683                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3684                         Error_ExpectingTypeName (l, expr);
3685
3686                         $$ = null;
3687                 } else 
3688                         $$ = new ComposedCast ((Expression) $1, "*", l);
3689           }
3690         | builtin_types STAR
3691           {
3692                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3693           }
3694         | VOID STAR
3695           {
3696                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3697           }
3698         | local_variable_pointer_type STAR
3699           {
3700                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3701           }
3702         ;
3703
3704 local_variable_declaration
3705         : local_variable_type variable_declarators
3706           {
3707                 if ($1 != null)
3708                         $$ = new DictionaryEntry ($1, $2);
3709                 else
3710                         $$ = null;
3711           }
3712         | local_variable_pointer_type opt_rank_specifier variable_declarators
3713         {
3714                 if ($1 != null){
3715                         Expression t;
3716
3717                         if ((string) $2 == "")
3718                                 t = (Expression) $1;
3719                         else
3720                                 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3721                         $$ = new DictionaryEntry (t, $3);
3722                 } else 
3723                         $$ = null;
3724         }
3725         ;
3726
3727 local_constant_declaration
3728         : CONST local_variable_type constant_declarators
3729           {
3730                 if ($2 != null)
3731                         $$ = new DictionaryEntry ($2, $3);
3732                 else
3733                         $$ = null;
3734           }
3735         ;
3736
3737 expression_statement
3738         : statement_expression SEMICOLON
3739           {
3740                 $$ = $1;
3741           }
3742         ;
3743
3744         //
3745         // We have to do the wrapping here and not in the case above,
3746         // because statement_expression is used for example in for_statement
3747         //
3748 statement_expression
3749         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3750         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3751         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3752         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3753         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3754         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3755         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3756         | error {
3757                 Report.Error (1002, lexer.Location, "Expecting `;'");
3758                 $$ = null;
3759           }
3760         ;
3761
3762 object_creation_expression
3763         : object_or_delegate_creation_expression
3764           { note ("complain if this is a delegate maybe?"); } 
3765         ;
3766
3767 selection_statement
3768         : if_statement
3769         | switch_statement
3770         ; 
3771
3772 if_statement
3773         : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS 
3774           embedded_statement
3775           { 
3776                 Location l = (Location) $3;
3777
3778                 $$ = new If ((Expression) $4, (Statement) $6, l);
3779
3780                 if (RootContext.WarningLevel >= 4){
3781                         if ($6 == EmptyStatement.Value)
3782                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3783                 }
3784
3785           }
3786         | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3787           embedded_statement ELSE embedded_statement
3788           {
3789                 Location l = (Location) $3;
3790
3791                 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3792           }
3793         ;
3794
3795 switch_statement
3796         : SWITCH OPEN_PARENS _mark_
3797           { 
3798                 switch_stack.Push (current_block);
3799           }
3800           expression CLOSE_PARENS 
3801           switch_block
3802           {
3803                 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3804                 current_block = (Block) switch_stack.Pop ();
3805           }
3806         ;
3807
3808 switch_block
3809         : OPEN_BRACE
3810           opt_switch_sections
3811           CLOSE_BRACE
3812           {
3813                 $$ = $2;
3814           }
3815         ;
3816
3817 opt_switch_sections
3818         : /* empty */           
3819           {
3820                 Report.Error (1522, lexer.Location, "Empty switch block"); 
3821           }
3822         | switch_sections
3823         ;
3824
3825 switch_sections
3826         : switch_section 
3827           {
3828                 ArrayList sections = new ArrayList (4);
3829
3830                 sections.Add ($1);
3831                 $$ = sections;
3832           }
3833         | switch_sections switch_section
3834           {
3835                 ArrayList sections = (ArrayList) $1;
3836
3837                 sections.Add ($2);
3838                 $$ = sections;
3839           }
3840         ;
3841
3842 switch_section
3843         : switch_labels
3844           {
3845                 current_block = current_block.CreateSwitchBlock (lexer.Location);
3846           }
3847           statement_list 
3848           {
3849                 Block topmost = current_block;
3850
3851                 while (topmost.Implicit)
3852                         topmost = topmost.Parent;
3853                 $$ = new SwitchSection ((ArrayList) $1, topmost);
3854           }
3855         ;
3856
3857 switch_labels
3858         : switch_label 
3859           {
3860                 ArrayList labels = new ArrayList (4);
3861
3862                 labels.Add ($1);
3863                 $$ = labels;
3864           }
3865         | switch_labels switch_label 
3866           {
3867                 ArrayList labels = (ArrayList) ($1);
3868                 labels.Add ($2);
3869
3870                 $$ = labels;
3871           }
3872         ;
3873
3874 switch_label
3875         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3876         | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
3877         | error {
3878                 Report.Error (
3879                         1523, lexer.Location, 
3880                         "The keyword case or default must precede code in switch block");
3881           }
3882         ;
3883
3884 iteration_statement
3885         : while_statement
3886         | do_statement
3887         | for_statement
3888         | foreach_statement
3889         ;
3890
3891 while_statement
3892         : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3893         {
3894                 Location l = (Location) $3;
3895                 $$ = new While ((Expression) $4, (Statement) $6, l);
3896         
3897                 if (RootContext.WarningLevel >= 4){
3898                         if ($6 == EmptyStatement.Value)
3899                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3900                 }
3901         }
3902         ;
3903
3904 do_statement
3905         : DO embedded_statement 
3906           WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3907           {
3908                 Location l = (Location) $5;
3909
3910                 $$ = new Do ((Statement) $2, (Expression) $6, l);
3911           }
3912         ;
3913
3914 for_statement
3915         : FOR OPEN_PARENS 
3916           opt_for_initializer SEMICOLON _mark_
3917           {
3918                 Block assign_block = new Block (current_block);
3919                 current_block = assign_block;
3920
3921                 if ($3 is DictionaryEntry){
3922                         DictionaryEntry de = (DictionaryEntry) $3;
3923                         
3924                         Expression type = (Expression) de.Key;
3925                         ArrayList var_declarators = (ArrayList) de.Value;
3926
3927                         foreach (VariableDeclaration decl in var_declarators){
3928
3929                                 LocalInfo vi;
3930
3931                                 vi = current_block.AddVariable (
3932                                         type, decl.identifier, current_local_parameters, decl.Location);
3933                                 if (vi == null)
3934                                         continue;
3935
3936                                 Location l = lexer.Location;
3937                                 Expression expr;
3938                                 if (decl.expression_or_array_initializer is Expression){
3939                                         expr = (Expression) decl.expression_or_array_initializer;
3940                                 } else if (decl.expression_or_array_initializer == null) {
3941                                         expr = null;
3942                                 } else {
3943                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3944                                         expr = new ArrayCreation (type, "", init, decl.Location);
3945                                 }
3946                                         
3947                                 LocalVariableReference var;
3948                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
3949
3950                                 if (expr != null) {
3951                                         Assign a = new Assign (var, expr, decl.Location);
3952                                         
3953                                         assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3954                                 }
3955                         }
3956                         
3957                         // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3958                         // This can be referred to as $6 below.
3959                         $$ = null;
3960                 } else {
3961                         $$ = $3;
3962                 }
3963           } 
3964           opt_for_condition SEMICOLON
3965           opt_for_iterator CLOSE_PARENS 
3966           embedded_statement
3967           {
3968                 Location l = (Location) $5;
3969
3970                 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3971
3972                 if (RootContext.WarningLevel >= 4){
3973                         if ($11 == EmptyStatement.Value)
3974                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3975                 }
3976
3977                 current_block.AddStatement (f);
3978                 while (current_block.Implicit)
3979                         current_block = current_block.Parent;
3980                 $$ = current_block;
3981                 current_block = current_block.Parent;
3982           }
3983         ;
3984
3985 opt_for_initializer
3986         : /* empty */           { $$ = EmptyStatement.Value; }
3987         | for_initializer       
3988         ;
3989
3990 for_initializer
3991         : local_variable_declaration
3992         | statement_expression_list
3993         ;
3994
3995 opt_for_condition
3996         : /* empty */           { $$ = null; }
3997         | boolean_expression
3998         ;
3999
4000 opt_for_iterator
4001         : /* empty */           { $$ = EmptyStatement.Value; }
4002         | for_iterator
4003         ;
4004
4005 for_iterator
4006         : statement_expression_list
4007         ;
4008
4009 statement_expression_list
4010         : statement_expression  
4011           {
4012                 // CHANGE: was `null'
4013                 Block b = new Block (current_block, Block.Flags.Implicit);   
4014
4015                 b.AddStatement ((Statement) $1);
4016                 $$ = b;
4017           }
4018         | statement_expression_list COMMA statement_expression
4019           {
4020                 Block b = (Block) $1;
4021
4022                 b.AddStatement ((Statement) $3);
4023                 $$ = $1;
4024           }
4025         ;
4026
4027 foreach_statement
4028         : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4029         {
4030                 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4031                 $$ = null;
4032         }
4033         | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4034           expression CLOSE_PARENS 
4035           {
4036                 Block foreach_block = new Block (current_block);
4037                 current_block = foreach_block;
4038
4039                 Location l = lexer.Location;
4040                 LocalInfo vi;
4041
4042                 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4043                 if (vi != null) {
4044                         vi.ReadOnly = true;
4045
4046                         // Get a writable reference to this read-only variable.
4047                         //
4048                         // Note that the $$ here refers to the value of _this_ code block,
4049                         // not the value of the LHS non-terminal.  This can be referred to as $9 below.
4050                         $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4051                 } else {
4052                         $$ = null;
4053                 }
4054           } 
4055           embedded_statement 
4056           {
4057                 LocalVariableReference v = (LocalVariableReference) $9;
4058                 Location l = (Location) $6;
4059
4060                 if (v != null) {
4061                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4062                         current_block.AddStatement (f);
4063                 }
4064
4065                 while (current_block.Implicit)
4066                           current_block = current_block.Parent;
4067                 $$ = current_block;
4068                 current_block = current_block.Parent;
4069           }
4070         ;
4071
4072 jump_statement
4073         : break_statement
4074         | continue_statement
4075         | goto_statement
4076         | return_statement
4077         | throw_statement
4078         | yield_statement
4079         ;
4080
4081 break_statement
4082         : BREAK SEMICOLON
4083           {
4084                 $$ = new Break (lexer.Location);
4085           }
4086         ;
4087
4088 continue_statement
4089         : CONTINUE SEMICOLON
4090           {
4091                 $$ = new Continue (lexer.Location);
4092           }
4093         ;
4094
4095 goto_statement
4096         : GOTO IDENTIFIER SEMICOLON 
4097           {
4098                 $$ = new Goto (current_block, (string) $2, lexer.Location);
4099           }
4100         | GOTO CASE constant_expression SEMICOLON
4101           {
4102                 $$ = new GotoCase ((Expression) $3, lexer.Location);
4103           }
4104         | GOTO DEFAULT SEMICOLON 
4105           {
4106                 $$ = new GotoDefault (lexer.Location);
4107           }
4108         ; 
4109
4110 return_statement
4111         : RETURN opt_expression SEMICOLON
4112           {
4113                 $$ = new Return ((Expression) $2, lexer.Location);
4114           }
4115         ;
4116
4117 throw_statement
4118         : THROW opt_expression SEMICOLON
4119           {
4120                 $$ = new Throw ((Expression) $2, lexer.Location);
4121           }
4122         ;
4123
4124 yield_statement 
4125         : IDENTIFIER RETURN expression SEMICOLON
4126           {
4127                 string s = (string) $1;
4128                 if (s != "yield"){
4129                         Report.Error (1003, lexer.Location, "; expected");
4130                         $$ = null;
4131                 }
4132                 if (RootContext.Version == LanguageVersion.ISO_1){
4133                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4134                         $$ = null;
4135                 }
4136                 if (iterator_container == null){
4137                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4138                         $$ = null;
4139                 } else {
4140                         iterator_container.SetYields ();
4141                         $$ = new Yield ((Expression) $3, lexer.Location); 
4142                 }
4143           }
4144         | IDENTIFIER RETURN SEMICOLON
4145         {
4146                 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4147                 $$ = null;
4148         }
4149         | IDENTIFIER BREAK SEMICOLON
4150           {
4151                 string s = (string) $1;
4152                 if (s != "yield"){
4153                         Report.Error (1003, lexer.Location, "; expected");
4154                         $$ = null;
4155                 }
4156                 if (RootContext.Version == LanguageVersion.ISO_1){
4157                         Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4158                         $$ = null;
4159                 }
4160                 if (iterator_container == null){
4161                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4162                         $$ = null;
4163                 } else {
4164                         iterator_container.SetYields ();
4165                         $$ = new YieldBreak (lexer.Location);
4166                 }
4167           }
4168         ;
4169
4170 opt_expression
4171         : /* empty */
4172         | expression
4173         ;
4174
4175 try_statement
4176         : TRY block catch_clauses 
4177         {
4178                 Catch g = null;
4179                 
4180                 ArrayList c = (ArrayList)$3;
4181                 for (int i = 0; i < c.Count; ++i) {
4182                         Catch cc = (Catch) c [i];
4183                         if (cc.IsGeneral) {
4184                                 if (i != c.Count - 1)
4185                                         Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4186                                 g = cc;
4187                                 c.RemoveAt (i);
4188                                 i--;
4189                         }
4190                 }
4191
4192                 // Now s contains the list of specific catch clauses
4193                 // and g contains the general one.
4194                 
4195                 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4196         }
4197         | TRY block opt_catch_clauses FINALLY block
4198           {
4199                 Catch g = null;
4200                 ArrayList s = new ArrayList (4);
4201                 ArrayList catch_list = (ArrayList) $3;
4202
4203                 if (catch_list != null){
4204                         foreach (Catch cc in catch_list) {
4205                                 if (cc.IsGeneral)
4206                                         g = cc;
4207                                 else
4208                                         s.Add (cc);
4209                         }
4210                 }
4211
4212                 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4213           }
4214         | TRY block error 
4215           {
4216                 Report.Error (1524, lexer.Location, "Expected catch or finally");
4217           }
4218         ;
4219
4220 opt_catch_clauses
4221         : /* empty */  { $$ = null; }
4222         | catch_clauses
4223         ;
4224
4225 catch_clauses
4226         : catch_clause 
4227           {
4228                 ArrayList l = new ArrayList (4);
4229
4230                 l.Add ($1);
4231                 $$ = l;
4232           }
4233         | catch_clauses catch_clause
4234           {
4235                 ArrayList l = (ArrayList) $1;
4236
4237                 l.Add ($2);
4238                 $$ = l;
4239           }
4240         ;
4241
4242 opt_identifier
4243         : /* empty */   { $$ = null; }
4244         | IDENTIFIER
4245         ;
4246
4247 catch_clause 
4248         : CATCH opt_catch_args 
4249         {
4250                 Expression type = null;
4251                 string id = null;
4252                 
4253                 if ($2 != null) {
4254                         DictionaryEntry cc = (DictionaryEntry) $2;
4255                         type = (Expression) cc.Key;
4256                         id   = (string) cc.Value;
4257
4258                         if (id != null){
4259                                 ArrayList one = new ArrayList (4);
4260                                 Location loc = lexer.Location;
4261
4262                                 one.Add (new VariableDeclaration (id, null, loc));
4263
4264                                 current_block = new Block (current_block);
4265                                 Block b = declare_local_variables (type, one, loc);
4266                                 current_block = b;
4267                         }
4268                 }
4269         } block {
4270                 Expression type = null;
4271                 string id = null;
4272
4273                 if ($2 != null){
4274                         DictionaryEntry cc = (DictionaryEntry) $2;
4275                         type = (Expression) cc.Key;
4276                         id   = (string) cc.Value;
4277
4278                         if (id != null){
4279                                 while (current_block.Implicit)
4280                                         current_block = current_block.Parent;
4281                                 current_block = current_block.Parent;
4282                         }
4283                 }
4284
4285                 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4286         }
4287         ;
4288
4289 opt_catch_args
4290         : /* empty */ { $$ = null; }
4291         | catch_args
4292         ;         
4293
4294 catch_args 
4295         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
4296         {
4297                 $$ = new DictionaryEntry ($2, $3);
4298         }
4299         ;
4300
4301 checked_statement
4302         : CHECKED block
4303           {
4304                 $$ = new Checked ((Block) $2);
4305           }
4306         ;
4307
4308 unchecked_statement
4309         : UNCHECKED block
4310           {
4311                 $$ = new Unchecked ((Block) $2);
4312           }
4313         ;
4314
4315 unsafe_statement
4316         : UNSAFE 
4317         {
4318                 if (!RootContext.Unsafe){
4319                         Report.Error (227, lexer.Location, 
4320                                 "Unsafe code can only be used if --unsafe is used");
4321                 }
4322         } block {
4323                 $$ = new Unsafe ((Block) $3);
4324         }
4325         ;
4326
4327 fixed_statement
4328         : FIXED OPEN_PARENS 
4329           type fixed_pointer_declarators 
4330           CLOSE_PARENS _mark_
4331           {
4332                 ArrayList list = (ArrayList) $4;
4333                 Expression type = (Expression) $3;
4334                 Location l = lexer.Location;
4335                 int top = list.Count;
4336
4337                 Block assign_block = new Block (current_block);
4338                 current_block = assign_block;
4339
4340                 for (int i = 0; i < top; i++){
4341                         Pair p = (Pair) list [i];
4342                         LocalInfo v;
4343
4344                         v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4345                         if (v == null)
4346                                 continue;
4347
4348                         v.ReadOnly = true;
4349                         v.Pinned = true;
4350                         p.First = v;
4351                         list [i] = p;
4352                 }
4353           }
4354           embedded_statement 
4355           {
4356                 Location l = (Location) $6;
4357
4358                 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4359
4360                 if (RootContext.WarningLevel >= 4){
4361                         if ($8 == EmptyStatement.Value)
4362                                 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4363                 }
4364
4365                 current_block.AddStatement (f);
4366                 while (current_block.Implicit)
4367                         current_block = current_block.Parent;
4368                 $$ = current_block;
4369                 current_block = current_block.Parent;
4370           }
4371         ;
4372
4373 fixed_pointer_declarators
4374         : fixed_pointer_declarator      { 
4375                 ArrayList declarators = new ArrayList (4);
4376                 if ($1 != null)
4377                         declarators.Add ($1);
4378                 $$ = declarators;
4379           }
4380         | fixed_pointer_declarators COMMA fixed_pointer_declarator
4381           {
4382                 ArrayList declarators = (ArrayList) $1;
4383                 if ($3 != null)
4384                         declarators.Add ($3);
4385                 $$ = declarators;
4386           }
4387         ;
4388
4389 fixed_pointer_declarator
4390         : IDENTIFIER ASSIGN expression
4391           {     
4392                 $$ = new Pair ($1, $3);
4393           }
4394         | IDENTIFIER
4395           {
4396                 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4397                 $$ = null;
4398           }
4399         ;
4400
4401 lock_statement
4402         : LOCK OPEN_PARENS expression CLOSE_PARENS 
4403           {
4404                 //
4405           } 
4406           embedded_statement
4407           {
4408                 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4409           }
4410         ;
4411
4412 using_statement
4413         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4414           {
4415                 Block assign_block = new Block (current_block);
4416                 current_block = assign_block;
4417
4418                 if ($3 is DictionaryEntry){
4419                         DictionaryEntry de = (DictionaryEntry) $3;
4420                         Location l = lexer.Location;
4421
4422                         Expression type = (Expression) de.Key;
4423                         ArrayList var_declarators = (ArrayList) de.Value;
4424
4425                         ArrayList vars = new ArrayList (4);
4426
4427                         foreach (VariableDeclaration decl in var_declarators){
4428
4429                                 LocalInfo vi    = current_block.AddVariable (
4430                                         type, decl.identifier, 
4431                                         current_local_parameters, decl.Location);
4432                                 if (vi == null)
4433                                         continue;
4434                                 vi.ReadOnly = true;
4435
4436                                 Expression expr;
4437                                 if (decl.expression_or_array_initializer is Expression){
4438                                         expr = (Expression) decl.expression_or_array_initializer;
4439                                 } else {
4440                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4441                                         if (init == null) {
4442                                                 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4443                                         }
4444                                         
4445                                         expr = new ArrayCreation (type, "", init, decl.Location);
4446                                 }
4447
4448                                 LocalVariableReference var;
4449
4450                                 // Get a writable reference to this read-only variable.
4451                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4452
4453                                 // This is so that it is not a warning on using variables
4454                                 vi.Used = true;
4455
4456                                 vars.Add (new DictionaryEntry (var, expr));                             
4457
4458                                 // Assign a = new Assign (var, expr, decl.Location);
4459                                 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4460                         }
4461
4462                         // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4463                         // It can be referred to as $6 below.
4464                         $$ = new DictionaryEntry (type, vars);
4465                  } else {
4466                         $$ = $3;
4467                  }
4468           } 
4469           embedded_statement
4470           {
4471                 Using u = new Using ($6, (Statement) $7, (Location) $5);
4472                 current_block.AddStatement (u);
4473                 while (current_block.Implicit)
4474                         current_block = current_block.Parent;
4475                 $$ = current_block;
4476                 current_block = current_block.Parent;
4477           }
4478         ; 
4479
4480 resource_acquisition
4481         : local_variable_declaration
4482         | expression
4483         ;
4484
4485 // Utility rule to save location information
4486 _mark_
4487         : /* empty */
4488         { $$ = lexer.Location; }
4489
4490 %%
4491
4492 // <summary>
4493 //   A class used to pass around variable declarations and constants
4494 // </summary>
4495 public class VariableDeclaration {
4496         public string identifier;
4497         public object expression_or_array_initializer;
4498         public Location Location;
4499         public Attributes OptAttributes;
4500         public string DocComment;
4501
4502         public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4503         {
4504                 this.identifier = id;
4505                 this.expression_or_array_initializer = eoai;
4506                 this.Location = l;
4507                 this.OptAttributes = opt_attrs;
4508         }
4509
4510         public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4511         {
4512         }
4513 }
4514
4515 /// <summary>
4516 ///  Used to pass around interface property information
4517 /// </summary>
4518 public class InterfaceAccessorInfo {
4519
4520         public readonly Accessor Get, Set;
4521
4522         public InterfaceAccessorInfo (bool has_get, bool has_set,
4523                                       Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4524         {
4525                 if (get_mod != 0)
4526                         Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4527                 if (set_mod != 0)
4528                         Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4529                         
4530                 if (has_get)
4531                         Get = new Accessor (null, 0, get_attrs, get_loc);
4532                 if (has_set)
4533                         Set = new Accessor (null, 0, set_attrs, set_loc);
4534         }
4535
4536 }
4537
4538
4539 // <summary>
4540 //   A class used to hold info about an indexer declarator
4541 // </summary>
4542 public class IndexerDeclaration {
4543         public Expression type;
4544         public MemberName interface_type;
4545         public Parameters param_list;
4546
4547         public IndexerDeclaration (Expression type, MemberName interface_type,
4548                                    Parameters param_list)
4549         {
4550                 this.type = type;
4551                 this.interface_type = interface_type;
4552                 this.param_list = param_list;
4553         }
4554 }
4555
4556 //
4557 // We use this when we do not have an object in advance that is an IIteratorContainer
4558 //
4559 public class SimpleIteratorContainer : IIteratorContainer {
4560         public bool Yields;
4561
4562         public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4563
4564         //
4565         // Reset and return
4566         //
4567         public static SimpleIteratorContainer GetSimple () { 
4568                 Simple.Yields = false;
4569                 return Simple;
4570         }
4571
4572         public void SetYields () { Yields = true; } 
4573 }
4574
4575 // <summary>
4576 //  A class used to hold info about an operator declarator
4577 // </summary>
4578 public class OperatorDeclaration {
4579         public Operator.OpType optype;
4580         public Expression ret_type, arg1type, arg2type;
4581         public string arg1name, arg2name;
4582         public Location location;
4583
4584         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
4585                                     Expression arg1type, string arg1name,
4586                                     Expression arg2type, string arg2name, Location location)
4587         {
4588                 optype = op;
4589                 this.ret_type = ret_type;
4590                 this.arg1type = arg1type;
4591                 this.arg1name = arg1name;
4592                 this.arg2type = arg2type;
4593                 this.arg2name = arg2name;
4594                 this.location = location;
4595         }
4596
4597 }
4598
4599 void Error_ExpectingTypeName (Location l, Expression expr)
4600 {
4601         if (expr is Invocation){
4602                 Report.Error (1002, l, "; expected");
4603         } else {
4604                 Report.Error (-1, l, "Invalid Type definition");
4605         }
4606 }
4607
4608 TypeContainer pop_current_class ()
4609 {
4610         TypeContainer retval = current_class;
4611
4612         current_class = current_class.Parent;
4613         current_container = current_container.Parent;
4614         
4615         if (current_class != current_container) {
4616                 if (!(current_class is ClassPart) ||
4617                     ((ClassPart) current_class).PartialContainer != current_container)
4618                         throw new InternalErrorException ();
4619         } else if (current_container is ClassPart)
4620                 current_container = ((ClassPart) current_class).PartialContainer;
4621
4622         return retval;
4623 }
4624
4625 // <summary>
4626 //   Given the @class_name name, it creates a fully qualified name
4627 //   based on the containing declaration space
4628 // </summary>
4629 MemberName
4630 MakeName (MemberName class_name)
4631 {
4632         Namespace ns = current_namespace.NS;
4633
4634         if (current_container.Name == ""){
4635                 if (ns.Name != "")
4636                         return new MemberName (ns.MemberName, class_name);
4637                 else
4638                         return class_name;
4639         } else {
4640                 return new MemberName (current_container.MemberName, class_name);
4641         }
4642 }
4643
4644 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4645 {
4646         Block implicit_block;
4647         ArrayList inits = null;
4648
4649         //
4650         // We use the `Used' property to check whether statements
4651         // have been added to the current block.  If so, we need
4652         // to create another block to contain the new declaration
4653         // otherwise, as an optimization, we use the same block to
4654         // add the declaration.
4655         //
4656         // FIXME: A further optimization is to check if the statements
4657         // that were added were added as part of the initialization
4658         // below.  In which case, no other statements have been executed
4659         // and we might be able to reduce the number of blocks for
4660         // situations like this:
4661         //
4662         // int j = 1;  int k = j + 1;
4663         //
4664         if (current_block.Used)
4665                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4666         else
4667                 implicit_block = current_block;
4668
4669         foreach (VariableDeclaration decl in variable_declarators){
4670
4671                 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4672                         if (decl.expression_or_array_initializer != null){
4673                                 if (inits == null)
4674                                         inits = new ArrayList (4);
4675                                 inits.Add (decl);
4676                         }
4677                 }
4678         }
4679
4680         if (inits == null)
4681                 return implicit_block;
4682
4683         foreach (VariableDeclaration decl in inits){
4684                 Assign assign;
4685                 Expression expr;
4686                 
4687                 if (decl.expression_or_array_initializer is Expression){
4688                         expr = (Expression) decl.expression_or_array_initializer;
4689
4690                 } else {
4691                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4692                         
4693                         expr = new ArrayCreation (type, "", init, decl.Location);
4694                 }
4695
4696                 LocalVariableReference var;
4697                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4698
4699                 assign = new Assign (var, expr, decl.Location);
4700
4701                 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4702         }
4703         
4704         return implicit_block;
4705 }
4706
4707 Block declare_local_constants (Expression type, ArrayList declarators)
4708 {
4709         Block implicit_block;
4710
4711         if (current_block.Used)
4712                 implicit_block = new Block (current_block, Block.Flags.Implicit);
4713         else
4714                 implicit_block = current_block;
4715
4716         foreach (VariableDeclaration decl in declarators){
4717                 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4718                                                   current_local_parameters, decl.Location);
4719         }
4720         
4721         return implicit_block;
4722 }
4723
4724 void CheckAttributeTarget (string a)
4725 {
4726         switch (a) {
4727
4728         case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4729                 return;
4730                 
4731         default :
4732                 Location l = lexer.Location;
4733                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4734                 break;
4735         }
4736
4737 }
4738
4739 void CheckUnaryOperator (Operator.OpType op)
4740 {
4741         switch (op) {
4742                 
4743         case Operator.OpType.LogicalNot: 
4744         case Operator.OpType.OnesComplement: 
4745         case Operator.OpType.Increment:
4746         case Operator.OpType.Decrement:
4747         case Operator.OpType.True: 
4748         case Operator.OpType.False: 
4749         case Operator.OpType.Addition: 
4750         case Operator.OpType.Subtraction:
4751                 
4752                 break;
4753                 
4754         default :
4755                 Location l = lexer.Location;
4756                 Report.Error (1019, l, "Overloadable unary operator expected"); 
4757                 break;
4758                 
4759         }
4760 }
4761
4762 void CheckBinaryOperator (Operator.OpType op)
4763 {
4764         switch (op) {
4765                 
4766         case Operator.OpType.Addition: 
4767         case Operator.OpType.Subtraction: 
4768         case Operator.OpType.Multiply:
4769         case Operator.OpType.Division:
4770         case Operator.OpType.Modulus: 
4771         case Operator.OpType.BitwiseAnd: 
4772         case Operator.OpType.BitwiseOr:
4773         case Operator.OpType.ExclusiveOr: 
4774         case Operator.OpType.LeftShift: 
4775         case Operator.OpType.RightShift:
4776         case Operator.OpType.Equality: 
4777         case Operator.OpType.Inequality:
4778         case Operator.OpType.GreaterThan: 
4779         case Operator.OpType.LessThan: 
4780         case Operator.OpType.GreaterThanOrEqual:
4781         case Operator.OpType.LessThanOrEqual:
4782                 break;
4783                 
4784         default :
4785                 Location l = lexer.Location;
4786                 Report.Error (1020, l, "Overloadable binary operator expected");
4787                 break;
4788         }
4789         
4790 }
4791
4792 void syntax_error (Location l, string msg)
4793 {
4794         Report.Error (1003, l, "Syntax error, " + msg);
4795 }
4796
4797 void output (string s)
4798 {
4799         Console.WriteLine (s);
4800 }
4801
4802 void note (string s)
4803 {
4804         // Used to put annotations
4805 }
4806
4807 Tokenizer lexer;
4808
4809 public Tokenizer Lexer {
4810         get {
4811                 return lexer;
4812         }
4813 }                  
4814
4815 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4816 {
4817         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4818         this.name = file.Name;
4819         this.file = file;
4820         current_container = RootContext.Tree.Types;
4821         // TODO: Make RootContext.Tree.Types a PartialContainer.
4822         current_class = current_container;
4823         current_container.NamespaceEntry = current_namespace;
4824         oob_stack = new Stack ();
4825         switch_stack = new Stack ();
4826
4827         lexer = new Tokenizer (reader, file, defines);
4828 }
4829
4830 public void parse ()
4831 {
4832         try {
4833                 if (yacc_verbose_flag > 1)
4834                         yyparse (lexer, new yydebug.yyDebugSimple ());
4835                 else
4836                         yyparse (lexer);
4837                 Tokenizer tokenizer = lexer as Tokenizer;
4838                 tokenizer.cleanup ();           
4839         } catch (Exception e){
4840                 // 
4841                 // Removed for production use, use parser verbose to get the output.
4842                 //
4843                 // Console.WriteLine (e);
4844                 Report.Error (-25, lexer.Location, "Parsing error");
4845                 if (yacc_verbose_flag > 0)
4846                         Console.WriteLine (e);
4847         }
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 }