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