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