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