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