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