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