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