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