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