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