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