2003-09-22 Martin Baulig <martin@ximian.com>
[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, (ArrayList) $8, new_class.Location), 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                 ArrayList constraints = (ArrayList) $1;
2953
2954                 constraints.Add ($2);
2955                 $$ = constraints;
2956           }
2957         ; 
2958
2959 type_parameter_constraints_clause
2960         : WHERE type_parameter COLON type_parameter_constraints {
2961                 ArrayList constraints = new ArrayList (1);
2962                 constraints.Add (new Constraints ((string) $2, (ArrayList) $4, lexer.Location));
2963                 $$ = constraints;
2964           }
2965         ; 
2966
2967 type_parameter_constraints
2968         : type_parameter_constraint {
2969                 ArrayList constraints = new ArrayList (1);
2970                 constraints.Add ($1);
2971                 $$ = constraints;
2972           }
2973         | type_parameter_constraints COMMA type_parameter_constraint {
2974                 ArrayList constraints = (ArrayList) $1;
2975
2976                 constraints.Add ($3);
2977                 $$ = constraints;
2978           }
2979         ;
2980
2981 type_parameter_constraint
2982         : type
2983         | NEW OPEN_PARENS CLOSE_PARENS {
2984                 $$ = true;
2985           }
2986         ;
2987
2988 //
2989 // Statements (8.2)
2990 //
2991
2992 //
2993 // A block is "contained" on the following places:
2994 //      method_body
2995 //      property_declaration as part of the accessor body (get/set)
2996 //      operator_declaration
2997 //      constructor_declaration
2998 //      destructor_declaration
2999 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3000 //      
3001 block
3002         : OPEN_BRACE 
3003           {
3004                 current_block = new Block (current_block, current_local_parameters,
3005                                            lexer.Location, Location.Null);
3006           } 
3007           opt_statement_list CLOSE_BRACE 
3008           { 
3009                 while (current_block.Implicit)
3010                         current_block = current_block.Parent;
3011                 $$ = current_block;
3012                 current_block.SetEndLocation (lexer.Location);
3013                 current_block = current_block.Parent;
3014           }
3015         ;
3016
3017 opt_statement_list
3018         : /* empty */
3019         | statement_list 
3020         ;
3021
3022 statement_list
3023         : statement
3024         | statement_list statement
3025         ;
3026
3027 statement
3028         : declaration_statement
3029           {
3030                 if ($1 != null && (Block) $1 != current_block){
3031                         current_block.AddStatement ((Statement) $1);
3032                         current_block = (Block) $1;
3033                 }
3034           }
3035         | embedded_statement
3036           {
3037                 Statement s = (Statement) $1;
3038
3039
3040                 current_block.AddStatement ((Statement) $1);
3041           }
3042         | labeled_statement
3043         ;
3044
3045 embedded_statement
3046         : block
3047         | empty_statement
3048         | expression_statement
3049         | selection_statement
3050         | iteration_statement
3051         | jump_statement                  
3052         | try_statement
3053         | checked_statement
3054         | unchecked_statement
3055         | lock_statement
3056         | using_statement
3057         | unsafe_statement
3058         | fixed_statement
3059         ;
3060
3061 empty_statement
3062         : SEMICOLON
3063           {
3064                   $$ = new EmptyStatement ();
3065           }
3066         ;
3067
3068 labeled_statement
3069         : IDENTIFIER COLON 
3070           {
3071                 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3072
3073                 if (!current_block.AddLabel ((string) $1, labeled)){
3074                         Location l = lexer.Location;
3075                         Report.Error (140, l, "The label '" + ((string) $1) + "' is a duplicate");
3076                 }       
3077                 current_block.AddStatement (labeled);
3078           }
3079           statement
3080         ;
3081
3082 declaration_statement
3083         : local_variable_declaration SEMICOLON
3084           {
3085                 if ($1 != null){
3086                         DictionaryEntry de = (DictionaryEntry) $1;
3087
3088                         $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3089                 }
3090           }
3091
3092         | local_constant_declaration SEMICOLON
3093           {
3094                 if ($1 != null){
3095                         DictionaryEntry de = (DictionaryEntry) $1;
3096
3097                         $$ = declare_local_constant ((Expression) de.Key, (VariableDeclaration) de.Value);
3098                 }
3099           }
3100         ;
3101
3102 /* 
3103  * The following is from Rhys' grammar:
3104  * > Types in local variable declarations must be recognized as 
3105  * > expressions to prevent reduce/reduce errors in the grammar.
3106  * > The expressions are converted into types during semantic analysis.
3107  */
3108 local_variable_type
3109         : primary_expression opt_rank_specifier
3110           { 
3111                 // FIXME: Do something smart here regarding the composition of the type.
3112
3113                 // Ok, the above "primary_expression" is there to get rid of
3114                 // both reduce/reduce and shift/reduces in the grammar, it should
3115                 // really just be "type_name".  If you use type_name, a reduce/reduce
3116                 // creeps up.  If you use qualified_identifier (which is all we need
3117                 // really) two shift/reduces appear.
3118                 // 
3119
3120                 // So the super-trick is that primary_expression
3121                 // can only be either a SimpleName or a MemberAccess. 
3122                 // The MemberAccess case arises when you have a fully qualified type-name like :
3123                 // Foo.Bar.Blah i;
3124                 // SimpleName is when you have
3125                 // Blah i;
3126                   
3127                 Expression expr = (Expression) $1;  
3128                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3129                         Error_ExpectingTypeName (lexer.Location, expr);
3130                         $$ = null;
3131                 } else {
3132                         //
3133                         // So we extract the string corresponding to the SimpleName
3134                         // or MemberAccess
3135                         // 
3136
3137                         if ((string) $2 == "")
3138                                 $$ = $1;
3139                         else
3140                                 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3141                 }
3142           }
3143         | builtin_types opt_rank_specifier
3144           {
3145                 if ((string) $2 == "")
3146                         $$ = $1;
3147                 else
3148                         $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3149           }
3150         ;
3151
3152 local_variable_pointer_type
3153         : primary_expression STAR
3154           {
3155                 Expression expr = (Expression) $1;  
3156                 Location l = lexer.Location;
3157
3158                 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3159                         Error_ExpectingTypeName (l, expr);
3160
3161                         $$ = null;
3162                 } else 
3163                         $$ = new ComposedCast ((Expression) $1, "*", l);
3164           }
3165         | builtin_types STAR
3166           {
3167                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3168           }
3169         | VOID STAR
3170           {
3171                 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3172           }
3173         | local_variable_pointer_type STAR
3174           {
3175                 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3176           }
3177         ;
3178
3179 local_variable_declaration
3180         : local_variable_type variable_declarators
3181           {
3182                 if ($1 != null)
3183                         $$ = new DictionaryEntry ($1, $2);
3184                 else
3185                         $$ = null;
3186           }
3187         | local_variable_pointer_type opt_rank_specifier variable_declarators
3188         {
3189                 if ($1 != null){
3190                         Expression t;
3191
3192                         if ((string) $2 == "")
3193                                 t = (Expression) $1;
3194                         else
3195                                 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3196                         $$ = new DictionaryEntry (t, $3);
3197                 } else 
3198                         $$ = null;
3199         }
3200         ;
3201
3202 local_constant_declaration
3203         : CONST local_variable_type constant_declarator
3204           {
3205                 if ($2 != null)
3206                         $$ = new DictionaryEntry ($2, $3);
3207                 else
3208                         $$ = null;
3209           }
3210         ;
3211
3212 expression_statement
3213         : statement_expression SEMICOLON
3214           {
3215                 $$ = $1;
3216           }
3217         ;
3218
3219         //
3220         // We have to do the wrapping here and not in the case above,
3221         // because statement_expression is used for example in for_statement
3222         //
3223 statement_expression
3224         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3225         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3226         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3227         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3228         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3229         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3230         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3231         | error {
3232                 Report.Error (1002, lexer.Location, "Expecting `;'");
3233                 $$ = null;
3234           }
3235         ;
3236
3237 object_creation_expression
3238         : object_or_delegate_creation_expression
3239           { note ("complain if this is a delegate maybe?"); } 
3240         ;
3241
3242 selection_statement
3243         : if_statement
3244         | switch_statement
3245         ; 
3246
3247 if_statement
3248         : if_statement_open if_statement_rest
3249           {
3250                 $$ = $2;
3251           }
3252         ;
3253
3254 if_statement_open
3255         : IF OPEN_PARENS 
3256           {
3257                 oob_stack.Push (lexer.Location);
3258           }
3259         ;
3260
3261 if_statement_rest
3262         : boolean_expression CLOSE_PARENS 
3263           embedded_statement
3264           { 
3265                 Location l = (Location) oob_stack.Pop ();
3266
3267                 $$ = new If ((Expression) $1, (Statement) $3, l);
3268
3269                 if (RootContext.WarningLevel >= 3){
3270                         if ($3 is EmptyStatement)
3271                                 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3272                 }
3273
3274           }
3275         | boolean_expression CLOSE_PARENS
3276           embedded_statement ELSE embedded_statement
3277           {
3278                 Location l = (Location) oob_stack.Pop ();
3279
3280                 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3281           }
3282         ;
3283
3284 switch_statement
3285         : SWITCH OPEN_PARENS 
3286           { 
3287                 oob_stack.Push (lexer.Location);
3288                 switch_stack.Push (current_block);
3289           }
3290           expression CLOSE_PARENS 
3291           switch_block
3292           {
3293                 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3294                 current_block = (Block) switch_stack.Pop ();
3295           }
3296         ;
3297
3298 switch_block
3299         : OPEN_BRACE
3300           opt_switch_sections
3301           CLOSE_BRACE
3302           {
3303                 $$ = $2;
3304           }
3305         ;
3306
3307 opt_switch_sections
3308         : /* empty */           
3309           {
3310                 Report.Error (1522, lexer.Location, "Empty switch block"); 
3311           }
3312         | switch_sections
3313         ;
3314
3315 switch_sections
3316         : switch_section 
3317           {
3318                 ArrayList sections = new ArrayList (4);
3319
3320                 sections.Add ($1);
3321                 $$ = sections;
3322           }
3323         | switch_sections switch_section
3324           {
3325                 ArrayList sections = (ArrayList) $1;
3326
3327                 sections.Add ($2);
3328                 $$ = sections;
3329           }
3330         ;
3331
3332 switch_section
3333         : switch_labels
3334           {
3335                 current_block = current_block.CreateSwitchBlock (lexer.Location);
3336           }
3337           statement_list 
3338           {
3339                 Block topmost = current_block;
3340
3341                 while (topmost.Implicit)
3342                         topmost = topmost.Parent;
3343                 $$ = new SwitchSection ((ArrayList) $1, topmost);
3344           }
3345         ;
3346
3347 switch_labels
3348         : switch_label 
3349           {
3350                 ArrayList labels = new ArrayList (4);
3351
3352                 labels.Add ($1);
3353                 $$ = labels;
3354           }
3355         | switch_labels switch_label 
3356           {
3357                 ArrayList labels = (ArrayList) ($1);
3358                 labels.Add ($2);
3359
3360                 $$ = labels;
3361           }
3362         ;
3363
3364 switch_label
3365         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3366         | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
3367         | error {
3368                 Report.Error (
3369                         1523, lexer.Location, 
3370                         "The keyword case or default must precede code in switch block");
3371           }
3372         ;
3373
3374 iteration_statement
3375         : while_statement
3376         | do_statement
3377         | for_statement
3378         | foreach_statement
3379         ;
3380
3381 while_statement
3382         : WHILE OPEN_PARENS 
3383         {
3384                 oob_stack.Push (lexer.Location);
3385         }
3386         boolean_expression CLOSE_PARENS embedded_statement
3387         {
3388                 Location l = (Location) oob_stack.Pop ();
3389                 $$ = new While ((Expression) $4, (Statement) $6, l);
3390         
3391                 if (RootContext.WarningLevel >= 3){
3392                         if ($6 is EmptyStatement)
3393                                 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3394                 }
3395         }
3396         ;
3397
3398 do_statement
3399         : DO embedded_statement 
3400           WHILE OPEN_PARENS {
3401                 oob_stack.Push (lexer.Location);
3402           }
3403           boolean_expression CLOSE_PARENS SEMICOLON
3404           {
3405                 Location l = (Location) oob_stack.Pop ();
3406
3407                 $$ = new Do ((Statement) $2, (Expression) $6, l);
3408           }
3409         ;
3410
3411 for_statement
3412         : FOR OPEN_PARENS 
3413           opt_for_initializer SEMICOLON
3414           {
3415                 Block assign_block = new Block (current_block);
3416                 current_block = assign_block;
3417
3418                 if ($3 is DictionaryEntry){
3419                         DictionaryEntry de = (DictionaryEntry) $3;
3420                         
3421                         Expression type = (Expression) de.Key;
3422                         ArrayList var_declarators = (ArrayList) de.Value;
3423
3424                         foreach (VariableDeclaration decl in var_declarators){
3425
3426                                 LocalInfo vi;
3427
3428                                 vi = current_block.AddVariable (
3429                                         type, decl.identifier, current_local_parameters, decl.Location);
3430                                 if (vi == null)
3431                                         continue;
3432
3433                                 Location l = lexer.Location;
3434                                 Expression expr;
3435                                 if (decl.expression_or_array_initializer is Expression){
3436                                         expr = (Expression) decl.expression_or_array_initializer;
3437                                 } else if (decl.expression_or_array_initializer == null) {
3438                                         expr = null;
3439                                 } else {
3440                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3441                                         expr = new ArrayCreation (type, "", init, decl.Location);
3442                                 }
3443                                         
3444                                 LocalVariableReference var;
3445                                 var = new LocalVariableReference (assign_block, decl.identifier, l);
3446
3447                                 if (expr != null) {
3448                                         Assign a = new Assign (var, expr, decl.Location);
3449                                         
3450                                         assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3451                                 }
3452                         }
3453                         
3454                         $3 = null;
3455                 } 
3456                 oob_stack.Push (lexer.Location);
3457           } 
3458           opt_for_condition SEMICOLON
3459           opt_for_iterator CLOSE_PARENS 
3460           embedded_statement
3461           {
3462                 Location l = (Location) oob_stack.Pop ();
3463
3464                 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3465
3466                 if (RootContext.WarningLevel >= 3){
3467                         if ($10 is EmptyStatement)
3468                                 Report.Warning (642, lexer.Location, "Possibly mistaken empty statement");
3469                 }
3470
3471                 current_block.AddStatement (f);
3472                 while (current_block.Implicit)
3473                         current_block = current_block.Parent;
3474                 $$ = current_block;
3475                 current_block = current_block.Parent;
3476           }
3477         ;
3478
3479 opt_for_initializer
3480         : /* empty */           { $$ = new EmptyStatement (); }
3481         | for_initializer       
3482         ;
3483
3484 for_initializer
3485         : local_variable_declaration
3486         | statement_expression_list
3487         ;
3488
3489 opt_for_condition
3490         : /* empty */           { $$ = null; }
3491         | boolean_expression
3492         ;
3493
3494 opt_for_iterator
3495         : /* empty */           { $$ = new EmptyStatement (); }
3496         | for_iterator
3497         ;
3498
3499 for_iterator
3500         : statement_expression_list
3501         ;
3502
3503 statement_expression_list
3504         : statement_expression  
3505           {
3506                 // CHANGE: was `null'
3507                 Block b = new Block (current_block, Block.Flags.Implicit);   
3508
3509                 b.AddStatement ((Statement) $1);
3510                 $$ = b;
3511           }
3512         | statement_expression_list COMMA statement_expression
3513           {
3514                 Block b = (Block) $1;
3515
3516                 b.AddStatement ((Statement) $3);
3517                 $$ = $1;
3518           }
3519         ;
3520
3521 foreach_statement
3522         : FOREACH OPEN_PARENS type IDENTIFIER IN 
3523           {
3524                 oob_stack.Push (lexer.Location);
3525           }
3526           expression CLOSE_PARENS 
3527           {
3528                 oob_stack.Push (current_block);
3529
3530                 Block foreach_block = new Block (current_block, Block.Flags.Implicit);
3531                 LocalVariableReference v = null;
3532                 Location l = lexer.Location;
3533                 LocalInfo vi;
3534
3535                 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3536                 if (vi != null) {
3537                         vi.ReadOnly = true;
3538
3539                         // Get a writable reference to this read-only variable.
3540                         v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3541                 }
3542                 current_block = foreach_block;
3543
3544                 oob_stack.Push (v);
3545                 oob_stack.Push (current_block);
3546           } 
3547           embedded_statement 
3548           {
3549                 Block foreach_block = (Block) oob_stack.Pop ();
3550                 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3551                 Block prev_block = (Block) oob_stack.Pop ();
3552                 Location l = (Location) oob_stack.Pop ();
3553
3554                 current_block = prev_block;
3555
3556                 if (v != null) {
3557                         Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3558                         foreach_block.AddStatement (f);
3559                 }
3560
3561                 $$ = foreach_block;
3562           }
3563         ;
3564
3565 jump_statement
3566         : break_statement
3567         | continue_statement
3568         | goto_statement
3569         | return_statement
3570         | throw_statement
3571         | yield_statement
3572         ;
3573
3574 break_statement
3575         : BREAK SEMICOLON
3576           {
3577                 $$ = new Break (lexer.Location);
3578           }
3579         ;
3580
3581 continue_statement
3582         : CONTINUE SEMICOLON
3583           {
3584                 $$ = new Continue (lexer.Location);
3585           }
3586         ;
3587
3588 goto_statement
3589         : GOTO IDENTIFIER SEMICOLON 
3590           {
3591                 $$ = new Goto (current_block, (string) $2, lexer.Location);
3592           }
3593         | GOTO CASE constant_expression SEMICOLON
3594           {
3595                 $$ = new GotoCase ((Expression) $3, lexer.Location);
3596           }
3597         | GOTO DEFAULT SEMICOLON 
3598           {
3599                 $$ = new GotoDefault (lexer.Location);
3600           }
3601         ; 
3602
3603 return_statement
3604         : RETURN opt_expression SEMICOLON
3605           {
3606                 $$ = new Return ((Expression) $2, lexer.Location);
3607           }
3608         ;
3609
3610 throw_statement
3611         : THROW opt_expression SEMICOLON
3612           {
3613                 $$ = new Throw ((Expression) $2, lexer.Location);
3614           }
3615         ;
3616
3617 yield_statement 
3618         : YIELD expression SEMICOLON
3619           {
3620                 if (iterator_container == null){
3621                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3622                         $$ = null;
3623                 } else {
3624                         iterator_container.SetYields ();
3625                         $$ = new Yield ((Expression) $2, lexer.Location);
3626                 }
3627           }
3628         | YIELD BREAK SEMICOLON
3629           {
3630                 if (iterator_container == null){
3631                         Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
3632                         $$ = null;
3633                 } else {
3634                         iterator_container.SetYields ();
3635                         $$ = new YieldBreak (lexer.Location);
3636                 }
3637           }
3638         ;
3639
3640 opt_expression
3641         : /* empty */
3642         | expression
3643         ;
3644
3645 try_statement
3646         : TRY block catch_clauses 
3647         {
3648                 Catch g = null;
3649                 ArrayList s = new ArrayList (4);
3650                 
3651                 foreach (Catch cc in (ArrayList) $3) {
3652                         if (cc.IsGeneral)
3653                                 g = cc;
3654                         else
3655                                 s.Add (cc);
3656                 }
3657
3658                 // Now s contains the list of specific catch clauses
3659                 // and g contains the general one.
3660                 
3661                 $$ = new Try ((Block) $2, s, g, null, lexer.Location);
3662         }
3663         | TRY block opt_catch_clauses FINALLY block
3664           {
3665                 Catch g = null;
3666                 ArrayList s = new ArrayList (4);
3667                 ArrayList catch_list = (ArrayList) $3;
3668
3669                 if (catch_list != null){
3670                         foreach (Catch cc in catch_list) {
3671                                 if (cc.IsGeneral)
3672                                         g = cc;
3673                                 else
3674                                         s.Add (cc);
3675                         }
3676                 }
3677
3678                 $$ = new Try ((Block) $2, s, g, (Block) $5, lexer.Location);
3679           }
3680         | TRY block error 
3681           {
3682                 Report.Error (1524, lexer.Location, "Expected catch or finally");
3683           }
3684         ;
3685
3686 opt_catch_clauses
3687         : /* empty */  { $$ = null; }
3688         | catch_clauses
3689         ;
3690
3691 catch_clauses
3692         : catch_clause 
3693           {
3694                 ArrayList l = new ArrayList (4);
3695
3696                 l.Add ($1);
3697                 $$ = l;
3698           }
3699         | catch_clauses catch_clause
3700           {
3701                 ArrayList l = (ArrayList) $1;
3702
3703                 l.Add ($2);
3704                 $$ = l;
3705           }
3706         ;
3707
3708 opt_identifier
3709         : /* empty */   { $$ = null; }
3710         | IDENTIFIER
3711         ;
3712
3713 catch_clause 
3714         : CATCH opt_catch_args 
3715         {
3716                 Expression type = null;
3717                 string id = null;
3718                 
3719                 if ($2 != null) {
3720                         DictionaryEntry cc = (DictionaryEntry) $2;
3721                         type = (Expression) cc.Key;
3722                         id   = (string) cc.Value;
3723
3724                         if (id != null){
3725                                 ArrayList one = new ArrayList (4);
3726                                 Location loc = lexer.Location;
3727
3728                                 one.Add (new VariableDeclaration (id, null, loc));
3729
3730                                 $1 = current_block;
3731                                 current_block = new Block (current_block);
3732                                 Block b = declare_local_variables (type, one, loc);
3733                                 current_block = b;
3734                         }
3735                 }
3736         } block {
3737                 Expression type = null;
3738                 string id = null;
3739
3740                 if ($2 != null){
3741                         DictionaryEntry cc = (DictionaryEntry) $2;
3742                         type = (Expression) cc.Key;
3743                         id   = (string) cc.Value;
3744
3745                         if ($1 != null){
3746                                 //
3747                                 // FIXME: I can change this for an assignment.
3748                                 //
3749                                 while (current_block != (Block) $1)
3750                                         current_block = current_block.Parent;
3751                         }
3752                 }
3753
3754
3755                 $$ = new Catch (type, id , (Block) $4, lexer.Location);
3756         }
3757         ;
3758
3759 opt_catch_args
3760         : /* empty */ { $$ = null; }
3761         | catch_args
3762         ;         
3763
3764 catch_args 
3765         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
3766         {
3767                 $$ = new DictionaryEntry ($2, $3);
3768         }
3769         ;
3770
3771 checked_statement
3772         : CHECKED block
3773           {
3774                 $$ = new Checked ((Block) $2);
3775           }
3776         ;
3777
3778 unchecked_statement
3779         : UNCHECKED block
3780           {
3781                 $$ = new Unchecked ((Block) $2);
3782           }
3783         ;
3784
3785 unsafe_statement
3786         : UNSAFE 
3787         {
3788                 if (!RootContext.Unsafe){
3789                         Report.Error (227, lexer.Location, 
3790                                 "Unsafe code can only be used if --unsafe is used");
3791                 }
3792         } block {
3793                 $$ = new Unsafe ((Block) $3);
3794         }
3795         ;
3796
3797 fixed_statement
3798         : FIXED OPEN_PARENS 
3799           type fixed_pointer_declarators 
3800           CLOSE_PARENS 
3801           {
3802                 Block assign_block = new Block (current_block, Block.Flags.Implicit);
3803                 ArrayList list = (ArrayList) $4;
3804                 Expression type = (Expression) $3;
3805                 Location l = lexer.Location;
3806                 int top = list.Count;
3807
3808                 for (int i = 0; i < top; i++){
3809                         Pair p = (Pair) list [i];
3810                         LocalInfo v;
3811
3812                         v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
3813                         if (v == null)
3814                                 continue;
3815                         v.ReadOnly = true;
3816                         p.First = v;
3817                         list [i] = p;
3818                 }
3819                 current_block.AddStatement (assign_block);
3820                 current_block = assign_block;
3821                 oob_stack.Push (assign_block);
3822                 oob_stack.Push (l);
3823           }
3824           embedded_statement 
3825           {
3826                 Location l = (Location) oob_stack.Pop ();
3827                 Block assign_block = (Block) oob_stack.Pop ();
3828
3829                 ArrayList list = (ArrayList) $4;
3830                 int top = list.Count;
3831
3832                 $$ = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
3833           }
3834         ;
3835
3836 fixed_pointer_declarators
3837         : fixed_pointer_declarator      { 
3838                 ArrayList declarators = new ArrayList (4); 
3839                 declarators.Add ($1);
3840                 $$ = declarators;
3841           }
3842         | fixed_pointer_declarators COMMA fixed_pointer_declarator
3843           {
3844                 ArrayList declarators = (ArrayList) $1;
3845                 declarators.Add ($3);
3846                 $$ = declarators;
3847           }
3848         ;
3849
3850 fixed_pointer_declarator
3851         : IDENTIFIER ASSIGN expression
3852           {     
3853                 $$ = new Pair ($1, $3);
3854           }
3855         ;
3856
3857 lock_statement
3858         : LOCK OPEN_PARENS expression CLOSE_PARENS 
3859           {
3860                 //
3861           } 
3862           embedded_statement
3863           {
3864                 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3865           }
3866         ;
3867
3868 using_statement
3869         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS 
3870           {
3871                 Block assign_block = new Block (current_block);
3872                 current_block = assign_block;
3873
3874                 oob_stack.Push (lexer.Location);
3875                 
3876                 if ($3 is DictionaryEntry){
3877                         DictionaryEntry de = (DictionaryEntry) $3;
3878                         Location l = lexer.Location;
3879
3880                         Expression type = (Expression) de.Key;
3881                         ArrayList var_declarators = (ArrayList) de.Value;
3882
3883                         ArrayList vars = new ArrayList (4);
3884
3885                         foreach (VariableDeclaration decl in var_declarators){
3886
3887                                 LocalInfo vi    = current_block.AddVariable (
3888                                         type, decl.identifier, 
3889                                         current_local_parameters, decl.Location);
3890                                 if (vi == null)
3891                                         continue;
3892                                 vi.ReadOnly = true;
3893
3894                                 Expression expr;
3895                                 if (decl.expression_or_array_initializer is Expression){
3896                                         expr = (Expression) decl.expression_or_array_initializer;
3897                                 } else {
3898                                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3899                                         
3900                                         expr = new ArrayCreation (type, "", init, decl.Location);
3901                                 }
3902
3903                                 LocalVariableReference var;
3904
3905                                 // Get a writable reference to this read-only variable.
3906                                 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
3907
3908                                 // This is so that it is not a warning on using variables
3909                                 vi.Used = true;
3910
3911                                 vars.Add (new DictionaryEntry (var, expr));                             
3912
3913                                 // Assign a = new Assign (var, expr, decl.Location);
3914                                 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3915                         }
3916                         $3 = new DictionaryEntry (type, vars);
3917                  }
3918           } 
3919           embedded_statement
3920           {
3921                 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3922                 current_block.AddStatement (u);
3923                 while (current_block.Implicit)
3924                         current_block = current_block.Parent;
3925                 $$ = current_block;
3926                 current_block = current_block.Parent;
3927           }
3928         ; 
3929
3930 resource_acquisition
3931         : local_variable_declaration
3932         | expression
3933         ;
3934
3935 %%
3936
3937 // <summary>
3938 //   A class used to pass around variable declarations and constants
3939 // </summary>
3940 public class VariableDeclaration {
3941         public string identifier;
3942         public object expression_or_array_initializer;
3943         public Location Location;
3944         public Attributes OptAttributes;
3945
3946         public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
3947         {
3948                 this.identifier = id;
3949                 this.expression_or_array_initializer = eoai;
3950                 this.Location = l;
3951                 this.OptAttributes = opt_attrs;
3952         }
3953
3954         public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
3955         {
3956         }
3957 }
3958
3959 // <summary>
3960 //   A class used to hold info about an indexer declarator
3961 // </summary>
3962
3963 public class IndexerDeclaration {
3964         public Expression type;
3965         public string interface_type;
3966         public Parameters param_list;
3967
3968         public IndexerDeclaration (Expression type, string interface_type, Parameters param_list)
3969         {
3970                 this.type = type;
3971                 this.interface_type = interface_type;
3972                 this.param_list = param_list;
3973         }
3974 }
3975
3976 // <summary>
3977 //  A class used to hold info about an operator declarator
3978 // </summary>
3979
3980 public class OperatorDeclaration {
3981         public Operator.OpType optype;
3982         public Expression ret_type, arg1type, arg2type;
3983         public string arg1name, arg2name;
3984         public Location location;
3985
3986         public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
3987                                     Expression arg1type, string arg1name,
3988                                     Expression arg2type, string arg2name, Location location)
3989         {
3990                 optype = op;
3991                 this.ret_type = ret_type;
3992                 this.arg1type = arg1type;
3993                 this.arg1name = arg1name;
3994                 this.arg2type = arg2type;
3995                 this.arg2name = arg2name;
3996                 this.location = location;
3997         }
3998
3999 }
4000
4001 void Error_ExpectingTypeName (Location l, Expression expr)
4002 {
4003         if (expr is Invocation){
4004                 Report.Error (1002, l, "; expected");
4005         } else {
4006                 Report.Error (-1, l, "Invalid Type definition");
4007         }
4008 }
4009
4010 // <summary>
4011 //   Given the @class_name name, it creates a fully qualified name
4012 //   based on the containing declaration space
4013 // </summary>
4014 string 
4015 MakeName (string class_name)
4016 {
4017         string ns = current_namespace.FullName;
4018         string container_name = current_container.Name;
4019
4020         if (container_name == ""){
4021                 if (ns != "")
4022                         return ns + "." + class_name;
4023                 else
4024                         return class_name;
4025         } else
4026                 return container_name + "." + class_name;
4027 }
4028
4029 // <summary>
4030 //   Used to report back to the user the result of a declaration
4031 //   in the current declaration space
4032 // </summary>
4033 void 
4034 CheckDef (DeclSpace.AdditionResult result, string name, Location l)
4035 {
4036         if (result == DeclSpace.AdditionResult.Success)
4037                 return;
4038
4039         switch (result){
4040         case DeclSpace.AdditionResult.NameExists:
4041                 Report.Error (102, l, "The container `" + current_container.Name + 
4042                                  "' already contains a definition for `"+
4043                                  name + "'");
4044                 break;
4045
4046
4047                 //
4048                 // This is handled only for static Constructors, because
4049                 // in reality we handle these by the semantic analysis later
4050                 //
4051         case DeclSpace.AdditionResult.MethodExists:
4052                 Report.Error (
4053                         111, l, "Class `"+current_container.Name+
4054                         "' already defines a member called '" + 
4055                         name + "' with the same parameter types (more than one default constructor)");
4056                 break;
4057
4058         case DeclSpace.AdditionResult.EnclosingClash:
4059                 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
4060                 break;
4061                 
4062         case DeclSpace.AdditionResult.NotAConstructor:
4063                 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
4064                 break;
4065
4066         case DeclSpace.AdditionResult.Error:
4067                 // Error has already been reported.
4068                 break;
4069         }
4070 }
4071
4072 void 
4073 CheckDef (bool result, string name, Location l)
4074 {
4075         if (result)
4076                 return;
4077         CheckDef (DeclSpace.AdditionResult.NameExists, name, l);
4078 }
4079
4080 Expression DecomposeQI (string name, Location loc)
4081 {
4082         Expression o;
4083
4084         if (name.IndexOf ('.') == -1){
4085                 return new SimpleName (name, loc);
4086         } else {
4087                 int pos = name.LastIndexOf (".");
4088                 string left = name.Substring (0, pos);
4089                 string right = name.Substring (pos + 1);
4090
4091                 o = DecomposeQI (left, loc);
4092
4093                 return new MemberAccess (o, right, loc);
4094         }
4095 }
4096
4097 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4098 {
4099         Block implicit_block;
4100         ArrayList inits = null;
4101
4102         //
4103         // We use the `Used' property to check whether statements
4104         // have been added to the current block.  If so, we need
4105         // to create another block to contain the new declaration
4106         // otherwise, as an optimization, we use the same block to
4107         // add the declaration.
4108         //
4109         // FIXME: A further optimization is to check if the statements
4110         // that were added were added as part of the initialization
4111         // below.  In which case, no other statements have been executed
4112         // and we might be able to reduce the number of blocks for
4113         // situations like this:
4114         //
4115         // int j = 1;  int k = j + 1;
4116         //
4117         if (current_block.Used) {
4118                 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4119                 implicit_block.AddChildVariableNames (current_block);
4120         } else
4121                 implicit_block = current_block;
4122
4123         foreach (VariableDeclaration decl in variable_declarators){
4124
4125                 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4126                         if (decl.expression_or_array_initializer != null){
4127                                 if (inits == null)
4128                                         inits = new ArrayList (4);
4129                                 inits.Add (decl);
4130                         }
4131                 }
4132         }
4133
4134         if (inits == null)
4135                 return implicit_block;
4136
4137         foreach (VariableDeclaration decl in inits){
4138                 Assign assign;
4139                 Expression expr;
4140                 
4141                 if (decl.expression_or_array_initializer is Expression){
4142                         expr = (Expression) decl.expression_or_array_initializer;
4143
4144                 } else {
4145                         ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4146                         
4147                         expr = new ArrayCreation (type, "", init, decl.Location);
4148                 }
4149
4150                 LocalVariableReference var;
4151                 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4152
4153                 assign = new Assign (var, expr, decl.Location);
4154
4155                 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4156         }
4157         
4158         return implicit_block;
4159 }
4160
4161 Block declare_local_constant (Expression type, VariableDeclaration decl)
4162 {
4163         Block implicit_block;
4164
4165         if (current_block.Used)
4166                 implicit_block = new Block (current_block, Block.Flags.Implicit);
4167         else
4168                 implicit_block = current_block;
4169
4170         if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4171                                           current_local_parameters, decl.Location))){
4172         }
4173         
4174         return implicit_block;
4175 }
4176
4177 void CheckAttributeTarget (string a)
4178 {
4179         switch (a) {
4180
4181         case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
4182                 return;
4183                 
4184         default :
4185                 Location l = lexer.Location;
4186                 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4187                 break;
4188         }
4189
4190 }
4191
4192 void CheckUnaryOperator (Operator.OpType op)
4193 {
4194         switch (op) {
4195                 
4196         case Operator.OpType.LogicalNot: 
4197         case Operator.OpType.OnesComplement: 
4198         case Operator.OpType.Increment:
4199         case Operator.OpType.Decrement:
4200         case Operator.OpType.True: 
4201         case Operator.OpType.False: 
4202         case Operator.OpType.Addition: 
4203         case Operator.OpType.Subtraction:
4204                 
4205                 break;
4206                 
4207         default :
4208                 Location l = lexer.Location;
4209                 Report.Error (1019, l, "Overloadable unary operator expected"); 
4210                 break;
4211                 
4212         }
4213 }
4214
4215 void CheckBinaryOperator (Operator.OpType op)
4216 {
4217         switch (op) {
4218                 
4219         case Operator.OpType.Addition: 
4220         case Operator.OpType.Subtraction: 
4221         case Operator.OpType.Multiply:
4222         case Operator.OpType.Division:
4223         case Operator.OpType.Modulus: 
4224         case Operator.OpType.BitwiseAnd: 
4225         case Operator.OpType.BitwiseOr:
4226         case Operator.OpType.ExclusiveOr: 
4227         case Operator.OpType.LeftShift: 
4228         case Operator.OpType.RightShift:
4229         case Operator.OpType.Equality: 
4230         case Operator.OpType.Inequality:
4231         case Operator.OpType.GreaterThan: 
4232         case Operator.OpType.LessThan: 
4233         case Operator.OpType.GreaterThanOrEqual:
4234         case Operator.OpType.LessThanOrEqual:
4235                 break;
4236                 
4237         default :
4238                 Location l = lexer.Location;
4239                 Report.Error (1020, l, "Overloadable binary operator expected");
4240                 break;
4241         }
4242         
4243 }
4244
4245 void syntax_error (Location l, string msg)
4246 {
4247         Report.Error (1003, l, "Syntax error, " + msg);
4248 }
4249
4250 void output (string s)
4251 {
4252         Console.WriteLine (s);
4253 }
4254
4255 void note (string s)
4256 {
4257         // Used to put annotations
4258 }
4259
4260 Tokenizer lexer;
4261
4262 public Tokenizer Lexer {
4263         get {
4264                 return lexer;
4265         }
4266 }                  
4267
4268 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4269 {
4270         current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4271         this.name = file.Name;
4272         this.file = file;
4273         current_container = RootContext.Tree.Types;
4274         current_container.NamespaceEntry = current_namespace;
4275         oob_stack = new Stack ();
4276         switch_stack = new Stack ();
4277
4278         lexer = new Tokenizer (reader, file, defines);
4279 }
4280
4281 public void parse ()
4282 {
4283         try {
4284                 if (yacc_verbose_flag)
4285                         yyparse (lexer, new yydebug.yyDebugSimple ());
4286                 else
4287                         yyparse (lexer);
4288                 Tokenizer tokenizer = lexer as Tokenizer;
4289                 tokenizer.cleanup ();           
4290         } catch (Exception e){
4291                 // Please do not remove this, it is used during debugging
4292                 // of the grammar
4293                 //
4294                 Console.WriteLine (e);
4295                 Report.Error (-25, lexer.Location, "Parsing error");
4296                 if (Driver.parser_verbose)
4297                         Console.WriteLine (e);
4298         }
4299 }
4300
4301 void CheckToken (int error, int yyToken, string msg)
4302 {
4303         if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4304                 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyName [yyToken].ToLower ()));
4305                 return;
4306         }               
4307         Report.Error (error, lexer.Location, msg);
4308 }
4309
4310 void CheckIdentifierToken (int yyToken)
4311 {
4312         CheckToken (1041, yyToken, "Identifier expected");
4313 }
4314
4315 /* end end end */
4316 }