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