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