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