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