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