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